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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdp.xacml.application.common.operationshistory;
23 import com.att.research.xacml.api.XACML3;
24 import com.att.research.xacml.api.pip.PIPException;
25 import com.att.research.xacml.api.pip.PIPFinder;
26 import com.att.research.xacml.api.pip.PIPRequest;
27 import com.att.research.xacml.api.pip.PIPResponse;
28 import com.att.research.xacml.std.pip.StdMutablePIPResponse;
29 import com.att.research.xacml.std.pip.StdPIPResponse;
30 import com.google.common.base.Strings;
31 import java.sql.Timestamp;
32 import java.time.Instant;
33 import java.time.temporal.ChronoUnit;
34 import java.util.Arrays;
35 import java.util.Collection;
36 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
37 import org.onap.policy.pdp.xacml.application.common.std.StdOnapPip;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 public class CountRecentOperationsPip extends StdOnapPip {
43 public static final String ISSUER_NAME = "count-recent-operations";
44 private static Logger logger = LoggerFactory.getLogger(CountRecentOperationsPip.class);
46 public CountRecentOperationsPip() {
48 this.issuer = ISSUER_NAME;
52 public Collection<PIPRequest> attributesRequired() {
53 return Arrays.asList(PIP_REQUEST_ACTOR, PIP_REQUEST_RECIPE, PIP_REQUEST_TARGET);
59 * @param pipRequest the request
60 * @param pipFinder the pip finder
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());
68 // Determine if the issuer is correct
70 if (Strings.isNullOrEmpty(pipRequest.getIssuer())) {
71 logger.debug("issuer is null - returning empty response");
73 // We only respond to ourself as the issuer
75 return StdPIPResponse.PIP_RESPONSE_EMPTY;
77 if (! pipRequest.getIssuer().startsWith(ToscaDictionary.GUARD_ISSUER_PREFIX)) {
78 logger.debug("Issuer does not start with guard");
80 // We only respond to ourself as the issuer
82 return StdPIPResponse.PIP_RESPONSE_EMPTY;
85 // Parse out the issuer which denotes the time window
86 // Eg: any-prefix:tw:10:minute
88 String[] s1 = pipRequest.getIssuer().split("tw:");
89 String[] s2 = s1[1].split(":");
90 int timeWindowVal = Integer.parseInt(s2[0]);
91 String timeWindowScale = s2[1];
93 // Grab other attribute values
95 String actor = getAttribute(pipFinder, PIP_REQUEST_ACTOR);
96 String operation = getAttribute(pipFinder, PIP_REQUEST_RECIPE);
97 String target = getAttribute(pipFinder, PIP_REQUEST_TARGET);
98 String timeWindow = timeWindowVal + " " + timeWindowScale;
99 logger.info("Going to query DB about: actor {} operation {} target {} time window {}",
100 actor, operation, target, timeWindow);
104 if (actor == null || operation == null || target == null) {
106 // See if we have all the values
108 logger.error("missing attributes return empty");
109 return StdPIPResponse.PIP_RESPONSE_EMPTY;
112 // Ok do the database query
114 long operationCount = doDatabaseQuery(actor, operation, target, timeWindowVal, timeWindowScale);
116 // Create and return PipResponse
118 StdMutablePIPResponse pipResponse = new StdMutablePIPResponse();
119 this.addLongAttribute(pipResponse,
120 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
121 ToscaDictionary.ID_RESOURCE_GUARD_OPERATIONCOUNT,
124 return new StdPIPResponse(pipResponse);
127 private long doDatabaseQuery(String actor, String operation, String target, int timeWindowVal,
128 String timeWindowScale) {
129 logger.info("Querying operations history for {} {} {} {} {}",
130 actor, operation, target, timeWindowVal, timeWindowScale);
132 // Only can query if we have an EntityManager
135 logger.error("No EntityManager available");
143 // We are expecting a single result
145 return em.createQuery("select count(e) from Dbao e"
146 + " where e.outcome<>'Failure_Guard'"
148 + " and e.operation= ?2"
149 + " and e.target= ?3"
150 + " and e.endtime between"
151 + " ?4 and CURRENT_TIMESTAMP",
153 .setParameter(1, actor)
154 .setParameter(2, operation)
155 .setParameter(3, target)
156 .setParameter(4, Timestamp.from(Instant.now()
157 .minus(timeWindowVal,
158 stringToChronoUnit(timeWindowScale))))
160 } catch (Exception e) {
161 logger.error("Typed query failed ", e);
166 private ChronoUnit stringToChronoUnit(String scale) {
168 // Compute the time window
170 switch (scale.toLowerCase()) {
172 return ChronoUnit.SECONDS;
174 return ChronoUnit.MINUTES;
176 return ChronoUnit.HOURS;
178 return ChronoUnit.DAYS;
180 return ChronoUnit.WEEKS;
182 return ChronoUnit.MONTHS;
184 return ChronoUnit.YEARS;
189 logger.error("Unsupported time window scale value {}", scale);