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.pdp.xacml.application.common.std;
25 import com.att.research.xacml.std.annotations.XACMLAction;
26 import com.att.research.xacml.std.annotations.XACMLRequest;
27 import com.att.research.xacml.std.annotations.XACMLResource;
28 import com.att.research.xacml.std.annotations.XACMLSubject;
30 import java.util.ArrayList;
31 import java.util.Collection;
33 import java.util.Map.Entry;
37 import lombok.ToString;
39 import org.onap.policy.models.decisions.concepts.DecisionRequest;
44 @XACMLRequest(ReturnPolicyIdList = true)
45 public class StdMatchablePolicyRequest {
47 @XACMLSubject(includeInResults = true)
48 private String onapName;
50 @XACMLSubject(attributeId = "urn:org:onap:onap-component", includeInResults = true)
51 private String onapComponent;
53 @XACMLSubject(attributeId = "urn:org:onap:onap-instance", includeInResults = true)
54 private String onapInstance;
57 private String action;
60 // Unfortunately the annotations won't take an object.toString()
61 // So I could not use the ToscaDictionary class to put these id's
62 // into the annotations.
64 @XACMLResource(attributeId = "urn:org:onap:policy-scope-property", includeInResults = true)
65 Collection<String> policyScopes = new ArrayList<>();
67 @XACMLResource(attributeId = "urn:org:onap:policy-type-property", includeInResults = true)
68 Collection<String> policyTypes = new ArrayList<>();
70 public StdMatchablePolicyRequest() {
75 * Parses the DecisionRequest into a MonitoringRequest.
77 * @param decisionRequest Input DecisionRequest
78 * @return MonitoringRequest
80 @SuppressWarnings({"rawtypes", "unchecked"})
81 public static StdMatchablePolicyRequest createInstance(DecisionRequest decisionRequest) {
83 // Create our request object
85 StdMatchablePolicyRequest request = new StdMatchablePolicyRequest();
87 // Add the subject attributes
89 request.onapName = decisionRequest.getOnapName();
90 request.onapComponent = decisionRequest.getOnapComponent();
91 request.onapInstance = decisionRequest.getOnapInstance();
93 // Add the action attribute
95 request.action = decisionRequest.getAction();
97 // Add the resource attributes
99 Map<String, Object> resources = decisionRequest.getResource();
100 for (Entry<String, Object> entrySet : resources.entrySet()) {
102 // Making an assumption that these two fields are matchable.
103 // Its possible we may have to load the policy type model
104 // and use that to find the fields that are matchable.
106 if ("policyScope".equals(entrySet.getKey())) {
107 if (entrySet.getValue() instanceof Collection) {
108 addPolicyScopes(request, (Collection) entrySet.getValue());
109 } else if (entrySet.getValue() instanceof String) {
110 request.policyScopes.add(entrySet.getValue().toString());
114 if ("policyType".equals(entrySet.getKey())) {
115 if (entrySet.getValue() instanceof Collection) {
116 addPolicyTypes(request, (Collection) entrySet.getValue());
118 if (entrySet.getValue() instanceof String) {
119 request.policyTypes.add(entrySet.getValue().toString());
126 private static StdMatchablePolicyRequest addPolicyScopes(StdMatchablePolicyRequest request,
127 Collection<Object> scopes) {
128 for (Object scope : scopes) {
129 request.policyScopes.add(scope.toString());
134 private static StdMatchablePolicyRequest addPolicyTypes(StdMatchablePolicyRequest request,
135 Collection<Object> types) {
136 for (Object type : types) {
137 request.policyTypes.add(type.toString());