75b89d24a4dd8770c845f05487896ab864dfd6f2
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / std / StdCombinedPolicyRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019-2021 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 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Map;
32 import java.util.Map.Entry;
33 import lombok.Getter;
34 import lombok.Setter;
35 import lombok.ToString;
36 import org.onap.policy.models.decisions.concepts.DecisionRequest;
37
38 @Getter
39 @Setter
40 @ToString
41 @XACMLRequest(ReturnPolicyIdList = true)
42 public class StdCombinedPolicyRequest {
43
44     public static final String POLICY_TYPE_KEY = "policy-type";
45     public static final String POLICY_ID_KEY = "policy-id";
46
47     @XACMLSubject(includeInResults = true)
48     private String onapName;
49
50     @XACMLSubject(attributeId = "urn:org:onap:onap-component", includeInResults = true)
51     private String onapComponent;
52
53     @XACMLSubject(attributeId = "urn:org:onap:onap-instance",  includeInResults = true)
54     private String onapInstance;
55
56     @XACMLAction()
57     private String action;
58
59     @XACMLResource(includeInResults = true)
60     private Collection<String> resource = new ArrayList<>();
61
62     @XACMLResource(attributeId = "urn:org:onap:policy-type", includeInResults = true)
63     private Collection<String> resourcePolicyType = new ArrayList<>();
64
65     public StdCombinedPolicyRequest() {
66         super();
67     }
68
69     /**
70      * Parses the DecisionRequest into a MonitoringRequest.
71      *
72      * @param decisionRequest Input DecisionRequest
73      * @return MonitoringRequest
74      */
75     @SuppressWarnings({"unchecked", "rawtypes"})
76     public static StdCombinedPolicyRequest createInstance(DecisionRequest decisionRequest) {
77         //
78         // Create our request object
79         //
80         var request = new StdCombinedPolicyRequest();
81         //
82         // Add the subject attributes
83         //
84         request.onapName = decisionRequest.getOnapName();
85         request.onapComponent = decisionRequest.getOnapComponent();
86         request.onapInstance = decisionRequest.getOnapInstance();
87         //
88         // Add the action attribute
89         //
90         request.action = decisionRequest.getAction();
91         //
92         // Add the resource attributes
93         //
94         Map<String, Object> resources = decisionRequest.getResource();
95         for (Entry<String, Object> entrySet : resources.entrySet()) {
96             if (POLICY_ID_KEY.equals(entrySet.getKey())) {
97                 if (entrySet.getValue() instanceof Collection) {
98                     addPolicyIds(request, (Collection) entrySet.getValue());
99                 } else if (entrySet.getValue() instanceof String) {
100                     request.resource.add(entrySet.getValue().toString());
101                 }
102                 continue;
103             }
104             if (POLICY_TYPE_KEY.equals(entrySet.getKey())) {
105                 if (entrySet.getValue() instanceof Collection) {
106                     addPolicyTypes(request, (Collection) entrySet.getValue());
107                 } else if (entrySet.getValue() instanceof String) {
108                     request.resourcePolicyType.add(entrySet.getValue().toString());
109                 }
110             }
111         }
112         return request;
113     }
114
115     private static StdCombinedPolicyRequest addPolicyIds(StdCombinedPolicyRequest request, Collection<Object> ids) {
116         for (Object id : ids) {
117             request.resource.add(id.toString());
118         }
119         return request;
120     }
121
122     private static StdCombinedPolicyRequest addPolicyTypes(StdCombinedPolicyRequest request, Collection<Object> types) {
123         for (Object type : types) {
124             request.resourcePolicyType.add(type.toString());
125         }
126         return request;
127     }
128 }