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 java.util.Arrays;
29 import java.util.Collection;
30 import java.util.Properties;
31 import javax.persistence.NoResultException;
32 import org.onap.policy.database.ToscaDictionary;
33 import org.onap.policy.database.std.StdOnapPip;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 public class GetOperationOutcomePip extends StdOnapPip {
39 public static final String ISSUER_NAME = "get-operation-outcome";
40 private static Logger logger = LoggerFactory.getLogger(GetOperationOutcomePip.class);
42 public GetOperationOutcomePip() {
47 public Collection<PIPRequest> attributesRequired() {
48 return Arrays.asList(PIP_REQUEST_TARGET);
52 public void configure(String id, Properties properties) throws PIPException {
53 super.configure(id, properties, ISSUER_NAME);
59 * @param pipRequest the request
60 * @param pipFinder the pip finder
64 public PIPResponse getAttributes(PIPRequest pipRequest, PIPFinder pipFinder) throws PIPException {
65 logger.debug("getAttributes requesting attribute {} of type {} for issuer {}",
66 pipRequest.getAttributeId(), pipRequest.getDataTypeId(), pipRequest.getIssuer());
68 if (isRequestInvalid(pipRequest)) {
69 return StdPIPResponse.PIP_RESPONSE_EMPTY;
73 // Parse out the issuer which denotes the time window
74 // Eg: any-prefix:clname:some-controlloop-name
76 String[] s1 = pipRequest.getIssuer().split("clname:");
77 String clname = s1[1];
78 String target = getTarget(pipFinder);
84 // See if we have all the values
86 logger.error("missing attributes return empty");
87 return StdPIPResponse.PIP_RESPONSE_EMPTY;
90 logger.debug("Going to query DB about: clname={}, target={}", clname, target);
91 String outcome = doDatabaseQuery(clname, target);
92 logger.debug("Query result is: {}", outcome);
94 StdMutablePIPResponse pipResponse = new StdMutablePIPResponse();
95 this.addStringAttribute(pipResponse,
96 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
97 ToscaDictionary.ID_RESOURCE_GUARD_OPERATIONOUTCOME,
100 return new StdPIPResponse(pipResponse);
103 private String doDatabaseQuery(String clname, String target) {
104 logger.info("Querying operations history for {} {}",
109 Object result = null;
112 // We are expecting a single result
114 result = em.createQuery("select e.outcome from Dbao e"
115 + " where e.closedLoopName= ?1"
116 + " and e.target= ?2"
117 + " order by e.endtime desc")
118 .setParameter(1, clname)
119 .setParameter(2, target)
122 } catch (NoResultException ex) {
123 logger.debug("NoResultException for getSingleResult()", ex);
124 } catch (Exception ex) {
125 logger.error("doDatabaseQuery threw an exception", ex);
128 // Check our query results
130 logger.info("operations query returned {}", result);
131 return (String) result;