-
Notifications
You must be signed in to change notification settings - Fork 72
/
DiffService.scala
175 lines (156 loc) · 7.57 KB
/
DiffService.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
/*
*************************************************************************************
* Copyright 2011-2013 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.modification
import com.normation.rudder.domain.policies._
import com.normation.rudder.domain.nodes.NodeGroup
import com.normation.rudder.domain.nodes.NodeGroupDiff
import com.normation.rudder.repository.RoDirectiveRepository
import com.normation.cfclerk.domain.SectionSpec
import com.normation.rudder.domain.nodes.ModifyNodeGroupDiff
import com.normation.rudder.domain.parameters.GlobalParameter
import com.normation.rudder.domain.parameters.ModifyGlobalParameterDiff
import com.normation.cfclerk.domain.TechniqueName
/**
* A service that allows to build diff between
* objects.
*/
trait DiffService {
def diffDirective(
reference:Directive
, refRootSection : SectionSpec
, newItem:Directive
, newRootSection : SectionSpec
, techniqueName : TechniqueName
) : ModifyDirectiveDiff
def diffNodeGroup(reference:NodeGroup, newItem:NodeGroup) : ModifyNodeGroupDiff
def diffRule(reference:Rule, newItem:Rule) : ModifyRuleDiff
def diffGlobalParameter(reference:GlobalParameter, newItem:GlobalParameter) : ModifyGlobalParameterDiff
}
class DiffServiceImpl extends DiffService {
def toDiff[U,T](reference : U, newItem : U)(toData : U => T) : Option[SimpleDiff[T]] = {
val refValue = toData(reference)
val newValue = toData(newItem)
if ( refValue == newValue) None else Some(SimpleDiff(refValue,newValue))
}
def diffDirective(
reference:Directive
, refRootSection : SectionSpec
, newItem:Directive
, newRootSection : SectionSpec
, techniqueName : TechniqueName) : ModifyDirectiveDiff = {
import SectionVal._
def toDirectiveDiff[T] : (Directive => T) => Option[SimpleDiff[T]] = toDiff(reference,newItem) _
val refSectionVal = directiveValToSectionVal(refRootSection,reference.parameters)
val newSectionVal = directiveValToSectionVal(newRootSection,newItem.parameters)
val diffParameters = if (refSectionVal == newSectionVal) None else Some(SimpleDiff(refSectionVal,newSectionVal))
val diffName = toDirectiveDiff( _.name)
val diffShortDescription = toDirectiveDiff( _.shortDescription)
val diffLongDescription = toDirectiveDiff( _.longDescription)
val diffTechniqueVersion = toDirectiveDiff( _.techniqueVersion)
val diffPriority = toDirectiveDiff( _.priority)
val diffSystem = toDirectiveDiff( _.isSystem)
val diffEnable = toDirectiveDiff( _.isEnabled)
val diffPolicyMode = toDirectiveDiff( _.policyMode)
ModifyDirectiveDiff(
techniqueName
, reference.id
, reference.name
, diffName
, diffTechniqueVersion
, diffParameters
, diffShortDescription
, diffLongDescription
, diffPriority
, diffEnable
, diffSystem
, diffPolicyMode
)
}
def diffNodeGroup(reference:NodeGroup, newItem:NodeGroup) : ModifyNodeGroupDiff = {
val diffName = if (reference.name == newItem.name) None else Some(SimpleDiff(reference.name,newItem.name))
val diffDescription = if (reference.description == newItem.description) None else Some(SimpleDiff(reference.description,newItem.description))
val diffEnable = if (reference.isEnabled == newItem.isEnabled) None else Some(SimpleDiff(reference.isEnabled,newItem.isEnabled))
val diffDynamic = if (reference.isDynamic == newItem.isDynamic) None else Some(SimpleDiff(reference.isDynamic,newItem.isDynamic))
val diffServerList = if (reference.serverList == newItem.serverList) None else Some(SimpleDiff(reference.serverList,newItem.serverList))
val diffQuery = if (reference.query == newItem.query) None else Some(SimpleDiff(reference.query,newItem.query))
ModifyNodeGroupDiff (
reference.id
, reference.name
, diffName
, diffDescription
, diffQuery
, diffDynamic
, diffServerList
, diffEnable
, None
)
}
def diffRule(reference:Rule, newItem:Rule) : ModifyRuleDiff = {
val diffName = if (reference.name == newItem.name) None else Some(SimpleDiff(reference.name,newItem.name))
val diffCategory = if (reference.categoryId == newItem.categoryId) None else Some(SimpleDiff(reference.categoryId,newItem.categoryId))
val diffShortDescription = if (reference.shortDescription == newItem.shortDescription) None else Some(SimpleDiff(reference.shortDescription,newItem.shortDescription))
val diffLongDescription = if (reference.longDescription == newItem.longDescription) None else Some(SimpleDiff(reference.longDescription,newItem.longDescription))
val diffSystem = if (reference.isSystem == newItem.isSystem) None else Some(SimpleDiff(reference.isSystem,newItem.isSystem))
val diffEnable = if (reference.isEnabled == newItem.isEnabled) None else Some(SimpleDiff(reference.isEnabled,newItem.isEnabled))
val diffTarget = if (reference.targets == newItem.targets) None else Some(SimpleDiff(reference.targets,newItem.targets))
val diffDirectives = if (reference.directiveIds == newItem.directiveIds) None else Some(SimpleDiff(reference.directiveIds,newItem.directiveIds))
ModifyRuleDiff(
reference.id
, reference.name
, diffName
, None
, diffTarget
, diffDirectives
, diffShortDescription
, diffLongDescription
, None
, diffEnable
, diffSystem
, diffCategory
)
}
def diffGlobalParameter(reference:GlobalParameter, newItem:GlobalParameter) : ModifyGlobalParameterDiff = {
val diffValue = if (reference.value == newItem.value) None else Some(SimpleDiff(reference.value,newItem.value))
val diffDescription = if (reference.description == newItem.description) None else Some(SimpleDiff(reference.description,newItem.description))
val diffOverridable = if (reference.overridable == newItem.overridable) None else Some(SimpleDiff(reference.overridable,newItem.overridable))
ModifyGlobalParameterDiff(
reference.name
, diffValue
, diffDescription
, diffOverridable
)
}
}