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