2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pdp.xacml.application.common.std;
24 import com.att.research.xacml.api.Attribute;
25 import com.att.research.xacml.api.AttributeValue;
26 import com.att.research.xacml.api.DataTypeException;
27 import com.att.research.xacml.api.Identifier;
28 import com.att.research.xacml.api.XACML3;
29 import com.att.research.xacml.api.pip.PIPException;
30 import com.att.research.xacml.api.pip.PIPFinder;
31 import com.att.research.xacml.api.pip.PIPRequest;
32 import com.att.research.xacml.api.pip.PIPResponse;
33 import com.att.research.xacml.std.StdMutableAttribute;
34 import com.att.research.xacml.std.datatypes.DataTypes;
35 import com.att.research.xacml.std.pip.StdMutablePIPResponse;
36 import com.att.research.xacml.std.pip.StdPIPRequest;
37 import com.att.research.xacml.std.pip.engines.StdConfigurableEngine;
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 import javax.persistence.EntityManager;
44 import javax.persistence.Persistence;
45 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 public abstract class StdOnapPip extends StdConfigurableEngine {
51 protected static Logger logger = LoggerFactory.getLogger(StdOnapPip.class);
53 protected static final PIPRequest PIP_REQUEST_ACTOR = new StdPIPRequest(
54 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
55 ToscaDictionary.ID_RESOURCE_GUARD_ACTOR,
56 XACML3.ID_DATATYPE_STRING);
58 protected static final PIPRequest PIP_REQUEST_RECIPE = new StdPIPRequest(
59 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
60 ToscaDictionary.ID_RESOURCE_GUARD_RECIPE,
61 XACML3.ID_DATATYPE_STRING);
63 protected static final PIPRequest PIP_REQUEST_TARGET = new StdPIPRequest(
64 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
65 ToscaDictionary.ID_RESOURCE_GUARD_TARGETID,
66 XACML3.ID_DATATYPE_STRING);
68 protected Properties properties;
69 protected EntityManager em;
70 protected String issuer;
71 protected boolean shutdown = false;
73 protected StdOnapPip() {
78 public Collection<PIPRequest> attributesProvided() {
79 return Collections.emptyList();
83 public synchronized void configure(String id, Properties properties) throws PIPException {
85 // This most likely will never get called since configure is called
89 throw new PIPException("Engine is shutdown.");
91 super.configure(id, properties);
92 logger.info("Configuring historyDb PIP {}", properties);
93 this.properties = properties;
95 // Create our entity manager
100 // In case there are any overloaded properties for the JPA
102 var emProperties = new Properties();
103 emProperties.putAll(properties);
106 // Create the entity manager factory
108 em = Persistence.createEntityManagerFactory(
109 properties.getProperty(this.issuer + ".persistenceunit"),
110 emProperties).createEntityManager();
111 } catch (Exception e) {
112 logger.error("Persistence failed {} operations history db", e.getLocalizedMessage(), e);
117 public synchronized void shutdown() {
118 if (this.em != null) {
122 this.shutdown = true;
125 protected String getAttribute(PIPFinder pipFinder, PIPRequest pipRequest) {
127 // Get the actor value
129 var pipResponse = this.getAttribute(pipRequest, pipFinder);
130 if (pipResponse == null) {
131 logger.error("Need actor attribute which is not found");
137 return findFirstAttributeValue(pipResponse);
140 protected PIPResponse getAttribute(PIPRequest pipRequest, PIPFinder pipFinder) {
141 PIPResponse pipResponse = null;
143 pipResponse = pipFinder.getMatchingAttributes(pipRequest, this);
144 if (pipResponse.getStatus() != null && !pipResponse.getStatus().isOk()) {
145 logger.info("get attribute error retrieving {}: {}", pipRequest.getAttributeId(),
146 pipResponse.getStatus());
149 if (pipResponse != null && pipResponse.getAttributes().isEmpty()) {
150 logger.info("No value for {}", pipRequest.getAttributeId());
153 } catch (PIPException ex) {
154 logger.error("PIPException getting subject-id attribute", ex);
159 protected String findFirstAttributeValue(PIPResponse pipResponse) {
160 for (Attribute attribute: pipResponse.getAttributes()) {
161 Iterator<AttributeValue<String>> iterAttributeValues = attribute.findValues(DataTypes.DT_STRING);
162 while (iterAttributeValues.hasNext()) {
163 String value = iterAttributeValues.next().getValue();
172 protected void addIntegerAttribute(StdMutablePIPResponse stdPipResponse, Identifier category,
173 Identifier attributeId, int value, PIPRequest pipRequest) {
174 AttributeValue<BigInteger> attributeValue = null;
176 attributeValue = makeInteger(value);
177 } catch (Exception e) {
178 logger.error("Failed to convert {} to integer", value, e);
180 if (attributeValue != null) {
181 stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue,
182 pipRequest.getIssuer(), false));
186 protected void addLongAttribute(StdMutablePIPResponse stdPipResponse, Identifier category,
187 Identifier attributeId, long value, PIPRequest pipRequest) {
188 AttributeValue<BigInteger> attributeValue = null;
190 attributeValue = makeLong(value);
191 } catch (Exception e) {
192 logger.error("Failed to convert {} to long", value, e);
194 if (attributeValue != null) {
195 stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue,
196 pipRequest.getIssuer(), false));
200 protected void addStringAttribute(StdMutablePIPResponse stdPipResponse, Identifier category, Identifier attributeId,
201 String value, PIPRequest pipRequest) {
202 AttributeValue<String> attributeValue = null;
204 attributeValue = makeString(value);
205 } catch (Exception ex) {
206 logger.error("Failed to convert {} to an AttributeValue<String>", value, ex);
208 if (attributeValue != null) {
209 stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue,
210 pipRequest.getIssuer(), false));
214 // these may be overridden by junit tests
216 protected AttributeValue<BigInteger> makeInteger(int value) throws DataTypeException {
217 return DataTypes.DT_INTEGER.createAttributeValue(value);
220 protected AttributeValue<BigInteger> makeLong(long value) throws DataTypeException {
221 return DataTypes.DT_INTEGER.createAttributeValue(value);
224 protected AttributeValue<String> makeString(String value) throws DataTypeException {
225 return DataTypes.DT_STRING.createAttributeValue(value);