2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.policymodel.handling;
24 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.common.utils.validation.Assertions;
41 * This class analyses a policy model and shows what the usage of each context album, context item, data type, and event
44 * @author Liam Fallon (liam.fallon@ericsson.com)
46 public class PolicyAnalyser {
48 * Perform an analysis on a policy model.
50 * @param policyModel The policy model
51 * @return the analysis result of the policy model
53 public PolicyAnalysisResult analyse(final AxPolicyModel policyModel) {
54 Assertions.argumentNotNull(policyModel, "policyModel may not be null");
56 final PolicyAnalysisResult result = new PolicyAnalysisResult(policyModel);
58 for (final AxPolicy policy : policyModel.getPolicies().getPolicyMap().values()) {
59 for (final AxState state : policy.getStateMap().values()) {
60 analyseState(state, result);
64 for (final AxTask task : policyModel.getTasks().getTaskMap().values()) {
65 analyseTask(task, result);
68 for (final AxEvent event : policyModel.getEvents().getEventMap().values()) {
69 analyseEvent(event, result);
72 for (final AxContextAlbum contextAlbum : policyModel.getAlbums().getAll(null)) {
73 result.getContextSchemaUsage().get(contextAlbum.getItemSchema()).add(contextAlbum.getKey());
80 * Perform an analysis on a single policy in a policy model.
82 * @param policyModel The policy model
83 * @param policy The policy
84 * @return the analysis result of the policy model
86 public PolicyAnalysisResult analyse(final AxPolicyModel policyModel, final AxPolicy policy) {
87 Assertions.argumentNotNull(policyModel, "policyModel may not be null");
88 Assertions.argumentNotNull(policy, "policy may not be null");
90 final PolicyAnalysisResult result = new PolicyAnalysisResult(policyModel);
92 for (final AxState state : policy.getStateMap().values()) {
93 analyseState(state, result);
96 // Only analyse tasks used by this policy
97 for (final Entry<AxArtifactKey, Set<AxKey>> taskUsageEntry : result.getTaskUsage().entrySet()) {
98 // If the usage set is empty, then we skip the task as its not used in the policy
99 if (!taskUsageEntry.getValue().isEmpty()) {
100 analyseTask(policyModel.getTasks().getTaskMap().get(taskUsageEntry.getKey()), result);
104 // Only analyse events used by this policy, same approach as for tasks
105 for (final Entry<AxArtifactKey, Set<AxKey>> eventUsageEntry : result.getEventUsage().entrySet()) {
106 if (!eventUsageEntry.getValue().isEmpty()) {
107 analyseEvent(policyModel.getEvents().getEventMap().get(eventUsageEntry.getKey()), result);
111 // Only analyse context albums used by this policy, same approach as for tasks
112 for (final Entry<AxArtifactKey, Set<AxKey>> contextAlbumUsageEntry : result.getContextAlbumUsage().entrySet()) {
113 if (!contextAlbumUsageEntry.getValue().isEmpty()) {
114 final AxContextAlbum contextAlbum = policyModel.getAlbums().get(contextAlbumUsageEntry.getKey());
115 result.getContextSchemaUsage().get(contextAlbum.getItemSchema()).add(contextAlbum.getKey());
119 for (final AxEvent event : policyModel.getEvents().getEventMap().values()) {
120 analyseEvent(event, result);
123 for (final AxContextAlbum contextAlbum : policyModel.getAlbums().getAll(null)) {
124 result.getContextSchemaUsage().get(contextAlbum.getItemSchema()).add(contextAlbum.getKey());
131 * Analyse the usage of concepts by a state.
133 * @param state the state to analyse
134 * @param result the result
136 private void analyseState(final AxState state, final PolicyAnalysisResult result) {
137 // Event usage by state
138 result.getEventUsage().get(state.getTrigger()).add(state.getKey());
139 for (final AxStateOutput stateOutput : state.getStateOutputs().values()) {
140 result.getEventUsage().get(stateOutput.getOutgingEvent()).add(state.getKey());
143 // State Context Usage
144 for (final AxArtifactKey contextAlbumKey : state.getContextAlbumReferences()) {
145 result.getContextAlbumUsage().get(contextAlbumKey).add(state.getKey());
148 // Task usage by state
149 for (final AxArtifactKey task : state.getTaskReferences().keySet()) {
150 result.getTaskUsage().get(task).add(state.getKey());
155 * Analyse the usage of concepts by a task.
157 * @param task the task to analyse
158 * @param result the result
160 private void analyseTask(final AxTask task, final PolicyAnalysisResult result) {
161 // Task Context Usage
162 for (final AxArtifactKey contextAlbumKey : task.getContextAlbumReferences()) {
163 result.getContextAlbumUsage().get(contextAlbumKey).add(task.getKey());
166 // Task data type usage
167 for (final AxInputField inputField : task.getInputFields().values()) {
168 result.getContextSchemaUsage().get(inputField.getSchema()).add(task.getKey());
170 for (final AxOutputField outputField : task.getOutputFields().values()) {
171 result.getContextSchemaUsage().get(outputField.getSchema()).add(task.getKey());
176 * Analyse the usage of concepts by an event.
178 * @param event the event to analyse
179 * @param result the result of the analysis
181 private void analyseEvent(final AxEvent event, final PolicyAnalysisResult result) {
182 // Event data type usage
183 for (final AxField eventField : event.getFields()) {
184 result.getContextSchemaUsage().get(eventField.getSchema()).add(event.getKey());