2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.xacml.pdp.application.optimization;
26 import com.att.research.xacml.api.Advice;
27 import com.att.research.xacml.api.AttributeAssignment;
28 import com.att.research.xacml.api.Decision;
29 import com.att.research.xacml.api.Response;
30 import com.att.research.xacml.api.Result;
31 import java.nio.file.Path;
32 import java.util.Arrays;
33 import java.util.Collection;
34 import java.util.List;
36 import org.apache.commons.lang3.tuple.Pair;
37 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
38 import org.onap.policy.models.decisions.concepts.DecisionRequest;
39 import org.onap.policy.models.decisions.concepts.DecisionResponse;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
42 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
43 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
44 import org.onap.policy.pdp.xacml.application.common.std.StdXacmlApplicationServiceProvider;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public class OptimizationPdpApplication extends StdXacmlApplicationServiceProvider {
50 private static final Logger LOGGER = LoggerFactory.getLogger(OptimizationPdpApplication.class);
51 private static final String STRING_VERSION100 = "1.0.0";
52 private static final String RESOURCE_SUBSCRIBERNAME = "subscriberName";
53 private static final String RESOURCE_POLICYTYPE = "policy-type";
54 private static final String RESOURCE_SCOPE = "scope";
56 public static final String POLICYTYPE_AFFINITY = "onap.policies.optimization.resource.AffinityPolicy";
57 public static final String POLICYTYPE_SUBSCRIBER = "onap.policies.optimization.service.SubscriberPolicy";
58 public static final String POLICYTYPE_DISTANCE = "onap.policies.optimization.resource.DistancePolicy";
59 public static final String POLICYTYPE_HPA = "onap.policies.optimization.resource.HpaPolicy";
60 public static final String POLICYTYPE_OPTIMIZATION = "onap.policies.optimization.resource.OptimizationPolicy";
61 public static final String POLICYTYPE_PCI = "onap.policies.optimization.resource.PciPolicy";
62 public static final String POLICYTYPE_QUERY = "onap.policies.optimization.service.QueryPolicy";
63 public static final String POLICYTYPE_VIMFIT = "onap.policies.optimization.resource.Vim_fit";
64 public static final String POLICYTYPE_VNF = "onap.policies.optimization.resource.VnfPolicy";
65 public static final String ONAP_OPTIMIZATION_DERIVED_POLICY_TYPE = "onap.policies.optimization.";
67 private OptimizationPdpApplicationTranslator translator = new OptimizationPdpApplicationTranslator();
72 public OptimizationPdpApplication() {
75 applicationName = "optimization";
76 actions = Arrays.asList("optimize");
78 this.supportedPolicyTypes.add(new ToscaConceptIdentifier(POLICYTYPE_AFFINITY, STRING_VERSION100));
79 this.supportedPolicyTypes.add(new ToscaConceptIdentifier(POLICYTYPE_DISTANCE, STRING_VERSION100));
80 this.supportedPolicyTypes.add(new ToscaConceptIdentifier(POLICYTYPE_HPA, STRING_VERSION100));
81 this.supportedPolicyTypes.add(new ToscaConceptIdentifier(POLICYTYPE_OPTIMIZATION, STRING_VERSION100));
82 this.supportedPolicyTypes.add(new ToscaConceptIdentifier(POLICYTYPE_PCI, STRING_VERSION100));
83 this.supportedPolicyTypes.add(new ToscaConceptIdentifier(POLICYTYPE_QUERY, STRING_VERSION100));
84 this.supportedPolicyTypes.add(new ToscaConceptIdentifier(POLICYTYPE_SUBSCRIBER, STRING_VERSION100));
85 this.supportedPolicyTypes.add(new ToscaConceptIdentifier(POLICYTYPE_VIMFIT, STRING_VERSION100));
86 this.supportedPolicyTypes.add(new ToscaConceptIdentifier(POLICYTYPE_VNF, STRING_VERSION100));
90 public void initialize(Path pathForData, RestServerParameters policyApiParameters)
91 throws XacmlApplicationException {
93 // Store our API parameters and path for translator so it
94 // can go get Policy Types
96 this.translator.setPathForData(pathForData);
97 this.translator.setApiRestParameters(policyApiParameters);
99 // Let our super class do its thing
101 super.initialize(pathForData, policyApiParameters);
105 public boolean canSupportPolicyType(ToscaConceptIdentifier policyTypeId) {
107 // For the time being, restrict this if the version isn't known.
108 // Could be too difficult to support changing of versions dynamically.
110 for (ToscaConceptIdentifier supported : this.supportedPolicyTypes) {
111 if (policyTypeId.equals(supported)) {
112 LOGGER.info("optimization can support {}", supported);
117 // Support derived types
119 return policyTypeId.getName().startsWith(ONAP_OPTIMIZATION_DERIVED_POLICY_TYPE);
123 public Pair<DecisionResponse, Response> makeDecision(DecisionRequest request,
124 Map<String, String[]> requestQueryParams) {
126 // In case we have a subcriber policy
128 Response xacmlSubscriberResponse = null;
130 // Check if there are subject attributes for subscriber
132 if (hasSubscriberAttributes(request)) {
134 // We must do an initial request to pull subscriber attributes
136 LOGGER.info("Request Subscriber attributes");
138 // Convert the request
140 DecisionRequest subscriberRequest = new DecisionRequest(request);
142 // Override the PolicyType to ensure we are only looking at Subscriber Policies
144 if (subscriberRequest.getResource().containsKey(RESOURCE_POLICYTYPE)) {
145 subscriberRequest.getResource().remove(RESOURCE_POLICYTYPE);
147 subscriberRequest.getResource().put(RESOURCE_POLICYTYPE, POLICYTYPE_SUBSCRIBER);
149 // Convert to a XacmlRequest and get a decision
152 xacmlSubscriberResponse =
153 this.xacmlDecision(OptimizationSubscriberRequest.createInstance(subscriberRequest));
154 } catch (XacmlApplicationException e) {
155 LOGGER.error("Could not create subscriberName request", e);
158 // Check the response for subscriber attributes and add them
159 // to the initial request.
161 if (xacmlSubscriberResponse != null && ! addSubscriberAttributes(xacmlSubscriberResponse, request)) {
162 LOGGER.error("Failed to get subscriber role attributes");
164 // Convert to a DecisionResponse
166 return Pair.of(this.getTranslator().convertResponse(xacmlSubscriberResponse), xacmlSubscriberResponse);
172 Pair<DecisionResponse, Response> decisionPair = super.makeDecision(request, requestQueryParams);
174 // Add back in advice from subscriber
176 if (xacmlSubscriberResponse != null) {
177 addSubscriberAdvice(xacmlSubscriberResponse, decisionPair.getLeft());
183 protected ToscaPolicyTranslator getTranslator(String type) {
190 @SuppressWarnings("unchecked")
191 private boolean hasSubscriberAttributes(DecisionRequest request) {
192 return request.getContext() != null
193 && request.getContext().containsKey(RESOURCE_SUBSCRIBERNAME)
194 && request.getContext().get(RESOURCE_SUBSCRIBERNAME) instanceof List
195 && ! ((List<String>) request.getContext().get(RESOURCE_SUBSCRIBERNAME)).isEmpty();
198 private boolean addSubscriberAttributes(Response xacmlResponse, DecisionRequest initialRequest) {
200 // This has multiple results right now because of how the attributes were added to the
201 // request. That will have to be fixed in the future, for now find the Permit result
202 // and add the role attributes as they will be used in the next request.
204 for (Result result : xacmlResponse.getResults()) {
208 if (result.getStatus().isOk() && result.getDecision().equals(Decision.PERMIT)) {
210 // Pull out the advice which has attributes
212 scanAdvice(result.getAssociatedAdvice(), initialRequest);
214 // PLD this is an assumption that all is good
218 LOGGER.error("XACML result not ok {} or Permit {}", result.getStatus(), result.getDecision());
224 private void addSubscriberAdvice(Response xacmlResponse, DecisionResponse response) {
226 // Again find the Permit result
228 for (Result result : xacmlResponse.getResults()) {
232 if (result.getStatus().isOk() && Decision.PERMIT.equals(result.getDecision())) {
233 this.translator.scanAdvice(result.getAssociatedAdvice(), response);
239 @SuppressWarnings("unchecked")
240 private void scanAdvice(Collection<Advice> adviceCollection, DecisionRequest initialRequest) {
242 // There really should only be one advice object
244 for (Advice advice : adviceCollection) {
246 // Look for the optimization specific advice
248 if (! ToscaDictionary.ID_ADVICE_OPTIMIZATION_SUBSCRIBER.equals(advice.getId())) {
249 LOGGER.error("Unsupported advice id {}", advice.getId());
253 // Get the attributes and add them
255 for (AttributeAssignment attribute : advice.getAttributeAssignments()) {
257 // If this is subscriber role
259 if (ToscaDictionary.ID_ADVICE_OPTIMIZATION_SUBSCRIBER_ROLE.equals(attribute.getAttributeId())) {
260 ((List<String>) initialRequest.getResource().get(RESOURCE_SCOPE))
261 .add(attribute.getAttributeValue().getValue().toString());