Add junit coverage to xacml-pdp
[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.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;
36
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;
42
43 import javax.persistence.EntityManager;
44
45 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49
50 public abstract class StdOnapPip extends StdConfigurableEngine {
51     protected static Logger logger = LoggerFactory.getLogger(StdOnapPip.class);
52
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);
57
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);
62
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);
67
68     protected Properties properties;
69     protected EntityManager em;
70
71     public StdOnapPip() {
72         super();
73     }
74
75     @Override
76     public Collection<PIPRequest> attributesProvided() {
77         return Collections.emptyList();
78     }
79
80     @Override
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;
85     }
86
87     protected String getAttribute(PIPFinder pipFinder, PIPRequest pipRequest) {
88         //
89         // Get the actor value
90         //
91         PIPResponse pipResponse = this.getAttribute(pipRequest, pipFinder);
92         if (pipResponse == null) {
93             logger.error("Need actor attribute which is not found");
94             return null;
95         }
96         //
97         // Find the actor
98         //
99         return findFirstAttributeValue(pipResponse);
100     }
101
102     protected PIPResponse getAttribute(PIPRequest pipRequest, PIPFinder pipFinder) {
103         PIPResponse pipResponse = null;
104         try {
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());
110                 }
111                 pipResponse = null;
112             }
113             if (pipResponse != null && pipResponse.getAttributes().isEmpty()) {
114                 if (logger.isInfoEnabled()) {
115                     logger.info("No value for {}", pipRequest.getAttributeId().stringValue());
116                 }
117                 pipResponse = null;
118             }
119         } catch (PIPException ex) {
120             logger.error("PIPException getting subject-id attribute: " + ex.getMessage(), ex);
121         }
122         return pipResponse;
123     }
124
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();
131                     if (value != null) {
132                         return value;
133                     }
134                 }
135             }
136         }
137         return null;
138     }
139
140     protected void addIntegerAttribute(StdMutablePIPResponse stdPipResponse, Identifier category,
141             Identifier attributeId, int value, PIPRequest pipRequest) {
142         AttributeValue<BigInteger> attributeValue   = null;
143         try {
144             attributeValue  = DataTypes.DT_INTEGER.createAttributeValue(value);
145         } catch (Exception e) {
146             logger.error("Failed to convert {} to integer {}", value, e);
147         }
148         if (attributeValue != null) {
149             stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue,
150                     pipRequest.getIssuer(), false));
151         }
152     }
153
154     protected void addLongAttribute(StdMutablePIPResponse stdPipResponse, Identifier category,
155             Identifier attributeId, long value, PIPRequest pipRequest) {
156         AttributeValue<BigInteger> attributeValue   = null;
157         try {
158             attributeValue  = DataTypes.DT_INTEGER.createAttributeValue(value);
159         } catch (Exception e) {
160             logger.error("Failed to convert {} to long {}", value, e);
161         }
162         if (attributeValue != null) {
163             stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue,
164                     pipRequest.getIssuer(), false));
165         }
166     }
167
168     protected void addStringAttribute(StdMutablePIPResponse stdPipResponse, Identifier category, Identifier attributeId,
169             String value, PIPRequest pipRequest) {
170         AttributeValue<String> attributeValue = null;
171         try {
172             attributeValue = DataTypes.DT_STRING.createAttributeValue(value);
173         } catch (Exception ex) {
174             logger.error("Failed to convert {} to an AttributeValue<String>", value, ex);
175         }
176         if (attributeValue != null) {
177             stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue,
178                     pipRequest.getIssuer(), false));
179         }
180     }
181
182 }