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