05d4e469d2468e236f0867d097f55f44c9d6156a
[policy/drools-applications.git] /
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.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.FileNotFoundException;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.util.UUID;
36 import org.junit.Ignore;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.ExpectedException;
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 import org.yaml.snakeyaml.Yaml;
51 import org.yaml.snakeyaml.constructor.Constructor;
52 import org.yaml.snakeyaml.error.YAMLException;
53
54
55 public class ControlLoopPolicyBuilderTest {
56         
57         @Rule
58         public ExpectedException expectedException = ExpectedException.none();
59     
60     @Test
61     public void testControlLoop() {
62         try {
63             //
64             // Create a builder for our policy
65             //
66             ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
67             //
68             // Test add services
69             //
70             Service vSCP = new Service("vSCP");
71             Service vUSP = new Service("vUSP");
72             Service vTrinity = new Service("Trinity");
73             builder = builder.addService(vSCP, vUSP, vTrinity);
74             assertTrue(builder.getControlLoop().getServices().size() == 3);
75             //
76             // Test remove services
77             //
78             builder = builder.removeService(vSCP);
79             assertTrue(builder.getControlLoop().getServices().size() == 2);
80             builder = builder.removeAllServices();
81             assertTrue(builder.getControlLoop().getServices().size() == 0);
82             //
83             // Test add resources
84             //
85             Resource vCTS = new Resource("vCTS", ResourceType.VF);
86             Resource vCOM = new Resource("vCTS", ResourceType.VF);
87             Resource vRAR = new Resource("vCTS", ResourceType.VF);
88             builder = builder.addResource(vCTS, vCOM, vRAR);
89             assertTrue(builder.getControlLoop().getResources().size() == 3);
90             //
91             // Test remove resources
92             //
93             builder = builder.removeResource(vCTS);
94             assertTrue(builder.getControlLoop().getResources().size() == 2);
95             builder = builder.removeAllResources();
96             assertTrue(builder.getControlLoop().getResources().size() == 0);
97         } catch (BuilderException e) {
98             fail(e.getMessage());
99         }
100     }
101     
102     @Test
103     public void testAddNullService() throws BuilderException {
104         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
105         expectedException.expect(BuilderException.class);
106         expectedException.expectMessage("Service must not be null");
107         builder.addService((Service)null);
108     }
109     
110     @Test
111     public void testAddInvalidService() throws BuilderException {
112         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
113         expectedException.expect(BuilderException.class);
114         expectedException.expectMessage("Invalid service - need either a serviceUUID or serviceName");
115         builder.addService(new Service());
116     }
117     
118     @Test
119     public void testAddServiceWithUUID() throws BuilderException {
120         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
121         UUID uuid = UUID.randomUUID();
122         Service serviceWithUUID = new Service(uuid);
123         builder.addService(serviceWithUUID);
124         assertTrue(builder.getControlLoop().getServices().size() == 1);
125     }
126     
127     @Test
128     public void testAddNullResource() throws BuilderException {
129         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
130         expectedException.expect(BuilderException.class);
131         expectedException.expectMessage("Resource must not be null");
132         builder.addResource((Resource)null);
133     }
134     
135     
136     @Test
137     public void testAddInvalidResource() throws BuilderException {
138         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
139         expectedException.expect(BuilderException.class);
140         expectedException.expectMessage("Invalid resource - need either resourceUUID or resourceName");
141         builder.addResource(new Resource());
142     }
143     
144     @Test
145     public void testAddAndRemoveResourceWithUUID() throws BuilderException {
146         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
147         UUID uuid = UUID.randomUUID();
148         Resource resourceWithUUID = new Resource(uuid);
149         builder.addResource(resourceWithUUID);
150         assertTrue(builder.getControlLoop().getResources().size() == 1);
151         
152         builder.removeResource(resourceWithUUID);
153         assertTrue(builder.getControlLoop().getResources().size() == 0);
154     }
155     
156     @Test
157     public void testRemoveNullResource() throws BuilderException {
158         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
159         Resource resource = new Resource("resource1", ResourceType.VF);
160         builder.addResource(resource);
161         expectedException.expect(BuilderException.class);
162         expectedException.expectMessage("Resource must not be null");
163         builder.removeResource((Resource)null);
164     }
165     
166     @Test
167     public void testRemoveResourceNoExistingResources() throws BuilderException {
168         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
169         expectedException.expect(BuilderException.class);
170         expectedException.expectMessage("No existing resources to remove");
171         builder.removeResource(new Resource("resource1", ResourceType.VF));
172     }
173     
174     @Test
175     public void testRemoveInvalidResource() throws BuilderException {
176         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
177         Resource resource = new Resource("resource1", ResourceType.VF);
178         builder.addResource(resource);
179         expectedException.expect(BuilderException.class);
180         expectedException.expectMessage("Invalid resource - need either a resourceUUID or resourceName");
181         builder.removeResource(new Resource());
182     }
183     
184     @Test
185     public void testRemoveUnknownResource() throws BuilderException {
186         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
187         Resource resource = new Resource("resource1", ResourceType.VF);
188         builder.addResource(resource);
189         final String unknownResourceName = "reource2";
190         expectedException.expect(BuilderException.class);
191         expectedException.expectMessage("Unknown resource " + unknownResourceName);
192         builder.removeResource(new Resource(unknownResourceName, ResourceType.VF));
193     }
194     
195     @Test
196     public void testControlLoopWithInitialResourceAndServices() {
197         try {
198             Resource vCTS = new Resource("vCTS", ResourceType.VF);
199             Service vSCP = new Service("vSCP");
200             Service vUSP = new Service("vUSP");
201             ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, vCTS, vSCP, vUSP);
202             assertTrue(builder.getControlLoop().getResources().size() == 1);
203             assertTrue(builder.getControlLoop().getServices().size() == 2);
204         } catch (BuilderException e) {
205             fail(e.getMessage());
206         }
207     }
208     
209     @Test
210     public void testControlLoopWithInitialResourcesAndService() {
211         try {
212             Resource vCTS = new Resource("vCTS", ResourceType.VF);
213             Resource vCOM = new Resource("vCTS", ResourceType.VF);
214             Service vSCP = new Service("vSCP");
215             ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, vSCP, vCTS, vCOM);
216             assertTrue(builder.getControlLoop().getServices().size() == 1);
217             assertTrue(builder.getControlLoop().getResources().size() == 2);
218         } catch (BuilderException e) {
219             fail(e.getMessage());
220         }
221     }
222     
223     @Test
224     @Ignore
225     // I'VE MARKED THIS TEST CASE AS IGNORE BECAUSE THE TEST CASE FAILS
226     // This test case fails because builder.getControlLoop() returns an instance of ControlLoop copied using 
227     // the ControlLoop(ControlLoop controlLoop) constructor. 
228     // This constructor does not copy the value of pnf into the newly created object
229     // On the face of it, this looks like a bug, but perhaps there is a reason for this
230     // PLEASE ADVISE IF THE BEHAVIOUR IS INCORRECT OR THE TEST CASE IS INVALID
231     public void testControlLoopForPnf() {
232         try {
233                 PNF pnf = new PNF();
234                 pnf.setPNFType(PNFType.ENODEB);
235             ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400, pnf);
236             assertEquals(pnf, builder.getControlLoop().getPnf());
237             
238             builder.removePNF();
239             assertNull(builder.getControlLoop().getPnf());
240         } catch (BuilderException e) {
241             fail(e.getMessage());
242         }
243     }
244     
245     @Test
246     @Ignore
247     // Fails for the same reason as the above test case
248     public void testSetAndRemovePnf() throws BuilderException {
249         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
250         assertNull(builder.getControlLoop().getPnf());
251         
252         PNF pnf = new PNF();
253         pnf.setPNFType(PNFType.ENODEB);
254         builder.setPNF(pnf);
255         assertEquals(pnf, builder.getControlLoop().getPnf());
256         
257         builder.removePNF();
258         assertNull(builder.getControlLoop().getPnf());
259     }
260     
261     @Test
262     public void testSetNullPnf() throws BuilderException {
263         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
264         expectedException.expect(BuilderException.class);
265         expectedException.expectMessage("PNF must not be null");
266         builder.setPNF(null);
267     }
268     
269     @Test
270     public void testSetInvalidPnf() throws BuilderException {
271         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
272         expectedException.expect(BuilderException.class);
273         expectedException.expectMessage("Invalid PNF - need either pnfName or pnfType");
274         builder.setPNF(new PNF());
275     }
276     
277     @Test
278     public void testSetAbatement() throws BuilderException {
279         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
280         assertFalse(builder.getControlLoop().getAbatement());
281         builder = builder.setAbatement(true);
282         assertTrue(builder.getControlLoop().getAbatement());
283     }
284     
285     @Test
286     public void testSetNullAbatement() throws BuilderException {
287         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
288         expectedException.expect(BuilderException.class);
289         expectedException.expectMessage("abatement must not be null");
290         builder = builder.setAbatement(null);
291     }
292     
293     @Test
294     public void testTimeout() {
295         try {
296             //
297             // Create a builder for our policy
298             //
299             ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
300             //
301             // Test setTimeout
302             //
303             assertTrue(builder.getControlLoop().getTimeout() == 2400);
304             builder = builder.setTimeout(800);
305             assertTrue(builder.getControlLoop().getTimeout() == 800);
306             // 
307             // Test calculateTimeout
308             //
309             Policy trigger = builder.setTriggerPolicy(
310                     "Restart the VM",
311                     "Upon getting the trigger event, restart the VM",
312                     "APPC",
313                     new Target(TargetType.VM),
314                     "Restart",
315                     null,
316                     2,
317                     300);
318             @SuppressWarnings("unused")
319             Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult(
320                     "Rebuild VM",
321                     "If the restart fails, rebuild it",
322                     "APPC",
323                     new Target(TargetType.VM),
324                     "Rebuild",
325                     null,
326                     1,
327                     600,
328                     trigger.getId(),
329                     PolicyResult.FAILURE,
330                     PolicyResult.FAILURE_RETRIES,
331                     PolicyResult.FAILURE_TIMEOUT); 
332             assertTrue(builder.calculateTimeout().equals(new Integer(300 + 600)));
333             //
334         } catch (BuilderException e) {
335             fail(e.getMessage());
336         }
337     }
338     
339     @Test
340     public void testTriggerPolicyMethods() {
341         try {
342             ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
343             //
344             // Test isOpenLoop
345             //
346             assertTrue(builder.isOpenLoop());
347             //
348             // Test set initial trigger policy
349             //
350             Policy triggerPolicy1 = builder.setTriggerPolicy( 
351                     "Restart the VM",
352                     "Upon getting the trigger event, restart the VM",
353                     "APPC",
354                     new Target(TargetType.VM),
355                     "Restart",
356                     null,
357                     2,
358                     300);
359             assertTrue(builder.isOpenLoop() == false);
360             assertTrue(builder.getControlLoop().getTrigger_policy().equals(triggerPolicy1.getId()));
361             //
362             // Set trigger policy to a new policy 
363             //
364             @SuppressWarnings("unused")
365             Policy triggerPolicy2 = builder.setTriggerPolicy(
366                     "Rebuild the VM",
367                     "Upon getting the trigger event, rebuild the VM",
368                     "APPC",
369                     new Target(TargetType.VM),
370                     "Rebuild",
371                     null,
372                     2,
373                     300);
374             // 
375             // Test set trigger policy to another existing policy
376             //
377             @SuppressWarnings("unused")
378             ControlLoop cl = builder.setTriggerPolicy(triggerPolicy1.getId());
379             assertTrue(builder.getControlLoop().getTrigger_policy().equals(triggerPolicy1.getId()));
380             //
381             // Test get trigger policy
382             //
383             assertTrue(builder.getTriggerPolicy().equals(triggerPolicy1));
384             //
385         } catch (BuilderException e) {
386             fail(e.getMessage());
387         }
388     }
389  
390     @Test
391     public void testSetTriggerPolicyNullPolicyId() throws BuilderException {
392         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
393         expectedException.expect(BuilderException.class);
394         expectedException.expectMessage("Id must not be null");
395         builder.setTriggerPolicy(null);
396     }
397     
398     @Test
399     public void testSetTriggerPolicyNoPoliciesExist() throws BuilderException {
400         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
401         final String unknownPolicyId = "100";
402         expectedException.expect(BuilderException.class);
403         expectedException.expectMessage("Unknown policy " + unknownPolicyId);
404         builder.setTriggerPolicy(unknownPolicyId);
405     }
406     
407     @Test
408     public void testSetTriggerPolicyUnknownPolicy() throws BuilderException {
409         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
410         builder.setTriggerPolicy( 
411                 "Restart the VM",
412                 "Upon getting the trigger event, restart the VM",
413                 "APPC",
414                 new Target(TargetType.VM),
415                 "Restart",
416                 null,
417                 2,
418                 300);
419         final String unknownPolicyId = "100";
420         expectedException.expect(BuilderException.class);
421         expectedException.expectMessage("Unknown policy " + unknownPolicyId);
422         builder.setTriggerPolicy(unknownPolicyId);
423     }
424     
425     @Test
426     public void testAddRemovePolicies() {
427         try {
428             ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
429             Policy triggerPolicy = builder.setTriggerPolicy(
430                     "Restart the VM",
431                     "Upon getting the trigger event, restart the VM",
432                     "APPC",
433                     new Target(TargetType.VM),
434                     "Restart",
435                     null,
436                     2,
437                     300);
438             //
439             // Test create a policy and chain it to the results of trigger policy
440             //
441             Policy onRestartFailurePolicy1 = builder.setPolicyForPolicyResult(
442                     "Rebuild VM",
443                     "If the restart fails, rebuild it.",
444                     "APPC",
445                     new Target(TargetType.VM),
446                     "Rebuild",
447                     null,
448                     1,
449                     600,
450                     triggerPolicy.getId(),
451                     PolicyResult.FAILURE,
452                     PolicyResult.FAILURE_EXCEPTION,
453                     PolicyResult.FAILURE_RETRIES,
454                     PolicyResult.FAILURE_TIMEOUT,
455                     PolicyResult.FAILURE_GUARD);
456             //
457             assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy1.getId()));
458             assertTrue(builder.getTriggerPolicy().getFailure_exception().equals(onRestartFailurePolicy1.getId()));
459             assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy1.getId()));
460             assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy1.getId()));
461             assertTrue(builder.getTriggerPolicy().getFailure_guard().equals(onRestartFailurePolicy1.getId()));
462             
463             //
464             // Test create a policy and chain it to the results of trigger policy success
465             //
466             Policy onSuccessPolicy1 = builder.setPolicyForPolicyResult(
467                     "Do something",
468                     "If the restart succeeds, do something else.",
469                     "APPC",
470                     new Target(TargetType.VM),
471                     "SomethingElse",
472                     null,
473                     1,
474                     600,
475                     triggerPolicy.getId(),
476                     PolicyResult.SUCCESS);
477             //
478             assertTrue(builder.getTriggerPolicy().getSuccess().equals(onSuccessPolicy1.getId()));
479             
480             //
481             // Test remove policy
482             //
483             boolean removed = builder.removePolicy(onRestartFailurePolicy1.getId());
484             assertTrue(removed);
485             assertTrue(builder.getTriggerPolicy().getFailure().equals(FinalResult.FINAL_FAILURE.toString()));
486             assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(FinalResult.FINAL_FAILURE_RETRIES.toString()));
487             assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString()));
488             assertTrue(builder.getTriggerPolicy().getFailure_guard().equals(FinalResult.FINAL_FAILURE_GUARD.toString()));
489             //
490             // Create another policy and chain it to the results of trigger policy
491             //
492             Policy onRestartFailurePolicy2 = builder.setPolicyForPolicyResult( 
493                     "Rebuild VM",
494                     "If the restart fails, rebuild it.",
495                     "APPC",
496                     new Target(TargetType.VM),
497                     "Rebuild",
498                     null,
499                     2,
500                     600,
501                     triggerPolicy.getId(),
502                     PolicyResult.FAILURE,
503                     PolicyResult.FAILURE_RETRIES,
504                     PolicyResult.FAILURE_TIMEOUT);
505             //
506             // Test reset policy results
507             //
508             triggerPolicy = builder.resetPolicyResults(triggerPolicy.getId());
509             assertTrue(builder.getTriggerPolicy().getFailure().equals(FinalResult.FINAL_FAILURE.toString()));
510             assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(FinalResult.FINAL_FAILURE_RETRIES.toString()));
511             assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString()));
512             //                                                               
513             // Test set the policy results to an existing operational policy
514             //
515             onRestartFailurePolicy2 = builder.setPolicyForPolicyResult(
516                     onRestartFailurePolicy2.getId(), 
517                     triggerPolicy.getId(), 
518                     PolicyResult.FAILURE,
519                     PolicyResult.FAILURE_RETRIES,
520                     PolicyResult.FAILURE_TIMEOUT);
521             assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy2.getId()));
522             assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy2.getId()));
523             assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy2.getId()));
524             //                                                               
525             // Test set the policy result for success to an existing operational policy
526             //
527             onRestartFailurePolicy2 = builder.setPolicyForPolicyResult(
528                     onRestartFailurePolicy2.getId(), 
529                     triggerPolicy.getId(), 
530                     PolicyResult.FAILURE,
531                     PolicyResult.FAILURE_EXCEPTION,
532                     PolicyResult.FAILURE_GUARD,
533                     PolicyResult.FAILURE_RETRIES,
534                     PolicyResult.FAILURE_TIMEOUT,
535                     PolicyResult.SUCCESS);
536             assertTrue(builder.getTriggerPolicy().getFailure().equals(onRestartFailurePolicy2.getId()));
537             assertTrue(builder.getTriggerPolicy().getFailure_exception().equals(onRestartFailurePolicy2.getId()));
538             assertTrue(builder.getTriggerPolicy().getFailure_guard().equals(onRestartFailurePolicy2.getId()));
539             assertTrue(builder.getTriggerPolicy().getFailure_retries().equals(onRestartFailurePolicy2.getId()));
540             assertTrue(builder.getTriggerPolicy().getFailure_timeout().equals(onRestartFailurePolicy2.getId()));
541             assertTrue(builder.getTriggerPolicy().getSuccess().equals(onRestartFailurePolicy2.getId()));
542             
543             //
544             // Test remove all existing operational policies
545             //
546             builder = builder.removeAllPolicies();
547             assertTrue(builder.getControlLoop().getTrigger_policy().equals(FinalResult.FINAL_OPENLOOP.toString()));
548             //
549         } catch (BuilderException e) {
550             fail(e.getMessage());
551         }
552     }
553     
554     @Test
555     public void testAddToUnknownPolicy() throws BuilderException {
556         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
557         final String policyId = "100";
558         expectedException.expect(BuilderException.class);
559         expectedException.expectMessage("Unknown policy " + policyId);
560          
561         builder.setPolicyForPolicyResult(
562                 "Rebuild VM",
563                 "If the restart fails, rebuild it.",
564                 "APPC",
565                 new Target(TargetType.VM),
566                 "Rebuild",
567                 null,
568                 1,
569                 600,
570                 policyId,
571                 PolicyResult.FAILURE,
572                 PolicyResult.FAILURE_RETRIES,
573                 PolicyResult.FAILURE_TIMEOUT,
574                 PolicyResult.FAILURE_GUARD);
575     }
576     
577     @Test
578     public void testAddExistingPolicyToUnknownPolicy() throws BuilderException {
579         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
580         Policy triggerPolicy = builder.setTriggerPolicy(
581                 "Restart the VM",
582                 "Upon getting the trigger event, restart the VM",
583                 "APPC",
584                 new Target(TargetType.VM),
585                 "Restart",
586                 null,
587                 2,
588                 300);
589         
590         
591         Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult(
592                 "Rebuild VM",
593                 "If the restart fails, rebuild it.",
594                 "APPC",
595                 new Target(TargetType.VM),
596                 "Rebuild",
597                 null,
598                 1,
599                 600,
600                 triggerPolicy.getId(),
601                 PolicyResult.FAILURE);
602         
603         final String unknownPolicyId = "100";
604         expectedException.expect(BuilderException.class);
605         expectedException.expectMessage(unknownPolicyId + " does not exist");
606          
607         builder.setPolicyForPolicyResult(
608                         onRestartFailurePolicy.getId(), 
609                         unknownPolicyId, 
610                 PolicyResult.FAILURE);
611     }
612     
613     @Test
614     public void testAddUnknownExistingPolicyToPolicy() throws BuilderException {
615         ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
616         Policy triggerPolicy = builder.setTriggerPolicy(
617                 "Restart the VM",
618                 "Upon getting the trigger event, restart the VM",
619                 "APPC",
620                 new Target(TargetType.VM),
621                 "Restart",
622                 null,
623                 2,
624                 300);
625         
626         final String unknownPolicyId = "100";
627         expectedException.expect(BuilderException.class);
628         expectedException.expectMessage("Operational policy " + unknownPolicyId + " does not exist");
629          
630         builder.setPolicyForPolicyResult(
631                         unknownPolicyId, 
632                         triggerPolicy.getId(), 
633                 PolicyResult.FAILURE);
634     }
635
636     @Test
637     public void testAddOperationsAccumulateParams() {
638         try {
639             ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
640             Policy triggerPolicy = builder.setTriggerPolicy(
641                     "Restart the eNodeB",
642                     "Upon getting the trigger event, restart the eNodeB",
643                     "RANController",
644                     new Target(TargetType.PNF),
645                     "Restart",
646                     null,
647                     2,
648                     300);
649             //
650             // Add the operationsAccumulateParams
651             //
652             triggerPolicy = builder.addOperationsAccumulateParams(triggerPolicy.getId(), new OperationsAccumulateParams("15m", 5));
653             assertNotNull(builder.getTriggerPolicy().getOperationsAccumulateParams());
654             assertTrue(builder.getTriggerPolicy().getOperationsAccumulateParams().getPeriod().equals("15m"));
655             assertTrue(builder.getTriggerPolicy().getOperationsAccumulateParams().getLimit() == 5);
656             //
657         } catch (BuilderException e) {
658             fail(e.getMessage());
659         }
660     }
661     
662     
663     @Test
664     public void testBuildSpecification() {
665         try {
666             //
667             // Create the builder
668             //
669             ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 800);
670             //
671             // Set the first invalid trigger policy
672             //
673             Policy policy1 = builder.setTriggerPolicy(
674                     "Restart the VM",
675                     "Upon getting the trigger event, restart the VM",
676                     null,
677                     null,
678                     "Instantiate",
679                     null,
680                     2,
681                     300);
682             Results results = builder.buildSpecification();
683             //
684             // Check that ERRORs are in results for invalid policy arguments
685             //
686             boolean invalid_actor = false;
687             boolean invalid_recipe = false;
688             boolean invalid_target = false;
689             for (Message m : results.getMessages()) {
690                 if (m.getMessage().equals("Policy actor is null") && m.getLevel() == MessageLevel.ERROR) {
691                     invalid_actor = true;
692                 }
693                 if (m.getMessage().equals("Policy recipe is invalid") && m.getLevel() == MessageLevel.ERROR) {
694                     invalid_recipe = true;
695                 }
696                 if (m.getMessage().equals("Policy target is null") && m.getLevel() == MessageLevel.ERROR) {
697                     invalid_target = true;
698                 }
699             }
700             //
701             assertTrue(invalid_actor);
702             assertTrue(invalid_recipe);
703             assertTrue(invalid_target);
704             //
705             // Remove the invalid policy
706             //
707             //@SuppressWarnings("unused")
708             boolean removed = builder.removePolicy(policy1.getId());
709             assertTrue(removed);
710             assertTrue(builder.getTriggerPolicy() == null);
711             //
712             // Set a valid trigger policy
713             //
714             policy1 = builder.setTriggerPolicy(
715                     "Rebuild VM",
716                     "If the restart fails, rebuild it.",
717                     "APPC",
718                     new Target(TargetType.VM),
719                     "Rebuild",
720                     null,
721                     1,
722                     600);
723             //
724             // Set a second valid trigger policy
725             //
726             Policy policy2 = builder.setTriggerPolicy(
727                     "Restart the VM",
728                     "Upon getting the trigger event, restart the VM",
729                     "APPC",
730                     new Target(TargetType.VM),
731                     "Restart",
732                     null,
733                     2,
734                     300);
735             //
736             // Now, we have policy1 unreachable
737             //
738             results = builder.buildSpecification();
739             boolean unreachable = false;
740             for (Message m : results.getMessages()) {
741                 if (m.getMessage().equals("Policy " + policy1.getId() + " is not reachable.") && m.getLevel() == MessageLevel.WARNING) {
742                     unreachable = true;
743                     break;
744                 }
745             }
746             assertTrue(unreachable);
747             //
748             // Set policy1 for the failure results of policy2
749             //
750             policy1 = builder.setPolicyForPolicyResult(
751                     policy1.getId(), 
752                     policy2.getId(),
753                     PolicyResult.FAILURE,
754                     PolicyResult.FAILURE_RETRIES,
755                     PolicyResult.FAILURE_TIMEOUT);
756             results = builder.buildSpecification();
757             boolean invalid_timeout = false;
758             for (Message m : results.getMessages()) {
759                 if (m.getMessage().equals("controlLoop overall timeout is less than the sum of operational policy timeouts.") && m.getLevel() == MessageLevel.ERROR) {
760                     invalid_timeout = true;
761                     break;
762                 }
763             }
764             assertTrue(invalid_timeout);
765             //
766             // Remove policy2 (revert controlLoop back to open loop) 
767             //
768             removed = builder.removePolicy(policy2.getId());
769             //
770             // ControlLoop is open loop now, but it still has policies (policy1)
771             //
772             results = builder.buildSpecification();
773             unreachable = false;
774             for (Message m : results.getMessages()) {
775                 if (m.getMessage().equals("Open Loop policy contains policies. The policies will never be invoked.") && m.getLevel() == MessageLevel.WARNING) {
776                     unreachable = true;
777                     break;
778                 }
779             }
780             assertTrue(unreachable);
781             //
782         } catch (BuilderException e) {
783             fail(e.getMessage());
784         }
785     }
786     
787     
788     @Test
789     public void test() {
790         this.test("src/test/resources/v1.0.0/policy_Test.yaml");
791     }
792     
793     @Test
794     public void testEvilYaml() {
795         try (InputStream is = new FileInputStream(new File("src/test/resources/v1.0.0/test_evil.yaml"))) {
796             //
797             // Read the yaml into our Java Object
798             //
799             Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
800             yaml.load(is);
801         } catch (FileNotFoundException e) {
802             fail(e.getLocalizedMessage());
803         } catch (IOException e) {
804             fail(e.getLocalizedMessage());
805         } catch (YAMLException e) {
806             //
807             // Should have this
808             //
809         }
810     }
811     
812     public void test(String testFile) {
813         try (InputStream is = new FileInputStream(new File(testFile))) {
814             //
815             // Read the yaml into our Java Object
816             //
817             Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
818             Object obj = yaml.load(is);
819             assertNotNull(obj);
820             assertTrue(obj instanceof ControlLoopPolicy);
821             ControlLoopPolicy policyTobuild = (ControlLoopPolicy) obj;
822             //
823             // Now we're going to try to use the builder to build this.
824             //
825             ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(
826                     policyTobuild.getControlLoop().getControlLoopName(),
827                     policyTobuild.getControlLoop().getTimeout());
828             //
829             // Add services
830             //
831             if (policyTobuild.getControlLoop().getServices() != null) {
832                 builder = builder.addService(policyTobuild.getControlLoop().getServices().toArray(new Service[policyTobuild.getControlLoop().getServices().size()]));
833             }
834             //
835             // Add resources
836             //
837             if (policyTobuild.getControlLoop().getResources() != null) {
838                 builder = builder.addResource(policyTobuild.getControlLoop().getResources().toArray(new Resource[policyTobuild.getControlLoop().getResources().size()]));
839             }
840             //
841             // Set pnf
842             //
843             if (policyTobuild.getControlLoop().getPnf() != null) {
844                 builder = builder.setPNF(policyTobuild.getControlLoop().getPnf());
845             }
846             //
847             // Add the policies and be sure to set the trigger policy
848             //
849             if (policyTobuild.getPolicies() != null) {
850                 for (Policy policy : policyTobuild.getPolicies()) {
851                     if (policy.getId() == policyTobuild.getControlLoop().getTrigger_policy()) {
852                         builder.setTriggerPolicy(policy.getName(), policy.getDescription(), policy.getActor(), policy.getTarget(), policy.getRecipe(), null, policy.getRetry(), policy.getTimeout());
853                     }
854                 }
855             }
856         
857             // Question : how to change policy ID and results by using builder ??
858         
859             @SuppressWarnings("unused")
860             Results results = builder.buildSpecification();
861             
862         } catch (FileNotFoundException e) {
863             fail(e.getLocalizedMessage());
864         } catch (IOException e) {
865             fail(e.getLocalizedMessage());
866         } catch (BuilderException e) {
867             fail(e.getLocalizedMessage());
868         }
869         
870     }
871
872 }