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
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.database.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 java.util.Arrays;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.HashSet;
34 import java.util.Properties;
36 import org.onap.policy.database.ToscaDictionary;
37 import org.onap.policy.database.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 private static final Set<String> TIME_WINDOW_SCALES = Collections
47 .unmodifiableSet(new HashSet<>(Arrays.asList("minute", "hour", "day", "week", "month", "year")));
49 public CountRecentOperationsPip() {
54 public Collection<PIPRequest> attributesRequired() {
55 return Arrays.asList(PIP_REQUEST_ACTOR, PIP_REQUEST_RECIPE, PIP_REQUEST_TARGET);
59 public void configure(String id, Properties properties) throws PIPException {
60 super.configure(id, properties, ISSUER_NAME);
66 * @param pipRequest the request
67 * @param pipFinder the pip finder
71 public PIPResponse getAttributes(PIPRequest pipRequest, PIPFinder pipFinder) throws PIPException {
72 logger.debug("getAttributes requesting attribute {} of type {} for issuer {}",
73 pipRequest.getAttributeId(), pipRequest.getDataTypeId(), pipRequest.getIssuer());
75 if (isRequestInvalid(pipRequest)) {
76 return StdPIPResponse.PIP_RESPONSE_EMPTY;
80 // Parse out the issuer which denotes the time window
81 // Eg: any-prefix:tw:10:minute
83 String[] s1 = pipRequest.getIssuer().split("tw:");
84 String[] s2 = s1[1].split(":");
85 int timeWindowVal = Integer.parseInt(s2[0]);
86 String timeWindowScale = s2[1];
88 // Grab other attribute values
90 String actor = getActor(pipFinder);
91 String operation = getRecipe(pipFinder);
92 String target = getTarget(pipFinder);
93 String timeWindow = timeWindowVal + " " + timeWindowScale;
94 logger.info("Going to query DB about: actor {} operation {} target {} time window {}",
95 actor, operation, target, timeWindow);
99 if (actor == null || operation == null || target == null) {
101 // See if we have all the values
103 logger.error("missing attributes return empty");
104 return StdPIPResponse.PIP_RESPONSE_EMPTY;
107 // Ok do the database query
109 int operationCount = doDatabaseQuery(actor, operation, target, timeWindowVal, timeWindowScale);
111 // Create and return PipResponse
113 StdMutablePIPResponse pipResponse = new StdMutablePIPResponse();
114 this.addIntegerAttribute(pipResponse,
115 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
116 ToscaDictionary.ID_RESOURCE_GUARD_OPERATIONCOUNT,
119 return new StdPIPResponse(pipResponse);
122 private int doDatabaseQuery(String actor, String operation, String target, int timeWindowVal,
123 String timeWindowScale) {
124 logger.info("Querying operations history for {} {} {} {} {}",
125 actor, operation, target, timeWindowVal, timeWindowScale);
127 logger.error("No EntityManager available");
131 // Compute the time window
133 if (! TIME_WINDOW_SCALES.contains(timeWindowScale.toLowerCase())) {
137 logger.error("Unsupported time window scale value {}", timeWindowScale);
139 // Throw an exception instead?
146 Object result = null;
149 // Set up query --- operationshistory is magic, should fix sometime
151 String strQuery = "select count(*) as numops from operationshistory"
152 + " where outcome<>'Failure_Guard'"
156 + " and endtime between"
157 + " TIMESTAMPADD(?, ?, CURRENT_TIMESTAMP)"
158 + " and CURRENT_TIMESTAMP";
160 // We are expecting a single result
162 result = em.createNativeQuery(strQuery)
163 .setParameter(1, actor)
164 .setParameter(2, operation)
165 .setParameter(3, target)
166 .setParameter(4, timeWindowScale)
167 .setParameter(5, timeWindowVal * -1)
169 } catch (RuntimeException e) {
170 logger.error("Named query failed ", e);
173 // Check our query results
175 if (result != null) {
177 // Success let's see what JPA returned to us
179 logger.info("operations query returned {}", result);
181 // Should get back a number
183 if (result instanceof Number) {
184 return ((Number) result).intValue();
187 // We shouldn't really get this result, but just
188 // in case we'll do the dirty work of parsing the
189 // string representation of the object.
191 return Integer.parseInt(result.toString());
194 // We get here if we didn't get a result. Should
195 // we propagate back an exception?