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