2 * Copyright (c) 2013, 2017 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.config.facade.xml.runtime;
11 import com.google.common.base.Optional;
12 import com.google.common.collect.Collections2;
13 import com.google.common.collect.Sets;
15 import java.util.Map.Entry;
17 import javax.management.ObjectName;
18 import org.opendaylight.controller.config.facade.xml.mapping.config.InstanceConfig;
19 import org.opendaylight.controller.config.facade.xml.osgi.EnumResolver;
20 import org.opendaylight.controller.config.util.xml.XmlUtil;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
24 public class InstanceRuntime {
26 private static final String KEY_ATTRIBUTE_KEY = "key";
28 private final InstanceConfig instanceMapping;
29 private final Map<String, InstanceRuntime> childrenMappings;
30 private final Map<String, String> jmxToYangChildRbeMapping;
32 public InstanceRuntime(InstanceConfig instanceMapping, Map<String, InstanceRuntime> childrenMappings,
33 Map<String, String> jmxToYangChildRbeMapping) {
34 this.instanceMapping = instanceMapping;
35 this.childrenMappings = childrenMappings;
36 this.jmxToYangChildRbeMapping = jmxToYangChildRbeMapping;
40 * Finds all children runtime beans, same properties and values as current root
41 * + any number of additional properties.
43 private Set<ObjectName> findChildren(ObjectName innerRootBean, Set<ObjectName> childRbeOns) {
44 final Map<String, String> wantedProperties = innerRootBean.getKeyPropertyList();
46 return Sets.newHashSet(Collections2.filter(childRbeOns, on -> {
47 Map<String, String> localProperties = on.getKeyPropertyList();
48 for (Entry<String, String> propertyEntry : wantedProperties.entrySet()) {
49 if (!localProperties.containsKey(propertyEntry.getKey())) {
52 if (!localProperties.get(propertyEntry.getKey()).equals(propertyEntry.getValue())) {
55 if (localProperties.size() <= wantedProperties.size()) {
64 * Finds next level root runtime beans, beans that have the same properties as
65 * current root + one additional.
67 private Set<ObjectName> getRootBeans(Set<ObjectName> childRbeOns, final String string, final int keyListSize) {
68 return Sets.newHashSet(Collections2.filter(childRbeOns, on -> {
69 if (on.getKeyPropertyList().size() != keyListSize + 1) {
72 return on.getKeyPropertyList().containsKey(string);
76 public Element toXml(ObjectName rootOn, Set<ObjectName> childRbeOns, Document document, Element parentElement,
77 String namespace, final EnumResolver enumResolver) {
78 return toXml(rootOn, childRbeOns, document, null, parentElement, namespace, enumResolver);
81 public Element toXml(ObjectName rootOn, Set<ObjectName> childRbeOns, Document document, String instanceIndex,
82 Element parentElement, String namespace, final EnumResolver enumResolver) {
83 Element xml = instanceMapping.toXml(rootOn, namespace, document, parentElement, enumResolver);
85 if (instanceIndex != null) {
86 xml.setAttribute(KEY_ATTRIBUTE_KEY, instanceIndex);
89 for (Entry<String, InstanceRuntime> childMappingEntry : childrenMappings.entrySet()) {
90 Set<ObjectName> innerRootBeans = getRootBeans(childRbeOns, childMappingEntry.getKey(),
91 rootOn.getKeyPropertyList().size());
93 for (ObjectName objectName : innerRootBeans) {
94 Set<ObjectName> innerChildRbeOns = findChildren(objectName, childRbeOns);
95 String runtimeInstanceIndex = objectName.getKeyProperty(childMappingEntry.getKey());
97 String elementName = jmxToYangChildRbeMapping.get(childMappingEntry.getKey());
99 Element innerXml = XmlUtil.createElement(document, elementName, Optional.of(namespace));
100 childMappingEntry.getValue().toXml(objectName, innerChildRbeOns, document, runtimeInstanceIndex,
101 innerXml, namespace, enumResolver);
102 xml.appendChild(innerXml);