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