Upgrade Java 17 in xacml-pdp
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / operationshistory / GetOperationOutcomePip.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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=========================================================
18  */
19
20 package org.onap.policy.pdp.xacml.application.common.operationshistory;
21
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;
37
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);
41
42     public GetOperationOutcomePip() {
43         super();
44         this.issuer = ISSUER_NAME;
45     }
46
47     @Override
48     public Collection<PIPRequest> attributesRequired() {
49         return Arrays.asList(PIP_REQUEST_TARGET);
50     }
51
52     /**
53      * getAttributes.
54      *
55      * @param pipRequest the request
56      * @param pipFinder the pip finder
57      * @return PIPResponse
58      */
59     @Override
60     public PIPResponse getAttributes(PIPRequest pipRequest, PIPFinder pipFinder)
61         throws PIPException {
62         if (this.shutdown) {
63             throw new PIPException("Engine is shutdown");
64         }
65         logger.debug("getAttributes requesting attribute {} of type {} for issuer {}",
66             pipRequest.getAttributeId(), pipRequest.getDataTypeId(), pipRequest.getIssuer());
67         //
68         // Determine if the issuer is correct
69         //
70         if (Strings.isNullOrEmpty(pipRequest.getIssuer())) {
71             logger.error("issuer is null - returning empty response");
72             //
73             // We only respond to ourself as the issuer
74             //
75             return StdPIPResponse.PIP_RESPONSE_EMPTY;
76         }
77         if (!pipRequest.getIssuer().startsWith(ToscaDictionary.GUARD_ISSUER_PREFIX)) {
78             logger.error("Issuer does not start with guard");
79             //
80             // We only respond to ourself as the issuer
81             //
82             return StdPIPResponse.PIP_RESPONSE_EMPTY;
83         }
84         //
85         // Parse out the issuer which denotes the time window
86         // Eg: any-prefix:clname:some-controlloop-name
87         //
88         String[] s1 = pipRequest.getIssuer().split("clname:");
89         String clname = s1[1];
90         String target = null;
91         target = getAttribute(pipFinder, PIP_REQUEST_TARGET);
92
93         logger.debug("Going to query DB about: clname={}, target={}", clname, target);
94         String outcome = doDatabaseQuery(clname);
95         logger.info("Query result is: {}", outcome);
96
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);
101     }
102
103     private String doDatabaseQuery(String clname) {
104         logger.info("Querying operations history for {}", clname);
105         //
106         // Only can query if we have an EntityManager
107         //
108         if (em == null) {
109             logger.error("No EntityManager available");
110             return null;
111         }
112         //
113         // Do the query
114         //
115         try {
116             //
117             // We are expecting a single result
118             //
119             String result = em
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();
123
124             // Check the value of result
125             if (result.equalsIgnoreCase("Started")) {
126                 return ("In_Progress");
127             } else {
128                 return ("Complete");
129             }
130         } catch (NoResultException e) {
131             logger.trace("No results", e);
132         } catch (Exception e) {
133             logger.error("Typed query failed", e);
134         }
135         return null;
136     }
137 }