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