8319de413879e528fe22c28fdf8430bfe263f278
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / LegacyOperationalPolicyProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
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
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.api.main.rest.provider;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28 import javax.ws.rs.core.Response;
29
30 import org.apache.commons.lang3.tuple.Pair;
31 import org.onap.policy.models.base.PfConceptKey;
32 import org.onap.policy.models.base.PfModelException;
33 import org.onap.policy.models.pdp.concepts.PdpGroup;
34 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
36 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
37
38 /**
39  * Class to provide all kinds of legacy operational policy operations.
40  *
41  * @author Chenfei Gao (cgao@research.att.com)
42  */
43 public class LegacyOperationalPolicyProvider extends CommonModelProvider {
44
45     private static final String INVALID_POLICY_VERSION = "legacy policy version is not an integer";
46     private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
47     private static final PfConceptKey LEGACY_OPERATIONAL_TYPE =
48             new PfConceptKey("onap.policies.controlloop.Operational", "1.0.0");
49
50     /**
51      * Default constructor.
52      */
53     public LegacyOperationalPolicyProvider() throws PfModelException {
54         super();
55     }
56
57     /**
58      * Retrieves a list of operational policies matching specified ID and version.
59      *
60      * @param policyId the ID of policy
61      * @param policyVersion the version of policy
62      *
63      * @return the LegacyOperationalPolicy object
64      */
65     public LegacyOperationalPolicy fetchOperationalPolicy(String policyId, String policyVersion)
66             throws PfModelException {
67
68         if (policyVersion != null) {
69             validNumber(policyVersion, INVALID_POLICY_VERSION);
70         }
71         return modelsProvider.getOperationalPolicy(policyId, policyVersion);
72     }
73
74     /**
75      * Retrieves a list of deployed operational policies in each pdp group.
76      *
77      * @param policyId the ID of the policy
78      *
79      * @return a list of deployed policies in each pdp group
80      *
81      * @throws PfModelException the PfModel parsing exception
82      */
83     public Map<Pair<String, String>, List<LegacyOperationalPolicy>> fetchDeployedOperationalPolicies(String policyId)
84             throws PfModelException {
85
86         return collectDeployedPolicies(
87                 policyId, LEGACY_OPERATIONAL_TYPE, modelsProvider::getOperationalPolicy, List::add, new ArrayList<>());
88     }
89
90     /**
91      * Creates a new operational policy.
92      *
93      * @param body the entity body of policy
94      *
95      * @return the LegacyOperationalPolicy object
96      */
97     public LegacyOperationalPolicy createOperationalPolicy(LegacyOperationalPolicy body) throws PfModelException {
98
99         return modelsProvider.createOperationalPolicy(body);
100     }
101
102     /**
103      * Deletes the operational policies matching specified ID and version.
104      *
105      * @param policyId the ID of policy
106      * @param policyVersion the version of policy
107      *
108      * @return the LegacyOperationalPolicy object
109      */
110     public LegacyOperationalPolicy deleteOperationalPolicy(String policyId, String policyVersion)
111             throws PfModelException {
112
113         validNumber(policyVersion, INVALID_POLICY_VERSION);
114         validateDeleteEligibility(policyId, policyVersion);
115
116         return modelsProvider.deleteOperationalPolicy(policyId, policyVersion);
117     }
118
119     /**
120      * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted.
121      *
122      * @param policyId the ID of policy
123      * @param policyVersion the version of policy
124      *
125      * @throws PfModelException the PfModel parsing exception
126      */
127     private void validateDeleteEligibility(String policyId, String policyVersion) throws PfModelException {
128
129         List<ToscaPolicyIdentifier> policies = new ArrayList<>();
130         policies.add(new ToscaPolicyIdentifier(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
131         PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyList(policies).build();
132
133         List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);
134
135         if (!pdpGroups.isEmpty()) {
136             throw new PfModelException(Response.Status.CONFLICT,
137                     constructDeletePolicyViolationMessage(policyId, policyVersion, pdpGroups));
138         }
139     }
140 }