Merge "Bug 509: Fixed incorrect merging of Data Store Writes / Events"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / toxml / RuntimeBeanEntryWritingStrategy.java
1 /*
2  * Copyright (c) 2013 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.netconf.confignetconfconnector.mapping.attributes.toxml;
10
11 import java.util.Map;
12 import java.util.Map.Entry;
13
14 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
15 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
16 import org.w3c.dom.Document;
17 import org.w3c.dom.Element;
18
19 import com.google.common.base.Optional;
20
21 public class RuntimeBeanEntryWritingStrategy extends CompositeAttributeWritingStrategy {
22
23     public RuntimeBeanEntryWritingStrategy(Document document, String key,
24             Map<String, AttributeWritingStrategy> innerStrats) {
25         super(document, key, innerStrats);
26     }
27
28     /*
29      * (non-Javadoc)
30      *
31      * @see org.opendaylight.controller.config.netconf.mapping.attributes.toxml.
32      * AttributeWritingStrategy#writeElement(org.w3c.dom.Element,
33      * java.lang.Object)
34      */
35     @Override
36     public void writeElement(Element parentElement, String namespace, Object value) {
37         Util.checkType(value, Map.class);
38
39         Element innerNode = XmlUtil.createElement(document, key, Optional.<String>absent());
40
41         Map<?, ?> map = (Map<?, ?>) value;
42
43         for (Entry<?, ?> runtimeBeanInstanceMappingEntry : map.entrySet()) {
44
45             // wrap runtime attributes with number assigned to current runtime
46             // bean
47             Util.checkType(runtimeBeanInstanceMappingEntry.getValue(), Map.class);
48             Map<?, ?> innerMap = (Map<?, ?>) runtimeBeanInstanceMappingEntry.getValue();
49             Element runtimeInstanceNode = XmlUtil.createElement(document, "_"
50                     + (String) runtimeBeanInstanceMappingEntry.getKey(), Optional.<String>absent());
51             innerNode.appendChild(runtimeInstanceNode);
52
53             for (Entry<?, ?> innerObjectEntry : innerMap.entrySet()) {
54
55                 Util.checkType(innerObjectEntry.getKey(), String.class);
56
57                 String innerKey = (String) innerObjectEntry.getKey();
58                 Object innerValue = innerObjectEntry.getValue();
59
60                 innerStrats.get(innerKey).writeElement(runtimeInstanceNode, namespace, innerValue);
61             }
62         }
63         parentElement.appendChild(innerNode);
64
65     }
66
67 }