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