Use yaml policies in models-examples
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / std / StdOnapPip.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 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  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdp.xacml.application.common.std;
22
23 import com.att.research.xacml.api.Attribute;
24 import com.att.research.xacml.api.AttributeValue;
25 import com.att.research.xacml.api.DataTypeException;
26 import com.att.research.xacml.api.Identifier;
27 import com.att.research.xacml.api.XACML3;
28 import com.att.research.xacml.api.pip.PIPException;
29 import com.att.research.xacml.api.pip.PIPFinder;
30 import com.att.research.xacml.api.pip.PIPRequest;
31 import com.att.research.xacml.api.pip.PIPResponse;
32 import com.att.research.xacml.std.StdMutableAttribute;
33 import com.att.research.xacml.std.datatypes.DataTypes;
34 import com.att.research.xacml.std.pip.StdMutablePIPResponse;
35 import com.att.research.xacml.std.pip.StdPIPRequest;
36 import com.att.research.xacml.std.pip.engines.StdConfigurableEngine;
37
38 import java.math.BigInteger;
39 import java.util.Collection;
40 import java.util.Collections;
41 import java.util.Iterator;
42 import java.util.Properties;
43
44 import javax.persistence.EntityManager;
45
46 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50
51 public abstract class StdOnapPip extends StdConfigurableEngine {
52     protected static Logger logger = LoggerFactory.getLogger(StdOnapPip.class);
53
54     protected static final PIPRequest PIP_REQUEST_ACTOR   = new StdPIPRequest(
55             XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
56             ToscaDictionary.ID_RESOURCE_GUARD_ACTOR,
57             XACML3.ID_DATATYPE_STRING);
58
59     protected static final PIPRequest PIP_REQUEST_RECIPE  = new StdPIPRequest(
60             XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
61             ToscaDictionary.ID_RESOURCE_GUARD_RECIPE,
62             XACML3.ID_DATATYPE_STRING);
63
64     protected static final PIPRequest PIP_REQUEST_TARGET  = new StdPIPRequest(
65             XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
66             ToscaDictionary.ID_RESOURCE_GUARD_TARGETID,
67             XACML3.ID_DATATYPE_STRING);
68
69     protected Properties properties;
70     protected EntityManager em;
71
72     public StdOnapPip() {
73         super();
74     }
75
76     @Override
77     public Collection<PIPRequest> attributesProvided() {
78         return Collections.emptyList();
79     }
80
81     @Override
82     public void configure(String id, Properties properties) throws PIPException {
83         super.configure(id, properties);
84         logger.info("Configuring historyDb PIP {}", properties);
85         this.properties = properties;
86     }
87
88     protected String getAttribute(PIPFinder pipFinder, PIPRequest pipRequest) {
89         //
90         // Get the actor value
91         //
92         PIPResponse pipResponse = this.getAttribute(pipRequest, pipFinder);
93         if (pipResponse == null) {
94             logger.error("Need actor attribute which is not found");
95             return null;
96         }
97         //
98         // Find the actor
99         //
100         return findFirstAttributeValue(pipResponse);
101     }
102
103     protected PIPResponse getAttribute(PIPRequest pipRequest, PIPFinder pipFinder) {
104         PIPResponse pipResponse = null;
105         try {
106             pipResponse = pipFinder.getMatchingAttributes(pipRequest, this);
107             if (pipResponse.getStatus() != null && !pipResponse.getStatus().isOk()) {
108                 logger.info("get attribute error retrieving {}: {}", pipRequest.getAttributeId(),
109                                 pipResponse.getStatus());
110                 pipResponse = null;
111             }
112             if (pipResponse != null && pipResponse.getAttributes().isEmpty()) {
113                 logger.info("No value for {}", pipRequest.getAttributeId());
114                 pipResponse = null;
115             }
116         } catch (PIPException ex) {
117             logger.error("PIPException getting subject-id attribute: " + ex.getMessage(), ex);
118         }
119         return pipResponse;
120     }
121
122     protected String findFirstAttributeValue(PIPResponse pipResponse) {
123         for (Attribute attribute: pipResponse.getAttributes()) {
124             Iterator<AttributeValue<String>> iterAttributeValues    = attribute.findValues(DataTypes.DT_STRING);
125             while (iterAttributeValues.hasNext()) {
126                 String value   = iterAttributeValues.next().getValue();
127                 if (value != null) {
128                     return value;
129                 }
130             }
131         }
132         return null;
133     }
134
135     protected void addIntegerAttribute(StdMutablePIPResponse stdPipResponse, Identifier category,
136             Identifier attributeId, int value, PIPRequest pipRequest) {
137         AttributeValue<BigInteger> attributeValue   = null;
138         try {
139             attributeValue  = makeInteger(value);
140         } catch (Exception e) {
141             logger.error("Failed to convert {} to integer {}", value, e);
142         }
143         if (attributeValue != null) {
144             stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue,
145                     pipRequest.getIssuer(), false));
146         }
147     }
148
149     protected void addLongAttribute(StdMutablePIPResponse stdPipResponse, Identifier category,
150             Identifier attributeId, long value, PIPRequest pipRequest) {
151         AttributeValue<BigInteger> attributeValue   = null;
152         try {
153             attributeValue  = makeLong(value);
154         } catch (Exception e) {
155             logger.error("Failed to convert {} to long {}", value, e);
156         }
157         if (attributeValue != null) {
158             stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue,
159                     pipRequest.getIssuer(), false));
160         }
161     }
162
163     protected void addStringAttribute(StdMutablePIPResponse stdPipResponse, Identifier category, Identifier attributeId,
164             String value, PIPRequest pipRequest) {
165         AttributeValue<String> attributeValue = null;
166         try {
167             attributeValue = makeString(value);
168         } catch (Exception ex) {
169             logger.error("Failed to convert {} to an AttributeValue<String>", value, ex);
170         }
171         if (attributeValue != null) {
172             stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue,
173                     pipRequest.getIssuer(), false));
174         }
175     }
176
177     // these may be overridden by junit tests
178
179     protected AttributeValue<BigInteger> makeInteger(int value) throws DataTypeException {
180         return DataTypes.DT_INTEGER.createAttributeValue(value);
181     }
182
183     protected AttributeValue<BigInteger> makeLong(long value) throws DataTypeException {
184         return DataTypes.DT_INTEGER.createAttributeValue(value);
185     }
186
187     protected AttributeValue<String> makeString(String value) throws DataTypeException {
188         return DataTypes.DT_STRING.createAttributeValue(value);
189     }
190
191 }