Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PDP / src / test / java / org / openecomp / policy / pdp / test / conformance / ConformanceTestEngine.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PDP
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.pdp.test.conformance;
22
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;
27
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; 
37
38 /**
39  * ConformanceTestEngine handles the creation of the PDPEngine for a ConformanceTest instance.
40  * 
41  * @version $Revision: 1.2 $
42  */
43 public class ConformanceTestEngine {
44         private Logger logger   = FlexLogger.getLogger(ConformanceTestEngine.class);
45         
46         private PDPEngineFactory pdpEngineFactory;
47         private ScopeResolver scopeResolver;
48         private boolean lenientRequests;
49         private boolean lenientPolicies;
50         private int iterations                  = 1;
51         
52         // total of all first calls to decide()
53         private long firstDecideTime;
54         private int numberOfFirstDecides = 0;
55         
56         // total of all non-first-calls to decide()
57         private long decideTimeMultiple;
58         
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;
62         
63         protected PDPEngineFactory getPDPEngineFactory() throws FactoryException {
64                 if (this.pdpEngineFactory == null) {
65                         this.pdpEngineFactory   = PDPEngineFactory.newInstance();
66                         this.pdpEngineFactory.setScopeResolver(this.scopeResolver);
67                 }
68                 return this.pdpEngineFactory;
69         }
70         
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;
76         }
77         
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());
81                 }
82                 PDPEngineFactory thisPDPEngineFactory   = null;
83                 try {
84                         thisPDPEngineFactory    = this.getPDPEngineFactory();
85                 } catch (FactoryException ex) {
86                         return new ConformanceTestResult(conformanceTest, ex);
87                 }
88                 
89                 ConformanceTestResult conformanceTestResult     = new ConformanceTestResult(conformanceTest, iterations);
90                 
91                 /*
92                  * Load the request
93                  */
94                 Request request                 = null;
95                 boolean isLenient               = DOMProperties.isLenient();
96                 try {
97                         DOMProperties.setLenient(this.lenientRequests);
98                         try {
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;
105                                 
106                         }
107                         
108                         /*
109                          * Load the expected response
110                          */
111                         Response response               = null;
112                         try {
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;
119                         }
120                         
121                         /*
122                          * Set up the configuration for the policy finder
123                          */
124                         conformanceTest.getRepository().setXACMLProperties();
125                         DOMProperties.setLenient(this.lenientPolicies);
126                         
127                         /*
128                          * Create the engine
129                          */
130                         PDPEngine pdpEngine             = null;
131                         try {
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;
138                         }
139                         if (pdpEngine == null) {
140                                 logger.error("Null PDP engine");
141                                 conformanceTestResult.setError(new NullPointerException("Null engine"));
142                                 return conformanceTestResult;
143                         }
144                         
145                         /*
146                          * Run the request
147                          */
148                         long startTime, endTime;
149                         long curDecideTime      = this.firstDecideTime;
150                         try {
151                                 startTime       = System.nanoTime();
152                                 response        = pdpEngine.decide(request);
153                                 endTime = System.nanoTime();
154 //System.out.println(endTime  - startTime);
155                                 // add to total
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;
165                         }
166                         if (response == null) {
167                                 logger.error("Null Response");
168                                 conformanceTestResult.setError(new NullPointerException("Null Response"));
169                                 return conformanceTestResult;                   
170                         }
171                         
172                         long localLoopTime = 0;
173                         try {
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);
185                                 }
186                         } catch (Exception ex) {
187                                 logger.error("Exception in iterated decide", ex);
188                                 return conformanceTestResult;
189                         }
190
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);
196                         
197                         long elapsedDecideTime  = this.firstDecideTime - curDecideTime;
198                         logger.info("Decide Time: " + elapsedDecideTime + "ns");
199                         
200                         return conformanceTestResult;
201                 } finally {
202                         DOMProperties.setLenient(isLenient);
203                 }
204         }
205
206         public long getFirstDecideTime() {
207                 return this.firstDecideTime;
208         }
209         
210         public long getDecideTimeMultiple() {
211                 return this.decideTimeMultiple;
212         }
213         
214         
215         public long getAvgFirstDecideTime() {
216                 return this.firstDecideTime / numberOfFirstDecides;
217         }
218         public long getAvgDecideTimeMultiple() {
219                 return this.avgDecideTimeMultiple / numberOfFirstDecides;
220         }
221 }