BUG 652 leafref CCE & BUG 720 colons problem
[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.netconf.confignetconfconnector.mapping.attributes.mapping.ObjectNameAttributeMappingStrategy;
12 import org.opendaylight.controller.netconf.util.xml.XmlElement;
13 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
14
15 import java.util.List;
16 import java.util.Map;
17
18 public class ObjectNameAttributeReadingStrategy extends AbstractAttributeReadingStrategy {
19
20     private static final Object PREFIX_SEPARATOR = ":";
21
22     public ObjectNameAttributeReadingStrategy(String nullableDefault) {
23         super(nullableDefault);
24     }
25
26     @Override
27     AttributeConfigElement readElementHook(List<XmlElement> configNodes) {
28
29         XmlElement firstChild = configNodes.get(0);
30         Preconditions.checkState(configNodes.size() == 1, "This element should be present only once " + firstChild
31                 + " but was " + configNodes.size());
32
33         Preconditions.checkNotNull(firstChild, "Element %s should be present", firstChild);
34         return AttributeConfigElement.create(getNullableDefault(), resolve(firstChild));
35     }
36
37     private ObjectNameAttributeMappingStrategy.MappedDependency resolve(XmlElement firstChild) {
38         XmlElement typeElement = firstChild.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.TYPE_KEY);
39         Map.Entry<String, String> prefixNamespace = typeElement.findNamespaceOfTextContent();
40
41         String serviceName = checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
42
43         XmlElement nameElement = firstChild.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.NAME_KEY);
44         String dependencyName = nameElement.getTextContent();
45
46         return new ObjectNameAttributeMappingStrategy.MappedDependency(prefixNamespace.getValue(), serviceName,
47                 dependencyName);
48     }
49
50     public static String checkPrefixAndExtractServiceName(XmlElement typeElement, Map.Entry<String, String> prefixNamespace) {
51         String serviceName = typeElement.getTextContent();
52
53         Preconditions.checkState(prefixNamespace.equals("") == false, "Service %s value not prefixed with namespace",
54                 XmlNetconfConstants.TYPE_KEY);
55         String prefix = prefixNamespace.getKey() + PREFIX_SEPARATOR;
56         Preconditions.checkState(serviceName.startsWith(prefix),
57                 "Service %s not correctly prefixed, expected %s, but was %s", XmlNetconfConstants.TYPE_KEY, prefix,
58                 serviceName);
59         serviceName = serviceName.substring(prefix.length());
60         return serviceName;
61     }
62
63 }