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 public static final String POLICY_TYPE_KEY = "policyType";
48 public static final String POLICY_SCOPE_KEY = "policyScope";
50 @XACMLSubject(includeInResults = true)
51 private String onapName;
53 @XACMLSubject(attributeId = "urn:org:onap:onap-component", includeInResults = true)
54 private String onapComponent;
56 @XACMLSubject(attributeId = "urn:org:onap:onap-instance", includeInResults = true)
57 private String onapInstance;
60 private String action;
63 // Unfortunately the annotations won't take an object.toString()
64 // So I could not use the ToscaDictionary class to put these id's
65 // into the annotations.
67 @XACMLResource(attributeId = "urn:org:onap:policy-scope-property", includeInResults = true)
68 Collection<String> policyScopes = new ArrayList<>();
70 @XACMLResource(attributeId = "urn:org:onap:policy-type-property", includeInResults = true)
71 Collection<String> policyTypes = new ArrayList<>();
73 public StdMatchablePolicyRequest() {
78 * Parses the DecisionRequest into a MonitoringRequest.
80 * @param decisionRequest Input DecisionRequest
81 * @return MonitoringRequest
83 @SuppressWarnings({"rawtypes", "unchecked"})
84 public static StdMatchablePolicyRequest createInstance(DecisionRequest decisionRequest) {
86 // Create our request object
88 StdMatchablePolicyRequest request = new StdMatchablePolicyRequest();
90 // Add the subject attributes
92 request.onapName = decisionRequest.getOnapName();
93 request.onapComponent = decisionRequest.getOnapComponent();
94 request.onapInstance = decisionRequest.getOnapInstance();
96 // Add the action attribute
98 request.action = decisionRequest.getAction();
100 // Add the resource attributes
102 Map<String, Object> resources = decisionRequest.getResource();
103 for (Entry<String, Object> entrySet : resources.entrySet()) {
105 // Making an assumption that these two fields are matchable.
106 // Its possible we may have to load the policy type model
107 // and use that to find the fields that are matchable.
109 if (POLICY_SCOPE_KEY.equals(entrySet.getKey())) {
110 if (entrySet.getValue() instanceof Collection) {
111 addPolicyScopes(request, (Collection) entrySet.getValue());
112 } else if (entrySet.getValue() instanceof String) {
113 request.policyScopes.add(entrySet.getValue().toString());
117 if (POLICY_TYPE_KEY.equals(entrySet.getKey())) {
118 if (entrySet.getValue() instanceof Collection) {
119 addPolicyTypes(request, (Collection) entrySet.getValue());
121 if (entrySet.getValue() instanceof String) {
122 request.policyTypes.add(entrySet.getValue().toString());
129 private static StdMatchablePolicyRequest addPolicyScopes(StdMatchablePolicyRequest request,
130 Collection<Object> scopes) {
131 for (Object scope : scopes) {
132 request.policyScopes.add(scope.toString());
137 private static StdMatchablePolicyRequest addPolicyTypes(StdMatchablePolicyRequest request,
138 Collection<Object> types) {
139 for (Object type : types) {
140 request.policyTypes.add(type.toString());