[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / ONAP-PDP / src / test / java / org / onap / policy / pdp / test / conformance / ConformanceTestEngine.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-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.onap.policy.pdp.test.conformance;
22
23 import org.onap.policy.common.logging.flexlogger.FlexLogger;
24 import org.onap.policy.common.logging.flexlogger.Logger;
25
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; 
35
36 /**
37  * ConformanceTestEngine handles the creation of the PDPEngine for a ConformanceTest instance.
38  * 
39  * @version $Revision: 1.2 $
40  */
41 public class ConformanceTestEngine {
42         private Logger logger   = FlexLogger.getLogger(ConformanceTestEngine.class);
43         
44         private PDPEngineFactory pdpEngineFactory;
45         private ScopeResolver scopeResolver;
46         private boolean lenientRequests;
47         private boolean lenientPolicies;
48         private int iterations                  = 1;
49         
50         // total of all first calls to decide()
51         private long firstDecideTime;
52         private int numberOfFirstDecides = 0;
53         
54         // total of all non-first-calls to decide()
55         private long decideTimeMultiple;
56         
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;
60         
61         protected PDPEngineFactory getPDPEngineFactory() throws FactoryException {
62                 if (this.pdpEngineFactory == null) {
63                         this.pdpEngineFactory   = PDPEngineFactory.newInstance();
64                         this.pdpEngineFactory.setScopeResolver(this.scopeResolver);
65                 }
66                 return this.pdpEngineFactory;
67         }
68         
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;
74         }
75         
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());
79                 }
80                 PDPEngineFactory thisPDPEngineFactory   = null;
81                 try {
82                         thisPDPEngineFactory    = this.getPDPEngineFactory();
83                 } catch (FactoryException ex) {
84                         return new ConformanceTestResult(conformanceTest, ex);
85                 }
86                 
87                 ConformanceTestResult conformanceTestResult     = new ConformanceTestResult(conformanceTest, iterations);
88                 
89                 /*
90                  * Load the request
91                  */
92                 Request request                 = null;
93                 boolean isLenient               = DOMProperties.isLenient();
94                 try {
95                         DOMProperties.setLenient(this.lenientRequests);
96                         try {
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;
103                                 
104                         }
105                         
106                         /*
107                          * Load the expected response
108                          */
109                         Response response               = null;
110                         try {
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;
117                         }
118                         
119                         /*
120                          * Set up the configuration for the policy finder
121                          */
122                         conformanceTest.getRepository().setXACMLProperties();
123                         DOMProperties.setLenient(this.lenientPolicies);
124                         
125                         /*
126                          * Create the engine
127                          */
128                         PDPEngine pdpEngine             = null;
129                         try {
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;
136                         }
137                         if (pdpEngine == null) {
138                                 logger.error("Null PDP engine");
139                                 conformanceTestResult.setError(new NullPointerException("Null engine"));
140                                 return conformanceTestResult;
141                         }
142                         
143                         /*
144                          * Run the request
145                          */
146                         long startTime, endTime;
147                         long curDecideTime      = this.firstDecideTime;
148                         try {
149                                 startTime       = System.nanoTime();
150                                 response        = pdpEngine.decide(request);
151                                 endTime = System.nanoTime();
152 //System.out.println(endTime  - startTime);
153                                 // add to total
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;
163                         }
164                         if (response == null) {
165                                 logger.error("Null Response");
166                                 conformanceTestResult.setError(new NullPointerException("Null Response"));
167                                 return conformanceTestResult;                   
168                         }
169                         
170                         long localLoopTime = 0;
171                         try {
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);
183                                 }
184                         } catch (Exception ex) {
185                                 logger.error("Exception in iterated decide", ex);
186                                 return conformanceTestResult;
187                         }
188
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);
194                         
195                         long elapsedDecideTime  = this.firstDecideTime - curDecideTime;
196                         logger.info("Decide Time: " + elapsedDecideTime + "ns");
197                         
198                         return conformanceTestResult;
199                 } finally {
200                         DOMProperties.setLenient(isLenient);
201                 }
202         }
203
204         public long getFirstDecideTime() {
205                 return this.firstDecideTime;
206         }
207         
208         public long getDecideTimeMultiple() {
209                 return this.decideTimeMultiple;
210         }
211         
212         
213         public long getAvgFirstDecideTime() {
214                 return this.firstDecideTime / numberOfFirstDecides;
215         }
216         public long getAvgDecideTimeMultiple() {
217                 return this.avgDecideTimeMultiple / numberOfFirstDecides;
218         }
219 }