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