c516877a03df15b6b727f196b6ff31036c24c33d
[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     public GuardPolicyRequest() {
74         super();
75     }
76
77     /**
78      * Parses the DecisionRequest into a StdMetadataPolicyRequest.
79      *
80      * @param decisionRequest Input DecisionRequest
81      * @return StdMetadataPolicyRequest
82      * @throws ToscaPolicyConversionException If we cannot parse the request
83      */
84     @SuppressWarnings("unchecked")
85     public static GuardPolicyRequest createInstance(DecisionRequest decisionRequest)
86             throws ToscaPolicyConversionException {
87         //
88         // Create our return object
89         //
90         GuardPolicyRequest request = new GuardPolicyRequest();
91         //
92         // Add the subject attributes
93         //
94         request.onapName = decisionRequest.getOnapName();
95         request.onapComponent = decisionRequest.getOnapComponent();
96         request.onapInstance = decisionRequest.getOnapInstance();
97         request.requestId = decisionRequest.getRequestId();
98         //
99         // Now pull from the resources
100         //
101         Map<String, Object> resources = decisionRequest.getResource();
102         //
103         // Just in case nothing is in there
104         //
105         if (resources == null || resources.isEmpty() || !resources.containsKey(STR_GUARD)) {
106             //
107             // Perhaps we throw an exception and then caller
108             // can put together a response
109             //
110             return request;
111         }
112         Map<String, Object> guard = (Map<String, Object>) resources.get(STR_GUARD);
113         if (guard == null || guard.isEmpty()) {
114             //
115             // again, same problem throw an exception?
116             //
117             return request;
118         }
119         //
120         // Find our fields
121         //
122         if (guard.containsKey("actor")) {
123             request.actorId = guard.get("actor").toString();
124         }
125         if (guard.containsKey("operation")) {
126             request.operationId = guard.get("operation").toString();
127         }
128         if (guard.containsKey("clname")) {
129             request.clnameId = guard.get("clname").toString();
130         }
131         if (guard.containsKey("target")) {
132             request.targetId = guard.get("target").toString();
133         }
134         if (guard.containsKey("vfCount")) {
135             try {
136                 request.vfCount = Integer.decode(guard.get("vfCount").toString());
137             } catch (NumberFormatException e) {
138                 throw new ToscaPolicyConversionException("Failed to decode vfCount", e);
139             }
140         }
141
142         return request;
143     }
144
145 }