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