96677f3207ab0019ffba5931ec4559cf2277d2e3
[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.io.Closeable;
24 import java.io.IOException;
25 import java.time.Instant;
26 import java.util.UUID;
27 import java.util.concurrent.TimeUnit;
28 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
32 import org.onap.policy.clamp.controlloop.participant.dcae.httpclient.ClampHttpClient;
33 import org.onap.policy.clamp.controlloop.participant.dcae.httpclient.ConsulDcaeHttpClient;
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.common.endpoints.parameters.RestServerParameters;
37 import org.onap.policy.models.base.PfModelException;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * This class handles implementation of controlLoopElement updates.
44  */
45 public class ControlLoopElementHandler implements ControlLoopElementListener, Closeable {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopElementHandler.class);
48     private final ClampHttpClient clampClient;
49     private final ConsulDcaeHttpClient consulClient;
50
51     private static final String LOOP = "pmsh_loop";
52     private static final String TEMPLATE = "LOOP_TEMPLATE_k8s_pmsh";
53
54     private static final String BLUEPRINT_DEPLOYED = "BLUEPRINT_DEPLOYED";
55     private static final String MICROSERVICE_INSTALLED_SUCCESSFULLY = "MICROSERVICE_INSTALLED_SUCCESSFULLY";
56     private static final int CHECK_COUNT = 10;
57
58     private static final String BODY_CONSUL =
59             "{ \"subscription\": { \"subscriptionName\": \"subscriptiona\", \"administrativeState\": \"UNLOCKED\", "
60                     + "\"fileBasedGP\": 15, \"fileLocation\": \"/pm/pm.xml\", \"nfFilter\": "
61                     + "{ \"nfNames\": [ \"^pnf1.*\" ], \"modelInvariantIDs\": "
62                     + "[ \"5845y423-g654-6fju-po78-8n53154532k6\", \"7129e420-d396-4efb-af02-6b83499b12f8\" ], "
63                     + "\"modelVersionIDs\": [ \"e80a6ae3-cafd-4d24-850d-e14c084a5ca9\" ] }, \"measurementGroups\": "
64                     + "[ { \"measurementGroup\": { \"measurementTypes\": [ { \"measurementType\": \"countera\" }, "
65                     + "{ \"measurementType\": \"counterb\" } ], \"managedObjectDNsBasic\": [ { \"DN\": \"dna\" }, "
66                     + "{ \"DN\": \"dnb\" } ] } }, { \"measurementGroup\": { \"measurementTypes\": "
67                     + "[ { \"measurementType\": \"counterc\" }, { \"measurementType\": \"counterd\" } ], "
68                     + "\"managedObjectDNsBasic\": " + "[ { \"DN\": \"dnc\" }, { \"DN\": \"dnd\" } ] } } ] } }";
69
70     /**
71      * Constructor.
72      */
73     public ControlLoopElementHandler(RestServerParameters clampParameters, RestServerParameters consulParameters) {
74         clampClient = new ClampHttpClient(clampParameters);
75         consulClient = new ConsulDcaeHttpClient(consulParameters);
76     }
77
78     /**
79      * Callback method to handle a control loop element state change.
80      *
81      * @param controlLoopElementId the ID of the control loop element
82      * @param currentState the current state of the control loop element
83      * @param newState the state to which the control loop element is changing to
84      */
85     @Override
86     public void controlLoopElementStateChange(UUID controlLoopElementId, ControlLoopState currentState,
87             ControlLoopOrderedState newState) {
88         switch (newState) {
89             case UNINITIALISED:
90                 Loop loop = clampClient.getstatus(LOOP);
91                 if (loop != null) {
92                     clampClient.undeploy(LOOP);
93                     DcaeHandler.getInstance().getDcaeProvider().getIntermediaryApi()
94                         .updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.UNINITIALISED);
95                 }
96                 break;
97             case PASSIVE:
98                 DcaeHandler.getInstance().getDcaeProvider().getIntermediaryApi()
99                     .updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.PASSIVE);
100                 break;
101             case RUNNING:
102                 DcaeHandler.getInstance().getDcaeProvider().getIntermediaryApi()
103                     .updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.RUNNING);
104                 break;
105             default:
106                 LOGGER.debug("Unknown orderedstate {}", newState);
107                 break;
108         }
109     }
110
111     private Loop getStatus() throws PfModelException {
112         Loop loop = clampClient.getstatus(LOOP);
113         if (loop == null) {
114             loop = clampClient.create(LOOP, TEMPLATE);
115         }
116         if (loop == null) {
117             throw new PfModelException(null, "");
118         }
119         return loop;
120     }
121
122     private void deploy() throws PfModelException {
123         if (!consulClient.deploy(BODY_CONSUL)) {
124             throw new PfModelException(null, "deploy to consul failed");
125         }
126         if (!clampClient.deploy(LOOP)) {
127             throw new PfModelException(null, "deploy failed");
128         }
129     }
130
131     /**
132      * Callback method to handle an update on a control loop element.
133      *
134      * @param element the information on the control loop element
135      * @param controlLoopDefinition toscaServiceTemplate
136      * @throws PfModelException in case of an exception
137      */
138     @Override
139     public void controlLoopElementUpdate(ControlLoopElement element, ToscaServiceTemplate controlLoopDefinition)
140             throws PfModelException {
141         try {
142             Loop loop = getStatus();
143
144             if (BLUEPRINT_DEPLOYED.equals(ClampHttpClient.getStatusCode(loop))) {
145                 deploy();
146                 boolean deployedFlag = false;
147                 for (int i = 0; i < CHECK_COUNT; i++) {
148                     //sleep 10 seconds
149                     TimeUnit.SECONDS.sleep(CHECK_COUNT);
150                     loop = getStatus();
151                     String status = ClampHttpClient.getStatusCode(loop);
152                     if (MICROSERVICE_INSTALLED_SUCCESSFULLY.equals(status)) {
153                         DcaeHandler.getInstance().getDcaeProvider().getIntermediaryApi()
154                             .updateControlLoopElementState(element.getId(), element.getOrderedState(),
155                                             ControlLoopState.PASSIVE);
156                         deployedFlag = true;
157                         break;
158                     }
159                 }
160                 if (!deployedFlag) {
161                     LOGGER.warn("DCAE is not deployed properly, ClElement state will be UNINITIALISED2PASSIVE");
162                     DcaeHandler.getInstance().getDcaeProvider().getIntermediaryApi()
163                         .updateControlLoopElementState(element.getId(), element.getOrderedState(),
164                                       ControlLoopState.UNINITIALISED2PASSIVE);
165                 }
166             }
167         } catch (PfModelException e) {
168             throw e;
169         } catch (Exception e) {
170             throw new PfModelException(null, e.getMessage(), e);
171         }
172     }
173
174     /**
175      * Handle controlLoopElement statistics.
176      *
177      * @param controlLoopElementId controlloop element id
178      */
179     @Override
180     public void handleStatistics(UUID controlLoopElementId) {
181         ControlLoopElement clElement = DcaeHandler.getInstance().getDcaeProvider()
182                 .getIntermediaryApi().getControlLoopElement(controlLoopElementId);
183         if (clElement != null) {
184             ClElementStatistics clElementStatistics = new ClElementStatistics();
185             clElementStatistics.setControlLoopState(clElement.getState());
186             clElementStatistics.setTimeStamp(Instant.now());
187             DcaeHandler.getInstance().getDcaeProvider().getIntermediaryApi()
188                 .updateControlLoopElementStatistics(controlLoopElementId, clElementStatistics);
189         }
190     }
191
192     @Override
193     public void close() throws IOException {
194         clampClient.close();
195         consulClient.close();
196     }
197 }