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;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.stereotype.Service;
43 * Provider for PAP component to change state of PDP group.
45 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
48 public class PdpGroupStateChangeProvider extends PdpMessageGenerator {
50 private static final Logger LOGGER = LoggerFactory.getLogger(PdpGroupStateChangeProvider.class);
53 * Constructs the object.
55 public PdpGroupStateChangeProvider() {
60 * Changes state of a PDP group.
62 * @param groupName name of the PDP group
63 * @param pdpGroupState state of the PDP group
64 * @return a pair containing the status and the response
65 * @throws PfModelException in case of errors
67 public Pair<HttpStatus, PdpGroupStateChangeResponse> changeGroupState(final String groupName,
68 final PdpState pdpGroupState) throws PfModelException {
69 synchronized (updateLock) {
70 switch (pdpGroupState) {
72 handleActiveState(groupName);
75 handlePassiveState(groupName);
78 throw new PfModelException(Response.Status.BAD_REQUEST,
79 "Only ACTIVE or PASSIVE state changes are allowed");
81 return Pair.of(HttpStatus.OK, new PdpGroupStateChangeResponse());
85 private void handleActiveState(final String groupName) throws PfModelException {
86 try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
87 final List<PdpGroup> pdpGroups = databaseProvider.getPdpGroups(groupName);
88 if (!pdpGroups.isEmpty() && !PdpState.ACTIVE.equals(pdpGroups.get(0).getPdpGroupState())) {
89 updatePdpGroupAndPdp(databaseProvider, pdpGroups, PdpState.ACTIVE);
90 sendPdpMessage(pdpGroups.get(0), PdpState.ACTIVE, databaseProvider);
95 private void handlePassiveState(final String groupName) throws PfModelException {
96 try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
97 final List<PdpGroup> pdpGroups = databaseProvider.getPdpGroups(groupName);
98 if (!pdpGroups.isEmpty() && !PdpState.PASSIVE.equals(pdpGroups.get(0).getPdpGroupState())) {
99 updatePdpGroupAndPdp(databaseProvider, pdpGroups, PdpState.PASSIVE);
100 sendPdpMessage(pdpGroups.get(0), PdpState.PASSIVE, databaseProvider);
105 private void updatePdpGroupAndPdp(final PolicyModelsProvider databaseProvider, final List<PdpGroup> pdpGroups,
106 final PdpState pdpState) throws PfModelException {
107 pdpGroups.get(0).setPdpGroupState(pdpState);
108 for (final PdpSubGroup subGroup : pdpGroups.get(0).getPdpSubgroups()) {
109 for (final Pdp pdp : subGroup.getPdpInstances()) {
110 pdp.setPdpState(pdpState);
113 databaseProvider.updatePdpGroups(pdpGroups);
115 LOGGER.debug("Updated PdpGroup and Pdp in DB - {} ", pdpGroups);
118 private void sendPdpMessage(final PdpGroup pdpGroup, final PdpState pdpState,
119 final PolicyModelsProvider databaseProvider) throws PfModelException {
120 String pdpGroupName = pdpGroup.getName();
121 for (final PdpSubGroup subGroup : pdpGroup.getPdpSubgroups()) {
122 List<ToscaPolicy> policies = getToscaPolicies(subGroup, databaseProvider);
123 for (final Pdp pdp : subGroup.getPdpInstances()) {
124 String pdpInstanceId = pdp.getInstanceId();
125 final var pdpUpdatemessage =
126 createPdpUpdateMessage(pdpGroup.getName(), subGroup, pdp.getInstanceId(),
128 final var pdpStateChangeMessage =
129 createPdpStateChangeMessage(pdpGroupName, subGroup, pdpInstanceId, pdpState);
130 updateDeploymentStatus(pdpGroupName, subGroup.getPdpType(), pdpInstanceId,
131 pdpStateChangeMessage.getState(), databaseProvider, pdpUpdatemessage.getPoliciesToBeDeployed());
132 requestMap.addRequest(pdpUpdatemessage, pdpStateChangeMessage);
133 LOGGER.debug("Sent PdpUpdate message - {}", pdpUpdatemessage);
134 LOGGER.debug("Sent PdpStateChange message - {}", pdpStateChangeMessage);