Merge "Bug 615: Removed xtend from Topology Manager"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / toxml / CompositeAttributeWritingStrategy.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 CompositeAttributeWritingStrategy implements AttributeWritingStrategy {
22
23     protected final String key;
24     protected final Document document;
25     protected final Map<String, AttributeWritingStrategy> innerStrats;
26
27     public CompositeAttributeWritingStrategy(Document document, String key,
28             Map<String, AttributeWritingStrategy> innerStrats) {
29         this.document = document;
30         this.key = key;
31         this.innerStrats = innerStrats;
32     }
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(document, key, Optional.of(namespace));
39
40         Map<?, ?> map = (Map<?, ?>) value;
41
42         for (Entry<?, ?> innerObjectEntry : map.entrySet()) {
43             Util.checkType(innerObjectEntry.getKey(), String.class);
44
45             String innerKey = (String) innerObjectEntry.getKey();
46             Object innerValue = innerObjectEntry.getValue();
47
48             innerStrats.get(innerKey).writeElement(innerNode, namespace, innerValue);
49         }
50         parentElement.appendChild(innerNode);
51     }
52 }