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.http.main.handler;
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.lang.invoke.MethodHandles;
28 import java.util.UUID;
29 import java.util.concurrent.ConcurrentHashMap;
30 import java.util.concurrent.ExecutorService;
31 import java.util.concurrent.Executors;
32 import javax.validation.ConstraintViolation;
33 import javax.validation.Validation;
34 import javax.validation.ValidationException;
36 import org.apache.commons.lang3.tuple.Pair;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
40 import org.onap.policy.clamp.controlloop.participant.http.main.models.ConfigRequest;
41 import org.onap.policy.clamp.controlloop.participant.http.main.webclient.ClHttpClient;
42 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
43 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
44 import org.onap.policy.common.utils.coder.Coder;
45 import org.onap.policy.common.utils.coder.CoderException;
46 import org.onap.policy.common.utils.coder.StandardCoder;
47 import org.onap.policy.models.base.PfModelException;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.springframework.stereotype.Component;
55 * This class handles implementation of controlLoopElement updates.
58 public class ControlLoopElementHandler implements ControlLoopElementListener, Closeable {
60 private static final Coder CODER = new StandardCoder();
62 private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
64 private ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
66 private Map<ToscaConceptIdentifier, Pair<Integer, String>> restResponseMap = new ConcurrentHashMap<>();
69 private ParticipantIntermediaryApi intermediaryApi;
72 * Handle controlLoopElement statistics.
74 * @param controlLoopElementId controlloop element id
77 public void handleStatistics(UUID controlLoopElementId) throws PfModelException {
78 // Implementation not needed for http participant
83 * Handle a control loop element state change.
85 * @param controlLoopElementId the ID of the control loop element
86 * @param currentState the current state of the control loop element
87 * @param newState the state to which the control loop element is changing to
88 * @throws PfModelException in case of a model exception
91 public void controlLoopElementStateChange(UUID controlLoopElementId, ControlLoopState currentState,
92 ControlLoopOrderedState newState) throws PfModelException {
93 // Implementation not needed for http participant
97 * Callback method to handle an update on a control loop element.
99 * @param element the information on the control loop element
100 * @param nodeTemplate toscaNodeTemplate
103 public void controlLoopElementUpdate(ControlLoopElement element, ToscaNodeTemplate nodeTemplate) {
105 var configRequest = CODER.convert(nodeTemplate.getProperties(), ConfigRequest.class);
106 Set<ConstraintViolation<ConfigRequest>> violations = Validation.buildDefaultValidatorFactory()
107 .getValidator().validate(configRequest);
108 if (violations.isEmpty()) {
109 invokeHttpClient(configRequest);
111 LOGGER.error("Violations found in the config request parameters: {}", violations);
112 throw new ValidationException("Constraint violations in the config request");
114 } catch (CoderException | ValidationException e) {
115 LOGGER.error("Error invoking the http request for the config ", e);
120 * Invoke a runnable thread to execute http requests.
121 * @param configRequest ConfigRequest
123 public void invokeHttpClient(ConfigRequest configRequest) {
124 // Invoke runnable thread to execute https requests of all config entities
125 executor.execute(new ClHttpClient(configRequest, restResponseMap));
129 * Closes this stream and releases any system resources associated
130 * with it. If the stream is already closed then invoking this
131 * method has no effect.
133 * @throws IOException if an I/O error occurs
136 public void close() throws IOException {