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.base.Optional;
12 import com.google.common.base.Predicate;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.Sets;
16 import java.util.Map.Entry;
18 import javax.management.ObjectName;
19 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.InstanceConfig;
20 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
24 public class InstanceRuntime {
29 private static final String KEY_ATTRIBUTE_KEY = "key";
31 private final InstanceConfig instanceMapping;
32 private final Map<String, InstanceRuntime> childrenMappings;
33 private final Map<String, String> jmxToYangChildRbeMapping;
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;
43 * Finds all children runtime beans, same properties and values as current
44 * root + any number of additional properties
46 private Set<ObjectName> findChildren(ObjectName innerRootBean, Set<ObjectName> childRbeOns) {
47 final Map<String, String> wantedProperties = innerRootBean.getKeyPropertyList();
49 return Sets.newHashSet(Collections2.filter(childRbeOns, new Predicate<ObjectName>() {
52 public boolean apply(ObjectName on) {
53 Map<String, String> localProperties = on.getKeyPropertyList();
54 for (Entry<String, String> propertyEntry : wantedProperties.entrySet()) {
55 if (!localProperties.containsKey(propertyEntry.getKey())){
58 if (!localProperties.get(propertyEntry.getKey()).equals(propertyEntry.getValue())){
61 if (localProperties.size() <= wantedProperties.size()){
71 * Finds next level root runtime beans, beans that have the same properties
72 * as current root + one additional
74 private Set<ObjectName> getRootBeans(Set<ObjectName> childRbeOns, final String string, final int keyListSize) {
75 return Sets.newHashSet(Collections2.filter(childRbeOns, new Predicate<ObjectName>() {
78 public boolean apply(ObjectName on) {
79 if (on.getKeyPropertyList().size() != keyListSize + 1){
82 return on.getKeyPropertyList().containsKey(string);
87 public Element toXml(ObjectName rootOn, Set<ObjectName> childRbeOns, Document document, Element parentElement, String namespace) {
88 return toXml(rootOn, childRbeOns, document, null, parentElement, namespace);
91 public Element toXml(ObjectName rootOn, Set<ObjectName> childRbeOns, Document document, String instanceIndex,
92 Element parentElement, String namespace) {
93 Element xml = instanceMapping.toXml(rootOn, namespace, document, parentElement);
95 if (instanceIndex != null) {
96 xml.setAttribute(KEY_ATTRIBUTE_KEY, instanceIndex);
99 for (Entry<String, InstanceRuntime> childMappingEntry : childrenMappings.entrySet()) {
100 Set<ObjectName> innerRootBeans = getRootBeans(childRbeOns, childMappingEntry.getKey(), rootOn
101 .getKeyPropertyList().size());
103 for (ObjectName objectName : innerRootBeans) {
104 Set<ObjectName> innerChildRbeOns = findChildren(objectName, childRbeOns);
105 String runtimeInstanceIndex = objectName.getKeyProperty(childMappingEntry.getKey());
107 String elementName = jmxToYangChildRbeMapping.get(childMappingEntry.getKey());
109 Element innerXml = XmlUtil.createElement(document, elementName, Optional.of(namespace));
110 childMappingEntry.getValue().toXml(objectName, innerChildRbeOns, document,
111 runtimeInstanceIndex, innerXml, namespace);
112 xml.appendChild(innerXml);