Move PAP database provider to spring boot default
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PdpGroupStateChangeProvider.java
1 /*-
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-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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest;
24
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.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;
42
43 /**
44  * Provider for PAP component to change state of PDP group.
45  *
46  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
47  */
48 @Service
49 public class PdpGroupStateChangeProvider extends PdpMessageGenerator {
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(PdpGroupStateChangeProvider.class);
52
53     @Autowired
54     private PdpGroupService pdpGroupService;
55
56     /**
57      * Constructs the object.
58      */
59     public PdpGroupStateChangeProvider() {
60         super(false);
61     }
62
63     /**
64      * Changes state of a PDP group.
65      *
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
70      */
71     public Pair<HttpStatus, PdpGroupStateChangeResponse> changeGroupState(final String groupName,
72             final PdpState pdpGroupState) throws PfModelException {
73         synchronized (updateLock) {
74             switch (pdpGroupState) {
75                 case ACTIVE:
76                     handleActiveState(groupName);
77                     break;
78                 case PASSIVE:
79                     handlePassiveState(groupName);
80                     break;
81                 default:
82                     throw new PfModelException(Response.Status.BAD_REQUEST,
83                             "Only ACTIVE or PASSIVE state changes are allowed");
84             }
85             return Pair.of(HttpStatus.OK, new PdpGroupStateChangeResponse());
86         }
87     }
88
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);
94         }
95     }
96
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);
102         }
103     }
104
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);
110             }
111         }
112         pdpGroupService.updatePdpGroups(pdpGroups);
113
114         LOGGER.debug("Updated PdpGroup and Pdp in DB - {} ", pdpGroups);
115     }
116
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(),
125                                 policies, null);
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);
133             }
134         }
135     }
136 }