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.Preconditions;
12 import com.google.common.collect.Maps;
14 import java.util.Date;
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;
22 public class SimpleIdentityRefAttributeReadingStrategy extends SimpleAttributeReadingStrategy {
24 private final String key;
25 private final Map<String, Map<Date, IdentityMapping>> identityMap;
27 public SimpleIdentityRefAttributeReadingStrategy(final String nullableDefault, final String key,
28 final Map<String, Map<Date, IdentityMapping>> identityMap) {
29 super(nullableDefault);
31 this.identityMap = identityMap;
35 protected String readElementContent(final XmlElement xmlElement) throws DocumentedException {
36 Map.Entry<String, String> namespaceOfTextContent = xmlElement.findNamespaceOfTextContent();
37 String content = xmlElement.getTextContent();
39 final String namespace;
40 final String localName;
41 if (namespaceOfTextContent.getKey().isEmpty()) {
43 namespace = xmlElement.getNamespace();
45 String prefix = namespaceOfTextContent.getKey() + ":";
46 Preconditions.checkArgument(content.startsWith(prefix), "Identity ref should be prefixed with \"%s\"",
48 localName = content.substring(prefix.length());
49 namespace = namespaceOfTextContent.getValue();
53 Map<Date, IdentityMapping> revisions = identityMap.get(namespace);
54 if (revisions.keySet().size() > 1) {
55 for (Map.Entry<Date, IdentityMapping> revisionToIdentityEntry : revisions.entrySet()) {
56 if (revisionToIdentityEntry.getValue().containsIdName(localName)) {
57 Preconditions.checkState(revision == null,
58 "Duplicate identity %s, in namespace %s, "
59 + "with revisions: %s, %s detected. Cannot map attribute",
60 localName, namespace, revision, revisionToIdentityEntry.getKey());
61 revision = revisionToIdentityEntry.getKey();
65 revision = revisions.keySet().iterator().next();
67 return QName.create(URI.create(namespace), revision, localName).toString();
71 protected Object postprocessParsedValue(final String textContent) {
72 HashMap<String, String> map = Maps.newHashMap();
73 map.put(key, textContent);
78 protected Object postprocessNullableDefault(final String nullableDefault) {
79 return nullableDefault == null ? null : postprocessParsedValue(nullableDefault);