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;
15 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.InstanceConfig;
16 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
17 import org.w3c.dom.Document;
18 import org.w3c.dom.Element;
20 import javax.management.ObjectName;
22 import java.util.Map.Entry;
25 public class InstanceRuntime {
30 private static final String KEY_ATTRIBUTE_KEY = "key";
32 private final InstanceConfig instanceMapping;
33 private final Map<String, InstanceRuntime> childrenMappings;
34 private final Map<String, String> jmxToYangChildRbeMapping;
36 public InstanceRuntime(InstanceConfig instanceMapping, Map<String, InstanceRuntime> childrenMappings,
37 Map<String, String> jmxToYangChildRbeMapping) {
38 this.instanceMapping = instanceMapping;
39 this.childrenMappings = childrenMappings;
40 this.jmxToYangChildRbeMapping = jmxToYangChildRbeMapping;
44 * Finds all children runtime beans, same properties and values as current
45 * root + any number of additional properties
47 private Set<ObjectName> findChildren(ObjectName innerRootBean, Set<ObjectName> childRbeOns) {
48 final Map<String, String> wantedProperties = innerRootBean.getKeyPropertyList();
50 return Sets.newHashSet(Collections2.filter(childRbeOns, new Predicate<ObjectName>() {
53 public boolean apply(ObjectName on) {
54 Map<String, String> localProperties = on.getKeyPropertyList();
55 for (Entry<String, String> propertyEntry : wantedProperties.entrySet()) {
56 if (!localProperties.containsKey(propertyEntry.getKey())){
59 if (!localProperties.get(propertyEntry.getKey()).equals(propertyEntry.getValue())){
62 if (localProperties.size() <= wantedProperties.size()){
72 * Finds next level root runtime beans, beans that have the same properties
73 * as current root + one additional
75 private Set<ObjectName> getRootBeans(Set<ObjectName> childRbeOns, final String string, final int keyListSize) {
76 return Sets.newHashSet(Collections2.filter(childRbeOns, new Predicate<ObjectName>() {
79 public boolean apply(ObjectName on) {
80 if (on.getKeyPropertyList().size() != keyListSize + 1){
83 if (!on.getKeyPropertyList().containsKey(string)){
91 public Element toXml(ObjectName rootOn, Set<ObjectName> childRbeOns, Document document, Element parentElement, String namespace) {
92 return toXml(rootOn, childRbeOns, document, null, parentElement, namespace);
95 public Element toXml(ObjectName rootOn, Set<ObjectName> childRbeOns, Document document, String instanceIndex,
96 Element parentElement, String namespace) {
97 Element xml = instanceMapping.toXml(rootOn, null, namespace, document, parentElement);
99 if (instanceIndex != null) {
100 xml.setAttribute(KEY_ATTRIBUTE_KEY, instanceIndex);
103 for (Entry<String, InstanceRuntime> childMappingEntry : childrenMappings.entrySet()) {
104 Set<ObjectName> innerRootBeans = getRootBeans(childRbeOns, childMappingEntry.getKey(), rootOn
105 .getKeyPropertyList().size());
107 for (ObjectName objectName : innerRootBeans) {
108 Set<ObjectName> innerChildRbeOns = findChildren(objectName, childRbeOns);
109 String runtimeInstanceIndex = objectName.getKeyProperty(childMappingEntry.getKey());
111 String elementName = jmxToYangChildRbeMapping.get(childMappingEntry.getKey());
113 Element innerXml = XmlUtil.createElement(document, elementName, Optional.<String>absent());
114 childMappingEntry.getValue().toXml(objectName, innerChildRbeOns, document,
115 runtimeInstanceIndex, innerXml, namespace);
116 xml.appendChild(innerXml);