d40e66ead6a152af32cd05f3e93f11194edd4964
[policy/apex-pdp.git] / model / policy-model / src / main / java / org / onap / policy / apex / model / policymodel / handling / PolicyAnalyser.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.policymodel.handling;
23
24 import java.util.Map.Entry;
25 import java.util.Set;
26
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;
40
41 /**
42  * This class analyses a policy model and shows what the usage of each context album, context item, data type, and event
43  * is.
44  *
45  * @author Liam Fallon (liam.fallon@ericsson.com)
46  */
47 public class PolicyAnalyser {
48     /**
49      * Perform an analysis on a policy model.
50      *
51      * @param policyModel The policy model
52      * @return the analysis result of the policy model
53      */
54     public PolicyAnalysisResult analyse(final AxPolicyModel policyModel) {
55         Assertions.argumentNotNull(policyModel, "policyModel may not be null");
56
57         final PolicyAnalysisResult result = new PolicyAnalysisResult(policyModel);
58
59         for (final AxPolicy policy : policyModel.getPolicies().getPolicyMap().values()) {
60             for (final AxState state : policy.getStateMap().values()) {
61                 analyseState(state, result);
62             }
63         }
64
65         for (final AxTask task : policyModel.getTasks().getTaskMap().values()) {
66             analyseTask(task, result);
67         }
68
69         for (final AxEvent event : policyModel.getEvents().getEventMap().values()) {
70             analyseEvent(event, result);
71         }
72
73         for (final AxContextAlbum contextAlbum : policyModel.getAlbums().getAll(null)) {
74             result.getContextSchemaUsage().get(contextAlbum.getItemSchema()).add(contextAlbum.getKey());
75         }
76
77         return result;
78     }
79
80     /**
81      * Perform an analysis on a single policy in a policy model.
82      *
83      * @param policyModel The policy model
84      * @param policy The policy
85      * @return the analysis result of the policy model
86      */
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");
90
91         final PolicyAnalysisResult result = new PolicyAnalysisResult(policyModel);
92
93         for (final AxState state : policy.getStateMap().values()) {
94             analyseState(state, result);
95         }
96
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);
102             }
103         }
104
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);
109             }
110         }
111
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());
117             }
118         }
119
120         for (final AxEvent event : policyModel.getEvents().getEventMap().values()) {
121             analyseEvent(event, result);
122         }
123
124         for (final AxContextAlbum contextAlbum : policyModel.getAlbums().getAll(null)) {
125             result.getContextSchemaUsage().get(contextAlbum.getItemSchema()).add(contextAlbum.getKey());
126         }
127
128         return result;
129     }
130
131     /**
132      * Analyse the usage of concepts by a state.
133      *
134      * @param state the state to analyse
135      * @param result the result
136      */
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());
142         }
143
144         // State Context Usage
145         for (final AxArtifactKey contextAlbumKey : state.getContextAlbumReferences()) {
146             result.getContextAlbumUsage().get(contextAlbumKey).add(state.getKey());
147         }
148
149         // Task usage by state
150         for (final AxArtifactKey task : state.getTaskReferences().keySet()) {
151             result.getTaskUsage().get(task).add(state.getKey());
152         }
153     }
154
155     /**
156      * Analyse the usage of concepts by a task.
157      *
158      * @param task the task to analyse
159      * @param result the result
160      */
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());
165         }
166
167         // Task data type usage
168         for (final AxInputField inputField : task.getInputFields().values()) {
169             result.getContextSchemaUsage().get(inputField.getSchema()).add(task.getKey());
170         }
171         for (final AxOutputField outputField : task.getOutputFields().values()) {
172             result.getContextSchemaUsage().get(outputField.getSchema()).add(task.getKey());
173         }
174     }
175
176     /**
177      * Analyse the usage of concepts by an event.
178      *
179      * @param event the event to analyse
180      * @param result the result of the analysis
181      */
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());
186         }
187     }
188 }