1e3e915096838a498912c0b22679cd9b2398ece8
[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
30 import java.util.Map;
31
32 import lombok.Getter;
33 import lombok.Setter;
34 import lombok.ToString;
35
36 import org.onap.policy.models.decisions.concepts.DecisionRequest;
37 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
38
39 @Getter
40 @Setter
41 @ToString
42 @XACMLRequest(ReturnPolicyIdList = true)
43 public class GuardPolicyRequest {
44     private static final String STR_GUARD = "guard";
45
46     @XACMLSubject(includeInResults = true)
47     private String onapName;
48
49     @XACMLSubject(includeInResults = true, attributeId = "urn:org:onap:onap-component")
50     private String onapComponent;
51
52     @XACMLSubject(includeInResults = true, attributeId = "urn:org:onap:onap-instance")
53     private String onapInstance;
54
55     @XACMLSubject(includeInResults = true, attributeId = "urn:org:onap:guard:request:request-id")
56     private String requestId;
57
58     @XACMLAction
59     private String action = STR_GUARD;
60
61     @XACMLResource(includeInResults = true, attributeId = "urn:org:onap:guard:clname:clname-id")
62     private String clnameId;
63
64     @XACMLResource(includeInResults = true, attributeId = "urn:org:onap:guard:actor:actor-id")
65     private String actorId;
66
67     @XACMLResource(includeInResults = true, attributeId = "urn:org:onap:guard:operation:operation-id")
68     private String operationId;
69
70     @XACMLResource(includeInResults = true, attributeId = "urn:org:onap:guard:target:target-id")
71     private String targetId;
72
73     @XACMLResource(includeInResults = true, attributeId = "urn:org:onap:guard:target:vf-count")
74     private Integer vfCount;
75
76     public GuardPolicyRequest() {
77         super();
78     }
79
80     /**
81      * Parses the DecisionRequest into a StdMetadataPolicyRequest.
82      *
83      * @param decisionRequest Input DecisionRequest
84      * @return StdMetadataPolicyRequest
85      * @throws ToscaPolicyConversionException If we cannot parse the request
86      */
87     @SuppressWarnings("unchecked")
88     public static GuardPolicyRequest createInstance(DecisionRequest decisionRequest) 
89             throws ToscaPolicyConversionException {
90         //
91         // Create our return object
92         //
93         GuardPolicyRequest request = new GuardPolicyRequest();
94         //
95         // Add the subject attributes
96         //
97         request.onapName = decisionRequest.getOnapName();
98         request.onapComponent = decisionRequest.getOnapComponent();
99         request.onapInstance = decisionRequest.getOnapInstance();
100         request.requestId = decisionRequest.getRequestId();
101         //
102         // Now pull from the resources
103         //
104         Map<String, Object> resources = decisionRequest.getResource();
105         //
106         // Just in case nothing is in there
107         //
108         if (resources == null || resources.isEmpty() || ! resources.containsKey(STR_GUARD)) {
109             //
110             // Perhaps we throw an exception and then caller
111             // can put together a response
112             //
113             return request;
114         }
115         Map<String, Object> guard = (Map<String, Object>) resources.get(STR_GUARD);
116         if (guard == null || guard.isEmpty()) {
117             //
118             // again, same problem throw an exception?
119             //
120             return request;
121         }
122         //
123         // Find our fields
124         //
125         if (guard.containsKey("actor")) {
126             request.actorId = guard.get("actor").toString();
127         }
128         if (guard.containsKey("recipe")) {
129             request.operationId = guard.get("recipe").toString();
130         }
131         if (guard.containsKey("clname")) {
132             request.clnameId = guard.get("clname").toString();
133         }
134         if (guard.containsKey("target")) {
135             request.targetId = guard.get("target").toString();
136         }
137         if (guard.containsKey("vfCount")) {
138             try {
139                 request.vfCount = Integer.decode(guard.get("vfCount").toString());
140             } catch (NumberFormatException e) {
141                 throw new ToscaPolicyConversionException("Failed to decode vfCount", e);
142             }
143         }
144
145         return request;
146     }
147
148 }