Add prefixes to type attribute in generated xml from netconf
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / fromxml / ObjectNameAttributeReadingStrategy.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 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml;
9
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
12 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
13 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.ObjectNameAttributeMappingStrategy;
14 import org.opendaylight.controller.netconf.util.xml.XmlElement;
15 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
16
17 import java.util.List;
18 import java.util.Map;
19
20 public class ObjectNameAttributeReadingStrategy extends AbstractAttributeReadingStrategy<AttributeIfc> {
21
22     private static final Object PREFIX_SEPARATOR = ":";
23
24     public ObjectNameAttributeReadingStrategy(DependencyAttribute attributeIfc) {
25         super(attributeIfc);
26     }
27
28     @Override
29     AttributeConfigElement readElementHook(List<XmlElement> configNodes) {
30
31         XmlElement firstChild = configNodes.get(0);
32         Preconditions.checkState(configNodes.size() == 1, "This element should be present only once " + firstChild
33                 + " but was " + configNodes.size());
34
35         Preconditions.checkNotNull(firstChild, "Element %s should be present", firstChild);
36         return AttributeConfigElement.create(getAttributeIfc(), resolve(firstChild));
37     }
38
39     private ObjectNameAttributeMappingStrategy.MappedDependency resolve(XmlElement firstChild) {
40         XmlElement typeElement = firstChild.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.TYPE_KEY);
41         Map.Entry<String, String> prefixNamespace = typeElement.findNamespaceOfTextContent();
42
43         String serviceName = checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
44
45         XmlElement nameElement = firstChild.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.NAME_KEY);
46         String dependencyName = nameElement.getTextContent();
47
48         return new ObjectNameAttributeMappingStrategy.MappedDependency(prefixNamespace.getValue(), serviceName,
49                 dependencyName);
50     }
51
52     public static String checkPrefixAndExtractServiceName(XmlElement typeElement, Map.Entry<String, String> prefixNamespace) {
53         String serviceName = typeElement.getTextContent();
54
55         Preconditions.checkState(prefixNamespace.equals("") == false, "Service %s value not prefixed with namespace",
56                 XmlNetconfConstants.TYPE_KEY);
57         String prefix = prefixNamespace.getKey() + PREFIX_SEPARATOR;
58         Preconditions.checkState(serviceName.startsWith(prefix),
59                 "Service %s not correctly prefixed, expected %s, but was %s", XmlNetconfConstants.TYPE_KEY, prefix,
60                 serviceName);
61         serviceName = serviceName.substring(prefix.length());
62         return serviceName;
63     }
64
65 }