Changes for checkstyle 8.32
[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 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;
39
40 /**
41  * This class analyses a policy model and shows what the usage of each context album, context item, data type, and event
42  * is.
43  *
44  * @author Liam Fallon (liam.fallon@ericsson.com)
45  */
46 public class PolicyAnalyser {
47     /**
48      * Perform an analysis on a policy model.
49      *
50      * @param policyModel The policy model
51      * @return the analysis result of the policy model
52      */
53     public PolicyAnalysisResult analyse(final AxPolicyModel policyModel) {
54         Assertions.argumentNotNull(policyModel, "policyModel may not be null");
55
56         final PolicyAnalysisResult result = new PolicyAnalysisResult(policyModel);
57
58         for (final AxPolicy policy : policyModel.getPolicies().getPolicyMap().values()) {
59             for (final AxState state : policy.getStateMap().values()) {
60                 analyseState(state, result);
61             }
62         }
63
64         for (final AxTask task : policyModel.getTasks().getTaskMap().values()) {
65             analyseTask(task, result);
66         }
67
68         for (final AxEvent event : policyModel.getEvents().getEventMap().values()) {
69             analyseEvent(event, result);
70         }
71
72         for (final AxContextAlbum contextAlbum : policyModel.getAlbums().getAll(null)) {
73             result.getContextSchemaUsage().get(contextAlbum.getItemSchema()).add(contextAlbum.getKey());
74         }
75
76         return result;
77     }
78
79     /**
80      * Perform an analysis on a single policy in a policy model.
81      *
82      * @param policyModel The policy model
83      * @param policy The policy
84      * @return the analysis result of the policy model
85      */
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");
89
90         final PolicyAnalysisResult result = new PolicyAnalysisResult(policyModel);
91
92         for (final AxState state : policy.getStateMap().values()) {
93             analyseState(state, result);
94         }
95
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);
101             }
102         }
103
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);
108             }
109         }
110
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());
116             }
117         }
118
119         for (final AxEvent event : policyModel.getEvents().getEventMap().values()) {
120             analyseEvent(event, result);
121         }
122
123         for (final AxContextAlbum contextAlbum : policyModel.getAlbums().getAll(null)) {
124             result.getContextSchemaUsage().get(contextAlbum.getItemSchema()).add(contextAlbum.getKey());
125         }
126
127         return result;
128     }
129
130     /**
131      * Analyse the usage of concepts by a state.
132      *
133      * @param state the state to analyse
134      * @param result the result
135      */
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());
141         }
142
143         // State Context Usage
144         for (final AxArtifactKey contextAlbumKey : state.getContextAlbumReferences()) {
145             result.getContextAlbumUsage().get(contextAlbumKey).add(state.getKey());
146         }
147
148         // Task usage by state
149         for (final AxArtifactKey task : state.getTaskReferences().keySet()) {
150             result.getTaskUsage().get(task).add(state.getKey());
151         }
152     }
153
154     /**
155      * Analyse the usage of concepts by a task.
156      *
157      * @param task the task to analyse
158      * @param result the result
159      */
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());
164         }
165
166         // Task data type usage
167         for (final AxInputField inputField : task.getInputFields().values()) {
168             result.getContextSchemaUsage().get(inputField.getSchema()).add(task.getKey());
169         }
170         for (final AxOutputField outputField : task.getOutputFields().values()) {
171             result.getContextSchemaUsage().get(outputField.getSchema()).add(task.getKey());
172         }
173     }
174
175     /**
176      * Analyse the usage of concepts by an event.
177      *
178      * @param event the event to analyse
179      * @param result the result of the analysis
180      */
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());
185         }
186     }
187 }