2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018-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=========================================================
19 package org.onap.policy.database.operationshistory;
21 import com.att.research.xacml.api.XACML3;
22 import com.att.research.xacml.api.pip.PIPException;
23 import com.att.research.xacml.api.pip.PIPFinder;
24 import com.att.research.xacml.api.pip.PIPRequest;
25 import com.att.research.xacml.api.pip.PIPResponse;
26 import com.att.research.xacml.std.pip.StdMutablePIPResponse;
27 import com.att.research.xacml.std.pip.StdPIPResponse;
28 import com.google.common.base.Strings;
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.Properties;
32 import javax.persistence.NoResultException;
33 import javax.persistence.Persistence;
34 import org.onap.policy.database.ToscaDictionary;
35 import org.onap.policy.database.std.StdOnapPip;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 public class GetOperationOutcomePip extends StdOnapPip {
41 public static final String ISSUER_NAME = "get-operation-outcome";
42 private static Logger logger = LoggerFactory.getLogger(GetOperationOutcomePip.class);
44 public GetOperationOutcomePip() {
49 public Collection<PIPRequest> attributesRequired() {
50 return Arrays.asList(PIP_REQUEST_TARGET);
54 public void configure(String id, Properties properties) throws PIPException {
55 super.configure(id, properties);
57 // Create our entity manager
62 // In case there are any overloaded properties for the JPA
64 Properties emProperties = new Properties(properties);
66 // Create the entity manager factory
68 em = Persistence.createEntityManagerFactory(
69 properties.getProperty(ISSUER_NAME + ".persistenceunit"),
70 emProperties).createEntityManager();
71 } catch (Exception e) {
72 logger.error("Persistence failed {} operations history db {}", e.getLocalizedMessage(), e);
79 * @param pipRequest the request
80 * @param pipFinder the pip finder
84 public PIPResponse getAttributes(PIPRequest pipRequest, PIPFinder pipFinder) throws PIPException {
85 logger.debug("getAttributes requesting attribute {} of type {} for issuer {}",
86 pipRequest.getAttributeId(), pipRequest.getDataTypeId(), pipRequest.getIssuer());
88 // Determine if the issuer is correct
90 if (Strings.isNullOrEmpty(pipRequest.getIssuer())) {
91 logger.debug("issuer is null - returning empty response");
93 // We only respond to ourself as the issuer
95 return StdPIPResponse.PIP_RESPONSE_EMPTY;
97 if (! pipRequest.getIssuer().startsWith(ToscaDictionary.GUARD_ISSUER_PREFIX)) {
98 logger.debug("Issuer does not start with guard");
100 // We only respond to ourself as the issuer
102 return StdPIPResponse.PIP_RESPONSE_EMPTY;
105 // Parse out the issuer which denotes the time window
106 // Eg: any-prefix:clname:some-controlloop-name
108 String[] s1 = pipRequest.getIssuer().split("clname:");
109 String clname = s1[1];
110 String target = null;
111 target = getTarget(pipFinder);
113 logger.debug("Going to query DB about: clname={}, target={}", clname, target);
114 String outcome = doDatabaseQuery(clname, target);
115 logger.debug("Query result is: {}", outcome);
117 StdMutablePIPResponse pipResponse = new StdMutablePIPResponse();
118 this.addStringAttribute(pipResponse,
119 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
120 ToscaDictionary.ID_RESOURCE_GUARD_OPERATIONOUTCOME,
123 return new StdPIPResponse(pipResponse);
126 private String doDatabaseQuery(String clname, String target) {
127 logger.info("Querying operations history for {} {} {} {} {}",
132 Object result = null;
135 // We are expecting a single result
137 result = em.createQuery("select e.outcome from Dbao e"
138 + " where e.closedLoopName= ?1"
139 + " and e.target= ?2"
140 + " order by e.endtime desc")
141 .setParameter(1, clname)
142 .setParameter(2, target)
145 } catch (NoResultException ex) {
146 logger.debug("NoResultException for getSingleResult()", ex);
147 } catch (Exception ex) {
148 logger.error("doDatabaseQuery threw an exception", ex);
151 // Check our query results
153 logger.info("operations query returned {}", result);
154 return (String) result;