Updated xml generated by get netconf operation to conform to yang schema in config-api
[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.Sets;
12 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
13 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
14 import org.w3c.dom.Document;
15 import org.w3c.dom.Element;
16
17 import javax.management.ObjectName;
18 import java.util.Collection;
19 import java.util.Set;
20
21 public class ModuleRuntime {
22
23     private final String moduleName;
24     private final InstanceRuntime instanceRuntime;
25
26     public ModuleRuntime(String moduleName, InstanceRuntime instanceRuntime) {
27         this.moduleName = moduleName;
28         this.instanceRuntime = instanceRuntime;
29     }
30
31     public InstanceRuntime getMbeanMapping() {
32         return instanceRuntime;
33     }
34
35     private ObjectName findRoot(Collection<ObjectName> runtimeBeanOns) {
36         for (ObjectName objectName : runtimeBeanOns) {
37             if (objectName.getKeyPropertyList().size() == 3)
38                 return objectName;
39         }
40         throw new IllegalStateException("Root runtime bean not found among " + runtimeBeanOns);
41     }
42
43     public Element toXml(String namespace, String instanceName, Collection<ObjectName> runtimeBeanOns, Document document) {
44         Element moduleElement = document.createElement(XmlNetconfConstants.MODULE_KEY);
45
46         final String prefix = getPrefix(namespace);
47         Element typeElement = XmlUtil.createPrefixedTextElement(document, XmlNetconfConstants.TYPE_KEY, prefix,
48                 moduleName);
49         XmlUtil.addPrefixedNamespaceAttr(typeElement, prefix, namespace);
50         moduleElement.appendChild(typeElement);
51
52         Element nameElement = XmlUtil.createTextElement(document, XmlNetconfConstants.NAME_KEY, instanceName);
53         moduleElement.appendChild(nameElement);
54
55         ObjectName rootName = findRoot(runtimeBeanOns);
56
57         Set<ObjectName> childrenRuntimeBeans = Sets.newHashSet(runtimeBeanOns);
58         childrenRuntimeBeans.remove(rootName);
59
60         instanceRuntime.toXml(rootName, childrenRuntimeBeans, document, moduleElement, namespace);
61
62         return moduleElement;
63     }
64
65     private String getPrefix(String namespace) {
66         return XmlNetconfConstants.PREFIX;
67     }
68
69 }