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.optimization;
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.Collections;
34 import java.util.List;
36 import java.util.Properties;
38 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
40 import org.onap.policy.models.decisions.concepts.DecisionRequest;
41 import org.onap.policy.models.decisions.concepts.DecisionResponse;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
43 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
44 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
45 import org.onap.policy.pdp.xacml.application.common.std.StdMatchableTranslator;
46 import org.onap.policy.pdp.xacml.application.common.std.StdXacmlApplicationServiceProvider;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class OptimizationPdpApplication extends StdXacmlApplicationServiceProvider {
52 private static final Logger LOGGER = LoggerFactory.getLogger(OptimizationPdpApplication.class);
53 private static final String STRING_VERSION100 = "1.0.0";
55 private StdMatchableTranslator translator = new StdMatchableTranslator();
56 private List<ToscaPolicyTypeIdentifier> supportedPolicyTypes = new ArrayList<>();
61 public OptimizationPdpApplication() {
62 this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
63 "onap.policies.optimization.AffinityPolicy", STRING_VERSION100));
64 this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
65 "onap.policies.optimization.DistancePolicy", STRING_VERSION100));
66 this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
67 "onap.policies.optimization.HpaPolicy", STRING_VERSION100));
68 this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
69 "onap.policies.optimization.OptimizationPolicy", STRING_VERSION100));
70 this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
71 "onap.policies.optimization.PciPolicy", STRING_VERSION100));
72 this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
73 "onap.policies.optimization.QueryPolicy", STRING_VERSION100));
74 this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
75 "onap.policies.optimization.SubscriberPolicy", STRING_VERSION100));
76 this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
77 "onap.policies.optimization.Vim_fit", STRING_VERSION100));
78 this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
79 "onap.policies.optimization.VnfPolicy", STRING_VERSION100));
83 public String applicationName() {
84 return "optimization";
88 public List<String> actionDecisionsSupported() {
89 return Arrays.asList("optimize");
93 public synchronized List<ToscaPolicyTypeIdentifier> supportedPolicyTypes() {
94 return Collections.unmodifiableList(supportedPolicyTypes);
98 public boolean canSupportPolicyType(ToscaPolicyTypeIdentifier policyTypeId) {
100 // For the time being, restrict this if the version isn't known.
101 // Could be too difficult to support changing of versions dynamically.
104 // For the time being, restrict this if the version isn't known.
105 // Could be too difficult to support changing of versions dynamically.
107 for (ToscaPolicyTypeIdentifier supported : this.supportedPolicyTypes) {
108 if (policyTypeId.equals(supported)) {
116 public synchronized void loadPolicies(Map<String, Object> toscaPolicies) {
119 // Convert the policies first
121 List<PolicyType> listPolicies = translator.scanAndConvertPolicies(toscaPolicies);
122 if (listPolicies.isEmpty()) {
123 throw new ToscaPolicyConversionException("Converted 0 policies");
126 // Create a copy of the properties object
128 Properties newProperties = this.getProperties();
130 // Iterate through the policies
132 for (PolicyType newPolicy : listPolicies) {
134 // Construct the filename
136 Path refPath = XacmlPolicyUtils.constructUniquePolicyFilename(newPolicy, this.getDataPath());
138 // Write the policy to disk
139 // Maybe check for an error
141 XACMLPolicyWriter.writePolicyFile(refPath, newPolicy);
143 // Add root policy to properties object
145 XacmlPolicyUtils.addRootPolicy(newProperties, refPath);
148 // Write the properties to disk
150 XacmlPolicyUtils.storeXacmlProperties(newProperties,
151 XacmlPolicyUtils.getPropertiesPath(this.getDataPath()));
155 this.createEngine(newProperties);
156 } catch (IOException | ToscaPolicyConversionException e) {
157 LOGGER.error("Failed to loadPolicies {}", e);
162 public synchronized DecisionResponse makeDecision(DecisionRequest request) {
164 // Convert to a XacmlRequest
166 Request xacmlRequest = translator.convertRequest(request);
168 // Now get a decision
170 Response xacmlResponse = this.xacmlDecision(xacmlRequest);
172 // Convert to a DecisionResponse
174 return translator.convertResponse(xacmlResponse);