80c57d360bd0f106b9198f185296e64ab44fd47e
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / LegacyGuardPolicyProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
4  * ================================================================================
5  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2020 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.api.main.rest.provider;
25
26 import java.util.HashMap;
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29 import java.util.Map.Entry;
30
31 import javax.ws.rs.core.Response;
32
33 import org.apache.commons.lang3.tuple.Pair;
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.base.PfModelException;
36 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
37 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
38
39 /**
40  * Class to provide all kinds of legacy guard policy operations.
41  *
42  * @author Chenfei Gao (cgao@research.att.com)
43  */
44 public class LegacyGuardPolicyProvider extends CommonModelProvider {
45
46     private static final Map<String, PfConceptKey> GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>();
47
48     static {
49         GUARD_POLICY_TYPE_MAP.put("guard.frequency.",
50                 new PfConceptKey("onap.policies.controlloop.guard.FrequencyLimiter:1.0.0"));
51         GUARD_POLICY_TYPE_MAP.put("guard.minmax.", new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0"));
52         GUARD_POLICY_TYPE_MAP.put("guard.blacklist.",
53                 new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0"));
54     }
55
56     /**
57      * Default constructor.
58      */
59     public LegacyGuardPolicyProvider() throws PfModelException {
60         super();
61     }
62
63     /**
64      * Retrieves a list of guard policies matching specified ID and version.
65      *
66      * @param policyId the ID of policy
67      * @param policyVersion the version of policy
68      *
69      * @return the map of LegacyGuardPolicyOutput objects
70      */
71     public Map<String, LegacyGuardPolicyOutput> fetchGuardPolicy(String policyId, String policyVersion)
72             throws PfModelException {
73
74         return modelsProvider.getGuardPolicy(policyId, policyVersion);
75     }
76
77     /**
78      * Retrieves a list of deployed guard policies in each pdp group.
79      *
80      * @param policyId the ID of the policy
81      *
82      * @return a list of deployed policies in each pdp group
83      *
84      * @throws PfModelException the PfModel parsing exception
85      */
86     public Map<Pair<String, String>, Map<String, LegacyGuardPolicyOutput>> fetchDeployedGuardPolicies(String policyId)
87             throws PfModelException {
88
89         return collectDeployedPolicies(policyId, getGuardPolicyType(policyId), modelsProvider::getGuardPolicy,
90                 Map::putAll, new HashMap<>());
91     }
92
93     /**
94      * Creates a new guard policy.
95      *
96      * @param body the entity body of policy
97      *
98      * @return the map of LegacyGuardPolicyOutput objectst
99      */
100     public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body) throws PfModelException {
101
102         return modelsProvider.createGuardPolicy(body);
103     }
104
105     /**
106      * Deletes the guard policies matching specified ID and version.
107      *
108      * @param policyId the ID of policy
109      * @param policyVersion the version of policy
110      *
111      * @return the map of LegacyGuardPolicyOutput objects
112      */
113     public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(String policyId, String policyVersion)
114             throws PfModelException {
115
116         return modelsProvider.deleteGuardPolicy(policyId, policyVersion);
117     }
118
119     /**
120      * Retrieves guard policy type given guard policy ID.
121      *
122      * @param policyId the ID of guard policy
123      *
124      * @return the concept key of guard policy type
125      *
126      * @throws PfModelException the PfModel parsing exception
127      */
128     private PfConceptKey getGuardPolicyType(String policyId) throws PfModelException {
129
130         for (Entry<String, PfConceptKey> guardPolicyTypeEntry : GUARD_POLICY_TYPE_MAP.entrySet()) {
131             if (policyId.startsWith(guardPolicyTypeEntry.getKey())) {
132                 return guardPolicyTypeEntry.getValue();
133             }
134         }
135         throw new PfModelException(Response.Status.BAD_REQUEST, "No policy type defined for " + policyId);
136     }
137 }