Merge "Add period after inheritDoc for Sonar"
[policy/apex-pdp.git] / model / policy-model / src / main / java / org / onap / policy / apex / model / policymodel / handling / PolicyModelComparer.java
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 comparison. It compares
38  * policy models on their keys, their context schema differences, their event differences, their context album
39  * differences, their task differences, their policy differences, and their key information differences.
40  *
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  */
43 public class PolicyModelComparer {
44     // Comparison of policy model keys
45     private final KeyDifference<AxArtifactKey> policyModelsKeyDifference;
46
47     // Comparison of context schemas
48     private final KeyDifference<AxArtifactKey> contextSchemasKeyDifference;
49     private KeyedMapDifference<AxArtifactKey, AxContextSchema> contextSchemaCompareResult = new KeyedMapDifference<>();
50
51     // Comparison of events
52     private final KeyDifference<AxArtifactKey> eventsKeyDifference;
53     private KeyedMapDifference<AxArtifactKey, AxEvent> eventComparisonResult = new KeyedMapDifference<>();
54
55     // Comparison of context albums
56     private final KeyDifference<AxArtifactKey> contextAlbumKeyDifference;
57     private KeyedMapDifference<AxArtifactKey, AxContextAlbum> contextAlbumComparisonResult = new KeyedMapDifference<>();
58
59     // Comparison of tasks
60     private final KeyDifference<AxArtifactKey> tasksKeyDifference;
61     private KeyedMapDifference<AxArtifactKey, AxTask> taskComparisonResult = new KeyedMapDifference<>();
62
63     // Comparison of policies
64     private final KeyDifference<AxArtifactKey> policiesKeyDifference;
65     private KeyedMapDifference<AxArtifactKey, AxPolicy> policyComparisonResult = new KeyedMapDifference<>();
66
67     // Comparison of key information
68     private final KeyDifference<AxArtifactKey> keyInformationKeyDifference;
69     private KeyedMapDifference<AxArtifactKey, AxKeyInfo> keyInfoComparisonResult = new KeyedMapDifference<>();
70
71     /**
72      * The Constructor.
73      *
74      * @param left the left
75      * @param right the right
76      */
77     public PolicyModelComparer(final AxPolicyModel left, final AxPolicyModel right) {
78         // @formatter:off
79         policyModelsKeyDifference   = new KeyComparer<AxArtifactKey>().compareKeys(left.getKey(), right.getKey());
80         contextSchemasKeyDifference = new KeyComparer<AxArtifactKey>().compareKeys(left.getKey(), right.getKey());
81         eventsKeyDifference         = new KeyComparer<AxArtifactKey>().compareKeys(left.getKey(), right.getKey());
82         contextAlbumKeyDifference   = new KeyComparer<AxArtifactKey>().compareKeys(left.getKey(), right.getKey());
83         tasksKeyDifference          = new KeyComparer<AxArtifactKey>().compareKeys(left.getKey(), right.getKey());
84         policiesKeyDifference       = new KeyComparer<AxArtifactKey>().compareKeys(left.getKey(), right.getKey());
85         keyInformationKeyDifference = new KeyComparer<AxArtifactKey>().compareKeys(left.getKey(), right.getKey());
86
87         contextSchemaCompareResult = new KeyedMapComparer<AxArtifactKey, AxContextSchema>().compareMaps(
88                 left.getSchemas().getSchemasMap(), right.getSchemas().getSchemasMap());
89         eventComparisonResult         = new KeyedMapComparer<AxArtifactKey, AxEvent>().compareMaps(
90                 left.getEvents().getEventMap(), right.getEvents().getEventMap());
91         contextAlbumComparisonResult  = new KeyedMapComparer<AxArtifactKey, AxContextAlbum>().compareMaps(
92                 left.getAlbums().getAlbumsMap(), right.getAlbums().getAlbumsMap());
93         taskComparisonResult          = new KeyedMapComparer<AxArtifactKey, AxTask>()
94                         .compareMaps(left.getTasks().getTaskMap(), right.getTasks().getTaskMap());
95         policyComparisonResult        = new KeyedMapComparer<AxArtifactKey, AxPolicy>().compareMaps(
96                 left.getPolicies().getPolicyMap(), right.getPolicies().getPolicyMap());
97         keyInfoComparisonResult       = new KeyedMapComparer<AxArtifactKey, AxKeyInfo>().compareMaps(
98                 left.getKeyInformation().getKeyInfoMap(), right.getKeyInformation().getKeyInfoMap());
99         // @formatter:on
100     }
101
102     /**
103      * Gets the difference between policy model keys on the two models.
104      *
105      * @return the difference between policy model keys
106      */
107     public KeyDifference<AxArtifactKey> getPolicyModelsKeyDifference() {
108         return policyModelsKeyDifference;
109     }
110
111     /**
112      * Gets the difference between context schema keys on the two models.
113      *
114      * @return the difference between context schema keys
115      */
116     public KeyDifference<AxArtifactKey> getContextSchemaKeyDifference() {
117         return contextSchemasKeyDifference;
118     }
119
120     /**
121      * Gets the difference between context schemas on the two models.
122      *
123      * @return the difference between context schemas
124      */
125     public KeyedMapDifference<AxArtifactKey, AxContextSchema> getContextSchemaComparisonResult() {
126         return contextSchemaCompareResult;
127     }
128
129     /**
130      * Gets the difference between event keys on the two models.
131      *
132      * @return the difference between event keys
133      */
134     public KeyDifference<AxArtifactKey> getEventKeyDifference() {
135         return eventsKeyDifference;
136     }
137
138     /**
139      * Gets the difference between the events on the two models.
140      *
141      * @return the difference between the events
142      */
143     public KeyedMapDifference<AxArtifactKey, AxEvent> getEventComparisonResult() {
144         return eventComparisonResult;
145     }
146
147     /**
148      * Gets the difference between context album keys on the two models.
149      *
150      * @return the difference between context album keys
151      */
152     public KeyDifference<AxArtifactKey> getContextAlbumKeyDifference() {
153         return contextAlbumKeyDifference;
154     }
155
156     /**
157      * Gets the difference between the context albums on the two models.
158      *
159      * @return the difference between the context albums
160      */
161     public KeyedMapDifference<AxArtifactKey, AxContextAlbum> getContextAlbumComparisonResult() {
162         return contextAlbumComparisonResult;
163     }
164
165     /**
166      * Gets the difference between task keys on the two models.
167      *
168      * @return the difference between task keys
169      */
170     public KeyDifference<AxArtifactKey> getTaskKeyDifference() {
171         return tasksKeyDifference;
172     }
173
174     /**
175      * Gets the difference between the tasks on the two models.
176      *
177      * @return the difference between the tasks
178      */
179     public KeyedMapDifference<AxArtifactKey, AxTask> getTaskComparisonResult() {
180         return taskComparisonResult;
181     }
182
183     /**
184      * Gets the difference between policy keys on the two models.
185      *
186      * @return the difference between policy keys
187      */
188     public KeyDifference<AxArtifactKey> getPolicykeyDifference() {
189         return policiesKeyDifference;
190     }
191
192     /**
193      * Gets the difference between the policies on the two models.
194      *
195      * @return the difference between the policies
196      */
197     public KeyedMapDifference<AxArtifactKey, AxPolicy> getPolicyComparisonResult() {
198         return policyComparisonResult;
199     }
200
201     /**
202      * Gets the difference between key information keys on the two models.
203      *
204      * @return the difference between key information keys
205      */
206     public KeyDifference<AxArtifactKey> getKeyInformationKeyDifference() {
207         return keyInformationKeyDifference;
208     }
209
210     /**
211      * Gets the difference between the key information on the two models.
212      *
213      * @return the difference between the key information
214      */
215     public KeyedMapDifference<AxArtifactKey, AxKeyInfo> getKeyInfoComparisonResult() {
216         return keyInfoComparisonResult;
217     }
218
219     /*
220      * (non-Javadoc)
221      *
222      * @see java.lang.Object#toString()
223      */
224     @Override
225     public String toString() {
226         return asString(true, true);
227     }
228
229     /**
230      * As string.
231      *
232      * @param diffsOnly the diffs only
233      * @param keysOnly the keys only
234      * @return the string
235      */
236     public String asString(final boolean diffsOnly, final boolean keysOnly) {
237         final StringBuilder builder = new StringBuilder();
238
239         builder.append("****** policy map differences ******\n");
240         builder.append(policyModelsKeyDifference.asString(diffsOnly));
241
242         builder.append("*** context schema differences ***\n");
243         builder.append(contextSchemasKeyDifference.asString(diffsOnly));
244         builder.append(contextSchemaCompareResult.asString(diffsOnly, keysOnly));
245
246         builder.append("*** event differences ***\n");
247         builder.append(eventsKeyDifference.asString(diffsOnly));
248         builder.append(eventComparisonResult.asString(diffsOnly, keysOnly));
249
250         builder.append("*** context album differences ***\n");
251         builder.append(contextAlbumKeyDifference.asString(diffsOnly));
252         builder.append(contextAlbumComparisonResult.asString(diffsOnly, keysOnly));
253
254         builder.append("*** task differences ***\n");
255         builder.append(tasksKeyDifference.asString(diffsOnly));
256         builder.append(taskComparisonResult.asString(diffsOnly, keysOnly));
257
258         builder.append("*** policy differences ***\n");
259         builder.append(policiesKeyDifference.asString(diffsOnly));
260         builder.append(policyComparisonResult.asString(diffsOnly, keysOnly));
261
262         builder.append("*** key information differences ***\n");
263         builder.append(keyInformationKeyDifference.asString(diffsOnly));
264         builder.append(keyInfoComparisonResult.asString(diffsOnly, keysOnly));
265
266         builder.append("***********************************\n");
267
268         return builder.toString();
269     }
270 }