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