2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.clamp.controlloop.participant.policy.main.handler;
25 import java.time.Instant;
26 import java.util.LinkedHashMap;
28 import java.util.Map.Entry;
29 import java.util.UUID;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType;
36 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
37 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
38 import org.onap.policy.clamp.controlloop.participant.policy.client.PolicyApiHttpClient;
39 import org.onap.policy.clamp.controlloop.participant.policy.client.PolicyPapHttpClient;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.base.PfModelRuntimeException;
42 import org.onap.policy.models.pdp.concepts.DeploymentSubGroup;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.stereotype.Component;
53 * This class handles implementation of controlLoopElement updates.
56 public class ControlLoopElementHandler implements ControlLoopElementListener {
58 private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopElementHandler.class);
59 private final Map<String, String> policyTypeMap = new LinkedHashMap<>();
60 private final Map<String, String> policyMap = new LinkedHashMap<>();
62 private final PolicyApiHttpClient apiHttpClient;
63 private final PolicyPapHttpClient papHttpClient;
66 private ParticipantIntermediaryApi intermediaryApi;
71 * @param apiHttpClient the Policy Api Http Client
72 * @param papHttpClient the Policy Pap Http Client
74 public ControlLoopElementHandler(PolicyApiHttpClient apiHttpClient, PolicyPapHttpClient papHttpClient) {
75 this.papHttpClient = papHttpClient;
76 this.apiHttpClient = apiHttpClient;
80 * Callback method to handle a control loop element state change.
82 * @param controlLoopId the ID of the control loop
83 * @param controlLoopElementId the ID of the control loop element
84 * @param currentState the current state of the control loop element
85 * @param orderedState the state to which the control loop element is changing to
88 public void controlLoopElementStateChange(ToscaConceptIdentifier controlLoopId,
89 UUID controlLoopElementId, ControlLoopState currentState,
90 ControlLoopOrderedState orderedState) {
91 switch (orderedState) {
94 undeployPolicies(controlLoopElementId);
95 deletePolicyData(controlLoopId, controlLoopElementId, orderedState);
96 intermediaryApi.updateControlLoopElementState(controlLoopId,
97 controlLoopElementId, orderedState, ControlLoopState.UNINITIALISED,
98 ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE);
99 } catch (PfModelRuntimeException e) {
100 LOGGER.debug("Undeploying/Deleting policy failed {}", controlLoopElementId, e);
105 undeployPolicies(controlLoopElementId);
106 } catch (PfModelRuntimeException e) {
107 LOGGER.debug("Undeploying policies failed - no policies to undeploy {}", controlLoopElementId);
109 intermediaryApi.updateControlLoopElementState(controlLoopId,
110 controlLoopElementId, orderedState, ControlLoopState.PASSIVE,
111 ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE);
114 LOGGER.info("Running state is not supported");
117 LOGGER.debug("Unknown orderedstate {}", orderedState);
122 private void deletePolicyData(ToscaConceptIdentifier controlLoopId,
123 UUID controlLoopElementId, ControlLoopOrderedState newState) {
124 // Delete all policies of this controlLoop from policy framework
125 for (Entry<String, String> policy : policyMap.entrySet()) {
126 apiHttpClient.deletePolicy(policy.getKey(), policy.getValue());
129 // Delete all policy types of this control loop from policy framework
130 for (Entry<String, String> policyType : policyTypeMap.entrySet()) {
131 apiHttpClient.deletePolicyType(policyType.getKey(), policyType.getValue());
133 policyTypeMap.clear();
134 intermediaryApi.updateControlLoopElementState(controlLoopId,
135 controlLoopElementId, newState, ControlLoopState.UNINITIALISED,
136 ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE);
139 private void deployPolicies(ToscaConceptIdentifier controlLoopId, UUID controlLoopElementId,
140 ControlLoopOrderedState newState) {
141 // Deploy all policies of this controlLoop from Policy Framework
142 if (policyMap.entrySet() != null) {
143 for (Entry<String, String> policy : policyMap.entrySet()) {
144 papHttpClient.handlePolicyDeployOrUndeploy(policy.getKey(), policy.getValue(),
145 DeploymentSubGroup.Action.POST);
147 LOGGER.debug("Policies deployed to {} successfully", controlLoopElementId);
149 LOGGER.debug("No policies to deploy to {}", controlLoopElementId);
151 intermediaryApi.updateControlLoopElementState(controlLoopId,
152 controlLoopElementId, newState, ControlLoopState.PASSIVE,
153 ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE);
156 private void undeployPolicies(UUID controlLoopElementId) {
157 // Undeploy all policies of this controlloop from Policy Framework
158 if (policyMap.entrySet() != null) {
159 for (Entry<String, String> policy : policyMap.entrySet()) {
160 papHttpClient.handlePolicyDeployOrUndeploy(policy.getKey(), policy.getValue(),
161 DeploymentSubGroup.Action.DELETE);
163 LOGGER.debug("Undeployed policies from {} successfully", controlLoopElementId);
165 LOGGER.debug("No policies are deployed to {}", controlLoopElementId);
170 * Callback method to handle an update on a control loop element.
172 * @param element the information on the control loop element
173 * @param clElementDefinition toscaNodeTemplate
174 * @throws PfModelException in case of an exception
177 public void controlLoopElementUpdate(ToscaConceptIdentifier controlLoopId, ControlLoopElement element,
178 ToscaNodeTemplate clElementDefinition)
179 throws PfModelException {
180 ToscaServiceTemplate controlLoopDefinition = element.getToscaServiceTemplateFragment();
181 if (controlLoopDefinition.getToscaTopologyTemplate() != null) {
182 if (controlLoopDefinition.getPolicyTypes() != null) {
183 for (ToscaPolicyType policyType : controlLoopDefinition.getPolicyTypes().values()) {
184 policyTypeMap.put(policyType.getName(), policyType.getVersion());
186 LOGGER.debug("Found Policy Types in control loop definition: {} , Creating Policy Types",
187 controlLoopDefinition.getName());
188 apiHttpClient.createPolicyType(controlLoopDefinition);
190 if (controlLoopDefinition.getToscaTopologyTemplate().getPolicies() != null) {
191 for (Map<String, ToscaPolicy> foundPolicyMap : controlLoopDefinition.getToscaTopologyTemplate()
193 for (ToscaPolicy policy : foundPolicyMap.values()) {
194 policyMap.put(policy.getName(), policy.getVersion());
197 LOGGER.debug("Found Policies in control loop definition: {} , Creating Policies",
198 controlLoopDefinition.getName());
199 apiHttpClient.createPolicy(controlLoopDefinition);
202 deployPolicies(controlLoopId, element.getId(), element.getOrderedState());
206 * Handle controlLoopElement statistics.
208 * @param controlLoopElementId controlloop element id
211 public void handleStatistics(UUID controlLoopElementId) throws PfModelException {
212 var clElement = intermediaryApi.getControlLoopElement(controlLoopElementId);
213 if (clElement != null) {
214 var clElementStatistics = new ClElementStatistics();
215 clElementStatistics.setControlLoopState(clElement.getState());
216 clElementStatistics.setTimeStamp(Instant.now());
217 intermediaryApi.updateControlLoopElementStatistics(controlLoopElementId, clElementStatistics);