Initial code drop of netconf protocol implementation
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / runtime / ModuleRuntime.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.collect.Multimap;
12 import com.google.common.collect.Sets;
13 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
14 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
15 import org.w3c.dom.Document;
16 import org.w3c.dom.Element;
17
18 import javax.management.ObjectName;
19 import java.util.Collection;
20 import java.util.Set;
21
22 public class ModuleRuntime {
23
24     private final String moduleName;
25     private final InstanceRuntime instanceRuntime;
26
27     public ModuleRuntime(String moduleName, InstanceRuntime instanceRuntime) {
28         this.moduleName = moduleName;
29         this.instanceRuntime = instanceRuntime;
30     }
31
32     public InstanceRuntime getMbeanMapping() {
33         return instanceRuntime;
34     }
35
36     private ObjectName findRoot(Collection<ObjectName> runtimeBeanOns) {
37         for (ObjectName objectName : runtimeBeanOns) {
38             if (objectName.getKeyPropertyList().size() == 3)
39                 return objectName;
40         }
41         throw new IllegalStateException("Root runtime bean not found among " + runtimeBeanOns);
42     }
43
44     public Element toXml(String namespace, Multimap<String, ObjectName> instances, Document document) {
45         Element root = document.createElement(XmlNetconfConstants.MODULE_KEY);
46         XmlUtil.addNamespaceAttr(root, namespace);
47
48         Element nameElement = XmlUtil.createTextElement(document, XmlNetconfConstants.NAME_KEY, moduleName);
49         root.appendChild(nameElement);
50
51         for (String instanceName : instances.keySet()) {
52             Element instance = document.createElement(XmlNetconfConstants.INSTANCE_KEY);
53
54             Element innerNameElement = XmlUtil.createTextElement(document, XmlNetconfConstants.NAME_KEY, instanceName);
55             instance.appendChild(innerNameElement);
56
57             Collection<ObjectName> runtimeBeanOns = instances.get(instanceName);
58             ObjectName rootName = findRoot(runtimeBeanOns);
59
60             Set<ObjectName> childrenRuntimeBeans = Sets.newHashSet(runtimeBeanOns);
61             childrenRuntimeBeans.remove(rootName);
62
63             instance.appendChild(instanceRuntime.toXml(rootName, childrenRuntimeBeans, document));
64
65             root.appendChild(instance);
66         }
67
68         return root;
69     }
70
71 }