2 * Copyright (c) 2013 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.netconf.confignetconfconnector.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.netconf.api.NetconfDocumentedException;
18 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
19 import org.opendaylight.controller.netconf.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, EditConfig.IdentityMapping>> identityMap;
28 public SimpleIdentityRefAttributeReadingStrategy(String nullableDefault, String key, Map<String, Map<Date,EditConfig.IdentityMapping>> identityMap) {
29 super(nullableDefault);
31 this.identityMap = identityMap;
35 protected String readElementContent(XmlElement xmlElement) throws NetconfDocumentedException {
37 Map.Entry<String, String> namespaceOfTextContent = xmlElement.findNamespaceOfTextContent();
38 String content = xmlElement.getTextContent();
40 String prefix = namespaceOfTextContent.getKey() + ":";
41 Preconditions.checkArgument(content.startsWith(prefix), "Identity ref should be prefixed");
43 String localName = content.substring(prefix.length());
44 String namespace = namespaceOfTextContent.getValue();
47 Map<Date, EditConfig.IdentityMapping> revisions = identityMap.get(namespace);
48 if(revisions.keySet().size() > 1) {
49 for (Map.Entry<Date, EditConfig.IdentityMapping> revisionToIdentityEntry : revisions.entrySet()) {
50 if(revisionToIdentityEntry.getValue().containsIdName(localName)) {
51 Preconditions.checkState(revision == null, "Duplicate identity %s, in namespace %s, with revisions: %s, %s detected. Cannot map attribute",
52 localName, namespace, revision, revisionToIdentityEntry.getKey());
53 revision = revisionToIdentityEntry.getKey();
57 revision = revisions.keySet().iterator().next();
60 return QName.create(URI.create(namespace), revision, localName).toString();
64 protected Object postprocessParsedValue(String textContent) {
65 HashMap<String,String> map = Maps.newHashMap();
66 map.put(key, textContent);
71 protected Object postprocessNullableDefault(String nullableDefault) {
72 return nullableDefault == null ? null : postprocessParsedValue(nullableDefault);