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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.participant.dcae.main.handler;
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;
43 * This class handles implementation of controlLoopElement updates.
45 public class ControlLoopElementHandler implements ControlLoopElementListener, Closeable {
47 private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopElementHandler.class);
48 private final ClampHttpClient clampClient;
49 private final ConsulDcaeHttpClient consulClient;
51 private static final String LOOP = "pmsh_loop";
52 private static final String TEMPLATE = "LOOP_TEMPLATE_k8s_pmsh";
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;
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\" } ] } } ] } }";
73 public ControlLoopElementHandler(RestServerParameters clampParameters, RestServerParameters consulParameters) {
74 clampClient = new ClampHttpClient(clampParameters);
75 consulClient = new ConsulDcaeHttpClient(consulParameters);
79 * Callback method to handle a control loop element state change.
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
86 public void controlLoopElementStateChange(UUID controlLoopElementId, ControlLoopState currentState,
87 ControlLoopOrderedState newState) {
90 Loop loop = clampClient.getstatus(LOOP);
92 clampClient.undeploy(LOOP);
93 DcaeHandler.getInstance().getDcaeProvider().getIntermediaryApi()
94 .updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.UNINITIALISED);
98 DcaeHandler.getInstance().getDcaeProvider().getIntermediaryApi()
99 .updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.PASSIVE);
102 DcaeHandler.getInstance().getDcaeProvider().getIntermediaryApi()
103 .updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.RUNNING);
106 LOGGER.debug("Unknown orderedstate {}", newState);
111 private Loop getStatus() throws PfModelException {
112 Loop loop = clampClient.getstatus(LOOP);
114 loop = clampClient.create(LOOP, TEMPLATE);
117 throw new PfModelException(null, "");
122 private void deploy() throws PfModelException {
123 if (!consulClient.deploy(BODY_CONSUL)) {
124 throw new PfModelException(null, "deploy to consul failed");
126 if (!clampClient.deploy(LOOP)) {
127 throw new PfModelException(null, "deploy failed");
132 * Callback method to handle an update on a control loop element.
134 * @param element the information on the control loop element
135 * @param controlLoopDefinition toscaServiceTemplate
136 * @throws PfModelException in case of an exception
139 public void controlLoopElementUpdate(ControlLoopElement element, ToscaServiceTemplate controlLoopDefinition)
140 throws PfModelException {
142 Loop loop = getStatus();
144 if (BLUEPRINT_DEPLOYED.equals(ClampHttpClient.getStatusCode(loop))) {
146 boolean deployedFlag = false;
147 for (int i = 0; i < CHECK_COUNT; i++) {
149 TimeUnit.SECONDS.sleep(CHECK_COUNT);
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);
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);
167 } catch (PfModelException e) {
169 } catch (Exception e) {
170 throw new PfModelException(null, e.getMessage(), e);
175 * Handle controlLoopElement statistics.
177 * @param controlLoopElementId controlloop element id
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);
193 public void close() throws IOException {
195 consulClient.close();