Adding optimization application finish guard
[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     @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     //
60     // Unfortunately the annotations won't take an object.toString()
61     // So I could not use the ToscaDictionary class to put these id's
62     // into the annotations.
63     //
64     @XACMLResource(attributeId = "urn:org:onap:policy-scope-property", includeInResults = true)
65     Collection<String> policyScopes = new ArrayList<>();
66
67     @XACMLResource(attributeId = "urn:org:onap:policy-type-property", includeInResults = true)
68     Collection<String> policyTypes = new ArrayList<>();
69
70     public StdMatchablePolicyRequest() {
71         super();
72     }
73
74     /**
75      * Parses the DecisionRequest into a MonitoringRequest.
76      *
77      * @param decisionRequest Input DecisionRequest
78      * @return MonitoringRequest
79      */
80     @SuppressWarnings({"rawtypes", "unchecked"})
81     public static StdMatchablePolicyRequest createInstance(DecisionRequest decisionRequest) {
82         //
83         // Create our request object
84         //
85         StdMatchablePolicyRequest request = new StdMatchablePolicyRequest();
86         //
87         // Add the subject attributes
88         //
89         request.onapName = decisionRequest.getOnapName();
90         request.onapComponent = decisionRequest.getOnapComponent();
91         request.onapInstance = decisionRequest.getOnapInstance();
92         //
93         // Add the action attribute
94         //
95         request.action = decisionRequest.getAction();
96         //
97         // Add the resource attributes
98         //
99         Map<String, Object> resources = decisionRequest.getResource();
100         for (Entry<String, Object> entrySet : resources.entrySet()) {
101             //
102             // Making an assumption that these two fields are matchable.
103             // Its possible we may have to load the policy type model
104             // and use that to find the fields that are matchable.
105             //
106             if ("policyScope".equals(entrySet.getKey())) {
107                 if (entrySet.getValue() instanceof Collection) {
108                     addPolicyScopes(request, (Collection) entrySet.getValue());
109                 } else if (entrySet.getValue() instanceof String) {
110                     request.policyScopes.add(entrySet.getValue().toString());
111                 }
112                 continue;
113             }
114             if ("policyType".equals(entrySet.getKey())) {
115                 if (entrySet.getValue() instanceof Collection) {
116                     addPolicyTypes(request, (Collection) entrySet.getValue());
117                 }
118                 if (entrySet.getValue() instanceof String) {
119                     request.policyTypes.add(entrySet.getValue().toString());
120                 }
121             }
122         }
123         return request;
124     }
125
126     private static StdMatchablePolicyRequest addPolicyScopes(StdMatchablePolicyRequest request,
127             Collection<Object> scopes) {
128         for (Object scope : scopes) {
129             request.policyScopes.add(scope.toString());
130         }
131         return request;
132     }
133
134     private static StdMatchablePolicyRequest addPolicyTypes(StdMatchablePolicyRequest request,
135             Collection<Object> types) {
136         for (Object type : types) {
137             request.policyTypes.add(type.toString());
138         }
139         return request;
140     }
141 }