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
9 package org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Maps;
15 import java.util.HashMap;
17 import org.opendaylight.controller.config.facade.xml.mapping.IdentityMapping;
18 import org.opendaylight.controller.config.util.xml.DocumentedException;
19 import org.opendaylight.controller.config.util.xml.XmlElement;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.Revision;
23 public class SimpleIdentityRefAttributeReadingStrategy extends SimpleAttributeReadingStrategy {
25 private final String key;
26 private final Map<String, Map<Optional<Revision>, IdentityMapping>> identityMap;
28 public SimpleIdentityRefAttributeReadingStrategy(final String nullableDefault, final String key,
29 final Map<String, Map<Optional<Revision>, IdentityMapping>> identityMap) {
30 super(nullableDefault);
32 this.identityMap = identityMap;
36 protected String readElementContent(final XmlElement xmlElement) throws DocumentedException {
37 Map.Entry<String, String> namespaceOfTextContent = xmlElement.findNamespaceOfTextContent();
38 String content = xmlElement.getTextContent();
40 final String namespace;
41 final String localName;
42 if (namespaceOfTextContent.getKey().isEmpty()) {
44 namespace = xmlElement.getNamespace();
46 String prefix = namespaceOfTextContent.getKey() + ":";
47 Preconditions.checkArgument(content.startsWith(prefix), "Identity ref should be prefixed with \"%s\"",
49 localName = content.substring(prefix.length());
50 namespace = namespaceOfTextContent.getValue();
53 Optional<Revision> revision = null;
54 Map<Optional<Revision>, IdentityMapping> revisions = identityMap.get(namespace);
55 if (revisions.keySet().size() > 1) {
56 for (Map.Entry<Optional<Revision>, IdentityMapping> revisionToIdentityEntry : revisions.entrySet()) {
57 if (revisionToIdentityEntry.getValue().containsIdName(localName)) {
58 Preconditions.checkState(revision == null,
59 "Duplicate identity %s, in namespace %s, "
60 + "with revisions: %s, %s detected. Cannot map attribute",
61 localName, namespace, revision, revisionToIdentityEntry.getKey());
62 revision = revisionToIdentityEntry.getKey();
66 revision = revisions.keySet().iterator().next();
68 return QName.create(URI.create(namespace), revision.toJavaUtil(), localName).toString();
72 protected Object postprocessParsedValue(final String textContent) {
73 HashMap<String, String> map = Maps.newHashMap();
74 map.put(key, textContent);
79 protected Object postprocessNullableDefault(final String nullableDefault) {
80 return nullableDefault == null ? null : postprocessParsedValue(nullableDefault);