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;
23 import java.util.Map.Entry;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
28 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
29 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
30 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
31 import org.onap.policy.apex.model.eventmodel.concepts.AxInputField;
32 import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField;
33 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
34 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
35 import org.onap.policy.apex.model.policymodel.concepts.AxState;
36 import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput;
37 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
38 import org.onap.policy.apex.model.utilities.Assertions;
41 * This class analyses a policy model and shows what the usage of each context album, context item, data type, and event is.
43 * @author Liam Fallon (liam.fallon@ericsson.com)
45 public class PolicyAnalyser {
47 * Perform an analysis on a policy model.
49 * @param policyModel The policy model
50 * @return the analysis result of the policy model
52 public PolicyAnalysisResult analyse(final AxPolicyModel policyModel) {
53 Assertions.argumentNotNull(policyModel, "policyModel may not be null");
55 final PolicyAnalysisResult result = new PolicyAnalysisResult(policyModel);
57 for (final AxPolicy policy : policyModel.getPolicies().getPolicyMap().values()) {
58 for (final AxState state : policy.getStateMap().values()) {
59 analyseState(state, result);
63 for (final AxTask task : policyModel.getTasks().getTaskMap().values()) {
64 analyseTask(task, result);
67 for (final AxEvent event : policyModel.getEvents().getEventMap().values()) {
68 analyseEvent(event, result);
71 for (final AxContextAlbum contextAlbum : policyModel.getAlbums().getAll(null)) {
72 result.getContextSchemaUsage().get(contextAlbum.getItemSchema()).add(contextAlbum.getKey());
79 * Perform an analysis on a single policy in a policy model.
81 * @param policyModel The policy model
82 * @param policy The policy
83 * @return the analysis result of the policy model
85 public PolicyAnalysisResult analyse(final AxPolicyModel policyModel, final AxPolicy policy) {
86 Assertions.argumentNotNull(policyModel, "policyModel may not be null");
87 Assertions.argumentNotNull(policy, "policy may not be null");
89 final PolicyAnalysisResult result = new PolicyAnalysisResult(policyModel);
91 for (final AxState state : policy.getStateMap().values()) {
92 analyseState(state, result);
95 // Only analyse tasks used by this policy
96 for (final Entry<AxArtifactKey, Set<AxKey>> taskUsageEntry : result.getTaskUsage().entrySet()) {
97 // If the usage set is empty, then we skip the task as its not used in the policy
98 if (!taskUsageEntry.getValue().isEmpty()) {
99 analyseTask(policyModel.getTasks().getTaskMap().get(taskUsageEntry.getKey()), result);
103 // Only analyse events used by this policy, same approach as for tasks
104 for (final Entry<AxArtifactKey, Set<AxKey>> eventUsageEntry : result.getEventUsage().entrySet()) {
105 if (!eventUsageEntry.getValue().isEmpty()) {
106 analyseEvent(policyModel.getEvents().getEventMap().get(eventUsageEntry.getKey()), result);
110 // Only analyse context albums used by this policy, same approach as for tasks
111 for (final Entry<AxArtifactKey, Set<AxKey>> contextAlbumUsageEntry : result.getContextAlbumUsage().entrySet()) {
112 if (!contextAlbumUsageEntry.getValue().isEmpty()) {
113 final AxContextAlbum contextAlbum = policyModel.getAlbums().get(contextAlbumUsageEntry.getKey());
114 result.getContextSchemaUsage().get(contextAlbum.getItemSchema()).add(contextAlbum.getKey());
118 for (final AxEvent event : policyModel.getEvents().getEventMap().values()) {
119 analyseEvent(event, result);
122 for (final AxContextAlbum contextAlbum : policyModel.getAlbums().getAll(null)) {
123 result.getContextSchemaUsage().get(contextAlbum.getItemSchema()).add(contextAlbum.getKey());
130 * Analyse the usage of concepts by a state.
132 * @param state the state to analyse
133 * @param result the result
135 private void analyseState(final AxState state, final PolicyAnalysisResult result) {
136 // Event usage by state
137 result.getEventUsage().get(state.getTrigger()).add(state.getKey());
138 for (final AxStateOutput stateOutput : state.getStateOutputs().values()) {
139 result.getEventUsage().get(stateOutput.getOutgingEvent()).add(state.getKey());
142 // State Context Usage
143 for (final AxArtifactKey contextAlbumKey : state.getContextAlbumReferences()) {
144 result.getContextAlbumUsage().get(contextAlbumKey).add(state.getKey());
147 // Task usage by state
148 for (final AxArtifactKey task : state.getTaskReferences().keySet()) {
149 result.getTaskUsage().get(task).add(state.getKey());
154 * Analyse the usage of concepts by a task.
156 * @param task the task to analyse
157 * @param result the result
159 private void analyseTask(final AxTask task, final PolicyAnalysisResult result) {
160 // Task Context Usage
161 for (final AxArtifactKey contextAlbumKey : task.getContextAlbumReferences()) {
162 result.getContextAlbumUsage().get(contextAlbumKey).add(task.getKey());
165 // Task data type usage
166 for (final AxInputField inputField : task.getInputFields().values()) {
167 result.getContextSchemaUsage().get(inputField.getSchema()).add(task.getKey());
169 for (final AxOutputField outputField : task.getOutputFields().values()) {
170 result.getContextSchemaUsage().get(outputField.getSchema()).add(task.getKey());
175 * Analyse the usage of concepts by an event.
177 * @param event the event to analyse
178 * @param result the result of the analysis
180 private void analyseEvent(final AxEvent event, final PolicyAnalysisResult result) {
181 // Event data type usage
182 for (final AxField eventField : event.getFields()) {
183 result.getContextSchemaUsage().get(eventField.getSchema()).add(event.getKey());