Test decision from main entry
[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.att.research.xacml.util.XACMLPolicyWriter;
28
29 import java.io.IOException;
30 import java.nio.file.Path;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Properties;
36
37 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
38
39 import org.onap.policy.models.decisions.concepts.DecisionRequest;
40 import org.onap.policy.models.decisions.concepts.DecisionResponse;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
42 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
43 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
44 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
45 import org.onap.policy.pdp.xacml.application.common.std.StdXacmlApplicationServiceProvider;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * This class implements the onap.policies.controlloop.Guard policy implementations.
51  *
52  * @author pameladragosh
53  *
54  */
55 public class GuardPdpApplication extends StdXacmlApplicationServiceProvider {
56
57     private static final Logger LOGGER = LoggerFactory.getLogger(GuardPdpApplication.class);
58     private static final String STRING_VERSION100 = "1.0.0";
59     private List<ToscaPolicyTypeIdentifier> supportedPolicyTypes = new ArrayList<>();
60     private LegacyGuardTranslator translator = new LegacyGuardTranslator();
61
62     /** Constructor.
63      *
64      */
65     public GuardPdpApplication() {
66         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier("onap.policies.controlloop.guard.FrequencyLimiter",
67                 STRING_VERSION100));
68         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier("onap.policies.controlloop.guard.MinMax",
69                 STRING_VERSION100));
70     }
71
72     @Override
73     public String applicationName() {
74         return "guard";
75     }
76
77     @Override
78     public List<String> actionDecisionsSupported() {
79         return Arrays.asList("guard");
80     }
81
82     @Override
83     public List<ToscaPolicyTypeIdentifier> supportedPolicyTypes() {
84         return supportedPolicyTypes;
85     }
86
87     @Override
88     public boolean canSupportPolicyType(ToscaPolicyTypeIdentifier policyTypeId) {
89         //
90         // For the time being, restrict this if the version isn't known.
91         // Could be too difficult to support changing of versions dynamically.
92         //
93         for (ToscaPolicyTypeIdentifier supported : this.supportedPolicyTypes) {
94             if (policyTypeId.equals(supported)) {
95                 return true;
96             }
97         }
98         return false;
99     }
100
101     @Override
102     public void loadPolicies(Map<String, Object> toscaPolicies) throws XacmlApplicationException {
103         try {
104             //
105             // Convert the policies first
106             //
107             List<PolicyType> listPolicies = translator.scanAndConvertPolicies(toscaPolicies);
108             if (listPolicies.isEmpty()) {
109                 throw new XacmlApplicationException("Converted 0 policies");
110             }
111             //
112             // Create a copy of the properties object
113             //
114             Properties newProperties = this.getProperties();
115             //
116             // Iterate through the policies
117             //
118             for (PolicyType newPolicy : listPolicies) {
119                 //
120                 // Construct the filename
121                 //
122                 Path refPath = XacmlPolicyUtils.constructUniquePolicyFilename(newPolicy, this.getDataPath());
123                 //
124                 // Write the policy to disk
125                 // Maybe check for an error
126                 //
127                 XACMLPolicyWriter.writePolicyFile(refPath, newPolicy);
128                 //
129                 // Add root policy to properties object
130                 //
131                 XacmlPolicyUtils.addRootPolicy(newProperties, refPath);
132             }
133             //
134             // Write the properties to disk
135             //
136             XacmlPolicyUtils.storeXacmlProperties(newProperties,
137                     XacmlPolicyUtils.getPropertiesPath(this.getDataPath()));
138             //
139             // Reload the engine
140             //
141             this.createEngine(newProperties);
142         } catch (IOException | ToscaPolicyConversionException e) {
143             LOGGER.error("Failed to loadPolicies {}", e);
144         }
145     }
146
147     @Override
148     public DecisionResponse makeDecision(DecisionRequest request) {
149         //
150         // Convert to a XacmlRequest
151         //
152         Request xacmlRequest = translator.convertRequest(request);
153         //
154         // Now get a decision
155         //
156         Response xacmlResponse = this.xacmlDecision(xacmlRequest);
157         //
158         // Convert to a DecisionResponse
159         //
160         return translator.convertResponse(xacmlResponse);
161     }
162
163 }