Add new guard filter policy type feature
[policy/xacml-pdp.git] / applications / guard / src / main / java / org / onap / policy / xacml / pdp / application / guard / GuardPolicyRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019-2020 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.xacml.pdp.application.guard;
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.Map;
30 import lombok.Getter;
31 import lombok.Setter;
32 import lombok.ToString;
33 import org.onap.policy.models.decisions.concepts.DecisionRequest;
34 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
35
36 @Getter
37 @Setter
38 @ToString
39 @XACMLRequest(ReturnPolicyIdList = true)
40 public class GuardPolicyRequest {
41     private static final String STR_GUARD = "guard";
42
43     @XACMLSubject(includeInResults = true)
44     private String onapName;
45
46     @XACMLSubject(includeInResults = true, attributeId = "urn:org:onap:onap-component")
47     private String onapComponent;
48
49     @XACMLSubject(includeInResults = true, attributeId = "urn:org:onap:onap-instance")
50     private String onapInstance;
51
52     @XACMLSubject(includeInResults = true, attributeId = "urn:org:onap:guard:request:request-id")
53     private String requestId;
54
55     @XACMLAction
56     private String action = STR_GUARD;
57
58     @XACMLResource(includeInResults = true, attributeId = "urn:org:onap:guard:clname:clname-id")
59     private String clnameId;
60
61     @XACMLResource(includeInResults = true, attributeId = "urn:org:onap:guard:actor:actor-id")
62     private String actorId;
63
64     @XACMLResource(includeInResults = true, attributeId = "urn:org:onap:guard:operation:operation-id")
65     private String operationId;
66
67     @XACMLResource(includeInResults = true, attributeId = "urn:org:onap:guard:target:target-id")
68     private String targetId;
69
70     @XACMLResource(includeInResults = true, attributeId = "urn:org:onap:guard:target:vf-count")
71     private Integer vfCount;
72
73     @XACMLResource(includeInResults = true, attributeId = "urn:org:onap:guard:target:generic-vnf.vnf-name")
74     private String vnfName;
75
76     @XACMLResource(includeInResults = true, attributeId = "urn:org:onap:guard:target:generic-vnf.vnf-id")
77     private String vnfId;
78
79     @XACMLResource(includeInResults = true, attributeId = "urn:org:onap:guard:target:generic-vnf.vnf-type")
80     private String vnfType;
81
82     @XACMLResource(includeInResults = true, attributeId = "urn:org:onap:guard:target:generic-vnf.nf-naming-code")
83     private String vnfNfNamingCode;
84
85     @XACMLResource(includeInResults = true, attributeId = "urn:org:onap:guard:target:vserver.vserver-id")
86     private String vserverId;
87
88     @XACMLResource(includeInResults = true, attributeId = "urn:org:onap:guard:target:cloud-region.cloud-region-id")
89     private String cloudRegionId;
90
91     public static final String PREFIX_RESOURCE_ATTRIBUTE_ID = "urn:org:onap:guard:target:";
92
93     public GuardPolicyRequest() {
94         super();
95     }
96
97     /**
98      * Parses the DecisionRequest into a StdMetadataPolicyRequest.
99      *
100      * @param decisionRequest Input DecisionRequest
101      * @return StdMetadataPolicyRequest
102      * @throws ToscaPolicyConversionException If we cannot parse the request
103      */
104     @SuppressWarnings("unchecked")
105     public static GuardPolicyRequest createInstance(DecisionRequest decisionRequest)
106             throws ToscaPolicyConversionException {
107         //
108         // Create our return object
109         //
110         GuardPolicyRequest request = new GuardPolicyRequest();
111         //
112         // Add the subject attributes
113         //
114         request.onapName = decisionRequest.getOnapName();
115         request.onapComponent = decisionRequest.getOnapComponent();
116         request.onapInstance = decisionRequest.getOnapInstance();
117         request.requestId = decisionRequest.getRequestId();
118         //
119         // Now pull from the resources
120         //
121         Map<String, Object> resources = decisionRequest.getResource();
122         //
123         // Just in case nothing is in there
124         //
125         if (resources == null || resources.isEmpty() || !resources.containsKey(STR_GUARD)) {
126             //
127             // Perhaps we throw an exception and then caller
128             // can put together a response
129             //
130             return request;
131         }
132         Map<String, Object> guard = (Map<String, Object>) resources.get(STR_GUARD);
133         if (guard == null || guard.isEmpty()) {
134             //
135             // again, same problem throw an exception?
136             //
137             return request;
138         }
139         //
140         // Find our fields
141         //
142         if (guard.containsKey("actor")) {
143             request.actorId = guard.get("actor").toString();
144         }
145         if (guard.containsKey("operation")) {
146             request.operationId = guard.get("operation").toString();
147         }
148         if (guard.containsKey("clname")) {
149             request.clnameId = guard.get("clname").toString();
150         }
151         if (guard.containsKey("target")) {
152             request.targetId = guard.get("target").toString();
153         }
154         if (guard.containsKey("vfCount")) {
155             try {
156                 request.vfCount = Integer.decode(guard.get("vfCount").toString());
157             } catch (NumberFormatException e) {
158                 throw new ToscaPolicyConversionException("Failed to decode vfCount", e);
159             }
160         }
161         if (guard.containsKey("generic-vnf.vnf-name")) {
162             request.vnfName = guard.get("generic-vnf.vnf-name").toString();
163         }
164         if (guard.containsKey("generic-vnf.vnf-id")) {
165             request.vnfId = guard.get("generic-vnf.vnf-id").toString();
166         }
167         if (guard.containsKey("generic-vnf.vnf-type")) {
168             request.vnfType = guard.get("generic-vnf.vnf-type").toString();
169         }
170         if (guard.containsKey("generic-vnf.nf-naming-code")) {
171             request.vnfNfNamingCode = guard.get("generic-vnf.nf-naming-code").toString();
172         }
173         if (guard.containsKey("vserver.vserver-id")) {
174             request.vserverId = guard.get("vserver.vserver-id").toString();
175         }
176         if (guard.containsKey("cloud-region.cloud-region-id")) {
177             request.cloudRegionId = guard.get("cloud-region.cloud-region-id").toString();
178         }
179
180         return request;
181     }
182
183 }