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