932ebbecefa245f005b5e24d55bfe2b2c00e198f
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
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.clamp.controlloop.participant.policy.main.handler;
22
23 import java.time.Instant;
24 import java.util.LinkedHashMap;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.UUID;
28 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
32 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.base.PfModelRuntimeException;
35 import org.onap.policy.models.provider.PolicyModelsProvider;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * This class handles implementation of controlLoopElement updates.
44  */
45 public class ControlLoopElementHandler implements ControlLoopElementListener {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopElementHandler.class);
48     private static final Map<String, String> policyTypeMap = new LinkedHashMap<>();
49     private static final Map<String, String> policyMap = new LinkedHashMap<>();
50
51     /**
52      * Callback method to handle a control loop element state change.
53      *
54      * @param controlLoopElementId the ID of the control loop element
55      * @param currentState the current state of the control loop element
56      * @param newState the state to which the control loop element is changing to
57      * @throws PfModelException in case of an exception
58     */
59     @Override
60     public void controlLoopElementStateChange(UUID controlLoopElementId,
61             ControlLoopState currentState,
62             ControlLoopOrderedState newState) throws PfModelException {
63         PolicyProvider policyProvider = PolicyHandler.getInstance().getPolicyProvider();
64         switch (newState) {
65             case UNINITIALISED:
66                 try {
67                     deletePolicyData(controlLoopElementId, newState);
68                 } catch (PfModelRuntimeException e) {
69                     LOGGER.debug("Delete policytpes failed", e);
70                 }
71                 break;
72             case PASSIVE:
73                 policyProvider.getIntermediaryApi()
74                     .updateControlLoopElementState(controlLoopElementId, newState,
75                             ControlLoopState.PASSIVE);
76                 break;
77             case RUNNING:
78                 policyProvider.getIntermediaryApi()
79                     .updateControlLoopElementState(controlLoopElementId, newState,
80                             ControlLoopState.RUNNING);
81                 break;
82             default:
83                 LOGGER.debug("Unknown orderedstate {}", newState);
84                 break;
85         }
86     }
87
88     private void deletePolicyData(UUID controlLoopElementId,
89             ControlLoopOrderedState newState) throws PfModelException {
90         PolicyModelsProvider dbProvider = PolicyHandler.getInstance().getDatabaseProvider();
91         PolicyProvider policyProvider = PolicyHandler.getInstance().getPolicyProvider();
92         if (policyMap != null) {
93             // Delete all policies of this controlLoop from policy framework
94             for (Entry<String, String> policy : policyMap.entrySet()) {
95                 dbProvider.deletePolicy(policy.getKey(), policy.getValue());
96             }
97         }
98         if (policyTypeMap != null) {
99             // Delete all policy types of this control loop from policy framework
100             for (Entry<String, String> policy : policyTypeMap.entrySet()) {
101                 dbProvider.deletePolicyType(policy.getKey(), policy.getValue());
102             }
103         }
104         policyProvider.getIntermediaryApi()
105             .updateControlLoopElementState(controlLoopElementId, newState,
106                     ControlLoopState.UNINITIALISED);
107     }
108
109     /**
110      * Callback method to handle an update on a control loop element.
111      *
112      * @param element the information on the control loop element
113      * @param controlLoopDefinition toscaServiceTemplate
114      * @throws PfModelException in case of an exception
115      */
116     @Override
117     public void controlLoopElementUpdate(ControlLoopElement element,
118             ToscaServiceTemplate controlLoopDefinition) throws PfModelException {
119         PolicyModelsProvider dbProvider = PolicyHandler.getInstance().getDatabaseProvider();
120         PolicyProvider policyProvider = PolicyHandler.getInstance().getPolicyProvider();
121
122         policyProvider.getIntermediaryApi()
123             .updateControlLoopElementState(element.getId(), element.getOrderedState(), ControlLoopState.PASSIVE);
124         if (controlLoopDefinition.getPolicyTypes() != null) {
125             for (ToscaPolicyType policyType : controlLoopDefinition.getPolicyTypes().values()) {
126                 policyTypeMap.put(policyType.getName(), policyType.getVersion());
127             }
128             dbProvider.createPolicyTypes(controlLoopDefinition);
129         }
130         if (controlLoopDefinition.getToscaTopologyTemplate().getPolicies() != null) {
131             for (Map<String, ToscaPolicy> foundPolicyMap : controlLoopDefinition
132                             .getToscaTopologyTemplate().getPolicies()) {
133                 for (ToscaPolicy policy : foundPolicyMap.values()) {
134                     policyMap.put(policy.getName(), policy.getVersion());
135                 }
136             }
137             dbProvider.createPolicies(controlLoopDefinition);
138         }
139     }
140
141     /**
142      * Handle controlLoopElement statistics.
143      *
144      * @param controlLoopElementId controlloop element id
145      */
146     @Override
147     public void handleStatistics(UUID controlLoopElementId) {
148         PolicyProvider policyProvider = PolicyHandler.getInstance().getPolicyProvider();
149         ControlLoopElement clElement = policyProvider.getIntermediaryApi()
150                .getControlLoopElement(controlLoopElementId);
151         if (clElement != null) {
152             ClElementStatistics clElementStatistics = new ClElementStatistics();
153             clElementStatistics.setControlLoopState(clElement.getState());
154             clElementStatistics.setTimeStamp(Instant.now());
155             policyProvider.getIntermediaryApi()
156                 .updateControlLoopElementStatistics(controlLoopElementId, clElementStatistics);
157         }
158     }
159 }