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.policy.main.handler;
23 import java.time.Instant;
24 import java.util.LinkedHashMap;
26 import java.util.Map.Entry;
27 import java.util.UUID;
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.intermediary.api.ControlLoopElementListener;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.base.PfModelRuntimeException;
35 import org.onap.policy.models.provider.PolicyModelsProvider;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
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 {
47 private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopElementHandler.class);
48 private static final Map<String, String> policyTypeMap = new LinkedHashMap<>();
49 private static final Map<String, String> policyMap = new LinkedHashMap<>();
52 * Callback method to handle a control loop element state change.
54 * @param controlLoopElementId the ID of the control loop element
55 * @param currentState the current state of the control loop element
56 * @param newState the state to which the control loop element is changing to
57 * @throws PfModelException in case of an exception
60 public void controlLoopElementStateChange(UUID controlLoopElementId,
61 ControlLoopState currentState,
62 ControlLoopOrderedState newState) throws PfModelException {
63 PolicyProvider policyProvider = PolicyHandler.getInstance().getPolicyProvider();
67 deletePolicyData(controlLoopElementId, newState);
68 } catch (PfModelRuntimeException e) {
69 LOGGER.debug("Delete policytpes failed", e);
73 policyProvider.getIntermediaryApi()
74 .updateControlLoopElementState(controlLoopElementId, newState,
75 ControlLoopState.PASSIVE);
78 policyProvider.getIntermediaryApi()
79 .updateControlLoopElementState(controlLoopElementId, newState,
80 ControlLoopState.RUNNING);
83 LOGGER.debug("Unknown orderedstate {}", newState);
88 private void deletePolicyData(UUID controlLoopElementId,
89 ControlLoopOrderedState newState) throws PfModelException {
90 PolicyModelsProvider dbProvider = PolicyHandler.getInstance().getDatabaseProvider();
91 PolicyProvider policyProvider = PolicyHandler.getInstance().getPolicyProvider();
92 if (policyMap != null) {
93 // Delete all policies of this controlLoop from policy framework
94 for (Entry<String, String> policy : policyMap.entrySet()) {
95 dbProvider.deletePolicy(policy.getKey(), policy.getValue());
98 if (policyTypeMap != null) {
99 // Delete all policy types of this control loop from policy framework
100 for (Entry<String, String> policy : policyTypeMap.entrySet()) {
101 dbProvider.deletePolicyType(policy.getKey(), policy.getValue());
104 policyProvider.getIntermediaryApi()
105 .updateControlLoopElementState(controlLoopElementId, newState,
106 ControlLoopState.UNINITIALISED);
110 * Callback method to handle an update on a control loop element.
112 * @param element the information on the control loop element
113 * @param controlLoopDefinition toscaServiceTemplate
114 * @throws PfModelException in case of an exception
117 public void controlLoopElementUpdate(ControlLoopElement element,
118 ToscaServiceTemplate controlLoopDefinition) throws PfModelException {
119 PolicyModelsProvider dbProvider = PolicyHandler.getInstance().getDatabaseProvider();
120 PolicyProvider policyProvider = PolicyHandler.getInstance().getPolicyProvider();
122 policyProvider.getIntermediaryApi()
123 .updateControlLoopElementState(element.getId(), element.getOrderedState(), ControlLoopState.PASSIVE);
124 if (controlLoopDefinition.getPolicyTypes() != null) {
125 for (ToscaPolicyType policyType : controlLoopDefinition.getPolicyTypes().values()) {
126 policyTypeMap.put(policyType.getName(), policyType.getVersion());
128 dbProvider.createPolicyTypes(controlLoopDefinition);
130 if (controlLoopDefinition.getToscaTopologyTemplate().getPolicies() != null) {
131 for (Map<String, ToscaPolicy> foundPolicyMap : controlLoopDefinition
132 .getToscaTopologyTemplate().getPolicies()) {
133 for (ToscaPolicy policy : foundPolicyMap.values()) {
134 policyMap.put(policy.getName(), policy.getVersion());
137 dbProvider.createPolicies(controlLoopDefinition);
142 * Handle controlLoopElement statistics.
144 * @param controlLoopElementId controlloop element id
147 public void handleStatistics(UUID controlLoopElementId) {
148 PolicyProvider policyProvider = PolicyHandler.getInstance().getPolicyProvider();
149 ControlLoopElement clElement = policyProvider.getIntermediaryApi()
150 .getControlLoopElement(controlLoopElementId);
151 if (clElement != null) {
152 ClElementStatistics clElementStatistics = new ClElementStatistics();
153 clElementStatistics.setControlLoopState(clElement.getState());
154 clElementStatistics.setTimeStamp(Instant.now());
155 policyProvider.getIntermediaryApi()
156 .updateControlLoopElementStatistics(controlLoopElementId, clElementStatistics);