2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml;
10 import com.google.common.base.Preconditions;
12 import com.google.common.base.Strings;
13 import java.util.List;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
18 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.ObjectNameAttributeMappingStrategy;
19 import org.opendaylight.controller.netconf.util.xml.XmlElement;
21 public class ObjectNameAttributeReadingStrategy extends AbstractAttributeReadingStrategy {
23 private static final Object PREFIX_SEPARATOR = ":";
25 public ObjectNameAttributeReadingStrategy(String nullableDefault) {
26 super(nullableDefault);
30 AttributeConfigElement readElementHook(List<XmlElement> configNodes) throws NetconfDocumentedException {
32 XmlElement firstChild = configNodes.get(0);
33 Preconditions.checkState(configNodes.size() == 1, "This element should be present only once " + firstChild
34 + " but was " + configNodes.size());
36 Preconditions.checkNotNull(firstChild, "Element %s should be present", firstChild);
37 return AttributeConfigElement.create(getNullableDefault(), resolve(firstChild));
40 private ObjectNameAttributeMappingStrategy.MappedDependency resolve(XmlElement firstChild) throws NetconfDocumentedException{
41 XmlElement typeElement = firstChild.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.TYPE_KEY);
42 Map.Entry<String, String> prefixNamespace = typeElement.findNamespaceOfTextContent();
44 String serviceName = checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
46 XmlElement nameElement = firstChild.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.NAME_KEY);
47 String dependencyName = nameElement.getTextContent();
49 return new ObjectNameAttributeMappingStrategy.MappedDependency(prefixNamespace.getValue(), serviceName,
53 public static String checkPrefixAndExtractServiceName(XmlElement typeElement, Map.Entry<String, String> prefixNamespace) throws NetconfDocumentedException {
54 String serviceName = typeElement.getTextContent();
55 // FIXME: comparing Entry with String:
56 Preconditions.checkState(!Strings.isNullOrEmpty(prefixNamespace.getKey()), "Service %s value not prefixed with namespace",
57 XmlNetconfConstants.TYPE_KEY);
58 String prefix = prefixNamespace.getKey() + PREFIX_SEPARATOR;
59 Preconditions.checkState(serviceName.startsWith(prefix),
60 "Service %s not correctly prefixed, expected %s, but was %s", XmlNetconfConstants.TYPE_KEY, prefix,
62 serviceName = serviceName.substring(prefix.length());