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