545a02e890f519b13c8dab0cc525a9d959913bd0
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.policymodel.handling;
22
23 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
24 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo;
25 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
26 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
27 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
28 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
29 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
30 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
31 import org.onap.policy.apex.model.utilities.comparison.KeyComparer;
32 import org.onap.policy.apex.model.utilities.comparison.KeyDifference;
33 import org.onap.policy.apex.model.utilities.comparison.KeyedMapComparer;
34 import org.onap.policy.apex.model.utilities.comparison.KeyedMapDifference;
35
36 /**
37  * This class compares two policy models {@link AxPolicyModel} and holds the result of that
38  * comparison. It compares policy models on their keys, their context schema differences, their
39  * event differences, their context album differences, their task differences, their policy
40  * differences, and their key information differences.
41  *
42  * @author Liam Fallon (liam.fallon@ericsson.com)
43  */
44 public class PolicyModelComparer {
45     // Comparison of policy model keys
46     private final KeyDifference<AxArtifactKey> policyModelsKeyDifference;
47
48     // Comparison of context schemas
49     private final KeyDifference<AxArtifactKey> contextSchemasKeyDifference;
50     private KeyedMapDifference<AxArtifactKey, AxContextSchema> contextSchemaComparisonResult =
51             new KeyedMapDifference<>();
52
53     // Comparison of events
54     private final KeyDifference<AxArtifactKey> eventsKeyDifference;
55     private KeyedMapDifference<AxArtifactKey, AxEvent> eventComparisonResult = new KeyedMapDifference<>();
56
57     // Comparison of context albums
58     private final KeyDifference<AxArtifactKey> contextAlbumKeyDifference;
59     private KeyedMapDifference<AxArtifactKey, AxContextAlbum> contextAlbumComparisonResult = new KeyedMapDifference<>();
60
61     // Comparison of tasks
62     private final KeyDifference<AxArtifactKey> tasksKeyDifference;
63     private KeyedMapDifference<AxArtifactKey, AxTask> taskComparisonResult = new KeyedMapDifference<>();
64
65     // Comparison of policies
66     private final KeyDifference<AxArtifactKey> policiesKeyDifference;
67     private KeyedMapDifference<AxArtifactKey, AxPolicy> policyComparisonResult = new KeyedMapDifference<>();
68
69     // Comparison of key information
70     private final KeyDifference<AxArtifactKey> keyInformationKeyDifference;
71     private KeyedMapDifference<AxArtifactKey, AxKeyInfo> keyInfoComparisonResult = new KeyedMapDifference<>();
72
73     /**
74      * The Constructor.
75      *
76      * @param left the left
77      * @param right the right
78      */
79     public PolicyModelComparer(final AxPolicyModel left, final AxPolicyModel right) {
80         // @formatter:off
81         policyModelsKeyDifference   = new KeyComparer<AxArtifactKey>().compareKeys(left.getKey(), right.getKey());
82         contextSchemasKeyDifference = new KeyComparer<AxArtifactKey>().compareKeys(left.getKey(), right.getKey());
83         eventsKeyDifference         = new KeyComparer<AxArtifactKey>().compareKeys(left.getKey(), right.getKey());
84         contextAlbumKeyDifference   = new KeyComparer<AxArtifactKey>().compareKeys(left.getKey(), right.getKey());
85         tasksKeyDifference          = new KeyComparer<AxArtifactKey>().compareKeys(left.getKey(), right.getKey());
86         policiesKeyDifference       = new KeyComparer<AxArtifactKey>().compareKeys(left.getKey(), right.getKey());
87         keyInformationKeyDifference = new KeyComparer<AxArtifactKey>().compareKeys(left.getKey(), right.getKey());
88
89         contextSchemaComparisonResult = new KeyedMapComparer<AxArtifactKey, AxContextSchema>().compareMaps(
90                 left.getSchemas().getSchemasMap(), right.getSchemas().getSchemasMap());
91         eventComparisonResult         = new KeyedMapComparer<AxArtifactKey, AxEvent>().compareMaps(
92                 left.getEvents().getEventMap(), right.getEvents().getEventMap());
93         contextAlbumComparisonResult  = new KeyedMapComparer<AxArtifactKey, AxContextAlbum>().compareMaps(
94                 left.getAlbums().getAlbumsMap(), right.getAlbums().getAlbumsMap());
95         taskComparisonResult          = new KeyedMapComparer<AxArtifactKey, AxTask>().compareMaps(left.getTasks().getTaskMap(), right.getTasks().getTaskMap());
96         policyComparisonResult        = new KeyedMapComparer<AxArtifactKey, AxPolicy>().compareMaps(
97                 left.getPolicies().getPolicyMap(), right.getPolicies().getPolicyMap());
98         keyInfoComparisonResult       = new KeyedMapComparer<AxArtifactKey, AxKeyInfo>().compareMaps(
99                 left.getKeyInformation().getKeyInfoMap(), right.getKeyInformation().getKeyInfoMap());
100         // @formatter:on
101     }
102
103     /**
104      * Gets the difference between policy model keys on the two models.
105      *
106      * @return the difference between policy model keys
107      */
108     public KeyDifference<AxArtifactKey> getPolicyModelsKeyDifference() {
109         return policyModelsKeyDifference;
110     }
111
112     /**
113      * Gets the difference between context schema keys on the two models.
114      *
115      * @return the difference between context schema keys
116      */
117     public KeyDifference<AxArtifactKey> getContextSchemaKeyDifference() {
118         return contextSchemasKeyDifference;
119     }
120
121     /**
122      * Gets the difference between context schemas on the two models.
123      *
124      * @return the difference between context schemas
125      */
126     public KeyedMapDifference<AxArtifactKey, AxContextSchema> getContextSchemaComparisonResult() {
127         return contextSchemaComparisonResult;
128     }
129
130     /**
131      * Gets the difference between event keys on the two models.
132      *
133      * @return the difference between event keys
134      */
135     public KeyDifference<AxArtifactKey> getEventKeyDifference() {
136         return eventsKeyDifference;
137     }
138
139     /**
140      * Gets the difference between the events on the two models.
141      *
142      * @return the difference between the events
143      */
144     public KeyedMapDifference<AxArtifactKey, AxEvent> getEventComparisonResult() {
145         return eventComparisonResult;
146     }
147
148     /**
149      * Gets the difference between context album keys on the two models.
150      *
151      * @return the difference between context album keys
152      */
153     public KeyDifference<AxArtifactKey> getContextAlbumKeyDifference() {
154         return contextAlbumKeyDifference;
155     }
156
157     /**
158      * Gets the difference between the context albums on the two models.
159      *
160      * @return the difference between the context albums
161      */
162     public KeyedMapDifference<AxArtifactKey, AxContextAlbum> getContextAlbumComparisonResult() {
163         return contextAlbumComparisonResult;
164     }
165
166     /**
167      * Gets the difference between task keys on the two models.
168      *
169      * @return the difference between task keys
170      */
171     public KeyDifference<AxArtifactKey> getTaskKeyDifference() {
172         return tasksKeyDifference;
173     }
174
175     /**
176      * Gets the difference between the tasks on the two models.
177      *
178      * @return the difference between the tasks
179      */
180     public KeyedMapDifference<AxArtifactKey, AxTask> getTaskComparisonResult() {
181         return taskComparisonResult;
182     }
183
184     /**
185      * Gets the difference between policy keys on the two models.
186      *
187      * @return the difference between policy keys
188      */
189     public KeyDifference<AxArtifactKey> getPolicykeyDifference() {
190         return policiesKeyDifference;
191     }
192
193     /**
194      * Gets the difference between the policies on the two models.
195      *
196      * @return the difference between the policies
197      */
198     public KeyedMapDifference<AxArtifactKey, AxPolicy> getPolicyComparisonResult() {
199         return policyComparisonResult;
200     }
201
202     /**
203      * Gets the difference between key information keys on the two models.
204      *
205      * @return the difference between key information keys
206      */
207     public KeyDifference<AxArtifactKey> getKeyInformationKeyDifference() {
208         return keyInformationKeyDifference;
209     }
210
211     /**
212      * Gets the difference between the key information on the two models.
213      *
214      * @return the difference between the key information
215      */
216     public KeyedMapDifference<AxArtifactKey, AxKeyInfo> getKeyInfoComparisonResult() {
217         return keyInfoComparisonResult;
218     }
219
220     /*
221      * (non-Javadoc)
222      *
223      * @see java.lang.Object#toString()
224      */
225     @Override
226     public String toString() {
227         return asString(true, true);
228     }
229
230     /**
231      * As string.
232      *
233      * @param diffsOnly the diffs only
234      * @param keysOnly the keys only
235      * @return the string
236      */
237     public String asString(final boolean diffsOnly, final boolean keysOnly) {
238         final StringBuilder builder = new StringBuilder();
239
240         builder.append("****** policy map differences ******\n");
241         builder.append(policyModelsKeyDifference.asString(diffsOnly));
242
243         builder.append("*** context schema differences ***\n");
244         builder.append(contextSchemasKeyDifference.asString(diffsOnly));
245         builder.append(contextSchemaComparisonResult.asString(diffsOnly, keysOnly));
246
247         builder.append("*** event differences ***\n");
248         builder.append(eventsKeyDifference.asString(diffsOnly));
249         builder.append(eventComparisonResult.asString(diffsOnly, keysOnly));
250
251         builder.append("*** context album differences ***\n");
252         builder.append(contextAlbumKeyDifference.asString(diffsOnly));
253         builder.append(contextAlbumComparisonResult.asString(diffsOnly, keysOnly));
254
255         builder.append("*** task differences ***\n");
256         builder.append(tasksKeyDifference.asString(diffsOnly));
257         builder.append(taskComparisonResult.asString(diffsOnly, keysOnly));
258
259         builder.append("*** policy differences ***\n");
260         builder.append(policiesKeyDifference.asString(diffsOnly));
261         builder.append(policyComparisonResult.asString(diffsOnly, keysOnly));
262
263         builder.append("*** key information differences ***\n");
264         builder.append(keyInformationKeyDifference.asString(diffsOnly));
265         builder.append(keyInfoComparisonResult.asString(diffsOnly, keysOnly));
266
267         builder.append("***********************************\n");
268
269         return builder.toString();
270     }
271 }