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 / SimpleIdentityRefAttributeWritingStrategy.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 com.google.common.base.Preconditions;
13 import java.util.Map;
14 import org.opendaylight.controller.config.facade.xml.util.Util;
15 import org.opendaylight.controller.config.util.xml.XmlUtil;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.w3c.dom.Document;
18 import org.w3c.dom.Element;
19
20 public class SimpleIdentityRefAttributeWritingStrategy extends SimpleAttributeWritingStrategy {
21
22     private static final String PREFIX = "prefix";
23
24     /**
25      * @param document
26      * @param key
27      */
28     public SimpleIdentityRefAttributeWritingStrategy(final Document document, final String key) {
29         super(document, key);
30     }
31
32     protected Object preprocess(final Object value) {
33         Util.checkType(value, Map.class);
34         Preconditions.checkArgument(((Map<?, ?>)value).size() == 1, "Unexpected number of values in %s, expected 1", value);
35         Object stringValue = ((Map<?, ?>) value).values().iterator().next();
36         Util.checkType(stringValue, String.class);
37
38         return stringValue;
39     }
40
41     @Override
42     protected Element createElement(final Document doc, final String key, final String value, final Optional<String> namespace) {
43         QName qName = QName.create(value);
44         String identityValue = qName.getLocalName();
45         String identityNamespace = qName.getNamespace().toString();
46         return XmlUtil.createTextElementWithNamespacedContent(doc, key, PREFIX, identityNamespace, identityValue, namespace);
47     }
48 }