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;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
29 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
30 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
31 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
32 import org.onap.policy.apex.model.eventmodel.concepts.AxInputField;
33 import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField;
34 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
35 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
36 import org.onap.policy.apex.model.policymodel.concepts.AxState;
37 import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput;
38 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
39 import org.onap.policy.common.utils.validation.Assertions;
42 * This class analyses a policy model and shows what the usage of each context album, context item, data type, and event
45 * @author Liam Fallon (liam.fallon@ericsson.com)
47 public class PolicyAnalyser {
49 * Perform an analysis on a policy model.
51 * @param policyModel The policy model
52 * @return the analysis result of the policy model
54 public PolicyAnalysisResult analyse(final AxPolicyModel policyModel) {
55 Assertions.argumentNotNull(policyModel, "policyModel may not be null");
57 final PolicyAnalysisResult result = new PolicyAnalysisResult(policyModel);
59 for (final AxPolicy policy : policyModel.getPolicies().getPolicyMap().values()) {
60 for (final AxState state : policy.getStateMap().values()) {
61 analyseState(state, result);
65 for (final AxTask task : policyModel.getTasks().getTaskMap().values()) {
66 analyseTask(task, result);
69 for (final AxEvent event : policyModel.getEvents().getEventMap().values()) {
70 analyseEvent(event, result);
73 for (final AxContextAlbum contextAlbum : policyModel.getAlbums().getAll(null)) {
74 result.getContextSchemaUsage().get(contextAlbum.getItemSchema()).add(contextAlbum.getKey());
81 * Perform an analysis on a single policy in a policy model.
83 * @param policyModel The policy model
84 * @param policy The policy
85 * @return the analysis result of the policy model
87 public PolicyAnalysisResult analyse(final AxPolicyModel policyModel, final AxPolicy policy) {
88 Assertions.argumentNotNull(policyModel, "policyModel may not be null");
89 Assertions.argumentNotNull(policy, "policy may not be null");
91 final PolicyAnalysisResult result = new PolicyAnalysisResult(policyModel);
93 for (final AxState state : policy.getStateMap().values()) {
94 analyseState(state, result);
97 // Only analyse tasks used by this policy
98 for (final Entry<AxArtifactKey, Set<AxKey>> taskUsageEntry : result.getTaskUsage().entrySet()) {
99 // If the usage set is empty, then we skip the task as its not used in the policy
100 if (!taskUsageEntry.getValue().isEmpty()) {
101 analyseTask(policyModel.getTasks().getTaskMap().get(taskUsageEntry.getKey()), result);
105 // Only analyse events used by this policy, same approach as for tasks
106 for (final Entry<AxArtifactKey, Set<AxKey>> eventUsageEntry : result.getEventUsage().entrySet()) {
107 if (!eventUsageEntry.getValue().isEmpty()) {
108 analyseEvent(policyModel.getEvents().getEventMap().get(eventUsageEntry.getKey()), result);
112 // Only analyse context albums used by this policy, same approach as for tasks
113 for (final Entry<AxArtifactKey, Set<AxKey>> contextAlbumUsageEntry : result.getContextAlbumUsage().entrySet()) {
114 if (!contextAlbumUsageEntry.getValue().isEmpty()) {
115 final AxContextAlbum contextAlbum = policyModel.getAlbums().get(contextAlbumUsageEntry.getKey());
116 result.getContextSchemaUsage().get(contextAlbum.getItemSchema()).add(contextAlbum.getKey());
120 for (final AxEvent event : policyModel.getEvents().getEventMap().values()) {
121 analyseEvent(event, result);
124 for (final AxContextAlbum contextAlbum : policyModel.getAlbums().getAll(null)) {
125 result.getContextSchemaUsage().get(contextAlbum.getItemSchema()).add(contextAlbum.getKey());
132 * Analyse the usage of concepts by a state.
134 * @param state the state to analyse
135 * @param result the result
137 private void analyseState(final AxState state, final PolicyAnalysisResult result) {
138 // Event usage by state
139 result.getEventUsage().get(state.getTrigger()).add(state.getKey());
140 for (final AxStateOutput stateOutput : state.getStateOutputs().values()) {
141 result.getEventUsage().get(stateOutput.getOutgingEvent()).add(state.getKey());
144 // State Context Usage
145 for (final AxArtifactKey contextAlbumKey : state.getContextAlbumReferences()) {
146 result.getContextAlbumUsage().get(contextAlbumKey).add(state.getKey());
149 // Task usage by state
150 for (final AxArtifactKey task : state.getTaskReferences().keySet()) {
151 result.getTaskUsage().get(task).add(state.getKey());
156 * Analyse the usage of concepts by a task.
158 * @param task the task to analyse
159 * @param result the result
161 private void analyseTask(final AxTask task, final PolicyAnalysisResult result) {
162 // Task Context Usage
163 for (final AxArtifactKey contextAlbumKey : task.getContextAlbumReferences()) {
164 result.getContextAlbumUsage().get(contextAlbumKey).add(task.getKey());
167 // Task data type usage
168 for (final AxInputField inputField : task.getInputFields().values()) {
169 result.getContextSchemaUsage().get(inputField.getSchema()).add(task.getKey());
171 for (final AxOutputField outputField : task.getOutputFields().values()) {
172 result.getContextSchemaUsage().get(outputField.getSchema()).add(task.getKey());
177 * Analyse the usage of concepts by an event.
179 * @param event the event to analyse
180 * @param result the result of the analysis
182 private void analyseEvent(final AxEvent event, final PolicyAnalysisResult result) {
183 // Event data type usage
184 for (final AxField eventField : event.getFields()) {
185 result.getContextSchemaUsage().get(eventField.getSchema()).add(event.getKey());