Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / attributes / fromxml / ObjectNameAttributeReadingStrategy.java
1 /*
2  * Copyright (c) 2015, 2017 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.config.facade.xml.mapping.attributes.fromxml;
9
10 import com.google.common.base.Preconditions;
11 import java.util.List;
12 import java.util.Map;
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;
17
18 public class ObjectNameAttributeReadingStrategy extends AbstractAttributeReadingStrategy {
19
20     private static final Object PREFIX_SEPARATOR = ":";
21
22     public ObjectNameAttributeReadingStrategy(final String nullableDefault) {
23         super(nullableDefault);
24     }
25
26     @Override
27     AttributeConfigElement readElementHook(final List<XmlElement> configNodes) throws DocumentedException {
28
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());
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(final XmlElement firstChild)
38             throws DocumentedException {
39         XmlElement typeElement = firstChild.getOnlyChildElementWithSameNamespace(XmlMappingConstants.TYPE_KEY);
40         Map.Entry<String, String> prefixNamespace = typeElement.findNamespaceOfTextContent();
41
42         String serviceName = checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
43
44         XmlElement nameElement = firstChild.getOnlyChildElementWithSameNamespace(XmlMappingConstants.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(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()) {
57             return serviceName;
58         } else {
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,
62                     serviceName);
63             serviceName = serviceName.substring(prefix.length());
64             return serviceName;
65         }
66     }
67 }