2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.xacml.pdp.application.guard;
25 import com.att.research.xacml.api.Request;
26 import com.att.research.xacml.api.Response;
27 import com.att.research.xacml.api.pdp.PDPEngine;
28 import com.att.research.xacml.api.pdp.PDPException;
29 import com.att.research.xacml.util.XACMLPolicyWriter;
30 import com.google.common.collect.Lists;
32 import java.io.ByteArrayOutputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.nio.file.Path;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.HashMap;
39 import java.util.List;
41 import java.util.Map.Entry;
42 import java.util.Properties;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
46 import org.onap.policy.models.decisions.concepts.DecisionRequest;
47 import org.onap.policy.models.decisions.concepts.DecisionResponse;
48 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
49 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConverter;
50 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
51 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.yaml.snakeyaml.Yaml;
57 * This class implements the onap.policies.controlloop.Guard policy implementations.
59 * @author pameladragosh
62 public class GuardPdpApplication implements ToscaPolicyConverter, XacmlApplicationServiceProvider {
64 private static final Logger LOGGER = LoggerFactory.getLogger(GuardPdpApplication.class);
65 private static final String STRING_VERSION100 = "1.0.0";
66 private Map<String, String> supportedPolicyTypes = new HashMap<>();
67 private Path pathForData;
68 private Properties pdpProperties = null;
69 private PDPEngine pdpEngine = null;
74 public GuardPdpApplication() {
75 this.supportedPolicyTypes.put("onap.policies.controlloop.guard.FrequencyLimiter", STRING_VERSION100);
76 this.supportedPolicyTypes.put("onap.policies.controlloop.guard.MinMax", STRING_VERSION100);
80 public String applicationName() {
81 return "Guard Application";
85 public List<String> actionDecisionsSupported() {
86 return Arrays.asList("guard");
90 public void initialize(Path pathForData) {
94 this.pathForData = pathForData;
95 LOGGER.debug("New Path is {}", this.pathForData.toAbsolutePath());
97 // Look for and load the properties object
100 pdpProperties = XacmlPolicyUtils.loadXacmlProperties(XacmlPolicyUtils.getPropertiesPath(pathForData));
101 LOGGER.debug("{}", pdpProperties);
102 } catch (IOException e) {
103 LOGGER.error("{}", e);
108 PDPEngine newEngine = XacmlPolicyUtils.createEngine(pdpProperties);
109 if (newEngine != null) {
110 pdpEngine = newEngine;
115 public List<String> supportedPolicyTypes() {
116 return Lists.newArrayList(supportedPolicyTypes.keySet());
120 public boolean canSupportPolicyType(String policyType, String policyTypeVersion) {
122 // For the time being, restrict this if the version isn't known.
123 // Could be too difficult to support changing of versions dynamically.
125 if (! this.supportedPolicyTypes.containsKey(policyType)) {
129 // Must match version exactly
131 return this.supportedPolicyTypes.get(policyType).equals(policyTypeVersion);
135 public void loadPolicies(Map<String, Object> toscaPolicies) {
138 // Convert the policies first
140 List<PolicyType> listPolicies = this.convertPolicies(toscaPolicies);
141 if (listPolicies.isEmpty()) {
142 throw new ToscaPolicyConversionException("Converted 0 policies");
144 } catch (ToscaPolicyConversionException e) {
145 LOGGER.error("Failed to loadPolicies {}", e);
150 public DecisionResponse makeDecision(DecisionRequest request) {
152 // Convert to a XacmlRequest
154 Request xacmlRequest = this.convertRequest(request);
156 // Now get a decision
158 Response xacmlResponse = this.xacmlDecision(xacmlRequest);
160 // Convert to a DecisionResponse
162 return this.convertResponse(xacmlResponse);
166 public List<PolicyType> convertPolicies(InputStream isToscaPolicy) throws ToscaPolicyConversionException {
168 // Have snakeyaml parse the object
170 Yaml yaml = new Yaml();
171 Map<String, Object> toscaObject = yaml.load(isToscaPolicy);
173 // Return the policies
175 return scanAndConvertPolicies(toscaObject);
179 public List<PolicyType> convertPolicies(Map<String, Object> toscaObject) throws ToscaPolicyConversionException {
181 // Return the policies
183 return scanAndConvertPolicies(toscaObject);
187 public Request convertRequest(DecisionRequest request) {
188 // TODO Auto-generated method stub
193 public DecisionResponse convertResponse(Response response) {
194 // TODO Auto-generated method stub
198 @SuppressWarnings("unchecked")
199 private List<PolicyType> scanAndConvertPolicies(Map<String, Object> toscaObject)
200 throws ToscaPolicyConversionException {
204 List<PolicyType> scannedPolicies = new ArrayList<>();
206 // Iterate each of the Policies
208 List<Object> policies = (List<Object>) toscaObject.get("policies");
209 for (Object policyObject : policies) {
213 LOGGER.debug("Found policy {}", policyObject.getClass());
214 Map<String, Object> policyContents = (Map<String, Object>) policyObject;
215 for (Entry<String, Object> entrySet : policyContents.entrySet()) {
216 LOGGER.debug("Entry set {}", entrySet);
218 // Convert this policy
220 PolicyType policy = this.convertPolicy(entrySet);
221 try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
222 XACMLPolicyWriter.writePolicyFile(os, policy);
223 LOGGER.debug("{}", os);
224 } catch (IOException e) {
225 LOGGER.error("Failed to convert {}", e);
228 // Convert and add in the new policy
230 scannedPolicies.add(policy);
234 return scannedPolicies;
237 private PolicyType convertPolicy(Entry<String, Object> entrySet) throws ToscaPolicyConversionException {
243 * Make a decision call.
245 * @param request Incoming request object
246 * @return Response object
248 private synchronized Response xacmlDecision(Request request) {
250 // This is what we need to return
252 Response response = null;
256 long timeStart = System.currentTimeMillis();
258 response = this.pdpEngine.decide(request);
259 } catch (PDPException e) {
260 LOGGER.error("Xacml PDP Engine failed {}", e);
263 // Track the end of timing
265 long timeEnd = System.currentTimeMillis();
266 LOGGER.info("Elapsed Time: {}ms", (timeEnd - timeStart));