76c6f8f145ce71f5d3e6c41c236901252eb9fc1c
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-yaml
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.compiler;
22
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableMap;
25
26 import java.io.InputStream;
27 import java.io.Serializable;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Map.Entry;
33
34 import org.jgrapht.DirectedGraph;
35 import org.jgrapht.graph.ClassBasedEdgeFactory;
36 import org.jgrapht.graph.DefaultEdge;
37 import org.jgrapht.graph.DirectedMultigraph;
38 import org.onap.policy.controlloop.policy.ControlLoop;
39 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
40 import org.onap.policy.controlloop.policy.FinalResult;
41 import org.onap.policy.controlloop.policy.Policy;
42 import org.onap.policy.controlloop.policy.PolicyResult;
43 import org.onap.policy.controlloop.policy.TargetType;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.yaml.snakeyaml.Yaml;
47 import org.yaml.snakeyaml.constructor.Constructor;
48
49
50 public class ControlLoopCompiler implements Serializable {
51     private static final String OPERATION_POLICY = "Operation Policy ";
52     private static final long serialVersionUID = 1L;
53     private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopCompiler.class.getName());
54     
55     /**
56      * Compiles the policy from an object.
57      */
58     public static ControlLoopPolicy compile(ControlLoopPolicy policy, 
59                     ControlLoopCompilerCallback callback) throws CompilerException {
60         //
61         // Ensure the control loop is sane
62         //
63         validateControlLoop(policy.getControlLoop(), callback);
64         //
65         // Validate the policies
66         //
67         validatePolicies(policy, callback);
68         
69         return policy;
70     }
71     
72     /**
73      * Compiles the policy from an input stream.
74      * 
75      * @param yamlSpecification the yaml input stream
76      * @param callback method to callback during compilation
77      * @return Control Loop object
78      * @throws CompilerException throws any compile exception found
79      */
80     public static ControlLoopPolicy compile(InputStream yamlSpecification, 
81                     ControlLoopCompilerCallback callback) throws CompilerException {
82         Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
83         Object obj = yaml.load(yamlSpecification);
84         if (obj == null) {
85             throw new CompilerException("Could not parse yaml specification.");
86         }
87         if (! (obj instanceof ControlLoopPolicy)) {
88             throw new CompilerException("Yaml could not parse specification into required ControlLoopPolicy object");
89         }
90         return ControlLoopCompiler.compile((ControlLoopPolicy) obj, callback);
91     }
92     
93     private static void validateControlLoop(ControlLoop controlLoop, 
94                     ControlLoopCompilerCallback callback) throws CompilerException {
95         if (controlLoop == null && callback != null) {
96             callback.onError("controlLoop cannot be null");
97         }
98         if (controlLoop != null) {
99             if ((controlLoop.getControlLoopName() == null || controlLoop.getControlLoopName().length() < 1) 
100                             && callback != null) {
101                 callback.onError("Missing controlLoopName");
102             }
103             if ((!controlLoop.getVersion().contentEquals(ControlLoop.getCompilerVersion())) && callback != null) {
104                 callback.onError("Unsupported version for this compiler");
105             }
106             if (controlLoop.getTrigger_policy() == null || controlLoop.getTrigger_policy().length() < 1) {
107                 throw new CompilerException("trigger_policy is not valid");
108             }
109         }
110     }
111
112     private static void validatePolicies(ControlLoopPolicy policy, 
113                     ControlLoopCompilerCallback callback) throws CompilerException {
114         if (policy == null) {
115             throw new CompilerException("policy cannot be null");
116         }
117         if (policy.getPolicies() == null) {
118             callback.onWarning("controlLoop is an open loop.");   
119         } else {
120             //
121             // For this version we can use a directed multigraph, in the future we may not be able to
122             //
123             DirectedGraph<NodeWrapper, LabeledEdge> graph = 
124                             new DirectedMultigraph<>(new ClassBasedEdgeFactory<NodeWrapper, 
125                                             LabeledEdge>(LabeledEdge.class));
126             //
127             // Check to see if the trigger Event is for OpenLoop, we do so by
128             // attempting to create a FinalResult object from it. If its a policy id, this should
129             // return null.
130             //
131             FinalResult triggerResult = FinalResult.toResult(policy.getControlLoop().getTrigger_policy());
132             TriggerNodeWrapper triggerNode;
133             //
134             // Did this turn into a FinalResult object?
135             //
136             if (triggerResult != null) {
137                 validateOpenLoopPolicy(policy, triggerResult, callback);
138                 return;
139                 //
140             } else {
141                 validatePoliciesContainTriggerPolicyAndCombinedTimeoutIsOk(policy, callback);
142                 triggerNode = new TriggerNodeWrapper(policy.getControlLoop().getControlLoopName());
143             }
144             //
145             // Add in the trigger node
146             //
147             graph.addVertex(triggerNode);
148             //
149             // Add in our Final Result nodes. All paths should end to these nodes.
150             //
151             FinalResultNodeWrapper finalSuccess = new FinalResultNodeWrapper(FinalResult.FINAL_SUCCESS);
152             FinalResultNodeWrapper finalFailure = new FinalResultNodeWrapper(FinalResult.FINAL_FAILURE);
153             FinalResultNodeWrapper finalFailureTimeout = new FinalResultNodeWrapper(FinalResult.FINAL_FAILURE_TIMEOUT);
154             FinalResultNodeWrapper finalFailureRetries = new FinalResultNodeWrapper(FinalResult.FINAL_FAILURE_RETRIES);
155             FinalResultNodeWrapper finalFailureException = 
156                             new FinalResultNodeWrapper(FinalResult.FINAL_FAILURE_EXCEPTION);
157             FinalResultNodeWrapper finalFailureGuard = new FinalResultNodeWrapper(FinalResult.FINAL_FAILURE_GUARD);
158             graph.addVertex(finalSuccess);
159             graph.addVertex(finalFailure);
160             graph.addVertex(finalFailureTimeout);
161             graph.addVertex(finalFailureRetries);
162             graph.addVertex(finalFailureException);
163             graph.addVertex(finalFailureGuard);
164             //
165             // Work through the policies and add them in as nodes.
166             //
167             Map<Policy, PolicyNodeWrapper> mapNodes = addPoliciesAsNodes(policy, graph, triggerNode, callback);
168             //
169             // last sweep to connect remaining edges for policy results
170             //
171             for (Policy operPolicy : policy.getPolicies()) {
172                 PolicyNodeWrapper node = mapNodes.get(operPolicy);
173                 //
174                 // Just ensure this has something
175                 //
176                 if (node == null) {
177                     continue;
178                 }
179                 addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getSuccess(), finalSuccess, 
180                                 PolicyResult.SUCCESS, node);
181                 addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure(), finalFailure, 
182                                 PolicyResult.FAILURE, node);
183                 addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure_timeout(), finalFailureTimeout, 
184                                 PolicyResult.FAILURE_TIMEOUT, node);
185                 addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure_retries(), finalFailureRetries, 
186                                 PolicyResult.FAILURE_RETRIES, node);
187                 addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure_exception(), finalFailureException, 
188                                 PolicyResult.FAILURE_EXCEPTION, node);
189                 addEdge(graph, mapNodes, operPolicy.getId(), operPolicy.getFailure_guard(), finalFailureGuard, 
190                                 PolicyResult.FAILURE_GUARD, node);
191             }
192             validateNodesAndEdges(graph, callback);
193         }   
194     }
195     
196     private static void validateOpenLoopPolicy(ControlLoopPolicy policy, FinalResult triggerResult, 
197                     ControlLoopCompilerCallback callback) throws CompilerException {
198         //
199         // Ensure they didn't use some other FinalResult code
200         //
201         if (triggerResult != FinalResult.FINAL_OPENLOOP) {
202             throw new CompilerException("Unexpected Final Result for trigger_policy, should only be " 
203         + FinalResult.FINAL_OPENLOOP.toString() + " or a valid Policy ID");
204         }
205         //
206         // They really shouldn't have any policies attached.
207         //
208         if ((policy.getPolicies() != null || policy.getPolicies().isEmpty()) && callback != null ) {
209             callback.onWarning("Open Loop policy contains policies. The policies will never be invoked.");
210         }
211     }
212     
213     private static void validatePoliciesContainTriggerPolicyAndCombinedTimeoutIsOk(ControlLoopPolicy policy, 
214                     ControlLoopCompilerCallback callback) throws CompilerException {
215         int sum = 0;
216         boolean triggerPolicyFound = false;
217         for (Policy operPolicy : policy.getPolicies()) {
218             sum += operPolicy.getTimeout().intValue();
219             if (policy.getControlLoop().getTrigger_policy().equals(operPolicy.getId())) {
220                 triggerPolicyFound = true;
221             }
222         }
223         if (policy.getControlLoop().getTimeout().intValue() < sum && callback != null) {
224             callback.onError("controlLoop overall timeout is less than the sum of operational policy timeouts.");
225         }
226         
227         if (!triggerPolicyFound) {
228             throw new CompilerException("Unexpected value for trigger_policy, should only be " 
229         + FinalResult.FINAL_OPENLOOP.toString() + " or a valid Policy ID");
230         }
231     }
232     
233     private static Map<Policy, PolicyNodeWrapper> addPoliciesAsNodes(ControlLoopPolicy policy, 
234             DirectedGraph<NodeWrapper, LabeledEdge> graph, TriggerNodeWrapper triggerNode, 
235             ControlLoopCompilerCallback callback) {
236         Map<Policy, PolicyNodeWrapper> mapNodes = new HashMap<>();
237         for (Policy operPolicy : policy.getPolicies()) {
238             //
239             // Is it still ok to add?
240             //
241             if (!okToAdd(operPolicy, callback)) {
242                 //
243                 // Do not add it in
244                 //
245                 continue;
246             }
247             //
248             // Create wrapper policy node and save it into our map so we can
249             // easily retrieve it.
250             //
251             PolicyNodeWrapper node = new PolicyNodeWrapper(operPolicy);
252             mapNodes.put(operPolicy, node);
253             graph.addVertex(node);
254             //
255             // Is this the trigger policy?
256             //
257             if (operPolicy.getId().equals(policy.getControlLoop().getTrigger_policy())) {
258                 //
259                 // Yes add an edge from our trigger event node to this policy
260                 //
261                 graph.addEdge(triggerNode, node, new LabeledEdge(triggerNode, node, new TriggerEdgeWrapper("ONSET")));
262             }
263         }
264         return mapNodes;
265     }
266     
267     private static void addEdge(DirectedGraph<NodeWrapper, LabeledEdge> graph, Map<Policy, PolicyNodeWrapper> mapNodes,
268                     String policyId, String connectedPolicy, 
269                     FinalResultNodeWrapper finalResultNodeWrapper, 
270                     PolicyResult policyResult, NodeWrapper node) throws CompilerException {
271         FinalResult finalResult = FinalResult.toResult(finalResultNodeWrapper.getID());
272         if (FinalResult.isResult(connectedPolicy, finalResult)) {
273             graph.addEdge(node, finalResultNodeWrapper, new LabeledEdge(node, finalResultNodeWrapper, 
274                             new FinalResultEdgeWrapper(finalResult)));
275         } else {
276             PolicyNodeWrapper toNode = findPolicyNode(mapNodes, connectedPolicy);
277             if (toNode == null) {
278                 throw new CompilerException(OPERATION_POLICY + policyId + " is connected to unknown policy " 
279             + connectedPolicy);
280             } else {
281                 graph.addEdge(node, toNode, new LabeledEdge(node, toNode, new PolicyResultEdgeWrapper(policyResult)));
282             }
283         }
284     }
285     
286     private static void validateNodesAndEdges(DirectedGraph<NodeWrapper, LabeledEdge> graph, 
287                     ControlLoopCompilerCallback callback) throws CompilerException {
288         for (NodeWrapper node : graph.vertexSet()) {
289             if (node instanceof TriggerNodeWrapper) {
290                 validateTriggerNodeWrapper(graph, node);
291             } else if (node instanceof FinalResultNodeWrapper) {
292                 validateFinalResultNodeWrapper(graph, node);
293             } else if (node instanceof PolicyNodeWrapper) {
294                 validatePolicyNodeWrapper(graph, node, callback);
295             }
296             for (LabeledEdge edge : graph.outgoingEdgesOf(node)) {
297                 LOGGER.info(edge.from.getID() + " invokes " + edge.to.getID() + " upon " + edge.edge.getID());
298             }
299         }
300     }
301     
302     private static void validateTriggerNodeWrapper(DirectedGraph<NodeWrapper, LabeledEdge> graph, 
303                     NodeWrapper node) throws CompilerException {
304         if (LOGGER.isDebugEnabled()) {
305             LOGGER.info("Trigger Node {}", node.toString());
306         }
307         if (graph.inDegreeOf(node) > 0 ) {
308             //
309             // Really should NEVER get here unless someone messed up the code above.
310             //
311             throw new CompilerException("No inputs to event trigger");
312         }
313         //
314         // Should always be 1, except in the future we may support multiple events
315         //
316         if (graph.outDegreeOf(node) > 1) {
317             throw new CompilerException("The event trigger should only go to ONE node");
318         }
319     }
320     
321     private static void validateFinalResultNodeWrapper(DirectedGraph<NodeWrapper, LabeledEdge> graph, 
322                     NodeWrapper node) throws CompilerException {
323         if (LOGGER.isDebugEnabled()) {
324             LOGGER.info("FinalResult Node {}", node.toString());
325         }
326         //
327         // FinalResult nodes should NEVER have an out edge
328         //
329         if (graph.outDegreeOf(node) > 0) {
330             throw new CompilerException("FinalResult nodes should never have any out edges.");
331         }
332     }
333     
334     private static void validatePolicyNodeWrapper(DirectedGraph<NodeWrapper, LabeledEdge> graph, 
335                     NodeWrapper node, ControlLoopCompilerCallback callback) throws CompilerException {
336         if (LOGGER.isDebugEnabled()) {
337             LOGGER.info("Policy Node {}", node.toString());
338         }
339         //
340         // All Policy Nodes should have the 5 out degrees defined.
341         //
342         if (graph.outDegreeOf(node) != 6) {
343             throw new CompilerException("Policy node should ALWAYS have 6 out degrees.");
344         }
345         //
346         // All Policy Nodes should have at least 1 in degrees 
347         // 
348         if (graph.inDegreeOf(node) == 0 && callback != null) {
349             callback.onWarning("Policy " + node.getID() + " is not reachable.");
350         }
351     }
352     
353     private static boolean okToAdd(Policy operPolicy, ControlLoopCompilerCallback callback) {
354         boolean isOk = isPolicyIdOk(operPolicy, callback);
355         isOk = isActorOk(operPolicy, callback) ? isOk : false;
356         isOk = isRecipeOk(operPolicy, callback) ? isOk : false;
357         isOk = isTargetOk(operPolicy, callback) ? isOk : false;
358         isOk = arePolicyResultsOk(operPolicy, callback) ? isOk : false;
359         return isOk;
360     }
361     
362     private static boolean isPolicyIdOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
363         boolean isOk = true;
364         if (operPolicy.getId() == null || operPolicy.getId().length() < 1) {
365             if (callback != null) {
366                 callback.onError("Operational Policy has an bad ID");
367             }
368             isOk = false;
369         } else {
370             //
371             // Check if they decided to make the ID a result object
372             //
373             if (PolicyResult.toResult(operPolicy.getId()) != null) {
374                 if (callback != null) {
375                     callback.onError("Policy id is set to a PolicyResult " + operPolicy.getId());
376                 }
377                 isOk = false;
378             }
379             if (FinalResult.toResult(operPolicy.getId()) != null) {
380                 if (callback != null) {
381                     callback.onError("Policy id is set to a FinalResult " + operPolicy.getId());
382                 }
383                 isOk = false;
384             }
385         }
386         return isOk;
387     }
388     
389     private static boolean isActorOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
390         boolean isOk = true;
391         if (operPolicy.getActor() == null) {
392             if (callback != null) {
393                 callback.onError("Policy actor is null");
394             }
395             isOk = false;
396         }
397         //
398         // Construct a list for all valid actors
399         //
400         ImmutableList<String> actors = ImmutableList.of("APPC", "AOTS", "MSO", "SDNO", "SDNR", "AAI");
401         //
402         if (operPolicy.getActor() != null && (!actors.contains(operPolicy.getActor())) ) {
403             if (callback != null) {
404                 callback.onError("Policy actor is invalid");
405             }
406             isOk = false;
407         }
408         return isOk;
409     }
410     
411     private static boolean isRecipeOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
412         boolean isOk = true;
413         if (operPolicy.getRecipe() == null) {
414             if (callback != null) {
415                 callback.onError("Policy recipe is null");
416             }
417             isOk = false;
418         }
419         //
420         // NOTE: We need a way to find the acceptable recipe values (either Enum or a database that has these)
421         // 
422         ImmutableMap<String, List<String>> recipes = new ImmutableMap.Builder<String, List<String>>()
423                 .put("APPC", ImmutableList.of("Restart", "Rebuild", "Migrate", "ModifyConfig"))
424                 .put("AOTS", ImmutableList.of("checkMaintenanceWindow", 
425                                 "checkENodeBTicketHours", 
426                                 "checkEquipmentStatus", 
427                                 "checkEimStatus", 
428                                 "checkEquipmentMaintenance"))
429                 .put("MSO", ImmutableList.of("VF Module Create"))
430                 .put("SDNO", ImmutableList.of("health-diagnostic-type", 
431                                 "health-diagnostic", 
432                                 "health-diagnostic-history", 
433                                 "health-diagnostic-commands", 
434                                 "health-diagnostic-aes"))
435                 .put("SDNR", ImmutableList.of("Restart", "Reboot"))
436                 .build();
437         //
438         if (operPolicy.getRecipe() != null 
439                         && (!recipes.getOrDefault(operPolicy.getActor(), 
440                                         Collections.emptyList()).contains(operPolicy.getRecipe()))) {
441             if (callback != null) {
442                 callback.onError("Policy recipe is invalid");
443             }
444             isOk = false;
445         }
446         return isOk;
447     }
448     
449     private static boolean isTargetOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
450         boolean isOk = true;
451         if (operPolicy.getTarget() == null) {
452             if (callback != null) {
453                 callback.onError("Policy target is null");
454             }
455             isOk = false;
456         }
457         if (operPolicy.getTarget() != null 
458                         && operPolicy.getTarget().getType() != TargetType.VM 
459                         && operPolicy.getTarget().getType() != TargetType.VFC 
460                         && operPolicy.getTarget().getType() != TargetType.PNF) {
461             if (callback != null) {
462                 callback.onError("Policy target is invalid");
463             }
464             isOk = false;
465         }
466         return isOk;
467     }
468     
469     private static boolean arePolicyResultsOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
470         //
471         // Check that policy results are connected to either default final * or another policy
472         //
473         boolean isOk = isSuccessPolicyResultOk(operPolicy, callback);
474         isOk = isFailurePolicyResultOk(operPolicy, callback) ? isOk : false;
475         isOk = isFailureRetriesPolicyResultOk(operPolicy, callback) ? isOk : false;
476         isOk = isFailureTimeoutPolicyResultOk(operPolicy, callback) ? isOk : false;
477         isOk = isFailureExceptionPolicyResultOk(operPolicy, callback) ? isOk : false;
478         isOk = isFailureGuardPolicyResultOk(operPolicy, callback) ? isOk : false;
479         return isOk;
480     }
481     
482     private static boolean isSuccessPolicyResultOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
483         boolean isOk = true;
484         if (FinalResult.toResult(operPolicy.getSuccess()) != null 
485                         && !operPolicy.getSuccess().equals(FinalResult.FINAL_SUCCESS.toString())) {
486             if (callback != null) {
487                 callback.onError("Policy success is neither another policy nor FINAL_SUCCESS");
488             }
489             isOk = false;
490         }
491         return isOk;
492     }
493     
494     private static boolean isFailurePolicyResultOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
495         boolean isOk = true;
496         if (FinalResult.toResult(operPolicy.getFailure()) != null 
497                         && !operPolicy.getFailure().equals(FinalResult.FINAL_FAILURE.toString())) {
498             if (callback != null) {
499                 callback.onError("Policy failure is neither another policy nor FINAL_FAILURE");
500             }
501             isOk = false;
502         }
503         return isOk;
504     }
505     
506     private static boolean isFailureRetriesPolicyResultOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
507         boolean isOk = true;
508         if (FinalResult.toResult(operPolicy.getFailure_retries()) != null 
509                         && !operPolicy.getFailure_retries().equals(FinalResult.FINAL_FAILURE_RETRIES.toString())) {
510             if (callback != null) {
511                 callback.onError("Policy failure retries is neither another policy nor FINAL_FAILURE_RETRIES");
512             }
513             isOk = false;
514         }
515         return isOk;
516     }
517     
518     private static boolean isFailureTimeoutPolicyResultOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
519         boolean isOk = true;
520         if (FinalResult.toResult(operPolicy.getFailure_timeout()) != null 
521                         && !operPolicy.getFailure_timeout().equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString())) {
522             if (callback != null) {
523                 callback.onError("Policy failure timeout is neither another policy nor FINAL_FAILURE_TIMEOUT");
524             }
525             isOk = false;
526         }
527         return isOk;
528     }
529     
530     private static boolean isFailureExceptionPolicyResultOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
531         boolean isOk = true;
532         if (FinalResult.toResult(operPolicy.getFailure_exception()) != null 
533                         && !operPolicy.getFailure_exception().equals(FinalResult.FINAL_FAILURE_EXCEPTION.toString())) {
534             if (callback != null) {
535                 callback.onError("Policy failure exception is neither another policy nor FINAL_FAILURE_EXCEPTION");
536             }
537             isOk = false;
538         }
539         return isOk;
540     }
541     
542     private static boolean isFailureGuardPolicyResultOk(Policy operPolicy, ControlLoopCompilerCallback callback) {
543         boolean isOk = true;
544         if (FinalResult.toResult(operPolicy.getFailure_guard()) != null 
545                         && !operPolicy.getFailure_guard().equals(FinalResult.FINAL_FAILURE_GUARD.toString())) {
546             if (callback != null) {
547                 callback.onError("Policy failure guard is neither another policy nor FINAL_FAILURE_GUARD");
548             }
549             isOk = false;
550         }
551         return isOk;
552     }
553
554     private static PolicyNodeWrapper findPolicyNode(Map<Policy, PolicyNodeWrapper> mapNodes, String id) {
555         for (Entry<Policy, PolicyNodeWrapper> entry : mapNodes.entrySet()) {
556             if (entry.getKey().getId().equals(id)) {
557                 return entry.getValue();
558             }
559         }
560         return null;
561     }
562     
563     @FunctionalInterface
564     private interface NodeWrapper extends Serializable {
565         public String   getID();
566     }
567     
568     private static class TriggerNodeWrapper implements NodeWrapper {
569         private static final long serialVersionUID = -187644087811478349L;
570         private String closedLoopControlName;
571         
572         public TriggerNodeWrapper(String closedLoopControlName) {
573             this.closedLoopControlName = closedLoopControlName;
574         }
575
576         @Override
577         public String toString() {
578             return "TriggerNodeWrapper [closedLoopControlName=" + closedLoopControlName + "]";
579         }
580
581         @Override
582         public String getID() {
583             return closedLoopControlName;
584         }
585         
586     }
587         
588     private static class FinalResultNodeWrapper implements NodeWrapper {
589         private static final long serialVersionUID = 8540008796302474613L;
590         private FinalResult result;
591
592         public FinalResultNodeWrapper(FinalResult result) {
593             this.result = result;
594         }
595
596         @Override
597         public String toString() {
598             return "FinalResultNodeWrapper [result=" + result + "]";
599         }
600
601         @Override
602         public String getID() {
603             return result.toString();
604         }
605     }
606     
607     private static class PolicyNodeWrapper implements NodeWrapper {
608         private static final long serialVersionUID = 8170162175653823082L;
609         private transient Policy policy;
610         
611         public PolicyNodeWrapper(Policy operPolicy) {
612             this.policy = operPolicy;
613         }
614
615         @Override
616         public String toString() {
617             return "PolicyNodeWrapper [policy=" + policy + "]";
618         }
619
620         @Override
621         public String getID() {
622             return policy.getId();
623         }
624     }
625     
626     @FunctionalInterface
627     private interface EdgeWrapper extends Serializable {
628         public String getID();
629         
630     }
631     
632     private static class TriggerEdgeWrapper implements EdgeWrapper {
633         private static final long serialVersionUID = 2678151552623278863L;
634         private String trigger;
635         
636         public TriggerEdgeWrapper(String trigger) {
637             this.trigger = trigger;
638         }
639
640         @Override
641         public String getID() {
642             return trigger;
643         }
644
645         @Override
646         public String toString() {
647             return "TriggerEdgeWrapper [trigger=" + trigger + "]";
648         }
649         
650     }
651     
652     private static class PolicyResultEdgeWrapper implements EdgeWrapper {
653         private static final long serialVersionUID = 6078569477021558310L;
654         private PolicyResult policyResult;
655
656         public PolicyResultEdgeWrapper(PolicyResult policyResult) {
657             super();
658             this.policyResult = policyResult;
659         }
660
661         @Override
662         public String toString() {
663             return "PolicyResultEdgeWrapper [policyResult=" + policyResult + "]";
664         }
665
666         @Override
667         public String getID() {
668             return policyResult.toString();
669         }
670         
671         
672     }
673     
674     private static class FinalResultEdgeWrapper implements EdgeWrapper {
675         private static final long serialVersionUID = -1486381946896779840L;
676         private FinalResult finalResult;
677         
678         public FinalResultEdgeWrapper(FinalResult result) {
679             this.finalResult = result;
680         }
681
682         @Override
683         public String toString() {
684             return "FinalResultEdgeWrapper [finalResult=" + finalResult + "]";
685         }
686         
687         @Override
688         public String getID() {
689             return finalResult.toString();
690         }
691     }
692     
693     
694     private static class LabeledEdge extends DefaultEdge {
695         private static final long serialVersionUID = 579384429573385524L;
696         
697         private NodeWrapper from;
698         private NodeWrapper to;
699         private EdgeWrapper edge;
700         
701         public LabeledEdge(NodeWrapper from, NodeWrapper to, EdgeWrapper edge) {
702             this.from = from;
703             this.to = to;
704             this.edge = edge;
705         }
706         
707         @SuppressWarnings("unused")
708         public NodeWrapper from() {
709             return from;
710         }
711         
712         @SuppressWarnings("unused")
713         public NodeWrapper to() {
714             return to;
715         }
716         
717         @SuppressWarnings("unused")
718         public EdgeWrapper edge() {
719             return edge;
720         }
721
722         @Override
723         public String toString() {
724             return "LabeledEdge [from=" + from + ", to=" + to + ", edge=" + edge + "]";
725         }
726     }
727
728 }