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;
12 import java.util.Date;
13 import java.util.HashMap;
16 import com.google.common.base.Preconditions;
17 import com.google.common.collect.Maps;
18 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
19 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
20 import org.opendaylight.controller.netconf.util.xml.XmlElement;
21 import org.opendaylight.yangtools.yang.common.QName;
24 public class SimpleIdentityRefAttributeReadingStrategy extends SimpleAttributeReadingStrategy {
26 private final String key;
27 private final Map<String, Map<Date, EditConfig.IdentityMapping>> identityMap;
29 public SimpleIdentityRefAttributeReadingStrategy(String nullableDefault, String key, Map<String, Map<Date,EditConfig.IdentityMapping>> identityMap) {
30 super(nullableDefault);
32 this.identityMap = identityMap;
36 protected String readElementContent(XmlElement xmlElement) throws NetconfDocumentedException {
38 Map.Entry<String, String> namespaceOfTextContent = xmlElement.findNamespaceOfTextContent();
39 String content = xmlElement.getTextContent();
41 String prefix = namespaceOfTextContent.getKey() + ":";
42 Preconditions.checkArgument(content.startsWith(prefix), "Identity ref should be prefixed");
44 String localName = content.substring(prefix.length());
45 String namespace = namespaceOfTextContent.getValue();
48 Map<Date, EditConfig.IdentityMapping> revisions = identityMap.get(namespace);
49 if(revisions.keySet().size() > 1) {
50 for (Date date : revisions.keySet()) {
51 if(revisions.get(date).containsIdName(localName)) {
52 Preconditions.checkState(revision == null, "Duplicate identity %s, in namespace %s, with revisions: %s, %s detected. Cannot map attribute",
53 localName, namespace, revision, date);
58 revision = revisions.keySet().iterator().next();
61 return QName.create(URI.create(namespace), revision, localName).toString();
65 protected Object postprocessParsedValue(String textContent) {
66 HashMap<String,String> map = Maps.newHashMap();
67 map.put(key, textContent);
72 protected Object postprocessNullableDefault(String nullableDefault) {
73 return nullableDefault == null ? null : postprocessParsedValue(nullableDefault);