package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml;
import com.google.common.base.Preconditions;
-import com.google.common.base.Strings;
import java.util.List;
import java.util.Map;
import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
public static String checkPrefixAndExtractServiceName(XmlElement typeElement, Map.Entry<String, String> prefixNamespace) throws NetconfDocumentedException {
String serviceName = typeElement.getTextContent();
- // FIXME: comparing Entry with String:
- Preconditions.checkState(!Strings.isNullOrEmpty(prefixNamespace.getKey()), "Service %s value not prefixed with namespace",
+ Preconditions.checkNotNull(prefixNamespace.getKey(), "Service %s value cannot be linked to namespace",
XmlNetconfConstants.TYPE_KEY);
- String prefix = prefixNamespace.getKey() + PREFIX_SEPARATOR;
- Preconditions.checkState(serviceName.startsWith(prefix),
- "Service %s not correctly prefixed, expected %s, but was %s", XmlNetconfConstants.TYPE_KEY, prefix,
- serviceName);
- serviceName = serviceName.substring(prefix.length());
- return serviceName;
+ if(prefixNamespace.getKey().isEmpty()) {
+ return serviceName;
+ } else {
+ String prefix = prefixNamespace.getKey() + PREFIX_SEPARATOR;
+ Preconditions.checkState(serviceName.startsWith(prefix),
+ "Service %s not correctly prefixed, expected %s, but was %s", XmlNetconfConstants.TYPE_KEY, prefix,
+ serviceName);
+ serviceName = serviceName.substring(prefix.length());
+ return serviceName;
+ }
}
}
@Override
protected String readElementContent(XmlElement xmlElement) throws NetconfDocumentedException {
- // TODO test
Map.Entry<String, String> namespaceOfTextContent = xmlElement.findNamespaceOfTextContent();
String content = xmlElement.getTextContent();
- String prefix = namespaceOfTextContent.getKey() + ":";
- Preconditions.checkArgument(content.startsWith(prefix), "Identity ref should be prefixed");
-
- String localName = content.substring(prefix.length());
- String namespace = namespaceOfTextContent.getValue();
+ final String namespace;
+ final String localName;
+ if(namespaceOfTextContent.getKey().isEmpty()) {
+ localName = content;
+ namespace = xmlElement.getNamespace();
+ } else {
+ String prefix = namespaceOfTextContent.getKey() + ":";
+ Preconditions.checkArgument(content.startsWith(prefix), "Identity ref should be prefixed with \"%s\"", prefix);
+ localName = content.substring(prefix.length());
+ namespace = namespaceOfTextContent.getValue();
+ }
Date revision = null;
Map<Date, EditConfig.IdentityMapping> revisions = identityMap.get(namespace);
public static class IdentityMapping {
private final Map<String, IdentitySchemaNode> identityNameToSchemaNode;
- IdentityMapping() {
+ public IdentityMapping() {
this.identityNameToSchemaNode = Maps.newHashMap();
}
--- /dev/null
+package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml;
+
+import static org.junit.Assert.assertEquals;
+
+import com.google.common.collect.Maps;
+import java.net.URI;
+import java.util.Collections;
+import java.util.Date;
+import java.util.Map;
+import org.junit.Test;
+import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
+import org.opendaylight.controller.netconf.util.xml.XmlElement;
+
+public class SimpleIdentityRefAttributeReadingStrategyTest {
+
+ @Test
+ public void testReadIdRef() throws Exception {
+ final Map<String, Map<Date, EditConfig.IdentityMapping>> identityMapping = Maps.newHashMap();
+ final EditConfig.IdentityMapping value = new EditConfig.IdentityMapping();
+ final Date rev = new Date();
+ identityMapping.put("namespace", Collections.singletonMap(rev, value));
+ identityMapping.put("inner", Collections.singletonMap(rev, value));
+ final SimpleIdentityRefAttributeReadingStrategy key = new SimpleIdentityRefAttributeReadingStrategy(null, "key", identityMapping);
+
+ String read = key.readElementContent(XmlElement.fromString("<el xmlns=\"namespace\">local</el>"));
+ assertEquals(org.opendaylight.yangtools.yang.common.QName.create(URI.create("namespace"), rev, "local").toString(), read);
+
+ read = key.readElementContent(XmlElement.fromString("<el xmlns:a=\"inner\" xmlns=\"namespace\">a:local</el>"));
+ assertEquals(org.opendaylight.yangtools.yang.common.QName.create(URI.create("inner"), rev, "local").toString(), read);
+
+ read = key.readElementContent(XmlElement.fromString("<top xmlns=\"namespace\"><el>local</el></top>").getOnlyChildElement());
+ assertEquals(org.opendaylight.yangtools.yang.common.QName.create(URI.create("namespace"), rev, "local").toString(), read);
+ }
+}
\ No newline at end of file