Merge "Statistics-Manager - Performance Improvement 1) Introduced statistics request...
[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 java.net.URI;
12 import java.util.Date;
13 import java.util.HashMap;
14 import java.util.Map;
15
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;
22
23
24 public class SimpleIdentityRefAttributeReadingStrategy extends SimpleAttributeReadingStrategy {
25
26     private final String key;
27     private final Map<String, Map<Date, EditConfig.IdentityMapping>> identityMap;
28
29     public SimpleIdentityRefAttributeReadingStrategy(String nullableDefault, String key, Map<String, Map<Date,EditConfig.IdentityMapping>> identityMap) {
30         super(nullableDefault);
31         this.key = key;
32         this.identityMap = identityMap;
33     }
34
35     @Override
36     protected String readElementContent(XmlElement xmlElement) throws NetconfDocumentedException {
37         // TODO test
38         Map.Entry<String, String> namespaceOfTextContent = xmlElement.findNamespaceOfTextContent();
39         String content = xmlElement.getTextContent();
40
41         String prefix = namespaceOfTextContent.getKey() + ":";
42         Preconditions.checkArgument(content.startsWith(prefix), "Identity ref should be prefixed");
43
44         String localName = content.substring(prefix.length());
45         String namespace = namespaceOfTextContent.getValue();
46
47         Date revision = null;
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);
54                     revision = date;
55                 }
56             }
57         } else {
58             revision = revisions.keySet().iterator().next();
59         }
60
61         return QName.create(URI.create(namespace), revision, localName).toString();
62     }
63
64     @Override
65     protected Object postprocessParsedValue(String textContent) {
66         HashMap<String,String> map = Maps.newHashMap();
67         map.put(key, textContent);
68         return map;
69     }
70
71     @Override
72     protected Object postprocessNullableDefault(String nullableDefault) {
73         return nullableDefault == null ? null : postprocessParsedValue(nullableDefault);
74     }
75 }