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