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.
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.Collection;
33 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(properties);
70 // Create the entity manager factory
72 em = Persistence.createEntityManagerFactory(
73 properties.getProperty(ISSUER_NAME + ".persistenceunit"),
74 emProperties).createEntityManager();
75 } catch (Exception e) {
76 logger.error("Persistence failed {} operations history db {}", e.getLocalizedMessage(), e);
83 * @param pipRequest the request
84 * @param pipFinder the pip finder
88 public PIPResponse getAttributes(PIPRequest pipRequest, PIPFinder pipFinder) throws PIPException {
89 logger.debug("getAttributes requesting attribute {} of type {} for issuer {}",
90 pipRequest.getAttributeId(), pipRequest.getDataTypeId(), pipRequest.getIssuer());
92 // Determine if the issuer is correct
94 if (Strings.isNullOrEmpty(pipRequest.getIssuer())) {
95 logger.debug("issuer is null - returning empty response");
97 // We only respond to ourself as the issuer
99 return StdPIPResponse.PIP_RESPONSE_EMPTY;
101 if (! pipRequest.getIssuer().startsWith(ToscaDictionary.GUARD_ISSUER_PREFIX)) {
102 logger.debug("Issuer does not start with guard");
104 // We only respond to ourself as the issuer
106 return StdPIPResponse.PIP_RESPONSE_EMPTY;
109 // Parse out the issuer which denotes the time window
110 // Eg: any-prefix:clname:some-controlloop-name
112 String[] s1 = pipRequest.getIssuer().split("clname:");
113 String clname = s1[1];
114 String target = null;
115 target = getAttribute(pipFinder, PIP_REQUEST_TARGET);
117 logger.debug("Going to query DB about: clname={}, target={}", clname, target);
118 String outcome = doDatabaseQuery(clname, target);
119 logger.debug("Query result is: {}", outcome);
121 StdMutablePIPResponse pipResponse = new StdMutablePIPResponse();
122 this.addStringAttribute(pipResponse,
123 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
124 ToscaDictionary.ID_RESOURCE_GUARD_OPERATIONOUTCOME,
127 return new StdPIPResponse(pipResponse);
130 private String doDatabaseQuery(String clname, String target) {
131 logger.info("Querying operations history for {} {}", clname, target);
133 // Only can query if we have an EntityManager
136 logger.error("No EntityManager available");
144 // We are expecting a single result
146 return em.createQuery("select e.outcome from Dbao e"
147 + " where e.closedLoopName= ?1"
148 + " and e.target= ?2"
149 + " order by e.endtime desc",
151 .setParameter(1, clname)
152 .setParameter(2, target)
155 } catch (Exception e) {
156 logger.error("Typed query failed ", e);