Upgrade Java 17 in 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-2021 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
5  * Modifications Copyright (C) 2023 Nordix Foundation.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pdp.xacml.application.common.std;
24
25 import com.att.research.xacml.api.Attribute;
26 import com.att.research.xacml.api.AttributeValue;
27 import com.att.research.xacml.api.DataTypeException;
28 import com.att.research.xacml.api.Identifier;
29 import com.att.research.xacml.api.XACML3;
30 import com.att.research.xacml.api.pip.PIPException;
31 import com.att.research.xacml.api.pip.PIPFinder;
32 import com.att.research.xacml.api.pip.PIPRequest;
33 import com.att.research.xacml.api.pip.PIPResponse;
34 import com.att.research.xacml.std.StdMutableAttribute;
35 import com.att.research.xacml.std.datatypes.DataTypes;
36 import com.att.research.xacml.std.pip.StdMutablePIPResponse;
37 import com.att.research.xacml.std.pip.StdPIPRequest;
38 import com.att.research.xacml.std.pip.engines.StdConfigurableEngine;
39 import jakarta.persistence.EntityManager;
40 import jakarta.persistence.Persistence;
41 import java.math.BigInteger;
42 import java.util.Collection;
43 import java.util.Collections;
44 import java.util.Iterator;
45 import java.util.Properties;
46 import lombok.AccessLevel;
47 import lombok.NoArgsConstructor;
48 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 @NoArgsConstructor(access = AccessLevel.PROTECTED)
53 public abstract class StdOnapPip extends StdConfigurableEngine {
54     protected static Logger logger = LoggerFactory.getLogger(StdOnapPip.class);
55
56     protected static final PIPRequest PIP_REQUEST_ACTOR   = new StdPIPRequest(
57             XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
58             ToscaDictionary.ID_RESOURCE_GUARD_ACTOR,
59             XACML3.ID_DATATYPE_STRING);
60
61     protected static final PIPRequest PIP_REQUEST_RECIPE  = new StdPIPRequest(
62             XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
63             ToscaDictionary.ID_RESOURCE_GUARD_RECIPE,
64             XACML3.ID_DATATYPE_STRING);
65
66     protected static final PIPRequest PIP_REQUEST_TARGET  = new StdPIPRequest(
67             XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
68             ToscaDictionary.ID_RESOURCE_GUARD_TARGETID,
69             XACML3.ID_DATATYPE_STRING);
70
71     protected Properties properties;
72     protected EntityManager em;
73     protected String issuer;
74     protected boolean shutdown = false;
75
76     @Override
77     public Collection<PIPRequest> attributesProvided() {
78         return Collections.emptyList();
79     }
80
81     @Override
82     public synchronized void configure(String id, Properties properties) throws PIPException {
83         //
84         // This most likely will never get called since configure is called
85         // upon startup.
86         //
87         if (this.shutdown) {
88             throw new PIPException("Engine is shutdown.");
89         }
90         super.configure(id, properties);
91         logger.info("Configuring historyDb PIP {}", properties);
92         this.properties = properties;
93         //
94         // Create our entity manager
95         //
96         em = null;
97         try {
98             //
99             // In case there are any overloaded properties for the JPA
100             //
101             var emProperties = new Properties();
102             emProperties.putAll(properties);
103
104             //
105             // Create the entity manager factory
106             //
107             em = Persistence.createEntityManagerFactory(
108                     properties.getProperty(this.issuer + ".persistenceunit"),
109                     emProperties).createEntityManager();
110         } catch (Exception e) {
111             logger.error("Persistence failed {} operations history db", e.getLocalizedMessage(), e);
112         }
113     }
114
115     @Override
116     public synchronized void shutdown() {
117         if (this.em != null) {
118             this.em.close();
119             this.em = null;
120         }
121         this.shutdown = true;
122     }
123
124     protected String getAttribute(PIPFinder pipFinder, PIPRequest pipRequest) {
125         //
126         // Get the actor value
127         //
128         var pipResponse = this.getAttribute(pipRequest, pipFinder);
129         if (pipResponse == null) {
130             logger.error("Need actor attribute which is not found");
131             return null;
132         }
133         //
134         // Find the actor
135         //
136         return findFirstAttributeValue(pipResponse);
137     }
138
139     protected PIPResponse getAttribute(PIPRequest pipRequest, PIPFinder pipFinder) {
140         PIPResponse pipResponse = null;
141         try {
142             pipResponse = pipFinder.getMatchingAttributes(pipRequest, this);
143             if (pipResponse.getStatus() != null && !pipResponse.getStatus().isOk()) {
144                 logger.info("get attribute error retrieving {}: {}", pipRequest.getAttributeId(),
145                                 pipResponse.getStatus());
146                 pipResponse = null;
147             }
148             if (pipResponse != null && pipResponse.getAttributes().isEmpty()) {
149                 logger.info("No value for {}", pipRequest.getAttributeId());
150                 pipResponse = null;
151             }
152         } catch (PIPException ex) {
153             logger.error("PIPException getting subject-id attribute", ex);
154         }
155         return pipResponse;
156     }
157
158     protected String findFirstAttributeValue(PIPResponse pipResponse) {
159         for (Attribute attribute: pipResponse.getAttributes()) {
160             Iterator<AttributeValue<String>> iterAttributeValues    = attribute.findValues(DataTypes.DT_STRING);
161             while (iterAttributeValues.hasNext()) {
162                 String value   = iterAttributeValues.next().getValue();
163                 if (value != null) {
164                     return value;
165                 }
166             }
167         }
168         return null;
169     }
170
171     protected void addIntegerAttribute(StdMutablePIPResponse stdPipResponse, Identifier category,
172             Identifier attributeId, int value, PIPRequest pipRequest) {
173         AttributeValue<BigInteger> attributeValue   = null;
174         try {
175             attributeValue  = makeInteger(value);
176         } catch (Exception e) {
177             logger.error("Failed to convert {} to integer", value, e);
178         }
179         if (attributeValue != null) {
180             stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue,
181                     pipRequest.getIssuer(), false));
182         }
183     }
184
185     protected void addLongAttribute(StdMutablePIPResponse stdPipResponse, Identifier category,
186             Identifier attributeId, long value, PIPRequest pipRequest) {
187         AttributeValue<BigInteger> attributeValue   = null;
188         try {
189             attributeValue  = makeLong(value);
190         } catch (Exception e) {
191             logger.error("Failed to convert {} to long", value, e);
192         }
193         if (attributeValue != null) {
194             stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue,
195                     pipRequest.getIssuer(), false));
196         }
197     }
198
199     protected void addStringAttribute(StdMutablePIPResponse stdPipResponse, Identifier category, Identifier attributeId,
200             String value, PIPRequest pipRequest) {
201         AttributeValue<String> attributeValue = null;
202         try {
203             attributeValue = makeString(value);
204         } catch (Exception ex) {
205             logger.error("Failed to convert {} to an AttributeValue<String>", value, ex);
206         }
207         if (attributeValue != null) {
208             stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue,
209                     pipRequest.getIssuer(), false));
210         }
211     }
212
213     // these may be overridden by junit tests
214
215     protected AttributeValue<BigInteger> makeInteger(int value) throws DataTypeException {
216         return DataTypes.DT_INTEGER.createAttributeValue(value);
217     }
218
219     protected AttributeValue<BigInteger> makeLong(long value) throws DataTypeException {
220         return DataTypes.DT_INTEGER.createAttributeValue(value);
221     }
222
223     protected AttributeValue<String> makeString(String value) throws DataTypeException {
224         return DataTypes.DT_STRING.createAttributeValue(value);
225     }
226
227 }