BUG-3675 Support non-prefixed identityrefs in config subsystem
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / fromxml / SimpleIdentityRefAttributeReadingStrategy.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Maps;
13 import java.net.URI;
14 import java.util.Date;
15 import java.util.HashMap;
16 import java.util.Map;
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;
21
22
23 public class SimpleIdentityRefAttributeReadingStrategy extends SimpleAttributeReadingStrategy {
24
25     private final String key;
26     private final Map<String, Map<Date, EditConfig.IdentityMapping>> identityMap;
27
28     public SimpleIdentityRefAttributeReadingStrategy(String nullableDefault, String key, Map<String, Map<Date,EditConfig.IdentityMapping>> identityMap) {
29         super(nullableDefault);
30         this.key = key;
31         this.identityMap = identityMap;
32     }
33
34     @Override
35     protected String readElementContent(XmlElement xmlElement) throws NetconfDocumentedException {
36         Map.Entry<String, String> namespaceOfTextContent = xmlElement.findNamespaceOfTextContent();
37         String content = xmlElement.getTextContent();
38
39         final String namespace;
40         final String localName;
41         if(namespaceOfTextContent.getKey().isEmpty()) {
42             localName = content;
43             namespace = xmlElement.getNamespace();
44         } else {
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();
49         }
50
51         Date revision = null;
52         Map<Date, EditConfig.IdentityMapping> revisions = identityMap.get(namespace);
53         if(revisions.keySet().size() > 1) {
54             for (Map.Entry<Date, EditConfig.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();
59                 }
60             }
61         } else {
62             revision = revisions.keySet().iterator().next();
63         }
64
65         return QName.create(URI.create(namespace), revision, localName).toString();
66     }
67
68     @Override
69     protected Object postprocessParsedValue(String textContent) {
70         HashMap<String,String> map = Maps.newHashMap();
71         map.put(key, textContent);
72         return map;
73     }
74
75     @Override
76     protected Object postprocessNullableDefault(String nullableDefault) {
77         return nullableDefault == null ? null : postprocessParsedValue(nullableDefault);
78     }
79 }