6c5ff1d784023280d253ebd8b5435f6e7042cdd5
[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.model.Loop;
34 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
35 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
36 import org.onap.policy.models.base.PfModelException;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.stereotype.Component;
41
42 /**
43  * This class handles implementation of controlLoopElement updates.
44  */
45 @Component
46 public class ControlLoopElementHandler implements ControlLoopElementListener {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopElementHandler.class);
49     private final ClampHttpClient clampClient;
50     private final ConsulDcaeHttpClient consulClient;
51
52     @Setter
53     private ParticipantIntermediaryApi intermediaryApi;
54
55     private static final String LOOP = "pmsh_loop";
56     private static final String TEMPLATE = "LOOP_TEMPLATE_k8s_pmsh";
57     private static final String POLICY = "policy";
58
59     private static final String BLUEPRINT_DEPLOYED = "BLUEPRINT_DEPLOYED";
60     private static final String MICROSERVICE_INSTALLED_SUCCESSFULLY = "MICROSERVICE_INSTALLED_SUCCESSFULLY";
61     private static final int CHECK_COUNT = 10;
62
63     private static final String BODY_CONSUL =
64             "{ \"subscription\": { \"subscriptionName\": \"subscriptiona\", \"administrativeState\": \"UNLOCKED\", "
65                     + "\"fileBasedGP\": 15, \"fileLocation\": \"/pm/pm.xml\", \"nfFilter\": "
66                     + "{ \"nfNames\": [ \"^pnf1.*\" ], \"modelInvariantIDs\": "
67                     + "[ \"5845y423-g654-6fju-po78-8n53154532k6\", \"7129e420-d396-4efb-af02-6b83499b12f8\" ], "
68                     + "\"modelVersionIDs\": [ \"e80a6ae3-cafd-4d24-850d-e14c084a5ca9\" ] }, \"measurementGroups\": "
69                     + "[ { \"measurementGroup\": { \"measurementTypes\": [ { \"measurementType\": \"countera\" }, "
70                     + "{ \"measurementType\": \"counterb\" } ], \"managedObjectDNsBasic\": [ { \"DN\": \"dna\" }, "
71                     + "{ \"DN\": \"dnb\" } ] } }, { \"measurementGroup\": { \"measurementTypes\": "
72                     + "[ { \"measurementType\": \"counterc\" }, { \"measurementType\": \"counterd\" } ], "
73                     + "\"managedObjectDNsBasic\": " + "[ { \"DN\": \"dnc\" }, { \"DN\": \"dnd\" } ] } } ] } }";
74
75     /**
76      * Constructor.
77      *
78      * @param clampClient the CLAMP client
79      * @param consulClient the Consul client
80      */
81     public ControlLoopElementHandler(ClampHttpClient clampClient, ConsulDcaeHttpClient consulClient) {
82         this.clampClient = clampClient;
83         this.consulClient = consulClient;
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                 Loop 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         Loop 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, BODY_CONSUL)) {
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 controlLoopDefinition toscaServiceTemplate
142      * @throws PfModelException in case of an exception
143      */
144     @Override
145     public void controlLoopElementUpdate(ControlLoopElement element, ToscaServiceTemplate controlLoopDefinition)
146             throws PfModelException {
147         try {
148             Loop loop = getStatus();
149
150             if (BLUEPRINT_DEPLOYED.equals(ClampHttpClient.getStatusCode(loop))) {
151                 deploy();
152                 boolean deployedFlag = false;
153                 for (int i = 0; i < CHECK_COUNT; i++) {
154                     // sleep 10 seconds
155                     TimeUnit.SECONDS.sleep(CHECK_COUNT);
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 (Exception e) {
174             throw new PfModelException(null, e.getMessage(), e);
175         }
176     }
177
178     /**
179      * Handle controlLoopElement statistics.
180      *
181      * @param controlLoopElementId controlloop element id
182      */
183     @Override
184     public void handleStatistics(UUID controlLoopElementId) {
185         ControlLoopElement clElement = intermediaryApi.getControlLoopElement(controlLoopElementId);
186         if (clElement != null) {
187             ClElementStatistics clElementStatistics = new ClElementStatistics();
188             clElementStatistics.setControlLoopState(clElement.getState());
189             clElementStatistics.setTimeStamp(Instant.now());
190             intermediaryApi.updateControlLoopElementStatistics(controlLoopElementId, clElementStatistics);
191         }
192     }
193 }