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