2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.runtime;
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;
18 import javax.management.ObjectName;
19 import java.util.Collection;
22 public class ModuleRuntime {
24 private final String moduleName;
25 private final InstanceRuntime instanceRuntime;
27 public ModuleRuntime(String moduleName, InstanceRuntime instanceRuntime) {
28 this.moduleName = moduleName;
29 this.instanceRuntime = instanceRuntime;
32 public InstanceRuntime getMbeanMapping() {
33 return instanceRuntime;
36 private ObjectName findRoot(Collection<ObjectName> runtimeBeanOns) {
37 for (ObjectName objectName : runtimeBeanOns) {
38 if (objectName.getKeyPropertyList().size() == 3)
41 throw new IllegalStateException("Root runtime bean not found among " + runtimeBeanOns);
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);
48 Element nameElement = XmlUtil.createTextElement(document, XmlNetconfConstants.NAME_KEY, moduleName);
49 root.appendChild(nameElement);
51 for (String instanceName : instances.keySet()) {
52 Element instance = document.createElement(XmlNetconfConstants.INSTANCE_KEY);
54 Element innerNameElement = XmlUtil.createTextElement(document, XmlNetconfConstants.NAME_KEY, instanceName);
55 instance.appendChild(innerNameElement);
57 Collection<ObjectName> runtimeBeanOns = instances.get(instanceName);
58 ObjectName rootName = findRoot(runtimeBeanOns);
60 Set<ObjectName> childrenRuntimeBeans = Sets.newHashSet(runtimeBeanOns);
61 childrenRuntimeBeans.remove(rootName);
63 instance.appendChild(instanceRuntime.toXml(rootName, childrenRuntimeBeans, document));
65 root.appendChild(instance);