f8ad499f0b8476fbb542b43c3647c4ff293ea3eb
[policy/drools-applications.git] / controlloop / common / policy-yaml / src / test / java / org / onap / policy / controlloop / policy / ControlLoopPolicyBuilderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-yaml unit test
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.controlloop.policy;
22
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.UUID;
34
35 import org.junit.Test;
36 import org.yaml.snakeyaml.Yaml;
37 import org.yaml.snakeyaml.constructor.Constructor;
38 import org.yaml.snakeyaml.error.YAMLException;
39
40 import org.onap.policy.aai.PNF;
41 import org.onap.policy.aai.PNFType;
42 import org.onap.policy.controlloop.policy.builder.BuilderException;
43 import org.onap.policy.controlloop.policy.builder.ControlLoopPolicyBuilder;
44 import org.onap.policy.controlloop.policy.builder.Message;
45 import org.onap.policy.controlloop.policy.builder.MessageLevel;
46 import org.onap.policy.controlloop.policy.builder.Results;
47 import org.onap.policy.sdc.Resource;
48 import org.onap.policy.sdc.ResourceType;
49 import org.onap.policy.sdc.Service;
50
51
52 public class ControlLoopPolicyBuilderTest {
53         
54         @Test
55         public void testControlLoop() {
56         try {
57                         //
58                         // Create a builder for our policy
59                         //
60                         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
61                         //
62                         // Test add services
63                         //
64                         Service vSCP = new Service("vSCP");
65                         Service vUSP = new Service("vUSP");
66                         Service vTrinity = new Service("Trinity");
67                         builder = builder.addService(vSCP, vUSP, vTrinity);
68                         assertTrue(builder.getControlLoop().services.size() == 3);
69                         //
70                         // Test remove services
71                         //
72                         builder = builder.removeService(vSCP);
73                         assertTrue(builder.getControlLoop().services.size() == 2);
74                         builder = builder.removeAllServices();
75                         assertTrue(builder.getControlLoop().services.size() == 0);
76                         //
77                         // Test add resources
78                         //
79                         Resource vCTS = new Resource("vCTS", ResourceType.VF);
80                         Resource vCOM = new Resource("vCTS", ResourceType.VF);
81                         Resource vRAR = new Resource("vCTS", ResourceType.VF);
82                         builder = builder.addResource(vCTS, vCOM, vRAR);
83                         assertTrue(builder.getControlLoop().resources.size() == 3);
84                         //
85                         // Test remove resources
86                         //
87                         builder = builder.removeResource(vCTS);
88                         assertTrue(builder.getControlLoop().resources.size() == 2);
89                         builder = builder.removeAllResources();
90                         assertTrue(builder.getControlLoop().resources.size() == 0);
91                         //
92                         // Test set pnf
93                         //
94             PNF pnf = new PNF();
95             pnf.PNFName = UUID.randomUUID().toString();
96             pnf.PNFType = PNFType.ENODEB;
97             assertTrue(builder.getControlLoop().pnf == null);
98             builder = builder.setPNF(pnf);
99             assertTrue(builder.getControlLoop().pnf != null);
100             //
101             // Test remove pnf
102             //
103             builder = builder.removePNF();
104             assertTrue(builder.getControlLoop().pnf == null);
105             //
106             // Test set abatement
107             //
108             assertFalse(builder.getControlLoop().abatement);
109             builder = builder.setAbatement(true);
110             assertTrue(builder.getControlLoop().abatement);
111                 } catch (BuilderException e) {
112                         fail(e.getMessage());
113                 }
114         }
115         
116         @Test
117         public void testTimeout() {
118         try {
119             //
120             // Create a builder for our policy
121             //
122                         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
123             //
124             // Test setTimeout
125             //
126             assertTrue(builder.getControlLoop().timeout == 2400);
127             builder = builder.setTimeout(800);
128             assertTrue(builder.getControlLoop().timeout == 800);
129             // 
130             // Test calculateTimeout
131             //
132             Policy trigger = builder.setTriggerPolicy(
133                     "Restart the VM",
134                     "Upon getting the trigger event, restart the VM",
135                     "APPC",
136                     new Target(TargetType.VM),
137                     "Restart",
138                     null,
139                     2,
140                     300);
141             @SuppressWarnings("unused")
142                         Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult(
143                     "Rebuild VM",
144                     "If the restart fails, rebuild it",
145                     "APPC",
146                     new Target(TargetType.VM),
147                     "Rebuild",
148                     null,
149                     1,
150                     600,
151                     trigger.id,
152                     PolicyResult.FAILURE,
153                     PolicyResult.FAILURE_RETRIES,
154                     PolicyResult.FAILURE_TIMEOUT); 
155             assertTrue(builder.calculateTimeout().equals(new Integer(300 + 600)));
156             //
157         } catch (BuilderException e) {
158             fail(e.getMessage());
159         }
160         }
161         
162         @Test
163         public void testTriggerPolicyMethods() {
164             try {
165             ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
166             //
167             // Test isOpenLoop
168             //
169             assertTrue(builder.isOpenLoop());
170             //
171             // Test set initial trigger policy
172             //
173             Policy triggerPolicy1 = builder.setTriggerPolicy( 
174                     "Restart the VM",
175                     "Upon getting the trigger event, restart the VM",
176                     "APPC",
177                     new Target(TargetType.VM),
178                     "Restart",
179                     null,
180                     2,
181                     300);
182             assertTrue(builder.isOpenLoop() == false);
183             assertTrue(builder.getControlLoop().trigger_policy.equals(triggerPolicy1.id));
184             //
185             // Set trigger policy to a new policy 
186             //
187             @SuppressWarnings("unused")
188                         Policy triggerPolicy2 = builder.setTriggerPolicy(
189                     "Rebuild the VM",
190                     "Upon getting the trigger event, rebuild the VM",
191                     "APPC",
192                     new Target(TargetType.VM),
193                     "Rebuild",
194                     null,
195                     2,
196                     300);
197             // 
198             // Test set trigger policy to another existing policy
199             //
200             @SuppressWarnings("unused")
201                         ControlLoop cl = builder.setTriggerPolicy(triggerPolicy1.id);
202             assertTrue(builder.getControlLoop().trigger_policy.equals(triggerPolicy1.id));
203             //
204             // Test get trigger policy
205             //
206             assertTrue(builder.getTriggerPolicy().equals(triggerPolicy1));
207             //
208         } catch (BuilderException e) {
209             fail(e.getMessage());
210         }
211     }
212         
213         @Test
214         public void testAddRemovePolicies() {
215             try {
216                         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
217             Policy triggerPolicy = builder.setTriggerPolicy(
218                     "Restart the VM",
219                     "Upon getting the trigger event, restart the VM",
220                     "APPC",
221                     new Target(TargetType.VM),
222                     "Restart",
223                     null,
224                     2,
225                     300);
226             //
227             // Test create a policy and chain it to the results of trigger policy
228             //
229             Policy onRestartFailurePolicy1 = builder.setPolicyForPolicyResult(
230                     "Rebuild VM",
231                     "If the restart fails, rebuild it.",
232                     "APPC",
233                     new Target(TargetType.VM),
234                     "Rebuild",
235                     null,
236                     1,
237                     600,
238                     triggerPolicy.id,
239                     PolicyResult.FAILURE,
240                     PolicyResult.FAILURE_RETRIES,
241                     PolicyResult.FAILURE_TIMEOUT,
242                     PolicyResult.FAILURE_GUARD);
243             //
244             assertTrue(builder.getTriggerPolicy().failure.equals(onRestartFailurePolicy1.id));
245             assertTrue(builder.getTriggerPolicy().failure_retries.equals(onRestartFailurePolicy1.id));
246             assertTrue(builder.getTriggerPolicy().failure_timeout.equals(onRestartFailurePolicy1.id));
247             assertTrue(builder.getTriggerPolicy().failure_guard.equals(onRestartFailurePolicy1.id));
248             //
249             // Test remove policy
250             //
251             boolean removed = builder.removePolicy(onRestartFailurePolicy1.id);
252             assertTrue(removed);
253             assertTrue(builder.getTriggerPolicy().failure.equals(FinalResult.FINAL_FAILURE.toString()));
254             assertTrue(builder.getTriggerPolicy().failure_retries.equals(FinalResult.FINAL_FAILURE_RETRIES.toString()));
255             assertTrue(builder.getTriggerPolicy().failure_timeout.equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString()));
256             assertTrue(builder.getTriggerPolicy().failure_guard.equals(FinalResult.FINAL_FAILURE_GUARD.toString()));
257             //
258             // Create another policy and chain it to the results of trigger policy
259             //
260             Policy onRestartFailurePolicy2 = builder.setPolicyForPolicyResult( 
261                     "Rebuild VM",
262                     "If the restart fails, rebuild it.",
263                     "APPC",
264                     new Target(TargetType.VM),
265                     "Rebuild",
266                     null,
267                     2,
268                     600,
269                     triggerPolicy.id,
270                     PolicyResult.FAILURE,
271                     PolicyResult.FAILURE_RETRIES,
272                     PolicyResult.FAILURE_TIMEOUT);
273             //
274             // Test reset policy results
275             //
276             triggerPolicy = builder.resetPolicyResults(triggerPolicy.id);
277             assertTrue(builder.getTriggerPolicy().failure.equals(FinalResult.FINAL_FAILURE.toString()));
278             assertTrue(builder.getTriggerPolicy().failure_retries.equals(FinalResult.FINAL_FAILURE_RETRIES.toString()));
279             assertTrue(builder.getTriggerPolicy().failure_timeout.equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString()));
280             //                                                               
281             // Test set the policy results to an existing operational policy
282             //
283             onRestartFailurePolicy2 = builder.setPolicyForPolicyResult(
284                     onRestartFailurePolicy2.id, 
285                     triggerPolicy.id, 
286                     PolicyResult.FAILURE,
287                     PolicyResult.FAILURE_RETRIES,
288                     PolicyResult.FAILURE_TIMEOUT);
289             assertTrue(builder.getTriggerPolicy().failure.equals(onRestartFailurePolicy2.id));
290             assertTrue(builder.getTriggerPolicy().failure_retries.equals(onRestartFailurePolicy2.id));
291             assertTrue(builder.getTriggerPolicy().failure_timeout.equals(onRestartFailurePolicy2.id));
292             
293             //
294             // Test remove all existing operational policies
295             //
296             builder = builder.removeAllPolicies();
297             assertTrue(builder.getControlLoop().trigger_policy.equals(FinalResult.FINAL_OPENLOOP.toString()));
298             //
299         } catch (BuilderException e) {
300             fail(e.getMessage());
301         }
302     }
303
304         @Test
305         public void testAddOperationsAccumulateParams() {
306                 try {
307                         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
308             Policy triggerPolicy = builder.setTriggerPolicy(
309                     "Restart the eNodeB",
310                     "Upon getting the trigger event, restart the eNodeB",
311                     "SDNR",
312                     new Target(TargetType.PNF),
313                     "Restart",
314                     null,
315                     2,
316                     300);
317             //
318             // Add the operationsAccumulateParams
319             //
320             triggerPolicy = builder.addOperationsAccumulateParams(triggerPolicy.id, new OperationsAccumulateParams("15m", 5));
321             assertNotNull(builder.getTriggerPolicy().operationsAccumulateParams);
322             assertTrue(builder.getTriggerPolicy().operationsAccumulateParams.period.equals("15m"));
323             assertTrue(builder.getTriggerPolicy().operationsAccumulateParams.limit == 5);
324             //
325                 } catch (BuilderException e) {
326             fail(e.getMessage());
327         }
328         }
329         
330         
331         @Test
332         public void testBuildSpecification() {
333                 try {
334                         //
335                         // Create the builder
336                         //
337                         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 800);
338                         //
339                         // Set the first invalid trigger policy
340                         //
341                         Policy policy1 = builder.setTriggerPolicy(
342                     "Restart the VM",
343                     "Upon getting the trigger event, restart the VM",
344                     null,
345                     null,
346                     "Instantiate",
347                     null,
348                     2,
349                     300);
350                         Results results = builder.buildSpecification();
351                         //
352                         // Check that ERRORs are in results for invalid policy arguments
353                         //
354                         boolean invalid_actor = false;
355                         boolean invalid_recipe = false;
356                         boolean invalid_target = false;
357                         for (Message m : results.getMessages()) {
358                                 if (m.getMessage().equals("Policy actor is null") && m.getLevel() == MessageLevel.ERROR) {
359                                         invalid_actor = true;
360                                 }
361                                 if (m.getMessage().equals("Policy recipe is invalid") && m.getLevel() == MessageLevel.ERROR) {
362                                         invalid_recipe = true;
363                                 }
364                                 if (m.getMessage().equals("Policy target is null") && m.getLevel() == MessageLevel.ERROR) {
365                                         invalid_target = true;
366                                 }
367                         }
368                         //
369                         assertTrue(invalid_actor);
370                         assertTrue(invalid_recipe);
371                         assertTrue(invalid_target);
372                         //
373                         // Remove the invalid policy
374                         //
375                         //@SuppressWarnings("unused")
376                         boolean removed = builder.removePolicy(policy1.id);
377                         assertTrue(removed);
378                         assertTrue(builder.getTriggerPolicy() == null);
379                         //
380                         // Set a valid trigger policy
381                         //
382                         policy1 = builder.setTriggerPolicy(
383                     "Rebuild VM",
384                     "If the restart fails, rebuild it.",
385                     "APPC",
386                     new Target(TargetType.VM),
387                     "Rebuild",
388                     null,
389                     1,
390                     600);
391                         //
392                         // Set a second valid trigger policy
393                         //
394                         Policy policy2 = builder.setTriggerPolicy(
395                                         "Restart the VM",
396                     "Upon getting the trigger event, restart the VM",
397                     "APPC",
398                     new Target(TargetType.VM),
399                     "Restart",
400                     null,
401                     2,
402                     300);
403                         //
404                         // Now, we have policy1 unreachable
405                         //
406                         results = builder.buildSpecification();
407                         boolean unreachable = false;
408                         for (Message m : results.getMessages()) {
409                                 if (m.getMessage().equals("Policy " + policy1.id + " is not reachable.") && m.getLevel() == MessageLevel.WARNING) {
410                                         unreachable = true;
411                                         break;
412                                 }
413                         }
414                         assertTrue(unreachable);
415                         //
416                         // Set policy1 for the failure results of policy2
417                         //
418                         policy1 = builder.setPolicyForPolicyResult(
419                                         policy1.id, 
420                                         policy2.id,
421                                         PolicyResult.FAILURE,
422                     PolicyResult.FAILURE_RETRIES,
423                     PolicyResult.FAILURE_TIMEOUT);
424                         results = builder.buildSpecification();
425                         boolean invalid_timeout = false;
426                         for (Message m : results.getMessages()) {
427                                 if (m.getMessage().equals("controlLoop overall timeout is less than the sum of operational policy timeouts.") && m.getLevel() == MessageLevel.ERROR) {
428                                         invalid_timeout = true;
429                                         break;
430                                 }
431                         }
432                         assertTrue(invalid_timeout);
433                         //
434                         // Remove policy2 (revert controlLoop back to open loop) 
435                         //
436                         removed = builder.removePolicy(policy2.id);
437                         //
438                         // ControlLoop is open loop now, but it still has policies (policy1)
439                         //
440                         results = builder.buildSpecification();
441                         unreachable = false;
442                         for (Message m : results.getMessages()) {
443                                 if (m.getMessage().equals("Open Loop policy contains policies. The policies will never be invoked.") && m.getLevel() == MessageLevel.WARNING) {
444                                         unreachable = true;
445                                         break;
446                                 }
447                         }
448                         assertTrue(unreachable);
449                         //
450                 } catch (BuilderException e) {
451             fail(e.getMessage());
452         }
453         }
454         
455         
456         @Test
457         public void test() {
458                 this.test("src/test/resources/v1.0.0/policy_Test.yaml");
459         }
460         
461         @Test
462         public void testEvilYaml() {
463                 try (InputStream is = new FileInputStream(new File("src/test/resources/v1.0.0/test_evil.yaml"))) {
464                         //
465                         // Read the yaml into our Java Object
466                         //
467                         Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
468                         yaml.load(is);
469                 } catch (FileNotFoundException e) {
470                         fail(e.getLocalizedMessage());
471                 } catch (IOException e) {
472                         fail(e.getLocalizedMessage());
473                 } catch (YAMLException e) {
474                         //
475                         // Should have this
476                         //
477                 }
478         }
479         
480         public void test(String testFile) {
481                 try (InputStream is = new FileInputStream(new File(testFile))) {
482                         //
483                         // Read the yaml into our Java Object
484                         //
485                         Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
486                         Object obj = yaml.load(is);
487                         assertNotNull(obj);
488                         assertTrue(obj instanceof ControlLoopPolicy);
489                         ControlLoopPolicy policyTobuild = (ControlLoopPolicy) obj;
490                         //
491                         // Now we're going to try to use the builder to build this.
492                         //
493                         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(
494                                         policyTobuild.controlLoop.controlLoopName,
495                                         policyTobuild.controlLoop.timeout);
496                         //
497                         // Add services
498                         //
499                         if (policyTobuild.controlLoop.services != null) {
500                                 builder = builder.addService(policyTobuild.controlLoop.services.toArray(new Service[policyTobuild.controlLoop.services.size()]));
501                         }
502                         //
503                         // Add resources
504                         //
505                         if (policyTobuild.controlLoop.resources != null) {
506                                 builder = builder.addResource(policyTobuild.controlLoop.resources.toArray(new Resource[policyTobuild.controlLoop.resources.size()]));
507                         }
508                         //
509                         // Set pnf
510                         //
511                         if (policyTobuild.controlLoop.pnf != null) {
512                                 builder = builder.setPNF(policyTobuild.controlLoop.pnf);
513                         }
514                         //
515                         // Add the policies and be sure to set the trigger policy
516                         //
517                         if (policyTobuild.policies != null) {
518                                 for (Policy policy : policyTobuild.policies) {
519                                         if (policy.id == policyTobuild.controlLoop.trigger_policy) {
520                                                 builder.setTriggerPolicy(policy.name, policy.description, policy.actor, policy.target, policy.recipe, null, policy.retry, policy.timeout);
521                                         }
522                                 }
523                         }
524                 
525                         // Question : how to change policy ID and results by using builder ??
526                 
527                         @SuppressWarnings("unused")
528                         Results results = builder.buildSpecification();
529                         
530                 } catch (FileNotFoundException e) {
531                         fail(e.getLocalizedMessage());
532                 } catch (IOException e) {
533                         fail(e.getLocalizedMessage());
534                 } catch (BuilderException e) {
535                         fail(e.getLocalizedMessage());
536                 }
537                 
538         }
539
540 }