aboutsummaryrefslogtreecommitdiffstats
path: root/ECOMP-TEST/src/test/java/org/openecomp/policy/pdp/test/conformance/ConformanceTestEngine.java
blob: c24b5473f43e89d6353562b481ebfc957b84c5bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*-
 * ============LICENSE_START=======================================================
 * ECOMP-TEST
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.policy.pdp.test.conformance;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.att.research.xacml.api.Request;
import com.att.research.xacml.api.Response;
import com.att.research.xacml.api.pdp.PDPEngine;
import com.att.research.xacml.api.pdp.PDPEngineFactory;
import com.att.research.xacml.api.pdp.ScopeResolver;
import com.att.research.xacml.std.dom.DOMProperties;
import com.att.research.xacml.std.dom.DOMRequest;
import com.att.research.xacml.std.dom.DOMResponse;
import com.att.research.xacml.util.FactoryException;

/**
 * ConformanceTestEngine handles the creation of the PDPEngine for a ConformanceTest instance.
 * 
 * @version $Revision: 1.2 $
 */
public class ConformanceTestEngine {
	private Log logger	= LogFactory.getLog(ConformanceTestEngine.class);
	
	private PDPEngineFactory pdpEngineFactory;
	private ScopeResolver scopeResolver;
	private boolean lenientRequests;
	private boolean lenientPolicies;
	private int iterations			= 1;
	
	// total of all first calls to decide()
	private long firstDecideTime;
	private int numberOfFirstDecides = 0;
	
	// total of all non-first-calls to decide()
	private long decideTimeMultiple;
	
	// total of average time each test case uses for a Request
	// (sum of : for each test case, average of all non-first-call calls to decide() )
	private long avgDecideTimeMultiple = 0;
	
	protected PDPEngineFactory getPDPEngineFactory() throws FactoryException {
		if (this.pdpEngineFactory == null) {
			this.pdpEngineFactory	= PDPEngineFactory.newInstance();
			this.pdpEngineFactory.setScopeResolver(this.scopeResolver);
		}
		return this.pdpEngineFactory;
	}
	
	public ConformanceTestEngine(ScopeResolver scopeResolverIn, boolean lenientRequestsIn, boolean lenientPoliciesIn, int iterationsIn) {
		this.scopeResolver		= scopeResolverIn;
		this.lenientRequests	= lenientRequestsIn;
		this.lenientPolicies	= lenientPoliciesIn;
		this.iterations			= iterationsIn;
	}
	
	public ConformanceTestResult run(ConformanceTest conformanceTest) {
		if (conformanceTest.getRequest() == null || conformanceTest.getResponse() == null || conformanceTest.getRepository() == null) {
			logger.error("Incomplete Conformance Test: " + conformanceTest.getTestName());
		}
		PDPEngineFactory thisPDPEngineFactory	= null;
		try {
			thisPDPEngineFactory	= this.getPDPEngineFactory();
		} catch (FactoryException ex) {
			return new ConformanceTestResult(conformanceTest, ex);
		}
		
		ConformanceTestResult conformanceTestResult	= new ConformanceTestResult(conformanceTest, iterations);
		
		/*
		 * Load the request
		 */
		Request request			= null;
		boolean isLenient		= DOMProperties.isLenient();
		try {
			DOMProperties.setLenient(this.lenientRequests);
			try {
				request		= DOMRequest.load(conformanceTest.getRequest());
				conformanceTestResult.setRequest(request);
			} catch (Exception ex) {
				logger.error("Exception loading Request file " + conformanceTest.getRequest().getAbsolutePath(), ex);
				conformanceTestResult.setError(ex);
				return conformanceTestResult;
				
			}
			
			/*
			 * Load the expected response
			 */
			Response response		= null;
			try {
				response	= DOMResponse.load(conformanceTest.getResponse());
				conformanceTestResult.setExpectedResponse(response);
			} catch (Exception ex) {
				logger.error("Exception loading Response file " + conformanceTest.getResponse().getAbsolutePath(), ex);
				conformanceTestResult.setError(ex);
				return conformanceTestResult;
			}
			
			/*
			 * Set up the configuration for the policy finder
			 */
			conformanceTest.getRepository().setXACMLProperties();
			DOMProperties.setLenient(this.lenientPolicies);
			
			/*
			 * Create the engine
			 */
			PDPEngine pdpEngine		= null;
			try {
				// pdpEngine	= thisPDPEngineFactory.newEngine(conformanceTest.getRootPolicy(), conformanceTest.getReferencedPolicies(), pipFinderEngine);
				pdpEngine		= thisPDPEngineFactory.newEngine();
			} catch (Exception ex) {
				logger.error("Exception getting PDP engine instance", ex);
				conformanceTestResult.setError(ex);
				return conformanceTestResult;
			}
			if (pdpEngine == null) {
				logger.error("Null PDP engine");
				conformanceTestResult.setError(new NullPointerException("Null engine"));
				return conformanceTestResult;
			}
			
			/*
			 * Run the request
			 */
			long startTime, endTime;
			long curDecideTime	= this.firstDecideTime;
			try {
				startTime	= System.nanoTime();
				response	= pdpEngine.decide(request);
				endTime = System.nanoTime();
//System.out.println(endTime  - startTime);
				// add to total
				this.firstDecideTime	+= endTime - startTime;
				this.numberOfFirstDecides++;
				// remember just this test
				conformanceTestResult.setFirstCallTime(endTime - startTime);
				conformanceTestResult.setActualResponse(response);
			} catch (Exception ex) {
				logger.error("Exception in decide", ex);
				conformanceTestResult.setError(ex);
				return conformanceTestResult;
			}
			if (response == null) {
				logger.error("Null Response");
				conformanceTestResult.setError(new NullPointerException("Null Response"));
				return conformanceTestResult;			
			}
			
			long localLoopTime = 0;
			try {
				// if user requested non-first-call calls to decide() to get performance info, run them now.
				// We can ignore the result since we are only interested in how long they take to process the Request.
				for (int i = 0 ; i < this.iterations ; i++) {
					startTime	= System.nanoTime();
					pdpEngine.decide(request);
					endTime = System.nanoTime();
//System.out.println(endTime - startTime);					
					// add to the global total for all tests
					this.decideTimeMultiple	+= (endTime - startTime);
					// remember just this one test's info
					localLoopTime += (endTime - startTime);
				}
			} catch (Exception ex) {
				logger.error("Exception in iterated decide", ex);
				return conformanceTestResult;
			}

			// add to total average for non-first-call times for all test cases
			avgDecideTimeMultiple += (localLoopTime / iterations);
//System.out.println("localLoop="+localLoopTime + "   it="+iterations + "   avg=" + (localLoopTime / iterations) );
			// remember average time for just this test
			conformanceTestResult.setAverageTotalLoopTime(localLoopTime/iterations);
			
			long elapsedDecideTime	= this.firstDecideTime - curDecideTime;
			logger.info("Decide Time: " + elapsedDecideTime + "ns");
			
			return conformanceTestResult;
		} finally {
			DOMProperties.setLenient(isLenient);
		}
	}

	public long getFirstDecideTime() {
		return this.firstDecideTime;
	}
	
	public long getDecideTimeMultiple() {
		return this.decideTimeMultiple;
	}
	
	
	public long getAvgFirstDecideTime() {
		return this.firstDecideTime / numberOfFirstDecides;
	}
	public long getAvgDecideTimeMultiple() {
		return this.avgDecideTimeMultiple / numberOfFirstDecides;
	}
}