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