5a0db05018585d93d7a5efc81e9b3b1a431e086f
[policy/drools-applications.git] /
1 /*-
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
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.database.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 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;
36
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     }
45
46     @Override
47     public Collection<PIPRequest> attributesRequired() {
48         return Arrays.asList(PIP_REQUEST_TARGET);
49     }
50
51     @Override
52     public void configure(String id, Properties properties) throws PIPException {
53         super.configure(id, properties, ISSUER_NAME);
54     }
55
56     /**
57      * getAttributes.
58      *
59      * @param pipRequest the request
60      * @param pipFinder the pip finder
61      * @return PIPResponse
62      */
63     @Override
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());
67
68         if (isRequestInvalid(pipRequest)) {
69             return StdPIPResponse.PIP_RESPONSE_EMPTY;
70         }
71
72         //
73         // Parse out the issuer which denotes the time window
74         // Eg: any-prefix:clname:some-controlloop-name
75         //
76         String[] s1 = pipRequest.getIssuer().split("clname:");
77         String clname = s1[1];
78         String target = getTarget(pipFinder);
79         //
80         // Sanity check
81         //
82         if (target == null) {
83             //
84             // See if we have all the values
85             //
86             logger.error("missing attributes return empty");
87             return StdPIPResponse.PIP_RESPONSE_EMPTY;
88         }
89
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);
93
94         StdMutablePIPResponse pipResponse = new StdMutablePIPResponse();
95         this.addStringAttribute(pipResponse,
96                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
97                 ToscaDictionary.ID_RESOURCE_GUARD_OPERATIONOUTCOME,
98                 outcome,
99                 pipRequest);
100         return new StdPIPResponse(pipResponse);
101     }
102
103     private String doDatabaseQuery(String clname, String target) {
104         logger.info("Querying operations history for {} {}",
105                     clname, target);
106         //
107         // Do the query
108         //
109         Object result = null;
110         try {
111             //
112             // We are expecting a single result
113             //
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)
120                 .setMaxResults(1)
121                 .getSingleResult();
122         } catch (NoResultException ex) {
123             logger.debug("NoResultException for getSingleResult()", ex);
124         } catch (Exception ex) {
125             logger.error("doDatabaseQuery threw an exception", ex);
126         }
127         //
128         // Check our query results
129         //
130         logger.info("operations query returned {}", result);
131         return (String) result;
132     }
133 }