2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2023 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============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;
30 import jakarta.persistence.NoResultException;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
34 import org.onap.policy.pdp.xacml.application.common.std.StdOnapPip;
35 import org.slf4j.Logger;
36 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() {
44 this.issuer = ISSUER_NAME;
48 public Collection<PIPRequest> attributesRequired() {
49 return Arrays.asList(PIP_REQUEST_TARGET);
55 * @param pipRequest the request
56 * @param pipFinder the pip finder
60 public PIPResponse getAttributes(PIPRequest pipRequest, PIPFinder pipFinder)
63 throw new PIPException("Engine is shutdown");
65 logger.debug("getAttributes requesting attribute {} of type {} for issuer {}",
66 pipRequest.getAttributeId(), pipRequest.getDataTypeId(), pipRequest.getIssuer());
68 // Determine if the issuer is correct
70 if (Strings.isNullOrEmpty(pipRequest.getIssuer())) {
71 logger.error("issuer is null - returning empty response");
73 // We only respond to ourself as the issuer
75 return StdPIPResponse.PIP_RESPONSE_EMPTY;
77 if (!pipRequest.getIssuer().startsWith(ToscaDictionary.GUARD_ISSUER_PREFIX)) {
78 logger.error("Issuer does not start with guard");
80 // We only respond to ourself as the issuer
82 return StdPIPResponse.PIP_RESPONSE_EMPTY;
85 // Parse out the issuer which denotes the time window
86 // Eg: any-prefix:clname:some-controlloop-name
88 String[] s1 = pipRequest.getIssuer().split("clname:");
89 String clname = s1[1];
91 target = getAttribute(pipFinder, PIP_REQUEST_TARGET);
93 logger.debug("Going to query DB about: clname={}, target={}", clname, target);
94 String outcome = doDatabaseQuery(clname);
95 logger.info("Query result is: {}", outcome);
97 var pipResponse = new StdMutablePIPResponse();
98 this.addStringAttribute(pipResponse, XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
99 ToscaDictionary.ID_RESOURCE_GUARD_OPERATIONOUTCOME, outcome, pipRequest);
100 return new StdPIPResponse(pipResponse);
103 private String doDatabaseQuery(String clname) {
104 logger.info("Querying operations history for {}", clname);
106 // Only can query if we have an EntityManager
109 logger.error("No EntityManager available");
117 // We are expecting a single result
120 .createQuery("select e.outcome from OperationsHistory e" + " where e.closedLoopName= ?1"
121 + " order by e.starttime desc", String.class)
122 .setParameter(1, clname).setMaxResults(1).getSingleResult();
124 // Check the value of result
125 if (result.equalsIgnoreCase("Started")) {
126 return ("In_Progress");
130 } catch (NoResultException e) {
131 logger.trace("No results", e);
132 } catch (Exception e) {
133 logger.error("Typed query failed", e);