2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2021 AT&T Intellectual Property.
5 * Modifications Copyright (C) 2021 Bell Canada. 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.pap.main.rest;
25 import java.util.List;
26 import javax.ws.rs.core.Response;
27 import org.apache.commons.lang3.tuple.Pair;
28 import org.onap.policy.models.base.PfModelException;
29 import org.onap.policy.models.pap.concepts.PdpGroupStateChangeResponse;
30 import org.onap.policy.models.pdp.concepts.Pdp;
31 import org.onap.policy.models.pdp.concepts.PdpGroup;
32 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
33 import org.onap.policy.models.pdp.enums.PdpState;
34 import org.onap.policy.models.provider.PolicyModelsProvider;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
36 import org.onap.policy.pap.main.comm.PdpMessageGenerator;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * Provider for PAP component to change state of PDP group.
43 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
45 public class PdpGroupStateChangeProvider extends PdpMessageGenerator {
47 private static final Logger LOGGER = LoggerFactory.getLogger(PdpGroupStateChangeProvider.class);
50 * Constructs the object.
52 public PdpGroupStateChangeProvider() {
57 * Changes state of a PDP group.
59 * @param groupName name of the PDP group
60 * @param pdpGroupState state of the PDP group
61 * @return a pair containing the status and the response
62 * @throws PfModelException in case of errors
64 public Pair<Response.Status, PdpGroupStateChangeResponse> changeGroupState(final String groupName,
65 final PdpState pdpGroupState) throws PfModelException {
66 synchronized (updateLock) {
67 switch (pdpGroupState) {
69 handleActiveState(groupName);
72 handlePassiveState(groupName);
75 throw new PfModelException(Response.Status.BAD_REQUEST,
76 "Only ACTIVE or PASSIVE state changes are allowed");
78 return Pair.of(Response.Status.OK, new PdpGroupStateChangeResponse());
82 private void handleActiveState(final String groupName) throws PfModelException {
83 try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
84 final List<PdpGroup> pdpGroups = databaseProvider.getPdpGroups(groupName);
85 if (!pdpGroups.isEmpty() && !PdpState.ACTIVE.equals(pdpGroups.get(0).getPdpGroupState())) {
86 updatePdpGroupAndPdp(databaseProvider, pdpGroups, PdpState.ACTIVE);
87 sendPdpMessage(pdpGroups.get(0), PdpState.ACTIVE, databaseProvider);
92 private void handlePassiveState(final String groupName) throws PfModelException {
93 try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
94 final List<PdpGroup> pdpGroups = databaseProvider.getPdpGroups(groupName);
95 if (!pdpGroups.isEmpty() && !PdpState.PASSIVE.equals(pdpGroups.get(0).getPdpGroupState())) {
96 updatePdpGroupAndPdp(databaseProvider, pdpGroups, PdpState.PASSIVE);
97 sendPdpMessage(pdpGroups.get(0), PdpState.PASSIVE, databaseProvider);
102 private void updatePdpGroupAndPdp(final PolicyModelsProvider databaseProvider, final List<PdpGroup> pdpGroups,
103 final PdpState pdpState) throws PfModelException {
104 pdpGroups.get(0).setPdpGroupState(pdpState);
105 for (final PdpSubGroup subGroup : pdpGroups.get(0).getPdpSubgroups()) {
106 for (final Pdp pdp : subGroup.getPdpInstances()) {
107 pdp.setPdpState(pdpState);
110 databaseProvider.updatePdpGroups(pdpGroups);
112 LOGGER.debug("Updated PdpGroup and Pdp in DB - {} ", pdpGroups);
115 private void sendPdpMessage(final PdpGroup pdpGroup, final PdpState pdpState,
116 final PolicyModelsProvider databaseProvider) throws PfModelException {
117 String pdpGroupName = pdpGroup.getName();
118 for (final PdpSubGroup subGroup : pdpGroup.getPdpSubgroups()) {
119 List<ToscaPolicy> policies = getToscaPolicies(subGroup, databaseProvider);
120 for (final Pdp pdp : subGroup.getPdpInstances()) {
121 String pdpInstanceId = pdp.getInstanceId();
122 final var pdpUpdatemessage =
123 createPdpUpdateMessage(pdpGroup.getName(), subGroup, pdp.getInstanceId(),
125 final var pdpStateChangeMessage =
126 createPdpStateChangeMessage(pdpGroupName, subGroup, pdpInstanceId, pdpState);
127 updateDeploymentStatus(pdpGroupName, subGroup.getPdpType(), pdpInstanceId,
128 pdpStateChangeMessage.getState(), databaseProvider, pdpUpdatemessage.getPoliciesToBeDeployed());
129 requestMap.addRequest(pdpUpdatemessage, pdpStateChangeMessage);
130 LOGGER.debug("Sent PdpUpdate message - {}", pdpUpdatemessage);
131 LOGGER.debug("Sent PdpStateChange message - {}", pdpStateChangeMessage);