-
Notifications
You must be signed in to change notification settings - Fork 72
/
XmlSerialisationImpl.scala
483 lines (449 loc) · 19 KB
/
XmlSerialisationImpl.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
*************************************************************************************
* Copyright 2011 Normation SAS
*************************************************************************************
*
* This file is part of Rudder.
*
* Rudder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In accordance with the terms of section 7 (7. Additional Terms.) of
* the GNU General Public License version 3, the copyright holders add
* the following Additional permissions:
* Notwithstanding to the terms of section 5 (5. Conveying Modified Source
* Versions) and 6 (6. Conveying Non-Source Forms.) of the GNU General
* Public License version 3, when you create a Related Module, this
* Related Module is not considered as a part of the work and may be
* distributed under the license agreement of your choice.
* A "Related Module" means a set of sources files including their
* documentation that, without modification of the Source Code, enables
* supplementary functions or services in addition to those offered by
* the Software.
*
* Rudder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rudder. If not, see <http://www.gnu.org/licenses/>.
*
*************************************************************************************
*/
package com.normation.rudder.services.marshalling
import com.normation.rudder.domain.policies.Directive
import com.normation.cfclerk.domain.TechniqueName
import com.normation.rudder.domain.policies.SectionVal
import net.liftweb.common._
import scala.xml.NodeSeq
import scala.xml.{Node => XNode, _}
import net.liftweb.common.Box._
import com.normation.cfclerk.domain.TechniqueVersion
import net.liftweb.util.Helpers.tryo
import com.normation.utils.Control.sequence
import com.normation.rudder.domain.policies.DirectiveId
import com.normation.rudder.domain.nodes.NodeGroup
import com.normation.rudder.domain.policies.Rule
import com.normation.rudder.domain.policies.RuleTarget
import com.normation.rudder.domain.policies.RuleId
import com.normation.inventory.domain.NodeId
import com.normation.rudder.services.queries.CmdbQueryParser
import com.normation.rudder.domain.nodes.NodeGroupId
import com.normation.exceptions.TechnicalException
import com.normation.rudder.domain.policies.ActiveTechniqueCategory
import com.normation.rudder.domain.policies.ActiveTechnique
import org.joda.time.format.ISODateTimeFormat
import com.normation.cfclerk.domain.SectionSpec
import com.normation.rudder.batch.{CurrentDeploymentStatus,SuccessStatus,ErrorStatus}
import com.normation.rudder.domain.nodes.NodeGroupCategory
import com.normation.rudder.services.marshalling.MarshallingUtil.createTrimedElem
import com.normation.utils.XmlUtils
import com.normation.rudder.domain.Constants._
import com.normation.rudder.domain.workflows.ChangeRequest
import com.normation.rudder.domain.workflows.ConfigurationChangeRequest
import scala.util.Try
import scala.util.Success
import com.normation.rudder.domain.nodes.AddNodeGroupDiff
import com.normation.rudder.domain.nodes.DeleteNodeGroupDiff
import com.normation.rudder.domain.nodes.ModifyToNodeGroupDiff
import com.normation.rudder.domain.workflows._
import com.normation.rudder.domain.policies.AddDirectiveDiff
import com.normation.cfclerk.services.TechniqueRepository
import com.normation.cfclerk.domain.TechniqueId
import com.normation.rudder.domain.policies.DeleteDirectiveDiff
import com.normation.rudder.domain.policies.ModifyToDirectiveDiff
import com.normation.cfclerk.xmlwriters.SectionSpecWriter
import com.normation.rudder.domain.policies.AddRuleDiff
import com.normation.rudder.domain.policies.DeleteRuleDiff
import com.normation.rudder.domain.policies.ModifyToRuleDiff
import com.normation.rudder.domain.parameters._
import com.normation.rudder.api.ApiAccount
import com.normation.rudder.rule.category.RuleCategory
import com.normation.rudder.domain.appconfig.RudderWebProperty
import com.normation.rudder.domain.policies.SimpleDiff
case class XmlSerializerImpl (
rule : RuleSerialisation
, directive : DirectiveSerialisation
, group : NodeGroupSerialisation
, globalParam : GlobalParameterSerialisation
, ruleCat : RuleCategorySerialisation
) extends XmlSerializer
class RuleSerialisationImpl(xmlVersion:String) extends RuleSerialisation {
def serialise(rule:Rule): Elem = {
createTrimedElem(XML_TAG_RULE, xmlVersion) {
<id>{rule.id.value}</id>
<displayName>{rule.name}</displayName>
<category>{rule.categoryId.value}</category>
<targets>{
rule.targets.map { target => <target>{target.target}</target> }
}</targets>
<directiveIds>{
rule.directiveIds.map { id => <id>{id.value}</id> }
}</directiveIds>
<shortDescription>{rule.shortDescription}</shortDescription>
<longDescription>{rule.longDescription}</longDescription>
<isEnabled>{rule.isEnabledStatus}</isEnabled>
<isSystem>{rule.isSystem}</isSystem>
}
}
}
/**
* That trait allows to serialise
* Node group categories to an XML file.
*/
class RuleCategorySerialisationImpl(xmlVersion:String) extends RuleCategorySerialisation {
def serialise(rc:RuleCategory): Elem = {
createTrimedElem(XML_TAG_RULE_CATEGORY, xmlVersion) (
<id>{rc.id.value}</id>
<displayName>{rc.name}</displayName>
<description>{rc.description}</description>
<isSystem>{rc.isSystem}</isSystem>
)
}
}
/**
* That trait allow to serialise
* active techniques categories to an XML file.
*/
class ActiveTechniqueCategorySerialisationImpl(xmlVersion:String) extends ActiveTechniqueCategorySerialisation {
def serialise(uptc:ActiveTechniqueCategory): Elem = {
createTrimedElem(XML_TAG_ACTIVE_TECHNIQUE_CATEGORY, xmlVersion) (
<id>{uptc.id.value}</id>
<displayName>{uptc.name}</displayName>
<description>{uptc.description}</description>
<isSystem>{uptc.isSystem}</isSystem>
)
}
}
/**
* That trait allows to serialise
* active techniques to an XML file.
*/
class ActiveTechniqueSerialisationImpl(xmlVersion:String) extends ActiveTechniqueSerialisation {
def serialise(activeTechnique:ActiveTechnique): Elem = {
createTrimedElem(XML_TAG_ACTIVE_TECHNIQUE, xmlVersion) (
<id>{activeTechnique.id.value}</id>
<techniqueName>{activeTechnique.techniqueName}</techniqueName>
<isEnabled>{activeTechnique.isEnabled}</isEnabled>
<isSystem>{activeTechnique.isSystem}</isSystem>
<versions>{ activeTechnique.acceptationDatetimes.map { case(version,date) =>
<version name={version.toString}>{date.toString(ISODateTimeFormat.dateTime)}</version>
} }</versions>
)
}
}
/**
* That trait allows to serialise
* directives an XML file.
*/
class DirectiveSerialisationImpl(xmlVersion:String) extends DirectiveSerialisation {
def serialise(
ptName : TechniqueName
, variableRootSection: SectionSpec
, directive : Directive
) = {
createTrimedElem(XML_TAG_DIRECTIVE, xmlVersion) (
<id>{directive.id.value}</id>
:: <displayName>{directive.name}</displayName>
:: <techniqueName>{ptName.value}</techniqueName>
:: <techniqueVersion>{directive.techniqueVersion}</techniqueVersion>
:: {SectionVal.toXml(SectionVal.directiveValToSectionVal(variableRootSection, directive.parameters))}
:: <shortDescription>{directive.shortDescription}</shortDescription>
:: <longDescription>{directive.longDescription}</longDescription>
:: <priority>{directive.priority}</priority>
:: <isEnabled>{directive.isEnabled}</isEnabled>
:: <isSystem>{directive.isSystem}</isSystem>
:: <policyMode>{directive.policyMode.map(_.name).getOrElse("default")}</policyMode>
:: Nil
)
}
}
/**
* That trait allows to serialise
* Node group categories to an XML file.
*/
class NodeGroupCategorySerialisationImpl(xmlVersion:String) extends NodeGroupCategorySerialisation {
def serialise(ngc:NodeGroupCategory): Elem = {
createTrimedElem(XML_TAG_NODE_GROUP_CATEGORY, xmlVersion) (
<id>{ngc.id.value}</id>
<displayName>{ngc.name}</displayName>
<description>{ngc.description}</description>
<isSystem>{ngc.isSystem}</isSystem>
)
}
}
class NodeGroupSerialisationImpl(xmlVersion:String) extends NodeGroupSerialisation {
def serialise(group:NodeGroup): Elem = {
createTrimedElem(XML_TAG_NODE_GROUP, xmlVersion) (
<id>{group.id.value}</id>
<displayName>{group.name}</displayName>
<description>{group.description}</description>
<query>{ group.query.map( _.toJSONString ).getOrElse("") }</query>
<isDynamic>{group.isDynamic}</isDynamic>
<nodeIds>{
if (group.isDynamic) {
NodeSeq.Empty
} else {
group.serverList.map { id => <id>{id.value}</id> }
}
}</nodeIds>
<isEnabled>{group.isEnabled}</isEnabled>
<isSystem>{group.isSystem}</isSystem>
)
}
}
/**
* That trait allows to serialise deployment status to an XML data
*/
class DeploymentStatusSerialisationImpl(xmlVersion:String) extends DeploymentStatusSerialisation {
def serialise(
deploymentStatus : CurrentDeploymentStatus) : Elem = {
createTrimedElem(XML_TAG_DEPLOYMENT_STATUS, xmlVersion) ( deploymentStatus match {
case d : SuccessStatus => (
<id>{d.id}</id>
<started>{d.started}</started>
<ended>{d.ended}</ended>
<status>success</status>
)
case d : ErrorStatus => (
<id>{d.id}</id>
<started>{d.started}</started>
<ended>{d.ended}</ended>
<status>failure</status>
<errorMessage>{d.failure.messageChain}</errorMessage>
)
case _ => throw new TechnicalException("Bad CurrentDeploymentStatus type, expected a success or an error")
}
) }
}
class GlobalParameterSerialisationImpl(xmlVersion:String) extends GlobalParameterSerialisation {
def serialise(param:GlobalParameter): Elem = {
createTrimedElem(XML_TAG_GLOBAL_PARAMETER, xmlVersion) (
<name>{param.name.value}</name>
<value>{param.value}</value>
<description>{param.description}</description>
<overridable>{param.overridable}</overridable>
)
}
}
/**
* That class allow to serialise change request changes to an XML data.
*
*/
class ChangeRequestChangesSerialisationImpl(
xmlVersion : String
, nodeGroupSerializer : NodeGroupSerialisation
, directiveSerializer : DirectiveSerialisation
, ruleSerializer : RuleSerialisation
, globalParamSerializer : GlobalParameterSerialisation
, techniqueRepo : TechniqueRepository
, sectionSerializer : SectionSpecWriter
) extends ChangeRequestChangesSerialisation with Loggable {
def serialise(changeRequest:ChangeRequest): Elem = {
def serializeGroupChange(change :NodeGroupChangeItem) : NodeSeq = {
<change>
<actor>{change.actor.name}</actor>
<date>{change.creationDate}</date>
<reason>{change.reason.getOrElse("")}</reason>
{ change.diff match {
case AddNodeGroupDiff(group) => <diff action="add">{nodeGroupSerializer.serialise(group)}</diff>
case DeleteNodeGroupDiff(group) => <diff action="delete">{nodeGroupSerializer.serialise(group)}</diff>
case ModifyToNodeGroupDiff(group) => <diff action="modifyTo">{nodeGroupSerializer.serialise(group)}</diff>
case _ => "should not be here"
} }
</change>
}
def serializeDirectiveChange(change :DirectiveChangeItem) : NodeSeq = {
<change>
<actor>{change.actor.name}</actor>
<date>{change.creationDate}</date>
<reason>{change.reason.getOrElse("")}</reason>
{ change.diff match {
case AddDirectiveDiff(techniqueName,directive) =>
techniqueRepo.get(TechniqueId(techniqueName,directive.techniqueVersion)) match {
case None => (s"Error, could not retrieve technique ${techniqueName} version ${directive.techniqueVersion.toString}")
case Some(technique) => <diff action="add">{directiveSerializer.serialise(techniqueName,technique.rootSection,directive)}</diff>
}
case DeleteDirectiveDiff(techniqueName,directive) =>
techniqueRepo.get(TechniqueId(techniqueName,directive.techniqueVersion)) match {
case None => (s"Error, could not retrieve technique ${techniqueName} version ${directive.techniqueVersion.toString}")
case Some(technique) => <diff action="delete">{directiveSerializer.serialise(techniqueName,technique.rootSection,directive)}</diff>
}
case ModifyToDirectiveDiff(techniqueName,directive,rootSection) =>
val rootSectionXml = change.diff match {
case ModifyToDirectiveDiff(_,_,rs) =>
sectionSerializer.serialize(rs).getOrElse(NodeSeq.Empty)
case _ => NodeSeq.Empty
}
<diff action="modifyTo">{directiveSerializer.serialise(techniqueName,rootSection,directive)}</diff> ++
<rootSection>{rootSectionXml}</rootSection>
} }
</change>
}
def serializeRuleChange(change : RuleChangeItem) : NodeSeq = {
<change>
<actor>{change.actor.name}</actor>
<date>{change.creationDate}</date>
<reason>{change.reason.getOrElse("")}</reason>
{ change.diff match {
case AddRuleDiff(rule) => <diff action="add">{ruleSerializer.serialise(rule)}</diff>
case DeleteRuleDiff(rule) => <diff action="delete">{ruleSerializer.serialise(rule)}</diff>
case ModifyToRuleDiff(rule) => <diff action="modifyTo">{ruleSerializer.serialise(rule)}</diff>
} }
</change>
}
def serializeGlobalParamChange(change : GlobalParameterChangeItem) : NodeSeq = {
<change>
<actor>{change.actor.name}</actor>
<date>{change.creationDate}</date>
<reason>{change.reason.getOrElse("")}</reason>
{ change.diff match {
case AddGlobalParameterDiff(param) => <diff action="add">{globalParamSerializer.serialise(param)}</diff>
case DeleteGlobalParameterDiff(param) => <diff action="delete">{globalParamSerializer.serialise(param)}</diff>
case ModifyToGlobalParameterDiff(param) => <diff action="modifyTo">{globalParamSerializer.serialise(param)}</diff>
} }
</change>
}
changeRequest match {
case changeRequest : ConfigurationChangeRequest =>
val groups = changeRequest.nodeGroups.map{ case (nodeGroupId,group) =>
<group id={nodeGroupId.value}>
<initialState>
{group.changes.initialState.map(nodeGroupSerializer.serialise(_)).getOrElse(NodeSeq.Empty)}
</initialState>
<firstChange>
{serializeGroupChange(group.changes.firstChange)}
</firstChange>
<nextChanges>
{group.changes.nextChanges.map(serializeGroupChange(_))}
</nextChanges>
</group>
}
val directives = changeRequest.directives.map{ case (directiveId,directive) =>
<directive id={directiveId.value}>
<initialState>
{directive.changes.initialState.map{
case (techniqueName,directive,rootSection) =>
directiveSerializer.serialise(techniqueName,rootSection,directive)
}.getOrElse(NodeSeq.Empty)
}
</initialState>
<firstChange>
{ serializeDirectiveChange(directive.changes.firstChange) }
</firstChange>
<nextChanges>
{directive.changes.nextChanges.map(serializeDirectiveChange(_))}
</nextChanges>
</directive>
}
val rules = changeRequest.rules.map{ case (ruleId, rule) =>
<rule id={ruleId.value}>
<initialState>
{rule.changes.initialState.map{
case (initialRule) =>
ruleSerializer.serialise(initialRule)
}.getOrElse(NodeSeq.Empty)
}
</initialState>
<firstChange>
{ serializeRuleChange(rule.changes.firstChange) }
</firstChange>
<nextChanges>
{ rule.changes.nextChanges.map(serializeRuleChange(_)) }
</nextChanges>
</rule>
}
val params = changeRequest.globalParams.map{ case (paramName, param) =>
<globalParameter name={paramName.value}>
<initialState>
{param.changes.initialState.map{
case (initialParam) =>
globalParamSerializer.serialise(initialParam)
}.getOrElse(NodeSeq.Empty)
}
</initialState>
<firstChange>
{ serializeGlobalParamChange(param.changes.firstChange) }
</firstChange>
<nextChanges>
{ param.changes.nextChanges.map(serializeGlobalParamChange(_)) }
</nextChanges>
</globalParameter>
}
createTrimedElem(XML_TAG_CHANGE_REQUEST, xmlVersion) (
<groups>
{groups}
</groups>
<directives>
{directives}
</directives>
<rules>
{rules}
</rules>
<globalParameters>
{params}
</globalParameters>
)
case _ => <not_implemented_yet />
}
}
}
/**
* That trait allows to serialise
* API Account to an XML file.
*/
class APIAccountSerialisationImpl(xmlVersion:String) extends APIAccountSerialisation {
def serialise(account:ApiAccount): Elem = {
createTrimedElem(XML_TAG_API_ACCOUNT, xmlVersion) (
<id>{account.id.value}</id>
<name>{account.name.value}</name>
<token>{account.token.value}</token>
<description>{account.description}</description>
<isEnabled>{account.isEnabled}</isEnabled>
<creationDate>{account.creationDate.toString(ISODateTimeFormat.dateTime)}</creationDate>
<tokenGenerationDate>{account.tokenGenerationDate.toString(ISODateTimeFormat.dateTime)}</tokenGenerationDate>
)
}
}
/**
* That trait allows to serialize a web property to an XML
*/
class GlobalPropertySerialisationImpl(xmlVersion:String) extends GlobalPropertySerialisation {
/**
* Version 6:
<globalPropertyUpdate fileFormat="6">
<name>{property.name.value}</name>
<value>{property.value}</value>
</globalPropertyUpdate>
*/
def serializeChange(oldProperty:RudderWebProperty, newProperty : RudderWebProperty) : Elem = {
val diff = SimpleDiff(oldProperty.value,newProperty.value)
<globalPropertyUpdate changetype="modify" fileFormat="6">
<name>{oldProperty.name.value}</name>
{SimpleDiff.stringToXml(<value/>,diff)}
</globalPropertyUpdate>
}
}