2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.lifecycle;
23 import java.util.List;
24 import java.util.function.BiFunction;
25 import lombok.NonNull;
26 import org.onap.policy.drools.system.PolicyController;
27 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
28 import org.onap.policy.models.pdp.concepts.PdpStateChange;
29 import org.onap.policy.models.pdp.concepts.PdpUpdate;
30 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
31 import org.onap.policy.models.pdp.enums.PdpState;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Support class for default functionality for running states.
40 public abstract class LifecycleStateRunning extends LifecycleStateDefault {
42 private static final Logger logger = LoggerFactory.getLogger(LifecycleState.class);
44 protected abstract boolean stateChangeToPassive(@NonNull PdpStateChange change);
46 protected abstract boolean stateChangeToActive(@NonNull PdpStateChange change);
48 protected abstract boolean deployPolicy(@NonNull PolicyController controller, @NonNull ToscaPolicy policy);
50 protected abstract boolean undeployPolicy(@NonNull PolicyController controller, @NonNull ToscaPolicy policy);
52 protected LifecycleStateRunning(LifecycleFsm manager) {
57 public boolean start() {
58 logger.warn("{}: start", this);
63 public boolean stop() {
65 boolean success = fsm.statusAction(PdpState.TERMINATED, null);
66 success = fsm.stopAction() && success;
67 return transitionToState(new LifecycleStateTerminated(fsm)) && success;
72 public void shutdown() {
80 public boolean status() {
82 return fsm.statusAction();
87 public boolean isAlive() {
92 public boolean stateChange(@NonNull PdpStateChange change) {
94 if (change.getState() == PdpState.PASSIVE) {
95 return stateChangeToPassive(change);
98 if (change.getState() == PdpState.ACTIVE) {
99 return stateChangeToActive(change);
102 logger.warn("{}: state-change: {}", this, change);
104 invalidStateChange(change);
110 public boolean update(@NonNull PdpUpdate update) {
112 if (update.getPdpHeartbeatIntervalMs() != null
113 && !fsm.setStatusIntervalAction(update.getPdpHeartbeatIntervalMs() / 1000)) {
114 fsm.statusAction(response(update.getRequestId(), PdpResponseStatus.FAIL,
115 "invalid interval: " + update.getPdpHeartbeatIntervalMs() + " seconds"));
119 fsm.setGroupAction(update.getPdpGroup(), update.getPdpSubgroup());
121 if (!updatePolicies(update.getPolicies())) {
122 fsm.statusAction(response(update.getRequestId(), PdpResponseStatus.FAIL, "cannot process policies"));
126 return fsm.statusAction(response(update.getRequestId(), PdpResponseStatus.SUCCESS, null));
130 protected boolean updatePolicies(List<ToscaPolicy> policies) {
131 if (policies == null) {
135 // Note that PAP sends the list of all ACTIVE policies with every
136 // UPDATE message. First, we will undeploy all policies that are
137 // running but are not present in this list. This will include
138 // policies that are overridden by a different version. Second,
139 // we will deploy those policies that are not installed but
140 // resent in this list.
142 boolean success = undeployPolicies(policies);
143 return deployPolicies(policies) && success;
146 protected boolean deployPolicies(List<ToscaPolicy> policies) {
147 return syncPolicies(fsm.getDeployablePoliciesAction(policies), this::deployPolicy);
150 protected boolean undeployPolicies(List<ToscaPolicy> policies) {
151 return syncPolicies(fsm.getUndeployablePoliciesAction(policies), this::undeployPolicy);
154 protected boolean syncPolicies(List<ToscaPolicy> policies,
155 BiFunction<PolicyController, ToscaPolicy, Boolean> sync) {
156 boolean success = true;
157 for (ToscaPolicy policy : policies) {
158 ToscaPolicyTypeIdentifier policyType = policy.getTypeIdentifier();
159 PolicyController controller = fsm.getController(policyType);
160 if (controller == null) {
161 logger.warn("no controller found for {}", policyType);
166 success = sync.apply(controller, policy) && success;
172 private void invalidStateChange(PdpStateChange change) {
173 logger.warn("{}: state-change: {}", this, change);
174 fsm.statusAction(response(change.getRequestId(), PdpResponseStatus.FAIL,
175 "invalid state change to " + change.getState()));
178 protected PdpResponseDetails response(String requestId, PdpResponseStatus responseStatus, String message) {
179 PdpResponseDetails response = new PdpResponseDetails();
180 response.setResponseTo(requestId);
181 response.setResponseStatus(responseStatus);
182 if (message != null) {
183 response.setResponseMessage(message);