Merge "Bug 509: Improve logging in InMemoryDataStore."
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / toxml / ObjectNameAttributeWritingStrategy.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 org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.ObjectNameAttributeMappingStrategy;
12 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
13 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
14 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
15 import org.w3c.dom.Document;
16 import org.w3c.dom.Element;
17
18 import com.google.common.base.Optional;
19
20 public class ObjectNameAttributeWritingStrategy implements AttributeWritingStrategy {
21
22     private final Document document;
23     private final String key;
24
25     /**
26      * @param document
27      * @param key
28      */
29     public ObjectNameAttributeWritingStrategy(Document document, String key) {
30         this.document = document;
31         this.key = key;
32     }
33
34     @Override
35     public void writeElement(Element parentElement, String namespace, Object value) {
36         Util.checkType(value, ObjectNameAttributeMappingStrategy.MappedDependency.class);
37         Element innerNode = XmlUtil.createElement(document, key, Optional.of(namespace));
38
39         String moduleName = ((ObjectNameAttributeMappingStrategy.MappedDependency) value).getServiceName();
40         String refName = ((ObjectNameAttributeMappingStrategy.MappedDependency) value).getRefName();
41         String namespaceForType = ((ObjectNameAttributeMappingStrategy.MappedDependency) value).getNamespace();
42
43         Element typeElement = XmlUtil.createPrefixedTextElement(document, XmlUtil.createPrefixedValue(XmlNetconfConstants.PREFIX, XmlNetconfConstants.TYPE_KEY), XmlNetconfConstants.PREFIX,
44                 moduleName, Optional.<String>of(namespaceForType));
45         innerNode.appendChild(typeElement);
46
47         final Element nameElement = XmlUtil.createTextElement(document, XmlNetconfConstants.NAME_KEY, refName, Optional.<String>absent());
48         innerNode.appendChild(nameElement);
49
50         parentElement.appendChild(innerNode);
51     }
52
53 }