2 * Copyright (c) 2015, 2017 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.config.facade.xml.mapping.attributes.fromxml;
10 import com.google.common.base.Preconditions;
11 import java.util.List;
13 import org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.ObjectNameAttributeMappingStrategy;
14 import org.opendaylight.controller.config.util.xml.DocumentedException;
15 import org.opendaylight.controller.config.util.xml.XmlElement;
16 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
18 public class ObjectNameAttributeReadingStrategy extends AbstractAttributeReadingStrategy {
20 private static final Object PREFIX_SEPARATOR = ":";
22 public ObjectNameAttributeReadingStrategy(final String nullableDefault) {
23 super(nullableDefault);
27 AttributeConfigElement readElementHook(final List<XmlElement> configNodes) throws DocumentedException {
29 XmlElement firstChild = configNodes.get(0);
30 Preconditions.checkState(configNodes.size() == 1,
31 "This element should be present only once " + firstChild + " but was " + configNodes.size());
33 Preconditions.checkNotNull(firstChild, "Element %s should be present", firstChild);
34 return AttributeConfigElement.create(getNullableDefault(), resolve(firstChild));
37 private ObjectNameAttributeMappingStrategy.MappedDependency resolve(final XmlElement firstChild)
38 throws DocumentedException {
39 XmlElement typeElement = firstChild.getOnlyChildElementWithSameNamespace(XmlMappingConstants.TYPE_KEY);
40 Map.Entry<String, String> prefixNamespace = typeElement.findNamespaceOfTextContent();
42 String serviceName = checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
44 XmlElement nameElement = firstChild.getOnlyChildElementWithSameNamespace(XmlMappingConstants.NAME_KEY);
45 String dependencyName = nameElement.getTextContent();
47 return new ObjectNameAttributeMappingStrategy.MappedDependency(prefixNamespace.getValue(), serviceName,
51 public static String checkPrefixAndExtractServiceName(final XmlElement typeElement,
52 final Map.Entry<String, String> prefixNamespace) throws DocumentedException {
53 String serviceName = typeElement.getTextContent();
54 Preconditions.checkNotNull(prefixNamespace.getKey(), "Service %s value cannot be linked to namespace",
55 XmlMappingConstants.TYPE_KEY);
56 if (prefixNamespace.getKey().isEmpty()) {
59 String prefix = prefixNamespace.getKey() + PREFIX_SEPARATOR;
60 Preconditions.checkState(serviceName.startsWith(prefix),
61 "Service %s not correctly prefixed, expected %s, but was %s", XmlMappingConstants.TYPE_KEY, prefix,
63 serviceName = serviceName.substring(prefix.length());