Merge "Use Policy Translator abstract class"
[policy/xacml-pdp.git] / applications / guard / src / main / java / org / onap / policy / xacml / pdp / application / guard / GuardPdpApplication.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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.xacml.pdp.application.guard;
24
25 import com.att.research.xacml.api.Request;
26 import com.att.research.xacml.api.Response;
27 import com.google.common.collect.Lists;
28
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
35
36 import org.onap.policy.models.decisions.concepts.DecisionRequest;
37 import org.onap.policy.models.decisions.concepts.DecisionResponse;
38 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
39 import org.onap.policy.pdp.xacml.application.common.std.StdMetadataTranslator;
40 import org.onap.policy.pdp.xacml.application.common.std.StdXacmlApplicationServiceProvider;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * This class implements the onap.policies.controlloop.Guard policy implementations.
46  *
47  * @author pameladragosh
48  *
49  */
50 public class GuardPdpApplication extends StdXacmlApplicationServiceProvider {
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(GuardPdpApplication.class);
53     private static final String STRING_VERSION100 = "1.0.0";
54     private Map<String, String> supportedPolicyTypes = new HashMap<>();
55     private StdMetadataTranslator translator = new StdMetadataTranslator();
56
57     /** Constructor.
58      *
59      */
60     public GuardPdpApplication() {
61         this.supportedPolicyTypes.put("onap.policies.controlloop.guard.FrequencyLimiter", STRING_VERSION100);
62         this.supportedPolicyTypes.put("onap.policies.controlloop.guard.MinMax", STRING_VERSION100);
63     }
64
65     @Override
66     public String applicationName() {
67         return "Guard Application";
68     }
69
70     @Override
71     public List<String> actionDecisionsSupported() {
72         return Arrays.asList("guard");
73     }
74
75     @Override
76     public List<String> supportedPolicyTypes() {
77         return Lists.newArrayList(supportedPolicyTypes.keySet());
78     }
79
80     @Override
81     public boolean canSupportPolicyType(String policyType, String policyTypeVersion) {
82         //
83         // For the time being, restrict this if the version isn't known.
84         // Could be too difficult to support changing of versions dynamically.
85         //
86         if (! this.supportedPolicyTypes.containsKey(policyType)) {
87             return false;
88         }
89         //
90         // Must match version exactly
91         //
92         return this.supportedPolicyTypes.get(policyType).equals(policyTypeVersion);
93     }
94
95     @Override
96     public void loadPolicies(Map<String, Object> toscaPolicies) {
97         try {
98             //
99             // Convert the policies first
100             //
101             List<PolicyType> listPolicies = translator.scanAndConvertPolicies(toscaPolicies);
102             if (listPolicies.isEmpty()) {
103                 throw new ToscaPolicyConversionException("Converted 0 policies");
104             }
105             //
106             // TODO update properties, save to disk, etc.
107             //
108         } catch (ToscaPolicyConversionException e) {
109             LOGGER.error("Failed to loadPolicies {}", e);
110         }
111     }
112
113     @Override
114     public DecisionResponse makeDecision(DecisionRequest request) {
115         //
116         // Convert to a XacmlRequest
117         //
118         Request xacmlRequest = translator.convertRequest(request);
119         //
120         // Now get a decision
121         //
122         Response xacmlResponse = this.xacmlDecision(xacmlRequest);
123         //
124         // Convert to a DecisionResponse
125         //
126         return translator.convertResponse(xacmlResponse);
127     }
128
129 }