Initial code drop of netconf protocol implementation
[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 com.google.common.base.Predicate;
12 import com.google.common.collect.Collections2;
13 import com.google.common.collect.Sets;
14 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.InstanceConfig;
15 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
16 import org.w3c.dom.Document;
17 import org.w3c.dom.Element;
18
19 import javax.management.ObjectName;
20 import java.util.Hashtable;
21 import java.util.Map;
22 import java.util.Map.Entry;
23 import java.util.Set;
24
25 public class InstanceRuntime {
26
27     /**
28      *
29      */
30     private static final String KEY_ATTRIBUTE_KEY = "key";
31
32     private final InstanceConfig instanceMapping;
33     private final Map<String, InstanceRuntime> childrenMappings;
34     private final Map<String, String> jmxToYangChildRbeMapping;
35
36     public InstanceRuntime(InstanceConfig instanceMapping, Map<String, InstanceRuntime> childrenMappings,
37             Map<String, String> jmxToYangChildRbeMapping) {
38         this.instanceMapping = instanceMapping;
39         this.childrenMappings = childrenMappings;
40         this.jmxToYangChildRbeMapping = jmxToYangChildRbeMapping;
41     }
42
43     /**
44      * Finds all children runtime beans, same properties and values as current
45      * root + any number of additional properties
46      */
47     private Set<ObjectName> findChildren(ObjectName innerRootBean, Set<ObjectName> childRbeOns) {
48         final Hashtable<String, String> wantedProperties = innerRootBean.getKeyPropertyList();
49
50         return Sets.newHashSet(Collections2.filter(childRbeOns, new Predicate<ObjectName>() {
51
52             @Override
53             public boolean apply(ObjectName on) {
54                 Hashtable<String, String> localProperties = on.getKeyPropertyList();
55                 for (Entry<String, String> propertyEntry : wantedProperties.entrySet()) {
56                     if (!localProperties.containsKey(propertyEntry.getKey()))
57                         return false;
58                     if (!localProperties.get(propertyEntry.getKey()).equals(propertyEntry.getValue()))
59                         return false;
60                     if (localProperties.size() <= wantedProperties.size())
61                         return false;
62                 }
63                 return true;
64             }
65         }));
66     }
67
68     /**
69      * Finds next level root runtime beans, beans that have the same properties
70      * as current root + one additional
71      */
72     private Set<ObjectName> getRootBeans(Set<ObjectName> childRbeOns, final String string, final int keyListSize) {
73         return Sets.newHashSet(Collections2.filter(childRbeOns, new Predicate<ObjectName>() {
74
75             @Override
76             public boolean apply(ObjectName on) {
77                 if (on.getKeyPropertyList().size() != keyListSize + 1)
78                     return false;
79                 if (!on.getKeyPropertyList().containsKey(string))
80                     return false;
81                 return true;
82             }
83         }));
84     }
85
86     public Element toXml(ObjectName rootOn, Set<ObjectName> childRbeOns, Document document) {
87         return toXml(rootOn, childRbeOns, document, null, null);
88     }
89
90     public Element toXml(ObjectName rootOn, Set<ObjectName> childRbeOns, Document document, String instanceIndex,
91             String keyName) {
92         Element xml = document.createElement(keyName == null ? XmlNetconfConstants.DATA_KEY : keyName);
93         // TODO namespace
94         xml = instanceMapping.toXml(rootOn, null, "namespace", document, xml);
95
96         if (instanceIndex != null) {
97             xml.setAttribute(KEY_ATTRIBUTE_KEY, instanceIndex);
98         }
99
100         for (Entry<String, InstanceRuntime> childMappingEntry : childrenMappings.entrySet()) {
101             Set<ObjectName> innerRootBeans = getRootBeans(childRbeOns, childMappingEntry.getKey(), rootOn
102                     .getKeyPropertyList().size());
103
104             for (ObjectName objectName : innerRootBeans) {
105                 Set<ObjectName> innerChildRbeOns = findChildren(objectName, childRbeOns);
106                 String runtimeInstanceIndex = objectName.getKeyProperty(childMappingEntry.getKey());
107
108                 String elementName = jmxToYangChildRbeMapping.get(childMappingEntry.getKey());
109                 xml.appendChild(childMappingEntry.getValue().toXml(objectName, innerChildRbeOns, document,
110                         runtimeInstanceIndex, elementName));
111             }
112         }
113
114         return xml;
115     }
116
117 }