3038b651d1168c498d51e98083f4a5835907e88b
[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 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 StdCombinedPolicyRequest {
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     public StdCombinedPolicyRequest() {
63         super();
64     }
65
66     /**
67      * Parses the DecisionRequest into a MonitoringRequest.
68      *
69      * @param decisionRequest Input DecisionRequest
70      * @return MonitoringRequest
71      */
72     @SuppressWarnings({"unchecked", "rawtypes"})
73     public static StdCombinedPolicyRequest createInstance(DecisionRequest decisionRequest) {
74         //
75         // Create our request object
76         //
77         StdCombinedPolicyRequest request = new StdCombinedPolicyRequest();
78         //
79         // Add the subject attributes
80         //
81         request.onapName = decisionRequest.getOnapName();
82         request.onapComponent = decisionRequest.getOnapComponent();
83         request.onapInstance = decisionRequest.getOnapInstance();
84         //
85         // Add the action attribute
86         //
87         request.action = decisionRequest.getAction();
88         //
89         // Add the resource attributes
90         //
91         Map<String, Object> resources = decisionRequest.getResource();
92         for (Entry<String, Object> entrySet : resources.entrySet()) {
93             if ("policy-id".equals(entrySet.getKey())) {
94                 if (entrySet.getValue() instanceof Collection) {
95                     addPolicyIds(request, (Collection) entrySet.getValue());
96                 } else if (entrySet.getValue() instanceof String) {
97                     request.resource.add(entrySet.getValue().toString());
98                 }
99                 continue;
100             }
101             if ("policy-type".equals(entrySet.getKey())) {
102                 if (entrySet.getValue() instanceof Collection) {
103                     addPolicyTypes(request, (Collection) entrySet.getValue());
104                 } else if (entrySet.getValue() instanceof String) {
105                     request.resource.add(entrySet.getValue().toString());
106                 }
107             }
108         }
109         return request;
110     }
111
112     private static StdCombinedPolicyRequest addPolicyIds(StdCombinedPolicyRequest request, Collection<Object> ids) {
113         for (Object id : ids) {
114             request.resource.add(id.toString());
115         }
116         return request;
117     }
118
119     private static StdCombinedPolicyRequest addPolicyTypes(StdCombinedPolicyRequest request, Collection<Object> types) {
120         for (Object type : types) {
121             request.resource.add(type.toString());
122         }
123         return request;
124     }
125 }