2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.policy.pdp.test.conformance;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
26 import org.openecomp.policy.common.logging.flexlogger.Logger;
28 import com.att.research.xacml.api.Request;
29 import com.att.research.xacml.api.Response;
30 import com.att.research.xacml.api.pdp.PDPEngine;
31 import com.att.research.xacml.api.pdp.PDPEngineFactory;
32 import com.att.research.xacml.api.pdp.ScopeResolver;
33 import com.att.research.xacml.std.dom.DOMProperties;
34 import com.att.research.xacml.std.dom.DOMRequest;
35 import com.att.research.xacml.std.dom.DOMResponse;
36 import com.att.research.xacml.util.FactoryException;
39 * ConformanceTestEngine handles the creation of the PDPEngine for a ConformanceTest instance.
41 * @version $Revision: 1.2 $
43 public class ConformanceTestEngine {
44 private Logger logger = FlexLogger.getLogger(ConformanceTestEngine.class);
46 private PDPEngineFactory pdpEngineFactory;
47 private ScopeResolver scopeResolver;
48 private boolean lenientRequests;
49 private boolean lenientPolicies;
50 private int iterations = 1;
52 // total of all first calls to decide()
53 private long firstDecideTime;
54 private int numberOfFirstDecides = 0;
56 // total of all non-first-calls to decide()
57 private long decideTimeMultiple;
59 // total of average time each test case uses for a Request
60 // (sum of : for each test case, average of all non-first-call calls to decide() )
61 private long avgDecideTimeMultiple = 0;
63 protected PDPEngineFactory getPDPEngineFactory() throws FactoryException {
64 if (this.pdpEngineFactory == null) {
65 this.pdpEngineFactory = PDPEngineFactory.newInstance();
66 this.pdpEngineFactory.setScopeResolver(this.scopeResolver);
68 return this.pdpEngineFactory;
71 public ConformanceTestEngine(ScopeResolver scopeResolverIn, boolean lenientRequestsIn, boolean lenientPoliciesIn, int iterationsIn) {
72 this.scopeResolver = scopeResolverIn;
73 this.lenientRequests = lenientRequestsIn;
74 this.lenientPolicies = lenientPoliciesIn;
75 this.iterations = iterationsIn;
78 public ConformanceTestResult run(ConformanceTest conformanceTest) {
79 if (conformanceTest.getRequest() == null || conformanceTest.getResponse() == null || conformanceTest.getRepository() == null) {
80 logger.error("Incomplete Conformance Test: " + conformanceTest.getTestName());
82 PDPEngineFactory thisPDPEngineFactory = null;
84 thisPDPEngineFactory = this.getPDPEngineFactory();
85 } catch (FactoryException ex) {
86 return new ConformanceTestResult(conformanceTest, ex);
89 ConformanceTestResult conformanceTestResult = new ConformanceTestResult(conformanceTest, iterations);
94 Request request = null;
95 boolean isLenient = DOMProperties.isLenient();
97 DOMProperties.setLenient(this.lenientRequests);
99 request = DOMRequest.load(conformanceTest.getRequest());
100 conformanceTestResult.setRequest(request);
101 } catch (Exception ex) {
102 logger.error("Exception loading Request file " + conformanceTest.getRequest().getAbsolutePath(), ex);
103 conformanceTestResult.setError(ex);
104 return conformanceTestResult;
109 * Load the expected response
111 Response response = null;
113 response = DOMResponse.load(conformanceTest.getResponse());
114 conformanceTestResult.setExpectedResponse(response);
115 } catch (Exception ex) {
116 logger.error("Exception loading Response file " + conformanceTest.getResponse().getAbsolutePath(), ex);
117 conformanceTestResult.setError(ex);
118 return conformanceTestResult;
122 * Set up the configuration for the policy finder
124 conformanceTest.getRepository().setXACMLProperties();
125 DOMProperties.setLenient(this.lenientPolicies);
130 PDPEngine pdpEngine = null;
132 // pdpEngine = thisPDPEngineFactory.newEngine(conformanceTest.getRootPolicy(), conformanceTest.getReferencedPolicies(), pipFinderEngine);
133 pdpEngine = thisPDPEngineFactory.newEngine();
134 } catch (Exception ex) {
135 logger.error("Exception getting PDP engine instance", ex);
136 conformanceTestResult.setError(ex);
137 return conformanceTestResult;
139 if (pdpEngine == null) {
140 logger.error("Null PDP engine");
141 conformanceTestResult.setError(new NullPointerException("Null engine"));
142 return conformanceTestResult;
148 long startTime, endTime;
149 long curDecideTime = this.firstDecideTime;
151 startTime = System.nanoTime();
152 response = pdpEngine.decide(request);
153 endTime = System.nanoTime();
154 //System.out.println(endTime - startTime);
156 this.firstDecideTime += endTime - startTime;
157 this.numberOfFirstDecides++;
158 // remember just this test
159 conformanceTestResult.setFirstCallTime(endTime - startTime);
160 conformanceTestResult.setActualResponse(response);
161 } catch (Exception ex) {
162 logger.error("Exception in decide", ex);
163 conformanceTestResult.setError(ex);
164 return conformanceTestResult;
166 if (response == null) {
167 logger.error("Null Response");
168 conformanceTestResult.setError(new NullPointerException("Null Response"));
169 return conformanceTestResult;
172 long localLoopTime = 0;
174 // if user requested non-first-call calls to decide() to get performance info, run them now.
175 // We can ignore the result since we are only interested in how long they take to process the Request.
176 for (int i = 0 ; i < this.iterations ; i++) {
177 startTime = System.nanoTime();
178 pdpEngine.decide(request);
179 endTime = System.nanoTime();
180 //System.out.println(endTime - startTime);
181 // add to the global total for all tests
182 this.decideTimeMultiple += (endTime - startTime);
183 // remember just this one test's info
184 localLoopTime += (endTime - startTime);
186 } catch (Exception ex) {
187 logger.error("Exception in iterated decide", ex);
188 return conformanceTestResult;
191 // add to total average for non-first-call times for all test cases
192 avgDecideTimeMultiple += (localLoopTime / iterations);
193 //System.out.println("localLoop="+localLoopTime + " it="+iterations + " avg=" + (localLoopTime / iterations) );
194 // remember average time for just this test
195 conformanceTestResult.setAverageTotalLoopTime(localLoopTime/iterations);
197 long elapsedDecideTime = this.firstDecideTime - curDecideTime;
198 logger.info("Decide Time: " + elapsedDecideTime + "ns");
200 return conformanceTestResult;
202 DOMProperties.setLenient(isLenient);
206 public long getFirstDecideTime() {
207 return this.firstDecideTime;
210 public long getDecideTimeMultiple() {
211 return this.decideTimeMultiple;
215 public long getAvgFirstDecideTime() {
216 return this.firstDecideTime / numberOfFirstDecides;
218 public long getAvgDecideTimeMultiple() {
219 return this.avgDecideTimeMultiple / numberOfFirstDecides;