2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021, 2023 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2021 AT&T Intellectual Property.
5 * Modifications Copyright (C) 2021-2022 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 jakarta.ws.rs.core.Response;
26 import java.util.List;
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.tosca.authorative.concepts.ToscaPolicy;
35 import org.onap.policy.pap.main.comm.PdpMessageGenerator;
36 import org.onap.policy.pap.main.service.PdpGroupService;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.stereotype.Service;
44 * Provider for PAP component to change state of PDP group.
46 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
49 public class PdpGroupStateChangeProvider extends PdpMessageGenerator {
51 private static final Logger LOGGER = LoggerFactory.getLogger(PdpGroupStateChangeProvider.class);
54 private PdpGroupService pdpGroupService;
57 * Constructs the object.
59 public PdpGroupStateChangeProvider() {
64 * Changes state of a PDP group.
66 * @param groupName name of the PDP group
67 * @param pdpGroupState state of the PDP group
68 * @return a pair containing the status and the response
69 * @throws PfModelException in case of errors
71 public Pair<HttpStatus, PdpGroupStateChangeResponse> changeGroupState(final String groupName,
72 final PdpState pdpGroupState) throws PfModelException {
73 synchronized (updateLock) {
74 switch (pdpGroupState) {
76 handleActiveState(groupName);
79 handlePassiveState(groupName);
82 throw new PfModelException(Response.Status.BAD_REQUEST,
83 "Only ACTIVE or PASSIVE state changes are allowed");
85 return Pair.of(HttpStatus.OK, new PdpGroupStateChangeResponse());
89 private void handleActiveState(final String groupName) throws PfModelException {
90 final List<PdpGroup> pdpGroups = pdpGroupService.getPdpGroups(groupName);
91 if (!pdpGroups.isEmpty() && !PdpState.ACTIVE.equals(pdpGroups.get(0).getPdpGroupState())) {
92 updatePdpGroupAndPdp(pdpGroups, PdpState.ACTIVE);
93 sendPdpMessage(pdpGroups.get(0), PdpState.ACTIVE);
97 private void handlePassiveState(final String groupName) throws PfModelException {
98 final List<PdpGroup> pdpGroups = pdpGroupService.getPdpGroups(groupName);
99 if (!pdpGroups.isEmpty() && !PdpState.PASSIVE.equals(pdpGroups.get(0).getPdpGroupState())) {
100 updatePdpGroupAndPdp(pdpGroups, PdpState.PASSIVE);
101 sendPdpMessage(pdpGroups.get(0), PdpState.PASSIVE);
105 private void updatePdpGroupAndPdp(final List<PdpGroup> pdpGroups, final PdpState pdpState) {
106 pdpGroups.get(0).setPdpGroupState(pdpState);
107 for (final PdpSubGroup subGroup : pdpGroups.get(0).getPdpSubgroups()) {
108 for (final Pdp pdp : subGroup.getPdpInstances()) {
109 pdp.setPdpState(pdpState);
112 pdpGroupService.updatePdpGroups(pdpGroups);
114 LOGGER.debug("Updated PdpGroup and Pdp in DB - {} ", pdpGroups);
117 private void sendPdpMessage(final PdpGroup pdpGroup, final PdpState pdpState) throws PfModelException {
118 String pdpGroupName = pdpGroup.getName();
119 for (final PdpSubGroup subGroup : pdpGroup.getPdpSubgroups()) {
120 List<ToscaPolicy> policies = getToscaPolicies(subGroup);
121 for (final Pdp pdp : subGroup.getPdpInstances()) {
122 String pdpInstanceId = pdp.getInstanceId();
123 final var pdpUpdatemessage =
124 createPdpUpdateMessage(pdpGroup.getName(), subGroup, pdp.getInstanceId(),
126 final var pdpStateChangeMessage =
127 createPdpStateChangeMessage(pdpGroupName, subGroup, pdpInstanceId, pdpState);
128 updateDeploymentStatus(pdpGroupName, subGroup.getPdpType(), pdpInstanceId,
129 pdpStateChangeMessage.getState(), pdpUpdatemessage.getPoliciesToBeDeployed());
130 requestMap.addRequest(pdpUpdatemessage, pdpStateChangeMessage);
131 LOGGER.debug("Sent PdpUpdate message - {}", pdpUpdatemessage);
132 LOGGER.debug("Sent PdpStateChange message - {}", pdpStateChangeMessage);