356480bc8d25ed5cc18e48cbad301b5e27cccc2c
[policy/xacml-pdp.git] / tutorials / tutorial-xacml-application / src / main / java / org / onap / policy / tutorial / tutorial / TutorialRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.policy.tutorial.tutorial;
20
21 import com.att.research.xacml.std.annotations.XACMLAction;
22 import com.att.research.xacml.std.annotations.XACMLRequest;
23 import com.att.research.xacml.std.annotations.XACMLResource;
24 import com.att.research.xacml.std.annotations.XACMLSubject;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import lombok.Getter;
28 import lombok.Setter;
29 import lombok.ToString;
30 import org.onap.policy.models.decisions.concepts.DecisionRequest;
31
32 @Getter
33 @Setter
34 @ToString
35 @XACMLRequest(ReturnPolicyIdList = true)
36 public class TutorialRequest {
37     //
38     // Excluding from results to demonstrate control as to which attributes can be returned.
39     //
40     @XACMLSubject(includeInResults = false)
41     private String onapName;
42
43     @XACMLSubject(attributeId = "urn:org:onap:onap-component", includeInResults = false)
44     private String onapComponent;
45
46     @XACMLSubject(attributeId = "urn:org:onap:onap-instance", includeInResults = false)
47     private String onapInstance;
48
49     @XACMLAction()
50     private String action;
51
52     //
53     // Including in results to demonstrate control as to which attributes can be returned.
54     //
55     @XACMLResource(attributeId = "urn:org:onap:tutorial-user", includeInResults = true)
56     private String user;
57
58     @XACMLResource(attributeId = "urn:org:onap:tutorial-entity", includeInResults = true)
59     private String entity;
60
61     @XACMLResource(attributeId = "urn:org:onap:tutorial-permission", includeInResults = true)
62     private String permission;
63
64     /**
65      * createRequest.
66      *
67      * @param decisionRequest Incoming
68      * @return TutorialRequest object
69      */
70     public static TutorialRequest createRequest(DecisionRequest decisionRequest) {
71         //
72         // Create our object
73         //
74         var request = new TutorialRequest();
75         //
76         // Add the subject attributes
77         //
78         request.onapName = decisionRequest.getOnapName();
79         request.onapComponent = decisionRequest.getOnapComponent();
80         request.onapInstance = decisionRequest.getOnapInstance();
81         //
82         // Add the action attribute
83         //
84         request.action = decisionRequest.getAction();
85         //
86         // Add the resource attributes
87         //
88         Map<String, Object> resources = decisionRequest.getResource();
89         for (Entry<String, Object> entrySet : resources.entrySet()) {
90             if ("user".equals(entrySet.getKey())) {
91                 request.user = entrySet.getValue().toString();
92             }
93             if ("entity".equals(entrySet.getKey())) {
94                 request.entity = entrySet.getValue().toString();
95             }
96             if ("permission".equals(entrySet.getKey())) {
97                 request.permission = entrySet.getValue().toString();
98             }
99         }
100
101         return request;
102     }
103 }