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