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