Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / policy-model / src / main / java / org / onap / policy / apex / model / policymodel / handling / PolicyAnalysisResult.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 java.util.Map;
24 import java.util.Map.Entry;
25 import java.util.Set;
26 import java.util.TreeMap;
27 import java.util.TreeSet;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
30 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
31 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
32
33 /**
34  * This class finds and holds the usage of context schemas, context albums, events, and tasks by the policies in a
35  * policy model.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public class PolicyAnalysisResult {
40     // Usage of context schemas
41     private final Map<AxArtifactKey, Set<AxKey>> contextSchemaUsage = new TreeMap<>();
42
43     // Usage of context maps
44     private final Map<AxArtifactKey, Set<AxKey>> contextAlbumUsage = new TreeMap<>();
45
46     // Usage of events
47     private final Map<AxArtifactKey, Set<AxKey>> eventUsage = new TreeMap<>();
48
49     // Usage of tasks
50     private final Map<AxArtifactKey, Set<AxKey>> taskUsage = new TreeMap<>();
51
52     /**
53      * This constructor creates a {@link PolicyAnalysisResult} instance that holds maps that contain the usage of
54      * context schemas, contxt albums, events, and tasks by all policies in a policy model.
55      *
56      * @param policyModel the policy model to analyse
57      */
58     public PolicyAnalysisResult(final AxPolicyModel policyModel) {
59         for (final AxArtifactKey contextSchemaKey : policyModel.getSchemas().getSchemasMap().keySet()) {
60             contextSchemaUsage.put(contextSchemaKey, new TreeSet<AxKey>());
61         }
62
63         for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : policyModel.getAlbums().getAlbumsMap()
64                         .entrySet()) {
65             contextAlbumUsage.put(contextAlbumEntry.getKey(), new TreeSet<AxKey>());
66         }
67
68         for (final AxArtifactKey eventKey : policyModel.getEvents().getEventMap().keySet()) {
69             eventUsage.put(eventKey, new TreeSet<AxKey>());
70         }
71
72         for (final AxArtifactKey taskKey : policyModel.getTasks().getTaskMap().keySet()) {
73             taskUsage.put(taskKey, new TreeSet<AxKey>());
74         }
75     }
76
77     /**
78      * Gets the context schemas used by policies in the policy model.
79      *
80      * @return the context schemas used by policies in the policy model
81      */
82     public Map<AxArtifactKey, Set<AxKey>> getContextSchemaUsage() {
83         return contextSchemaUsage;
84     }
85
86     /**
87      * Gets the context albums used by policies in the policy model.
88      *
89      * @return the context albums used by policies in the policy model
90      */
91     public Map<AxArtifactKey, Set<AxKey>> getContextAlbumUsage() {
92         return contextAlbumUsage;
93     }
94
95     /**
96      * Gets the events used by policies in the policy model.
97      *
98      * @return the events used by policies in the policy model
99      */
100     public Map<AxArtifactKey, Set<AxKey>> getEventUsage() {
101         return eventUsage;
102     }
103
104     /**
105      * Gets the tasks used by policies in the policy model.
106      *
107      * @return the tasks used by policies in the policy model
108      */
109     public Map<AxArtifactKey, Set<AxKey>> getTaskUsage() {
110         return taskUsage;
111     }
112
113     /**
114      * Gets the context schemas used by policies in the policy model.
115      *
116      * @return the context schemas used by policies in the policy model
117      */
118     public Set<AxArtifactKey> getUsedContextSchemas() {
119         return getUsedKeySet(contextSchemaUsage);
120     }
121
122     /**
123      * Gets the context albums used by policies in the policy model.
124      *
125      * @return the context albums used by policies in the policy model
126      */
127     public Set<AxArtifactKey> getUsedContextAlbums() {
128         return getUsedKeySet(contextAlbumUsage);
129     }
130
131     /**
132      * Gets the events used by policies in the policy model.
133      *
134      * @return the events used by policies in the policy model
135      */
136     public Set<AxArtifactKey> getUsedEvents() {
137         return getUsedKeySet(eventUsage);
138     }
139
140     /**
141      * Gets the tasks used by policies in the policy model.
142      *
143      * @return the tasks used by policies in the policy model
144      */
145     public Set<AxArtifactKey> getUsedTasks() {
146         return getUsedKeySet(taskUsage);
147     }
148
149     /**
150      * Gets the context schemas in the policy model that were not used by any policies in the policy model.
151      *
152      * @return the unused context schemas
153      */
154     public Set<AxArtifactKey> getUnusedContextSchemas() {
155         return getUnusedKeySet(contextSchemaUsage);
156     }
157
158     /**
159      * Gets the context albums in the policy model that were not used by any policies in the policy model.
160      *
161      * @return the unused context albums
162      */
163     public Set<AxArtifactKey> getUnusedContextAlbums() {
164         return getUnusedKeySet(contextAlbumUsage);
165     }
166
167     /**
168      * Gets the events in the policy model that were not used by any policies in the policy model.
169      *
170      * @return the unused events
171      */
172     public Set<AxArtifactKey> getUnusedEvents() {
173         return getUnusedKeySet(eventUsage);
174     }
175
176     /**
177      * Gets the tasks in the policy model that were not used by any policies in the policy model.
178      *
179      * @return the unused tasks
180      */
181     public Set<AxArtifactKey> getUnusedTasks() {
182         return getUnusedKeySet(taskUsage);
183     }
184
185     /**
186      * {@inheritDoc}.
187      */
188     @Override
189     public String toString() {
190         final StringBuilder builder = new StringBuilder();
191
192         builder.append(getUsageMapString("Context Schema usage", contextSchemaUsage));
193         builder.append(getUsageMapString("Context Album usage", contextAlbumUsage));
194         builder.append(getUsageMapString("Event usage", eventUsage));
195         builder.append(getUsageMapString("Task usage", taskUsage));
196
197         return builder.toString();
198     }
199
200     /**
201      * Gets the usage map string.
202      *
203      * @param header the header
204      * @param usageMap the usage map
205      * @return the usage map string
206      */
207     private String getUsageMapString(final String header, final Map<? extends AxKey, Set<AxKey>> usageMap) {
208         final StringBuilder builder = new StringBuilder();
209
210         builder.append(header);
211         builder.append('\n');
212         for (final Entry<? extends AxKey, Set<AxKey>> usageEntry : usageMap.entrySet()) {
213             builder.append(" ");
214             builder.append(usageEntry.getKey().getId());
215             if (usageEntry.getValue().isEmpty()) {
216                 builder.append(" (unused)\n");
217                 continue;
218             }
219
220             builder.append('\n');
221             for (final AxKey usageKey : usageEntry.getValue()) {
222                 builder.append("  ");
223                 builder.append(usageKey.getId());
224                 builder.append("\n");
225             }
226         }
227         return builder.toString();
228     }
229
230     /**
231      * Gets the used key set.
232      *
233      * @param usageMap the usage map
234      * @return the used key set
235      */
236     private Set<AxArtifactKey> getUsedKeySet(final Map<AxArtifactKey, Set<AxKey>> usageMap) {
237         final Set<AxArtifactKey> usedKeySet = new TreeSet<>();
238
239         for (final Entry<AxArtifactKey, Set<AxKey>> usageEntry : usageMap.entrySet()) {
240             if (!usageEntry.getValue().isEmpty()) {
241                 usedKeySet.add(usageEntry.getKey());
242             }
243         }
244
245         return usedKeySet;
246     }
247
248     /**
249      * Gets the unused key set.
250      *
251      * @param usageMap the usage map
252      * @return the unused key set
253      */
254     private Set<AxArtifactKey> getUnusedKeySet(final Map<AxArtifactKey, Set<AxKey>> usageMap) {
255         final Set<AxArtifactKey> usedKeySet = new TreeSet<>();
256
257         for (final Entry<AxArtifactKey, Set<AxKey>> usageEntry : usageMap.entrySet()) {
258             if (usageEntry.getValue().isEmpty()) {
259                 usedKeySet.add(usageEntry.getKey());
260             }
261         }
262
263         return usedKeySet;
264     }
265 }