Test decision from main entry
[policy/xacml-pdp.git] / applications / optimization / src / main / java / org / onap / policy / xacml / pdp / application / optimization / OptimizationPdpApplication.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.optimization;
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.Collections;
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.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;
49
50 public class OptimizationPdpApplication extends StdXacmlApplicationServiceProvider {
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(OptimizationPdpApplication.class);
53     private static final String STRING_VERSION100 = "1.0.0";
54
55     private StdMatchableTranslator translator = new StdMatchableTranslator();
56     private List<ToscaPolicyTypeIdentifier> supportedPolicyTypes = new ArrayList<>();
57
58     /**
59      * Constructor.
60      */
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));
80     }
81
82     @Override
83     public String applicationName() {
84         return "optimization";
85     }
86
87     @Override
88     public List<String> actionDecisionsSupported() {
89         return Arrays.asList("optimize");
90     }
91
92     @Override
93     public synchronized List<ToscaPolicyTypeIdentifier> supportedPolicyTypes() {
94         return Collections.unmodifiableList(supportedPolicyTypes);
95     }
96
97     @Override
98     public boolean canSupportPolicyType(ToscaPolicyTypeIdentifier policyTypeId) {
99         //
100         // For the time being, restrict this if the version isn't known.
101         // Could be too difficult to support changing of versions dynamically.
102         //
103         //
104         // For the time being, restrict this if the version isn't known.
105         // Could be too difficult to support changing of versions dynamically.
106         //
107         for (ToscaPolicyTypeIdentifier supported : this.supportedPolicyTypes) {
108             if (policyTypeId.equals(supported)) {
109                 return true;
110             }
111         }
112         return false;
113     }
114
115     @Override
116     public synchronized void loadPolicies(Map<String, Object> toscaPolicies) {
117         try {
118             //
119             // Convert the policies first
120             //
121             List<PolicyType> listPolicies = translator.scanAndConvertPolicies(toscaPolicies);
122             if (listPolicies.isEmpty()) {
123                 throw new ToscaPolicyConversionException("Converted 0 policies");
124             }
125             //
126             // Create a copy of the properties object
127             //
128             Properties newProperties = this.getProperties();
129             //
130             // Iterate through the policies
131             //
132             for (PolicyType newPolicy : listPolicies) {
133                 //
134                 // Construct the filename
135                 //
136                 Path refPath = XacmlPolicyUtils.constructUniquePolicyFilename(newPolicy, this.getDataPath());
137                 //
138                 // Write the policy to disk
139                 // Maybe check for an error
140                 //
141                 XACMLPolicyWriter.writePolicyFile(refPath, newPolicy);
142                 //
143                 // Add root policy to properties object
144                 //
145                 XacmlPolicyUtils.addRootPolicy(newProperties, refPath);
146             }
147             //
148             // Write the properties to disk
149             //
150             XacmlPolicyUtils.storeXacmlProperties(newProperties,
151                     XacmlPolicyUtils.getPropertiesPath(this.getDataPath()));
152             //
153             // Reload the engine
154             //
155             this.createEngine(newProperties);
156         } catch (IOException | ToscaPolicyConversionException e) {
157             LOGGER.error("Failed to loadPolicies {}", e);
158         }
159     }
160
161     @Override
162     public synchronized DecisionResponse makeDecision(DecisionRequest request) {
163         //
164         // Convert to a XacmlRequest
165         //
166         Request xacmlRequest = translator.convertRequest(request);
167         //
168         // Now get a decision
169         //
170         Response xacmlResponse = this.xacmlDecision(xacmlRequest);
171         //
172         // Convert to a DecisionResponse
173         //
174         return translator.convertResponse(xacmlResponse);
175     }
176
177 }