0e5edf84cf48a3d27295ef16d53688d4b2e6704a
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / std / StdMatchablePolicyRequest.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.pdp.xacml.application.common.std;
24
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;
29
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Map;
33 import java.util.Map.Entry;
34
35 import lombok.Getter;
36 import lombok.Setter;
37 import lombok.ToString;
38
39 import org.onap.policy.models.decisions.concepts.DecisionRequest;
40
41 @Getter
42 @Setter
43 @ToString
44 @XACMLRequest(ReturnPolicyIdList = true)
45 public class StdMatchablePolicyRequest {
46
47     public static final String POLICY_TYPE_KEY = "policyType";
48     public static final String POLICY_SCOPE_KEY = "policyScope";
49
50     @XACMLSubject(includeInResults = true)
51     private String onapName;
52
53     @XACMLSubject(attributeId = "urn:org:onap:onap-component", includeInResults = true)
54     private String onapComponent;
55
56     @XACMLSubject(attributeId = "urn:org:onap:onap-instance",  includeInResults = true)
57     private String onapInstance;
58
59     @XACMLAction()
60     private String action;
61
62     //
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.
66     //
67     @XACMLResource(attributeId = "urn:org:onap:policy-scope-property", includeInResults = true)
68     Collection<String> policyScopes = new ArrayList<>();
69
70     @XACMLResource(attributeId = "urn:org:onap:policy-type-property", includeInResults = true)
71     Collection<String> policyTypes = new ArrayList<>();
72
73     public StdMatchablePolicyRequest() {
74         super();
75     }
76
77     /**
78      * Parses the DecisionRequest into a MonitoringRequest.
79      *
80      * @param decisionRequest Input DecisionRequest
81      * @return MonitoringRequest
82      */
83     @SuppressWarnings({"rawtypes", "unchecked"})
84     public static StdMatchablePolicyRequest createInstance(DecisionRequest decisionRequest) {
85         //
86         // Create our request object
87         //
88         StdMatchablePolicyRequest request = new StdMatchablePolicyRequest();
89         //
90         // Add the subject attributes
91         //
92         request.onapName = decisionRequest.getOnapName();
93         request.onapComponent = decisionRequest.getOnapComponent();
94         request.onapInstance = decisionRequest.getOnapInstance();
95         //
96         // Add the action attribute
97         //
98         request.action = decisionRequest.getAction();
99         //
100         // Add the resource attributes
101         //
102         Map<String, Object> resources = decisionRequest.getResource();
103         for (Entry<String, Object> entrySet : resources.entrySet()) {
104             //
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.
108             //
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());
114                 }
115                 continue;
116             }
117             if (POLICY_TYPE_KEY.equals(entrySet.getKey())) {
118                 if (entrySet.getValue() instanceof Collection) {
119                     addPolicyTypes(request, (Collection) entrySet.getValue());
120                 }
121                 if (entrySet.getValue() instanceof String) {
122                     request.policyTypes.add(entrySet.getValue().toString());
123                 }
124             }
125         }
126         return request;
127     }
128
129     private static StdMatchablePolicyRequest addPolicyScopes(StdMatchablePolicyRequest request,
130             Collection<Object> scopes) {
131         for (Object scope : scopes) {
132             request.policyScopes.add(scope.toString());
133         }
134         return request;
135     }
136
137     private static StdMatchablePolicyRequest addPolicyTypes(StdMatchablePolicyRequest request,
138             Collection<Object> types) {
139         for (Object type : types) {
140             request.policyTypes.add(type.toString());
141         }
142         return request;
143     }
144 }