Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / runtime / InstanceRuntime.java
1 /*
2  * Copyright (c) 2013, 2017 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.config.facade.xml.runtime;
10
11 import com.google.common.base.Optional;
12 import com.google.common.collect.Collections2;
13 import com.google.common.collect.Sets;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import java.util.Set;
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;
23
24 public class InstanceRuntime {
25
26     private static final String KEY_ATTRIBUTE_KEY = "key";
27
28     private final InstanceConfig instanceMapping;
29     private final Map<String, InstanceRuntime> childrenMappings;
30     private final Map<String, String> jmxToYangChildRbeMapping;
31
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;
37     }
38
39     /**
40      * Finds all children runtime beans, same properties and values as current root
41      * + any number of additional properties.
42      */
43     private Set<ObjectName> findChildren(ObjectName innerRootBean, Set<ObjectName> childRbeOns) {
44         final Map<String, String> wantedProperties = innerRootBean.getKeyPropertyList();
45
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())) {
50                     return false;
51                 }
52                 if (!localProperties.get(propertyEntry.getKey()).equals(propertyEntry.getValue())) {
53                     return false;
54                 }
55                 if (localProperties.size() <= wantedProperties.size()) {
56                     return false;
57                 }
58             }
59             return true;
60         }));
61     }
62
63     /**
64      * Finds next level root runtime beans, beans that have the same properties as
65      * current root + one additional.
66      */
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) {
70                 return false;
71             }
72             return on.getKeyPropertyList().containsKey(string);
73         }));
74     }
75
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);
79     }
80
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);
84
85         if (instanceIndex != null) {
86             xml.setAttribute(KEY_ATTRIBUTE_KEY, instanceIndex);
87         }
88
89         for (Entry<String, InstanceRuntime> childMappingEntry : childrenMappings.entrySet()) {
90             Set<ObjectName> innerRootBeans = getRootBeans(childRbeOns, childMappingEntry.getKey(),
91                     rootOn.getKeyPropertyList().size());
92
93             for (ObjectName objectName : innerRootBeans) {
94                 Set<ObjectName> innerChildRbeOns = findChildren(objectName, childRbeOns);
95                 String runtimeInstanceIndex = objectName.getKeyProperty(childMappingEntry.getKey());
96
97                 String elementName = jmxToYangChildRbeMapping.get(childMappingEntry.getKey());
98
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);
103             }
104         }
105         return xml;
106     }
107 }