6731caf3043306ec402be798de10e0c340cbf107
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
5  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.model.policymodel.handling;
24
25 import java.util.Map.Entry;
26 import java.util.Set;
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.policymodel.concepts.AxPolicy;
33 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
34 import org.onap.policy.apex.model.policymodel.concepts.AxState;
35 import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput;
36 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
37 import org.onap.policy.common.utils.validation.Assertions;
38
39 /**
40  * This class analyses a policy model and shows what the usage of each context album, context item, data type, and event
41  * 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.getOutgoingEvent()).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
166     /**
167      * Analyse the usage of concepts by an event.
168      *
169      * @param event the event to analyse
170      * @param result the result of the analysis
171      */
172     private void analyseEvent(final AxEvent event, final PolicyAnalysisResult result) {
173         // Event data type usage
174         for (final AxField eventField : event.getFields()) {
175             result.getContextSchemaUsage().get(eventField.getSchema()).add(event.getKey());
176         }
177     }
178 }