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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdp.xacml.application.common.std;
23 import com.att.research.xacml.api.Attribute;
24 import com.att.research.xacml.api.AttributeValue;
25 import com.att.research.xacml.api.Identifier;
26 import com.att.research.xacml.api.XACML3;
27 import com.att.research.xacml.api.pip.PIPException;
28 import com.att.research.xacml.api.pip.PIPFinder;
29 import com.att.research.xacml.api.pip.PIPRequest;
30 import com.att.research.xacml.api.pip.PIPResponse;
31 import com.att.research.xacml.std.StdMutableAttribute;
32 import com.att.research.xacml.std.datatypes.DataTypes;
33 import com.att.research.xacml.std.pip.StdMutablePIPResponse;
34 import com.att.research.xacml.std.pip.StdPIPRequest;
35 import com.att.research.xacml.std.pip.engines.StdConfigurableEngine;
37 import java.math.BigInteger;
38 import java.util.Collection;
39 import java.util.Collections;
40 import java.util.Iterator;
41 import java.util.Properties;
43 import javax.persistence.EntityManager;
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;
76 public Collection<PIPRequest> attributesProvided() {
77 return Collections.emptyList();
81 public void configure(String id, Properties properties) throws PIPException {
82 super.configure(id, properties);
83 logger.info("Configuring historyDb PIP {}", properties);
84 this.properties = properties;
87 protected String getAttribute(PIPFinder pipFinder, PIPRequest pipRequest) {
89 // Get the actor value
91 PIPResponse pipResponse = this.getAttribute(pipRequest, pipFinder);
92 if (pipResponse == null) {
93 logger.error("Need actor attribute which is not found");
99 return findFirstAttributeValue(pipResponse);
102 protected PIPResponse getAttribute(PIPRequest pipRequest, PIPFinder pipFinder) {
103 PIPResponse pipResponse = null;
105 pipResponse = pipFinder.getMatchingAttributes(pipRequest, this);
106 if (pipResponse.getStatus() != null && !pipResponse.getStatus().isOk()) {
107 if (logger.isInfoEnabled()) {
108 logger.info("get attribute error retrieving {}: {}", pipRequest.getAttributeId().stringValue(),
109 pipResponse.getStatus());
113 if (pipResponse != null && pipResponse.getAttributes().isEmpty()) {
114 if (logger.isInfoEnabled()) {
115 logger.info("No value for {}", pipRequest.getAttributeId().stringValue());
119 } catch (PIPException ex) {
120 logger.error("PIPException getting subject-id attribute: " + ex.getMessage(), ex);
125 protected String findFirstAttributeValue(PIPResponse pipResponse) {
126 for (Attribute attribute: pipResponse.getAttributes()) {
127 Iterator<AttributeValue<String>> iterAttributeValues = attribute.findValues(DataTypes.DT_STRING);
128 if (iterAttributeValues != null) {
129 while (iterAttributeValues.hasNext()) {
130 String value = iterAttributeValues.next().getValue();
140 protected void addIntegerAttribute(StdMutablePIPResponse stdPipResponse, Identifier category,
141 Identifier attributeId, int value, PIPRequest pipRequest) {
142 AttributeValue<BigInteger> attributeValue = null;
144 attributeValue = DataTypes.DT_INTEGER.createAttributeValue(value);
145 } catch (Exception e) {
146 logger.error("Failed to convert {} to integer {}", value, e);
148 if (attributeValue != null) {
149 stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue,
150 pipRequest.getIssuer(), false));
154 protected void addLongAttribute(StdMutablePIPResponse stdPipResponse, Identifier category,
155 Identifier attributeId, long value, PIPRequest pipRequest) {
156 AttributeValue<BigInteger> attributeValue = null;
158 attributeValue = DataTypes.DT_INTEGER.createAttributeValue(value);
159 } catch (Exception e) {
160 logger.error("Failed to convert {} to long {}", value, e);
162 if (attributeValue != null) {
163 stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue,
164 pipRequest.getIssuer(), false));
168 protected void addStringAttribute(StdMutablePIPResponse stdPipResponse, Identifier category, Identifier attributeId,
169 String value, PIPRequest pipRequest) {
170 AttributeValue<String> attributeValue = null;
172 attributeValue = DataTypes.DT_STRING.createAttributeValue(value);
173 } catch (Exception ex) {
174 logger.error("Failed to convert {} to an AttributeValue<String>", value, ex);
176 if (attributeValue != null) {
177 stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue,
178 pipRequest.getIssuer(), false));