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.util.XACMLPolicyWriter;
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;
35 import java.util.Properties;
37 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
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;
50 * This class implements the onap.policies.controlloop.Guard policy implementations.
52 * @author pameladragosh
55 public class GuardPdpApplication extends StdXacmlApplicationServiceProvider {
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();
65 public GuardPdpApplication() {
66 this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier("onap.policies.controlloop.guard.FrequencyLimiter",
68 this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier("onap.policies.controlloop.guard.MinMax",
73 public String applicationName() {
78 public List<String> actionDecisionsSupported() {
79 return Arrays.asList("guard");
83 public List<ToscaPolicyTypeIdentifier> supportedPolicyTypes() {
84 return supportedPolicyTypes;
88 public boolean canSupportPolicyType(ToscaPolicyTypeIdentifier policyTypeId) {
90 // For the time being, restrict this if the version isn't known.
91 // Could be too difficult to support changing of versions dynamically.
93 for (ToscaPolicyTypeIdentifier supported : this.supportedPolicyTypes) {
94 if (policyTypeId.equals(supported)) {
102 public void loadPolicies(Map<String, Object> toscaPolicies) throws XacmlApplicationException {
105 // Convert the policies first
107 List<PolicyType> listPolicies = translator.scanAndConvertPolicies(toscaPolicies);
108 if (listPolicies.isEmpty()) {
109 throw new XacmlApplicationException("Converted 0 policies");
112 // Create a copy of the properties object
114 Properties newProperties = this.getProperties();
116 // Iterate through the policies
118 for (PolicyType newPolicy : listPolicies) {
120 // Construct the filename
122 Path refPath = XacmlPolicyUtils.constructUniquePolicyFilename(newPolicy, this.getDataPath());
124 // Write the policy to disk
125 // Maybe check for an error
127 XACMLPolicyWriter.writePolicyFile(refPath, newPolicy);
129 // Add root policy to properties object
131 XacmlPolicyUtils.addRootPolicy(newProperties, refPath);
134 // Write the properties to disk
136 XacmlPolicyUtils.storeXacmlProperties(newProperties,
137 XacmlPolicyUtils.getPropertiesPath(this.getDataPath()));
141 this.createEngine(newProperties);
142 } catch (IOException | ToscaPolicyConversionException e) {
143 LOGGER.error("Failed to loadPolicies {}", e);
148 public DecisionResponse makeDecision(DecisionRequest request) {
150 // Convert to a XacmlRequest
152 Request xacmlRequest = translator.convertRequest(request);
154 // Now get a decision
156 Response xacmlResponse = this.xacmlDecision(xacmlRequest);
158 // Convert to a DecisionResponse
160 return translator.convertResponse(xacmlResponse);