2  * ============LICENSE_START=======================================================
 
   3  * Copyright (C) 2019-2020 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
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  19 package org.onap.policy.pdp.xacml.application.common.operationshistory;
 
  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;
 
  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);
 
  42     public GetOperationOutcomePip() {
 
  44         this.issuer = ISSUER_NAME;
 
  48     public Collection<PIPRequest> attributesRequired() {
 
  49         return Arrays.asList(PIP_REQUEST_TARGET);
 
  55      * @param pipRequest the request
 
  56      * @param pipFinder the pip finder
 
  60     public PIPResponse getAttributes(PIPRequest pipRequest, PIPFinder pipFinder) throws PIPException {
 
  62             throw new PIPException("Engine is shutdown");
 
  64         logger.debug("getAttributes requesting attribute {} of type {} for issuer {}",
 
  65                 pipRequest.getAttributeId(), pipRequest.getDataTypeId(), pipRequest.getIssuer());
 
  67         // Determine if the issuer is correct
 
  69         if (Strings.isNullOrEmpty(pipRequest.getIssuer())) {
 
  70             logger.error("issuer is null - returning empty response");
 
  72             // We only respond to ourself as the issuer
 
  74             return StdPIPResponse.PIP_RESPONSE_EMPTY;
 
  76         if (! pipRequest.getIssuer().startsWith(ToscaDictionary.GUARD_ISSUER_PREFIX)) {
 
  77             logger.error("Issuer does not start with guard");
 
  79             // We only respond to ourself as the issuer
 
  81             return StdPIPResponse.PIP_RESPONSE_EMPTY;
 
  84         // Parse out the issuer which denotes the time window
 
  85         // Eg: any-prefix:clname:some-controlloop-name
 
  87         String[] s1 = pipRequest.getIssuer().split("clname:");
 
  88         String clname = s1[1];
 
  90         target = getAttribute(pipFinder, PIP_REQUEST_TARGET);
 
  92         logger.debug("Going to query DB about: clname={}, target={}", clname, target);
 
  93         String outcome = doDatabaseQuery(clname, target);
 
  94         logger.debug("Query result is: {}", outcome);
 
  96         StdMutablePIPResponse pipResponse = new StdMutablePIPResponse();
 
  97         this.addStringAttribute(pipResponse,
 
  98                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
 
  99                 ToscaDictionary.ID_RESOURCE_GUARD_OPERATIONOUTCOME,
 
 102         return new StdPIPResponse(pipResponse);
 
 105     private String doDatabaseQuery(String clname, String target) {
 
 106         logger.info("Querying operations history for {} {}", clname, target);
 
 108         // Only can query if we have an EntityManager
 
 111             logger.error("No EntityManager available");
 
 119             // We are expecting a single result
 
 121             return em.createQuery("select e.outcome from Dbao e"
 
 122                                   + " where e.closedLoopName= ?1"
 
 123                                   + " and e.target= ?2"
 
 124                                   + " order by e.endtime desc",
 
 126                 .setParameter(1, clname)
 
 127                 .setParameter(2, target)
 
 130         } catch (NoResultException e) {
 
 131             logger.trace("No results", e);
 
 132         } catch (Exception e) {
 
 133             logger.error("Typed query failed", e);