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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.policymodel.handling;
24 import java.util.Map.Entry;
26 import java.util.TreeMap;
27 import java.util.TreeSet;
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;
35 * This class finds and holds the usage of context schemas, context albums, events, and tasks by the policies in a policy model.
37 * @author Liam Fallon (liam.fallon@ericsson.com)
39 public class PolicyAnalysisResult {
40 // Usage of context schemas
41 private final Map<AxArtifactKey, Set<AxKey>> contextSchemaUsage = new TreeMap<>();
43 // Usage of context maps
44 private final Map<AxArtifactKey, Set<AxKey>> contextAlbumUsage = new TreeMap<>();
47 private final Map<AxArtifactKey, Set<AxKey>> eventUsage = new TreeMap<>();
50 private final Map<AxArtifactKey, Set<AxKey>> taskUsage = new TreeMap<>();
53 * This constructor creates a {@link PolicyAnalysisResult} instance that holds maps that contain the usage of context schemas, contxt albums, events, and
54 * tasks by all policies in a policy model.
56 * @param policyModel the policy model to analyse
58 public PolicyAnalysisResult(final AxPolicyModel policyModel) {
59 for (final AxArtifactKey contextSchemaKey : policyModel.getSchemas().getSchemasMap().keySet()) {
60 contextSchemaUsage.put(contextSchemaKey, new TreeSet<AxKey>());
63 for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : policyModel.getAlbums().getAlbumsMap().entrySet()) {
64 contextAlbumUsage.put(contextAlbumEntry.getKey(), new TreeSet<AxKey>());
67 for (final AxArtifactKey eventKey : policyModel.getEvents().getEventMap().keySet()) {
68 eventUsage.put(eventKey, new TreeSet<AxKey>());
71 for (final AxArtifactKey taskKey : policyModel.getTasks().getTaskMap().keySet()) {
72 taskUsage.put(taskKey, new TreeSet<AxKey>());
77 * Gets the context schemas used by policies in the policy model.
79 * @return the context schemas used by policies in the policy model
81 public Map<AxArtifactKey, Set<AxKey>> getContextSchemaUsage() {
82 return contextSchemaUsage;
86 * Gets the context albums used by policies in the policy model.
88 * @return the context albums used by policies in the policy model
90 public Map<AxArtifactKey, Set<AxKey>> getContextAlbumUsage() {
91 return contextAlbumUsage;
95 * Gets the events used by policies in the policy model.
97 * @return the events used by policies in the policy model
99 public Map<AxArtifactKey, Set<AxKey>> getEventUsage() {
104 * Gets the tasks used by policies in the policy model.
106 * @return the tasks used by policies in the policy model
108 public Map<AxArtifactKey, Set<AxKey>> getTaskUsage() {
113 * Gets the context schemas used by policies in the policy model.
115 * @return the context schemas used by policies in the policy model
117 public Set<AxArtifactKey> getUsedContextSchemas() {
118 return getUsedKeySet(contextSchemaUsage);
122 * Gets the context albums used by policies in the policy model.
124 * @return the context albums used by policies in the policy model
126 public Set<AxArtifactKey> getUsedContextAlbums() {
127 return getUsedKeySet(contextAlbumUsage);
131 * Gets the events used by policies in the policy model.
133 * @return the events used by policies in the policy model
135 public Set<AxArtifactKey> getUsedEvents() {
136 return getUsedKeySet(eventUsage);
140 * Gets the tasks used by policies in the policy model.
142 * @return the tasks used by policies in the policy model
144 public Set<AxArtifactKey> getUsedTasks() {
145 return getUsedKeySet(taskUsage);
149 * Gets the context schemas in the policy model that were not used by any policies in the policy model.
151 * @return the unused context schemas
153 public Set<AxArtifactKey> getUnusedContextSchemas() {
154 return getUnusedKeySet(contextSchemaUsage);
158 * Gets the context albums in the policy model that were not used by any policies in the policy model.
160 * @return the unused context albums
162 public Set<AxArtifactKey> getUnusedContextAlbums() {
163 return getUnusedKeySet(contextAlbumUsage);
167 * Gets the events in the policy model that were not used by any policies in the policy model.
169 * @return the unused events
171 public Set<AxArtifactKey> getUnusedEvents() {
172 return getUnusedKeySet(eventUsage);
176 * Gets the tasks in the policy model that were not used by any policies in the policy model.
178 * @return the unused tasks
180 public Set<AxArtifactKey> getUnusedTasks() {
181 return getUnusedKeySet(taskUsage);
187 * @see java.lang.Object#toString()
190 public String toString() {
191 final StringBuilder builder = new StringBuilder();
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));
198 return builder.toString();
202 * Gets the usage map string.
204 * @param header the header
205 * @param usageMap the usage map
206 * @return the usage map string
208 private String getUsageMapString(final String header, final Map<? extends AxKey, Set<AxKey>> usageMap) {
209 final StringBuilder builder = new StringBuilder();
211 builder.append(header);
212 builder.append('\n');
213 for (final Entry<? extends AxKey, Set<AxKey>> usageEntry : usageMap.entrySet()) {
215 builder.append(usageEntry.getKey().getId());
216 if (usageEntry.getValue().isEmpty()) {
217 builder.append(" (unused)\n");
221 builder.append('\n');
222 for (final AxKey usageKey : usageEntry.getValue()) {
224 builder.append(usageKey.getId());
225 builder.append("\n");
228 return builder.toString();
232 * Gets the used key set.
234 * @param usageMap the usage map
235 * @return the used key set
237 private Set<AxArtifactKey> getUsedKeySet(final Map<AxArtifactKey, Set<AxKey>> usageMap) {
238 final Set<AxArtifactKey> usedKeySet = new TreeSet<>();
240 for (final Entry<AxArtifactKey, Set<AxKey>> usageEntry : usageMap.entrySet()) {
241 if (!usageEntry.getValue().isEmpty()) {
242 usedKeySet.add(usageEntry.getKey());
250 * Gets the unused key set.
252 * @param usageMap the usage map
253 * @return the unused key set
255 private Set<AxArtifactKey> getUnusedKeySet(final Map<AxArtifactKey, Set<AxKey>> usageMap) {
256 final Set<AxArtifactKey> usedKeySet = new TreeSet<>();
258 for (final Entry<AxArtifactKey, Set<AxKey>> usageEntry : usageMap.entrySet()) {
259 if (usageEntry.getValue().isEmpty()) {
260 usedKeySet.add(usageEntry.getKey());