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.openecomp.policy.common.logging.flexlogger.FlexLogger;
24 import org.openecomp.policy.common.logging.flexlogger.Logger;
26 import com.att.research.xacml.api.Request;
27 import com.att.research.xacml.api.Response;
28 import com.att.research.xacml.api.pdp.PDPEngine;
29 import com.att.research.xacml.api.pdp.PDPEngineFactory;
30 import com.att.research.xacml.api.pdp.ScopeResolver;
31 import com.att.research.xacml.std.dom.DOMProperties;
32 import com.att.research.xacml.std.dom.DOMRequest;
33 import com.att.research.xacml.std.dom.DOMResponse;
34 import com.att.research.xacml.util.FactoryException;
37 * ConformanceTestEngine handles the creation of the PDPEngine for a ConformanceTest instance.
39 * @version $Revision: 1.2 $
41 public class ConformanceTestEngine {
42 private Logger logger = FlexLogger.getLogger(ConformanceTestEngine.class);
44 private PDPEngineFactory pdpEngineFactory;
45 private ScopeResolver scopeResolver;
46 private boolean lenientRequests;
47 private boolean lenientPolicies;
48 private int iterations = 1;
50 // total of all first calls to decide()
51 private long firstDecideTime;
52 private int numberOfFirstDecides = 0;
54 // total of all non-first-calls to decide()
55 private long decideTimeMultiple;
57 // total of average time each test case uses for a Request
58 // (sum of : for each test case, average of all non-first-call calls to decide() )
59 private long avgDecideTimeMultiple = 0;
61 protected PDPEngineFactory getPDPEngineFactory() throws FactoryException {
62 if (this.pdpEngineFactory == null) {
63 this.pdpEngineFactory = PDPEngineFactory.newInstance();
64 this.pdpEngineFactory.setScopeResolver(this.scopeResolver);
66 return this.pdpEngineFactory;
69 public ConformanceTestEngine(ScopeResolver scopeResolverIn, boolean lenientRequestsIn, boolean lenientPoliciesIn, int iterationsIn) {
70 this.scopeResolver = scopeResolverIn;
71 this.lenientRequests = lenientRequestsIn;
72 this.lenientPolicies = lenientPoliciesIn;
73 this.iterations = iterationsIn;
76 public ConformanceTestResult run(ConformanceTest conformanceTest) {
77 if (conformanceTest.getRequest() == null || conformanceTest.getResponse() == null || conformanceTest.getRepository() == null) {
78 logger.error("Incomplete Conformance Test: " + conformanceTest.getTestName());
80 PDPEngineFactory thisPDPEngineFactory = null;
82 thisPDPEngineFactory = this.getPDPEngineFactory();
83 } catch (FactoryException ex) {
84 return new ConformanceTestResult(conformanceTest, ex);
87 ConformanceTestResult conformanceTestResult = new ConformanceTestResult(conformanceTest, iterations);
92 Request request = null;
93 boolean isLenient = DOMProperties.isLenient();
95 DOMProperties.setLenient(this.lenientRequests);
97 request = DOMRequest.load(conformanceTest.getRequest());
98 conformanceTestResult.setRequest(request);
99 } catch (Exception ex) {
100 logger.error("Exception loading Request file " + conformanceTest.getRequest().getAbsolutePath(), ex);
101 conformanceTestResult.setError(ex);
102 return conformanceTestResult;
107 * Load the expected response
109 Response response = null;
111 response = DOMResponse.load(conformanceTest.getResponse());
112 conformanceTestResult.setExpectedResponse(response);
113 } catch (Exception ex) {
114 logger.error("Exception loading Response file " + conformanceTest.getResponse().getAbsolutePath(), ex);
115 conformanceTestResult.setError(ex);
116 return conformanceTestResult;
120 * Set up the configuration for the policy finder
122 conformanceTest.getRepository().setXACMLProperties();
123 DOMProperties.setLenient(this.lenientPolicies);
128 PDPEngine pdpEngine = null;
130 // pdpEngine = thisPDPEngineFactory.newEngine(conformanceTest.getRootPolicy(), conformanceTest.getReferencedPolicies(), pipFinderEngine);
131 pdpEngine = thisPDPEngineFactory.newEngine();
132 } catch (Exception ex) {
133 logger.error("Exception getting PDP engine instance", ex);
134 conformanceTestResult.setError(ex);
135 return conformanceTestResult;
137 if (pdpEngine == null) {
138 logger.error("Null PDP engine");
139 conformanceTestResult.setError(new NullPointerException("Null engine"));
140 return conformanceTestResult;
146 long startTime, endTime;
147 long curDecideTime = this.firstDecideTime;
149 startTime = System.nanoTime();
150 response = pdpEngine.decide(request);
151 endTime = System.nanoTime();
152 //System.out.println(endTime - startTime);
154 this.firstDecideTime += endTime - startTime;
155 this.numberOfFirstDecides++;
156 // remember just this test
157 conformanceTestResult.setFirstCallTime(endTime - startTime);
158 conformanceTestResult.setActualResponse(response);
159 } catch (Exception ex) {
160 logger.error("Exception in decide", ex);
161 conformanceTestResult.setError(ex);
162 return conformanceTestResult;
164 if (response == null) {
165 logger.error("Null Response");
166 conformanceTestResult.setError(new NullPointerException("Null Response"));
167 return conformanceTestResult;
170 long localLoopTime = 0;
172 // if user requested non-first-call calls to decide() to get performance info, run them now.
173 // We can ignore the result since we are only interested in how long they take to process the Request.
174 for (int i = 0 ; i < this.iterations ; i++) {
175 startTime = System.nanoTime();
176 pdpEngine.decide(request);
177 endTime = System.nanoTime();
178 //System.out.println(endTime - startTime);
179 // add to the global total for all tests
180 this.decideTimeMultiple += (endTime - startTime);
181 // remember just this one test's info
182 localLoopTime += (endTime - startTime);
184 } catch (Exception ex) {
185 logger.error("Exception in iterated decide", ex);
186 return conformanceTestResult;
189 // add to total average for non-first-call times for all test cases
190 avgDecideTimeMultiple += (localLoopTime / iterations);
191 //System.out.println("localLoop="+localLoopTime + " it="+iterations + " avg=" + (localLoopTime / iterations) );
192 // remember average time for just this test
193 conformanceTestResult.setAverageTotalLoopTime(localLoopTime/iterations);
195 long elapsedDecideTime = this.firstDecideTime - curDecideTime;
196 logger.info("Decide Time: " + elapsedDecideTime + "ns");
198 return conformanceTestResult;
200 DOMProperties.setLenient(isLenient);
204 public long getFirstDecideTime() {
205 return this.firstDecideTime;
208 public long getDecideTimeMultiple() {
209 return this.decideTimeMultiple;
213 public long getAvgFirstDecideTime() {
214 return this.firstDecideTime / numberOfFirstDecides;
216 public long getAvgDecideTimeMultiple() {
217 return this.avgDecideTimeMultiple / numberOfFirstDecides;