2 * Copyright (c) 2015 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;
23 public class SimpleIdentityRefAttributeReadingStrategy extends SimpleAttributeReadingStrategy {
25 private final String key;
26 private final Map<String, Map<Date, IdentityMapping>> identityMap;
28 public SimpleIdentityRefAttributeReadingStrategy(String nullableDefault, String key, Map<String, Map<Date, IdentityMapping>> identityMap) {
29 super(nullableDefault);
31 this.identityMap = identityMap;
35 protected String readElementContent(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\"", prefix);
47 localName = content.substring(prefix.length());
48 namespace = namespaceOfTextContent.getValue();
52 Map<Date, IdentityMapping> revisions = identityMap.get(namespace);
53 if(revisions.keySet().size() > 1) {
54 for (Map.Entry<Date, IdentityMapping> revisionToIdentityEntry : revisions.entrySet()) {
55 if(revisionToIdentityEntry.getValue().containsIdName(localName)) {
56 Preconditions.checkState(revision == null, "Duplicate identity %s, in namespace %s, with revisions: %s, %s detected. Cannot map attribute",
57 localName, namespace, revision, revisionToIdentityEntry.getKey());
58 revision = revisionToIdentityEntry.getKey();
62 revision = revisions.keySet().iterator().next();
64 return QName.create(URI.create(namespace), revision, localName).toString();
68 protected Object postprocessParsedValue(String textContent) {
69 HashMap<String,String> map = Maps.newHashMap();
70 map.put(key, textContent);
75 protected Object postprocessNullableDefault(String nullableDefault) {
76 return nullableDefault == null ? null : postprocessParsedValue(nullableDefault);