Merge "Bug 509: Fixed small discrepancies in Binding Data Change Events."
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / runtime / InstanceRuntime.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.runtime;
10
11 import java.util.Hashtable;
12 import java.util.Map;
13 import java.util.Map.Entry;
14 import java.util.Set;
15
16 import javax.management.ObjectName;
17
18 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.InstanceConfig;
19 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22
23 import com.google.common.base.Optional;
24 import com.google.common.base.Predicate;
25 import com.google.common.collect.Collections2;
26 import com.google.common.collect.Sets;
27
28 public class InstanceRuntime {
29
30     /**
31      *
32      */
33     private static final String KEY_ATTRIBUTE_KEY = "key";
34
35     private final InstanceConfig instanceMapping;
36     private final Map<String, InstanceRuntime> childrenMappings;
37     private final Map<String, String> jmxToYangChildRbeMapping;
38
39     public InstanceRuntime(InstanceConfig instanceMapping, Map<String, InstanceRuntime> childrenMappings,
40             Map<String, String> jmxToYangChildRbeMapping) {
41         this.instanceMapping = instanceMapping;
42         this.childrenMappings = childrenMappings;
43         this.jmxToYangChildRbeMapping = jmxToYangChildRbeMapping;
44     }
45
46     /**
47      * Finds all children runtime beans, same properties and values as current
48      * root + any number of additional properties
49      */
50     private Set<ObjectName> findChildren(ObjectName innerRootBean, Set<ObjectName> childRbeOns) {
51         final Hashtable<String, String> wantedProperties = innerRootBean.getKeyPropertyList();
52
53         return Sets.newHashSet(Collections2.filter(childRbeOns, new Predicate<ObjectName>() {
54
55             @Override
56             public boolean apply(ObjectName on) {
57                 Hashtable<String, String> localProperties = on.getKeyPropertyList();
58                 for (Entry<String, String> propertyEntry : wantedProperties.entrySet()) {
59                     if (!localProperties.containsKey(propertyEntry.getKey()))
60                         return false;
61                     if (!localProperties.get(propertyEntry.getKey()).equals(propertyEntry.getValue()))
62                         return false;
63                     if (localProperties.size() <= wantedProperties.size())
64                         return false;
65                 }
66                 return true;
67             }
68         }));
69     }
70
71     /**
72      * Finds next level root runtime beans, beans that have the same properties
73      * as current root + one additional
74      */
75     private Set<ObjectName> getRootBeans(Set<ObjectName> childRbeOns, final String string, final int keyListSize) {
76         return Sets.newHashSet(Collections2.filter(childRbeOns, new Predicate<ObjectName>() {
77
78             @Override
79             public boolean apply(ObjectName on) {
80                 if (on.getKeyPropertyList().size() != keyListSize + 1)
81                     return false;
82                 if (!on.getKeyPropertyList().containsKey(string))
83                     return false;
84                 return true;
85             }
86         }));
87     }
88
89     public Element toXml(ObjectName rootOn, Set<ObjectName> childRbeOns, Document document, Element parentElement, String namespace) {
90         return toXml(rootOn, childRbeOns, document, null, parentElement, namespace);
91     }
92
93     public Element toXml(ObjectName rootOn, Set<ObjectName> childRbeOns, Document document, String instanceIndex,
94                          Element parentElement, String namespace) {
95         Element xml = instanceMapping.toXml(rootOn, null, namespace, document, parentElement);
96
97         if (instanceIndex != null) {
98             xml.setAttribute(KEY_ATTRIBUTE_KEY, instanceIndex);
99         }
100
101         for (Entry<String, InstanceRuntime> childMappingEntry : childrenMappings.entrySet()) {
102             Set<ObjectName> innerRootBeans = getRootBeans(childRbeOns, childMappingEntry.getKey(), rootOn
103                     .getKeyPropertyList().size());
104
105             for (ObjectName objectName : innerRootBeans) {
106                 Set<ObjectName> innerChildRbeOns = findChildren(objectName, childRbeOns);
107                 String runtimeInstanceIndex = objectName.getKeyProperty(childMappingEntry.getKey());
108
109                 String elementName = jmxToYangChildRbeMapping.get(childMappingEntry.getKey());
110
111                 Element innerXml = XmlUtil.createElement(document, elementName, Optional.<String>absent());
112                 childMappingEntry.getValue().toXml(objectName, innerChildRbeOns, document,
113                         runtimeInstanceIndex, innerXml, namespace);
114                 xml.appendChild(innerXml);
115             }
116         }
117
118         return xml;
119     }
120
121 }