aboutsummaryrefslogtreecommitdiffstats
path: root/controlloop/common/policy-yaml/src/main/java/org/onap/policy/controlloop/compiler/ControlLoopCompiler.java
blob: f18752a62b6f5b5a0049790d500755c306b02318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
/*-
 * ============LICENSE_START=======================================================
 * policy-yaml
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.controlloop.compiler;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import java.io.InputStream;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.jgrapht.DirectedGraph;
import org.jgrapht.graph.ClassBasedEdgeFactory;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.DirectedMultigraph;
import org.onap.policy.controlloop.policy.ControlLoop;
import org.onap.policy.controlloop.policy.ControlLoopPolicy;
import org.onap.policy.controlloop.policy.FinalResult;
import org.onap.policy.controlloop.policy.Policy;
import org.onap.policy.controlloop.policy.PolicyResult;
import org.onap.policy.controlloop.policy.TargetType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;


public class ControlLoopCompiler implements Serializable {
    private static final String OPERATION_POLICY = "Operation Policy ";
    private static final long serialVersionUID = 1L;
    private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopCompiler.class.getName());
    
    /**
     * Compiles the policy from an object.
     */
    public static ControlLoopPolicy compile(ControlLoopPolicy policy, 
                    ControlLoopCompilerCallback callback) throws CompilerException {
        //
        // Ensure the control loop is sane
        //
        validateControlLoop(policy.getControlLoop(), callback);
        //
        // Validate the policies
        //
        validatePolicies(policy, callback);
        
        return policy;
    }
    
    /**
     * Compiles the policy from an input stream.
     * 
     * @param yamlSpecification the yaml input stream
     * @param callback method to callback during compilation
     * @return Control Loop object
     * @throws CompilerException throws any compile exception found
     */
    public static ControlLoopPolicy compile(InputStream yamlSpecification, 
                    ControlLoopCompilerCallback callback) throws CompilerException {
        Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
        Object obj = yaml.load(yamlSpecification);
        if (obj == null) {
            throw new CompilerException("Could not parse yaml specification.");
        }
        if (! (obj instanceof ControlLoopPolicy)) {
            throw new CompilerException("Yaml could not parse specification into required ControlLoopPolicy object");
        }
        return ControlLoopCompiler.compile((ControlLoopPolicy) obj, callback);
    }
    
    private static void validateControlLoop(ControlLoop controlLoop, 
                    ControlLoopCompilerCallback callback) throws CompilerException {
        if (controlLoop == null && callback != null) {
            callback.onError("controlLoop cannot be null");
        }
        if (controlLoop != null) {
            if ((controlLoop.getControlLoopName() == null || controlLoop.getControlLoopName().length() < 1) 
                            && callback != null) {
                callback.onError("Missing controlLoopName");
            }
            if ((!controlLoop.getVersion().contentEquals(ControlLoop.getCompilerVersion())) && callback != null) {
                callback.onError("Unsupported version for this compiler");
            }
            if (controlLoop.getTrigger_policy() == null || controlLoop.getTrigger_policy().length() < 1) {
                throw new CompilerException("trigger_policy is not valid");
            }
        }
    }

    private static void validatePolicies(ControlLoopPolicy policy, 
                    ControlLoopCompilerCallback callback) throws CompilerException {
        if (policy == null) {
            throw new CompilerException("policy cannot be null");
        }
        if (policy.getPolicies() == null) {
            callback.onWarning("controlLoop is an open loop.");   
        } else {
            //
            // For this version we can use a directed multigraph, in the future we may not be able to
            //
            DirectedGraph<NodeWrapper, LabeledEdge> graph = 
                            new DirectedMultigraph<>(new ClassBasedEdgeFactory<NodeWrapper, 
                                            LabeledEdge>(LabeledEdge.class));
            //
            // Check to see if the trigger Event is for OpenLoop, we do so by
            // attempting to create a FinalResult object from it. If its a policy id, this should
            // return null.
            //
            FinalResult triggerResult = FinalResult.toResult(policy.getControlLoop().getTrigger_policy());
            TriggerNodeWrapper triggerNode;
            //
            // Did this turn into a FinalResult object?
            //
            if (triggerResult != null) {
                validateOpenLoopPolicy(policy, triggerResult, callback);
                return;
                //
            } else {
                validatePoliciesContainTriggerPolicyAndCombinedTimeoutIsOk(policy, callback);
                triggerNode = new TriggerNodeWrapper(policy.getControlLoop().getControlLoopName());
            }
            //
            // Add in the trigger node
            //
            graph.addVertex(triggerNode);
            //
            // Add in our Final Result nodes. All paths should end to these nodes.
            //
            FinalResultNodeWrapper finalSuccess = new FinalResultNodeWrapper(FinalResult.FINAL_SUCCESS);
            FinalResultNodeWrapper finalFailure = new FinalResultNodeWrapper(FinalResult.FINAL_FAILURE);
            FinalResultNodeWrapper finalFailureTimeout = new FinalResultNodeWrapper(FinalResult.FINAL_FAILURE_TIMEOUT);
            FinalResultNodeWrapper finalFailureRetries = new FinalResultNodeWrapper(FinalResult.FINAL_FAILURE_RETRIES);
            FinalResultNodeWrapper finalFailureException = 
                            new FinalResultNodeWrapper(FinalResult.FINAL_FAILURE_EXCEPTION);
            FinalResultNodeWrapper finalFailureGuard = new FinalResultNodeWrapper(FinalResult.FINAL_FAILURE_GUARD);
            graph.addVertex(finalSuccess);
            graph.addVertex(finalFailure);
            graph.addVertex(finalFailureTimeout);
            graph.addVertex(finalFailureRetries);
            graph.addVertex(finalFailureException);
            graph.addVertex(finalFailureGuard);
            //
            // Work through the policies and add them in as nodes.
            //
            Map<Policy, PolicyNodeWrapper> mapNodes = addPoliciesAsNodes(policy, graph, triggerNode, callback);
            //
            // last sweep to connect remaining edges for policy results
            //
            for (Policy operPolicy : policy.getPolicies()) {
                PolicyNodeWrapper node = mapNodes.get(operPolicy);
                //
                // Just ensure this has something
                //
                if (node == null) {
                    continue;
                }
                addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getSuccess(), finalSuccess, 
                                PolicyResult.SUCCESS, node);
                addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure(), finalFailure, 
                                PolicyResult.FAILURE, node);
                addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure_timeout(), finalFailureTimeout, 
                                PolicyResult.FAILURE_TIMEOUT, node);
                addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure_retries(), finalFailureRetries, 
                                PolicyResult.FAILURE_RETRIES, node);
                addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure_exception(), finalFailureException, 
                                PolicyResult.FAILURE_EXCEPTION, node);
                addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure_guard(), finalFailureGuard, 
                                PolicyResult.FAILURE_GUARD, node);
            }
            validateNodesAndEdges(graph, callback);
        }   
    }
    
    private static void validateOpenLoopPolicy(ControlLoopPolicy policy, FinalResult triggerResult, 
                    ControlLoopCompilerCallback callback) throws CompilerException {
        //
        // Ensure they didn't use some other FinalResult code
        //
        if (triggerResult != FinalResult.FINAL_OPENLOOP) {
            throw new CompilerException("Unexpected Final Result for trigger_policy, should only be " 
        + FinalResult.FINAL_OPENLOOP.toString() + " or a valid Policy ID");
        }
        //
        // They really shouldn't have any policies attached.
        //
        if ((policy.getPolicies() != null || policy.getPolicies().isEmpty()) && callback != null ) {
            callback.onWarning("Open Loop policy contains policies. The policies will never be invoked.");
        }
    }
    
    private static void validatePoliciesContainTriggerPolicyAndCombinedTimeoutIsOk(ControlLoopPolicy policy, 
                    ControlLoopCompilerCallback callback) throws CompilerException {
        int sum = 0;
        boolean triggerPolicyFound = false;
        for (Policy operPolicy : policy.getPolicies()) {
            sum += operPolicy.getTimeout().intValue();
            if (policy.getControlLoop().getTrigger_policy().equals(operPolicy.getId())) {
                triggerPolicyFound = true;
            }
        }
        if (policy.getControlLoop().getTimeout().intValue() < sum && callback != null) {
            callback.onError("controlLoop overall timeout is less than the sum of operational policy timeouts.");
        }
        
        if (!triggerPolicyFound) {
            throw new CompilerException("Unexpected value for trigger_policy, should only be " 
        + FinalResult.FINAL_OPENLOOP.toString() + " or a valid Policy ID");
        }
    }
    
    private static Map<Policy, PolicyNodeWrapper> addPoliciesAsNodes(ControlLoopPolicy policy, 
            DirectedGraph<NodeWrapper, LabeledEdge> graph, TriggerNodeWrapper triggerNode, 
            ControlLoopCompilerCallback callback) {
        Map<Policy, PolicyNodeWrapper> mapNodes = new HashMap<>();
        for (Policy operPolicy : policy.getPolicies()) {
            //
            // Is it still ok to add?
            //
            if (!okToAdd(operPolicy, callback)) {
                //
                // Do not add it in
                //
                continue;
            }
            //
            // Create wrapper policy node and save it into our map so we can
            // easily retrieve it.
            //
            PolicyNodeWrapper node = new PolicyNodeWrapper(operPolicy);
            mapNodes.put(operPolicy, node);
            graph.addVertex(node);
            //
            // Is this the trigger policy?
            //
            if (operPolicy.getId().equals(policy.getControlLoop().getTrigger_policy())) {
                //
                // Yes add an edge from our trigger event node to this policy
                //
                graph.addEdge(triggerNode, node, new LabeledEdge(triggerNode, node, new TriggerEdgeWrapper("ONSET")));
            }
        }
        return mapNodes;
    }
    
    private static void addEdge(DirectedGraph<NodeWrapper, LabeledEdge> graph, Map<Policy, PolicyNodeWrapper> mapNodes,
                    String policyId, String connectedPolicy, 
                    FinalResultNodeWrapper finalResultNodeWrapper, 
                    PolicyResult policyResult, NodeWrapper node) throws CompilerException {
        FinalResult finalResult = FinalResult.toResult(finalResultNodeWrapper.getId());
        if (FinalResult.isResult(connectedPolicy, finalResult)) {
            graph.addEdge(node, finalResultNodeWrapper, new LabeledEdge(node, finalResultNodeWrapper, 
                            new FinalResultEdgeWrapper(finalResult)));
        } else {
            PolicyNodeWrapper toNode = findPolicyNode(mapNodes, connectedPolicy);
            if (toNode == null) {
                throw new CompilerException(OPERATION_POLICY + policyId + " is connected to unknown policy " 
            + connectedPolicy);
            } else {
                graph.addEdge(node, toNode, new LabeledEdge(node, toNode, new PolicyResultEdgeWrapper(policyResult)));
            }
        }
    }
    
    private static void validateNodesAndEdges(DirectedGraph<NodeWrapper, LabeledEdge> graph, 
                    ControlLoopCompilerCallback callback) throws CompilerException {
        for (NodeWrapper node : graph.vertexSet()) {
            if (node instanceof TriggerNodeWrapper) {
                validateTriggerNodeWrapper(graph, node);
            } else if (node instanceof FinalResultNodeWrapper) {
                validateFinalResultNodeWrapper(graph, node);
            } else if (node instanceof PolicyNodeWrapper) {
                validatePolicyNodeWrapper(graph, node, callback);
            }
            for (LabeledEdge edge : graph.outgoingEdgesOf(node)) {
                LOGGER.info("{} invokes {} upon {}", edge.from.getId(), edge.to.getId(), edge.edge.getId());
            }
        }
    }
    
    private static void validateTriggerNodeWrapper(DirectedGraph<NodeWrapper, LabeledEdge> graph, 
                    NodeWrapper node) throws CompilerException {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.info("Trigger Node {}", node);
        }
        if (graph.inDegreeOf(node) > 0 ) {
            //
            // Really should NEVER get here unless someone messed up the code above.
            //
            throw new CompilerException("No inputs to event trigger");
        }
        //
        // Should always be 1, except in the future we may support multiple events
        //
        if (graph.outDegreeOf(node) > 1) {
            throw new CompilerException("The event trigger should only go to ONE node");
        }
    }
    
    private static void validateFinalResultNodeWrapper(DirectedGraph<NodeWrapper, LabeledEdge> graph, 
                    NodeWrapper node) throws CompilerException {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.info("FinalResult Node {}", node);
        }
        //
        // FinalResult nodes should NEVER have an out edge
        //
        if (graph.outDegreeOf(node) > 0) {
            throw new CompilerException("FinalResult nodes should never have any out edges.");
        }
    }
    
    private static void validatePolicyNodeWrapper(DirectedGraph<NodeWrapper, LabeledEdge> graph, 
                    NodeWrapper node, ControlLoopCompilerCallback callback) throws CompilerException {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.info("Policy Node {}", node);
        }
        //
        // All Policy Nodes should have the 5 out degrees defined.
        //
        if (graph.outDegreeOf(node) != 6) {
            throw new CompilerException("Policy node should ALWAYS have 6 out degrees.");
        }
        //
        // All Policy Nodes should have at least 1 in degrees 
        // 
        if (graph.inDegreeOf(node) == 0 && callback != null) {
            callback.onWarning("Policy " + node.getId() + " is not reachable.");
        }
    }
    
    private static boolean okToAdd(Policy operPolicy, ControlLoopCompilerCallback callback) {
        boolean isOk = isPolicyIdOk(operPolicy, callback);
        if (! isActorOk(operPolicy, callback)) {
            isOk = false;
        }
        if (! isRecipeOk(operPolicy, callback)) {
            isOk = false;
        }
        if (! isTargetOk(operPolicy, callback) ) {
            isOk = false;
        }
        if (! arePolicyResultsOk(operPolicy, callback) ) {
            isOk = false;
        }
        return isOk;
    }
    
    private static boolean isPolicyIdOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
        boolean isOk = true;
        if (operPolicy.getId() == null || operPolicy.getId().length() < 1) {
            if (callback != null) {
                callback.onError("Operational Policy has an bad ID");
            }
            isOk = false;
        } else {
            //
            // Check if they decided to make the ID a result object
            //
            if (PolicyResult.toResult(operPolicy.getId()) != null) {
                if (callback != null) {
                    callback.onError("Policy id is set to a PolicyResult " + operPolicy.getId());
                }
                isOk = false;
            }
            if (FinalResult.toResult(operPolicy.getId()) != null) {
                if (callback != null) {
                    callback.onError("Policy id is set to a FinalResult " + operPolicy.getId());
                }
                isOk = false;
            }
        }
        return isOk;
    }
    
    private static boolean isActorOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
        boolean isOk = true;
        if (operPolicy.getActor() == null) {
            if (callback != null) {
                callback.onError("Policy actor is null");
            }
            isOk = false;
        }
        //
        // Construct a list for all valid actors
        //
        ImmutableList<String> actors = ImmutableList.of("APPC", "AOTS", "MSO", "SDNO", "SDNR", "AAI");
        //
        if (operPolicy.getActor() != null && (!actors.contains(operPolicy.getActor())) ) {
            if (callback != null) {
                callback.onError("Policy actor is invalid");
            }
            isOk = false;
        }
        return isOk;
    }
    
    private static boolean isRecipeOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
        boolean isOk = true;
        if (operPolicy.getRecipe() == null) {
            if (callback != null) {
                callback.onError("Policy recipe is null");
            }
            isOk = false;
        }
        //
        // NOTE: We need a way to find the acceptable recipe values (either Enum or a database that has these)
        // 
        ImmutableMap<String, List<String>> recipes = new ImmutableMap.Builder<String, List<String>>()
                .put("APPC", ImmutableList.of("Restart", "Rebuild", "Migrate", "ModifyConfig"))
                .put("AOTS", ImmutableList.of("checkMaintenanceWindow", 
                                "checkENodeBTicketHours", 
                                "checkEquipmentStatus", 
                                "checkEimStatus", 
                                "checkEquipmentMaintenance"))
                .put("MSO", ImmutableList.of("VF Module Create"))
                .put("SDNO", ImmutableList.of("health-diagnostic-type", 
                                "health-diagnostic", 
                                "health-diagnostic-history", 
                                "health-diagnostic-commands", 
                                "health-diagnostic-aes"))
                .put("SDNR", ImmutableList.of("Restart", "Reboot"))
                .build();
        //
        if (operPolicy.getRecipe() != null 
                        && (!recipes.getOrDefault(operPolicy.getActor(), 
                                        Collections.emptyList()).contains(operPolicy.getRecipe()))) {
            if (callback != null) {
                callback.onError("Policy recipe is invalid");
            }
            isOk = false;
        }
        return isOk;
    }
    
    private static boolean isTargetOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
        boolean isOk = true;
        if (operPolicy.getTarget() == null) {
            if (callback != null) {
                callback.onError("Policy target is null");
            }
            isOk = false;
        }
        if (operPolicy.getTarget() != null 
                        && operPolicy.getTarget().getType() != TargetType.VM 
                        && operPolicy.getTarget().getType() != TargetType.VFC 
                        && operPolicy.getTarget().getType() != TargetType.PNF) {
            if (callback != null) {
                callback.onError("Policy target is invalid");
            }
            isOk = false;
        }
        return isOk;
    }
    
    private static boolean arePolicyResultsOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
        //
        // Check that policy results are connected to either default final * or another policy
        //
        boolean isOk = isSuccessPolicyResultOk(operPolicy, callback);
        if (! isFailurePolicyResultOk(operPolicy, callback) ) {
            isOk = false;
        }
        if (! isFailureRetriesPolicyResultOk(operPolicy, callback) ) {
            isOk = false;
        }
        if (! isFailureTimeoutPolicyResultOk(operPolicy, callback) ) {
            isOk = false;
        }
        if (! isFailureExceptionPolicyResultOk(operPolicy, callback) ) {
            isOk = false;
        }
        if (! isFailureGuardPolicyResultOk(operPolicy, callback) ) {
            isOk = false;
        }
        return isOk;
    }
    
    private static boolean isSuccessPolicyResultOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
        boolean isOk = true;
        if (FinalResult.toResult(operPolicy.getSuccess()) != null 
                        && !operPolicy.getSuccess().equals(FinalResult.FINAL_SUCCESS.toString())) {
            if (callback != null) {
                callback.onError("Policy success is neither another policy nor FINAL_SUCCESS");
            }
            isOk = false;
        }
        return isOk;
    }
    
    private static boolean isFailurePolicyResultOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
        boolean isOk = true;
        if (FinalResult.toResult(operPolicy.getFailure()) != null 
                        && !operPolicy.getFailure().equals(FinalResult.FINAL_FAILURE.toString())) {
            if (callback != null) {
                callback.onError("Policy failure is neither another policy nor FINAL_FAILURE");
            }
            isOk = false;
        }
        return isOk;
    }
    
    private static boolean isFailureRetriesPolicyResultOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
        boolean isOk = true;
        if (FinalResult.toResult(operPolicy.getFailure_retries()) != null 
                        && !operPolicy.getFailure_retries().equals(FinalResult.FINAL_FAILURE_RETRIES.toString())) {
            if (callback != null) {
                callback.onError("Policy failure retries is neither another policy nor FINAL_FAILURE_RETRIES");
            }
            isOk = false;
        }
        return isOk;
    }
    
    private static boolean isFailureTimeoutPolicyResultOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
        boolean isOk = true;
        if (FinalResult.toResult(operPolicy.getFailure_timeout()) != null 
                        && !operPolicy.getFailure_timeout().equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString())) {
            if (callback != null) {
                callback.onError("Policy failure timeout is neither another policy nor FINAL_FAILURE_TIMEOUT");
            }
            isOk = false;
        }
        return isOk;
    }
    
    private static boolean isFailureExceptionPolicyResultOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
        boolean isOk = true;
        if (FinalResult.toResult(operPolicy.getFailure_exception()) != null 
                        && !operPolicy.getFailure_exception().equals(FinalResult.FINAL_FAILURE_EXCEPTION.toString())) {
            if (callback != null) {
                callback.onError("Policy failure exception is neither another policy nor FINAL_FAILURE_EXCEPTION");
            }
            isOk = false;
        }
        return isOk;
    }
    
    private static boolean isFailureGuardPolicyResultOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
        boolean isOk = true;
        if (FinalResult.toResult(operPolicy.getFailure_guard()) != null 
                        && !operPolicy.getFailure_guard().equals(FinalResult.FINAL_FAILURE_GUARD.toString())) {
            if (callback != null) {
                callback.onError("Policy failure guard is neither another policy nor FINAL_FAILURE_GUARD");
            }
            isOk = false;
        }
        return isOk;
    }

    private static PolicyNodeWrapper findPolicyNode(Map<Policy, PolicyNodeWrapper> mapNodes, String id) {
        for (Entry<Policy, PolicyNodeWrapper> entry : mapNodes.entrySet()) {
            if (entry.getKey().getId().equals(id)) {
                return entry.getValue();
            }
        }
        return null;
    }
    
    @FunctionalInterface
    private interface NodeWrapper extends Serializable {
        public String   getId();
    }
    
    private static class TriggerNodeWrapper implements NodeWrapper {
        private static final long serialVersionUID = -187644087811478349L;
        private String closedLoopControlName;
        
        public TriggerNodeWrapper(String closedLoopControlName) {
            this.closedLoopControlName = closedLoopControlName;
        }

        @Override
        public String toString() {
            return "TriggerNodeWrapper [closedLoopControlName=" + closedLoopControlName + "]";
        }

        @Override
        public String getId() {
            return closedLoopControlName;
        }
        
    }
        
    private static class FinalResultNodeWrapper implements NodeWrapper {
        private static final long serialVersionUID = 8540008796302474613L;
        private FinalResult result;

        public FinalResultNodeWrapper(FinalResult result) {
            this.result = result;
        }

        @Override
        public String toString() {
            return "FinalResultNodeWrapper [result=" + result + "]";
        }

        @Override
        public String getId() {
            return result.toString();
        }
    }
    
    private static class PolicyNodeWrapper implements NodeWrapper {
        private static final long serialVersionUID = 8170162175653823082L;
        private transient Policy policy;
        
        public PolicyNodeWrapper(Policy operPolicy) {
            this.policy = operPolicy;
        }

        @Override
        public String toString() {
            return "PolicyNodeWrapper [policy=" + policy + "]";
        }

        @Override
        public String getId() {
            return policy.getId();
        }
    }
    
    @FunctionalInterface
    private interface EdgeWrapper extends Serializable {
        public String getId();
        
    }
    
    private static class TriggerEdgeWrapper implements EdgeWrapper {
        private static final long serialVersionUID = 2678151552623278863L;
        private String trigger;
        
        public TriggerEdgeWrapper(String trigger) {
            this.trigger = trigger;
        }

        @Override
        public String getId() {
            return trigger;
        }

        @Override
        public String toString() {
            return "TriggerEdgeWrapper [trigger=" + trigger + "]";
        }
        
    }
    
    private static class PolicyResultEdgeWrapper implements EdgeWrapper {
        private static final long serialVersionUID = 6078569477021558310L;
        private PolicyResult policyResult;

        public PolicyResultEdgeWrapper(PolicyResult policyResult) {
            super();
            this.policyResult = policyResult;
        }

        @Override
        public String toString() {
            return "PolicyResultEdgeWrapper [policyResult=" + policyResult + "]";
        }

        @Override
        public String getId() {
            return policyResult.toString();
        }
        
        
    }
    
    private static class FinalResultEdgeWrapper implements EdgeWrapper {
        private static final long serialVersionUID = -1486381946896779840L;
        private FinalResult finalResult;
        
        public FinalResultEdgeWrapper(FinalResult result) {
            this.finalResult = result;
        }

        @Override
        public String toString() {
            return "FinalResultEdgeWrapper [finalResult=" + finalResult + "]";
        }
        
        @Override
        public String getId() {
            return finalResult.toString();
        }
    }
    
    
    private static class LabeledEdge extends DefaultEdge {
        private static final long serialVersionUID = 579384429573385524L;
        
        private NodeWrapper from;
        private NodeWrapper to;
        private EdgeWrapper edge;
        
        public LabeledEdge(NodeWrapper from, NodeWrapper to, EdgeWrapper edge) {
            this.from = from;
            this.to = to;
            this.edge = edge;
        }
        
        @SuppressWarnings("unused")
        public NodeWrapper from() {
            return from;
        }
        
        @SuppressWarnings("unused")
        public NodeWrapper to() {
            return to;
        }
        
        @SuppressWarnings("unused")
        public EdgeWrapper edge() {
            return edge;
        }

        @Override
        public String toString() {
            return "LabeledEdge [from=" + from + ", to=" + to + ", edge=" + edge + "]";
        }
    }

}