01b0709b29c0766baba4dcfcef8aac7ee082ebfe
[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 com.google.common.base.Strings;
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.Properties;
32 import javax.persistence.NoResultException;
33 import javax.persistence.Persistence;
34 import org.onap.policy.database.ToscaDictionary;
35 import org.onap.policy.database.std.StdOnapPip;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40 public class GetOperationOutcomePip extends StdOnapPip {
41     public static final String ISSUER_NAME = "get-operation-outcome";
42     private static Logger logger = LoggerFactory.getLogger(GetOperationOutcomePip.class);
43
44     public GetOperationOutcomePip() {
45         super();
46     }
47
48     @Override
49     public Collection<PIPRequest> attributesRequired() {
50         return Arrays.asList(PIP_REQUEST_TARGET);
51     }
52
53     @Override
54     public void configure(String id, Properties properties) throws PIPException {
55         super.configure(id, properties);
56         //
57         // Create our entity manager
58         //
59         em = null;
60         try {
61             //
62             // In case there are any overloaded properties for the JPA
63             //
64             Properties emProperties = new Properties(properties);
65             //
66             // Create the entity manager factory
67             //
68             em = Persistence.createEntityManagerFactory(
69                     properties.getProperty(ISSUER_NAME + ".persistenceunit"),
70                     emProperties).createEntityManager();
71         } catch (Exception e) {
72             logger.error("Persistence failed {} operations history db {}", e.getLocalizedMessage(), e);
73         }
74     }
75
76     /**
77      * getAttributes.
78      *
79      * @param pipRequest the request
80      * @param pipFinder the pip finder
81      * @return PIPResponse
82      */
83     @Override
84     public PIPResponse getAttributes(PIPRequest pipRequest, PIPFinder pipFinder) throws PIPException {
85         logger.debug("getAttributes requesting attribute {} of type {} for issuer {}",
86                 pipRequest.getAttributeId(), pipRequest.getDataTypeId(), pipRequest.getIssuer());
87         //
88         // Determine if the issuer is correct
89         //
90         if (Strings.isNullOrEmpty(pipRequest.getIssuer())) {
91             logger.debug("issuer is null - returning empty response");
92             //
93             // We only respond to ourself as the issuer
94             //
95             return StdPIPResponse.PIP_RESPONSE_EMPTY;
96         }
97         if (! pipRequest.getIssuer().startsWith(ToscaDictionary.GUARD_ISSUER_PREFIX)) {
98             logger.debug("Issuer does not start with guard");
99             //
100             // We only respond to ourself as the issuer
101             //
102             return StdPIPResponse.PIP_RESPONSE_EMPTY;
103         }
104         //
105         // Parse out the issuer which denotes the time window
106         // Eg: any-prefix:clname:some-controlloop-name
107         //
108         String[] s1 = pipRequest.getIssuer().split("clname:");
109         String clname = s1[1];
110         String target = null;
111         target = getTarget(pipFinder);
112
113         logger.debug("Going to query DB about: clname={}, target={}", clname, target);
114         String outcome = doDatabaseQuery(clname, target);
115         logger.debug("Query result is: {}", outcome);
116
117         StdMutablePIPResponse pipResponse = new StdMutablePIPResponse();
118         this.addStringAttribute(pipResponse,
119                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
120                 ToscaDictionary.ID_RESOURCE_GUARD_OPERATIONOUTCOME,
121                 outcome,
122                 pipRequest);
123         return new StdPIPResponse(pipResponse);
124     }
125
126     private String doDatabaseQuery(String clname, String target) {
127         logger.info("Querying operations history for {} {} {} {} {}",
128                     clname, target);
129         //
130         // Do the query
131         //
132         Object result = null;
133         try {
134             //
135             // We are expecting a single result
136             //
137             result = em.createQuery("select e.outcome from Dbao e"
138                                     + " where e.closedLoopName= ?1"
139                                     + " and e.target= ?2"
140                                     + " order by e.endtime desc")
141                 .setParameter(1, clname)
142                 .setParameter(2, target)
143                 .setMaxResults(1)
144                 .getSingleResult();
145         } catch (NoResultException ex) {
146             logger.debug("NoResultException for getSingleResult()", ex);
147         } catch (Exception ex) {
148             logger.error("doDatabaseQuery threw an exception", ex);
149         }
150         //
151         // Check our query results
152         //
153         logger.info("operations query returned {}", result);
154         return (String) result;
155     }
156 }