2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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.
16 * ============LICENSE_END=========================================================
20 package org.onap.policy.pdp.xacml.application.common.operationshistory;
22 import com.att.research.xacml.api.XACML3;
23 import com.att.research.xacml.api.pip.PIPException;
24 import com.att.research.xacml.api.pip.PIPFinder;
25 import com.att.research.xacml.api.pip.PIPRequest;
26 import com.att.research.xacml.api.pip.PIPResponse;
27 import com.att.research.xacml.std.pip.StdMutablePIPResponse;
28 import com.att.research.xacml.std.pip.StdPIPResponse;
29 import com.google.common.base.Strings;
31 import java.util.Arrays;
32 import java.util.Base64;
33 import java.util.Collection;
34 import java.util.Properties;
35 import javax.persistence.NoResultException;
36 import javax.persistence.Persistence;
38 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
39 import org.onap.policy.pdp.xacml.application.common.std.StdOnapPip;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 public class GetOperationOutcomePip extends StdOnapPip {
45 public static final String ISSUER_NAME = "get-operation-outcome";
46 private static Logger logger = LoggerFactory.getLogger(GetOperationOutcomePip.class);
48 public GetOperationOutcomePip() {
53 public Collection<PIPRequest> attributesRequired() {
54 return Arrays.asList(PIP_REQUEST_TARGET);
58 public void configure(String id, Properties properties) throws PIPException {
59 super.configure(id, properties);
61 // Create our entity manager
66 // In case there are any overloaded properties for the JPA
68 Properties emProperties = new Properties();
69 emProperties.putAll(properties);
72 // Need to decode the password before creating the EntityManager
74 String decodedPassword = new String(Base64.getDecoder()
75 .decode(emProperties.getProperty("javax.persistence.jdbc.password")));
76 emProperties.setProperty("javax.persistence.jdbc.password", decodedPassword);
79 // Create the entity manager factory
81 em = Persistence.createEntityManagerFactory(
82 properties.getProperty(ISSUER_NAME + ".persistenceunit"),
83 emProperties).createEntityManager();
84 } catch (Exception e) {
85 logger.error("Persistence failed {} operations history db {}", e.getLocalizedMessage(), e);
92 * @param pipRequest the request
93 * @param pipFinder the pip finder
97 public PIPResponse getAttributes(PIPRequest pipRequest, PIPFinder pipFinder) throws PIPException {
98 logger.debug("getAttributes requesting attribute {} of type {} for issuer {}",
99 pipRequest.getAttributeId(), pipRequest.getDataTypeId(), pipRequest.getIssuer());
101 // Determine if the issuer is correct
103 if (Strings.isNullOrEmpty(pipRequest.getIssuer())) {
104 logger.error("issuer is null - returning empty response");
106 // We only respond to ourself as the issuer
108 return StdPIPResponse.PIP_RESPONSE_EMPTY;
110 if (! pipRequest.getIssuer().startsWith(ToscaDictionary.GUARD_ISSUER_PREFIX)) {
111 logger.error("Issuer does not start with guard");
113 // We only respond to ourself as the issuer
115 return StdPIPResponse.PIP_RESPONSE_EMPTY;
118 // Parse out the issuer which denotes the time window
119 // Eg: any-prefix:clname:some-controlloop-name
121 String[] s1 = pipRequest.getIssuer().split("clname:");
122 String clname = s1[1];
123 String target = null;
124 target = getAttribute(pipFinder, PIP_REQUEST_TARGET);
126 logger.debug("Going to query DB about: clname={}, target={}", clname, target);
127 String outcome = doDatabaseQuery(clname, target);
128 logger.debug("Query result is: {}", outcome);
130 StdMutablePIPResponse pipResponse = new StdMutablePIPResponse();
131 this.addStringAttribute(pipResponse,
132 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
133 ToscaDictionary.ID_RESOURCE_GUARD_OPERATIONOUTCOME,
136 return new StdPIPResponse(pipResponse);
139 private String doDatabaseQuery(String clname, String target) {
140 logger.info("Querying operations history for {} {}", clname, target);
142 // Only can query if we have an EntityManager
145 logger.error("No EntityManager available");
153 // We are expecting a single result
155 return em.createQuery("select e.outcome from Dbao e"
156 + " where e.closedLoopName= ?1"
157 + " and e.target= ?2"
158 + " order by e.endtime desc",
160 .setParameter(1, clname)
161 .setParameter(2, target)
164 } catch (NoResultException e) {
165 logger.trace("No results", e);
166 } catch (Exception e) {
167 logger.error("Typed query failed", e);