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