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