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