5a0ddabfd7e300332c068e2a1d746aef1d7c25a0
[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.dcae.main.handler;
22
23 import java.time.Instant;
24 import java.util.UUID;
25 import java.util.concurrent.TimeUnit;
26 import lombok.Setter;
27 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
28 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
31 import org.onap.policy.clamp.controlloop.participant.dcae.httpclient.ClampHttpClient;
32 import org.onap.policy.clamp.controlloop.participant.dcae.httpclient.ConsulDcaeHttpClient;
33 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.ParticipantDcaeParameters;
34 import org.onap.policy.clamp.controlloop.participant.dcae.model.Loop;
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.common.utils.resources.ResourceUtils;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.stereotype.Component;
43
44 /**
45  * This class handles implementation of controlLoopElement updates.
46  */
47 @Component
48 public class ControlLoopElementHandler implements ControlLoopElementListener {
49
50     private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopElementHandler.class);
51     private final ClampHttpClient clampClient;
52     private final ConsulDcaeHttpClient consulClient;
53
54     @Setter
55     private ParticipantIntermediaryApi intermediaryApi;
56
57     private static final String LOOP = "pmsh_loop";
58     private static final String TEMPLATE = "LOOP_TEMPLATE_k8s_pmsh";
59     private static final String POLICY = "policy";
60
61     private static final String BLUEPRINT_DEPLOYED = "BLUEPRINT_DEPLOYED";
62     private static final String MICROSERVICE_INSTALLED_SUCCESSFULLY = "MICROSERVICE_INSTALLED_SUCCESSFULLY";
63
64     private int checkCount;
65     private int secCount;
66
67     private String bodyConsul;
68
69     /**
70      * Constructor.
71      *
72      * @param clampClient the CLAMP client
73      * @param consulClient the Consul client
74      */
75     public ControlLoopElementHandler(ClampHttpClient clampClient, ConsulDcaeHttpClient consulClient,
76             ParticipantDcaeParameters parameters) {
77         this.clampClient = clampClient;
78         this.consulClient = consulClient;
79         this.checkCount = parameters.getCheckCount();
80         this.secCount = parameters.getSecCount();
81         bodyConsul = ResourceUtils.getResourceAsString(parameters.getJsonBodyConsulPath());
82     }
83
84     /**
85      * Callback method to handle a control loop element state change.
86      *
87      * @param controlLoopElementId the ID of the control loop element
88      * @param currentState the current state of the control loop element
89      * @param newState the state to which the control loop element is changing to
90      */
91     @Override
92     public void controlLoopElementStateChange(UUID controlLoopElementId, ControlLoopState currentState,
93             ControlLoopOrderedState newState) {
94         switch (newState) {
95             case UNINITIALISED:
96                 Loop loop = clampClient.getstatus(LOOP);
97                 if (loop != null) {
98                     clampClient.undeploy(LOOP);
99                     intermediaryApi.updateControlLoopElementState(controlLoopElementId, newState,
100                             ControlLoopState.UNINITIALISED);
101                 }
102                 break;
103             case PASSIVE:
104                 intermediaryApi.updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.PASSIVE);
105                 break;
106             case RUNNING:
107                 intermediaryApi.updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.RUNNING);
108                 break;
109             default:
110                 LOGGER.debug("Unknown orderedstate {}", newState);
111                 break;
112         }
113     }
114
115     private Loop getStatus() throws PfModelException {
116         Loop loop = clampClient.getstatus(LOOP);
117         if (loop == null) {
118             loop = clampClient.create(LOOP, TEMPLATE);
119         }
120         if (loop == null) {
121             throw new PfModelException(null, "");
122         }
123         return loop;
124     }
125
126     private void deploy() throws PfModelException {
127         if (!consulClient.deploy(POLICY, bodyConsul)) {
128             throw new PfModelException(null, "deploy to consul failed");
129         }
130         if (!clampClient.deploy(LOOP)) {
131             throw new PfModelException(null, "deploy failed");
132         }
133     }
134
135     /**
136      * Callback method to handle an update on a control loop element.
137      *
138      * @param element the information on the control loop element
139      * @param controlLoopDefinition toscaServiceTemplate
140      * @throws PfModelException in case of an exception
141      */
142     @Override
143     public void controlLoopElementUpdate(ControlLoopElement element, ToscaServiceTemplate controlLoopDefinition)
144             throws PfModelException {
145         try {
146             Loop loop = getStatus();
147
148             if (BLUEPRINT_DEPLOYED.equals(ClampHttpClient.getStatusCode(loop))) {
149                 deploy();
150                 boolean deployedFlag = false;
151                 for (int i = 0; i < checkCount; i++) {
152                     // sleep 10 seconds
153                     TimeUnit.SECONDS.sleep(secCount);
154                     loop = getStatus();
155                     String status = ClampHttpClient.getStatusCode(loop);
156                     if (MICROSERVICE_INSTALLED_SUCCESSFULLY.equals(status)) {
157                         intermediaryApi.updateControlLoopElementState(element.getId(), element.getOrderedState(),
158                                 ControlLoopState.PASSIVE);
159                         deployedFlag = true;
160                         break;
161                     }
162                 }
163                 if (!deployedFlag) {
164                     LOGGER.warn("DCAE is not deployed properly, ClElement state will be UNINITIALISED2PASSIVE");
165                     intermediaryApi.updateControlLoopElementState(element.getId(), element.getOrderedState(),
166                             ControlLoopState.UNINITIALISED2PASSIVE);
167                 }
168             }
169         } catch (PfModelException e) {
170             throw e;
171         } catch (Exception e) {
172             throw new PfModelException(null, e.getMessage(), e);
173         }
174     }
175
176     /**
177      * Handle controlLoopElement statistics.
178      *
179      * @param controlLoopElementId controlloop element id
180      */
181     @Override
182     public void handleStatistics(UUID controlLoopElementId) {
183         ControlLoopElement clElement = intermediaryApi.getControlLoopElement(controlLoopElementId);
184         if (clElement != null) {
185             ClElementStatistics clElementStatistics = new ClElementStatistics();
186             clElementStatistics.setControlLoopState(clElement.getState());
187             clElementStatistics.setTimeStamp(Instant.now());
188             intermediaryApi.updateControlLoopElementStatistics(controlLoopElementId, clElementStatistics);
189         }
190     }
191 }