Merge "BUG-509: add missing copyright headers"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / toxml / SimpleIdentityRefAttributeWritingStrategy.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
13 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
14 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.w3c.dom.Document;
17 import org.w3c.dom.Element;
18
19 import com.google.common.base.Optional;
20 import com.google.common.base.Preconditions;
21
22 public class SimpleIdentityRefAttributeWritingStrategy extends SimpleAttributeWritingStrategy {
23
24     private static final char QNAME_SEPARATOR = ':';
25     private static final String PREFIX = "prefix";
26
27     /**
28      * @param document
29      * @param key
30      */
31     public SimpleIdentityRefAttributeWritingStrategy(Document document, String key) {
32         super(document, key);
33     }
34
35     protected Object preprocess(Object value) {
36         Util.checkType(value, Map.class);
37         Preconditions.checkArgument(((Map)value).size() == 1, "Unexpected number of values in %s, expected 1", value);
38         Object stringValue = ((Map) value).values().iterator().next();
39         Util.checkType(stringValue, String.class);
40
41         return stringValue;
42     }
43
44     @Override
45     protected Element createElement(Document doc, String key, String value, Optional<String> namespace) {
46         QName qName = QName.create(value);
47         String identity = qName.getLocalName();
48         String identityNamespace = qName.getNamespace().toString();
49         Element element = XmlUtil.createPrefixedTextElement(doc, XmlUtil.createPrefixedValue(PREFIX, key), PREFIX, identity, Optional.<String>of(identityNamespace));
50         return element;
51     }
52 }