Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / attributes / fromxml / SimpleIdentityRefAttributeReadingStrategy.java
1 /*
2  * Copyright (c) 2015, 2017 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.config.facade.xml.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.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;
21
22 public class SimpleIdentityRefAttributeReadingStrategy extends SimpleAttributeReadingStrategy {
23
24     private final String key;
25     private final Map<String, Map<Date, IdentityMapping>> identityMap;
26
27     public SimpleIdentityRefAttributeReadingStrategy(final String nullableDefault, final String key,
28             final Map<String, Map<Date, IdentityMapping>> identityMap) {
29         super(nullableDefault);
30         this.key = key;
31         this.identityMap = identityMap;
32     }
33
34     @Override
35     protected String readElementContent(final XmlElement xmlElement) throws DocumentedException {
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\"",
47                     prefix);
48             localName = content.substring(prefix.length());
49             namespace = namespaceOfTextContent.getValue();
50         }
51
52         Date revision = null;
53         Map<Date, IdentityMapping> revisions = identityMap.get(namespace);
54         if (revisions.keySet().size() > 1) {
55             for (Map.Entry<Date, IdentityMapping> revisionToIdentityEntry : revisions.entrySet()) {
56                 if (revisionToIdentityEntry.getValue().containsIdName(localName)) {
57                     Preconditions.checkState(revision == null,
58                             "Duplicate identity %s, in namespace %s, "
59                             + "with revisions: %s, %s detected. Cannot map attribute",
60                             localName, namespace, revision, revisionToIdentityEntry.getKey());
61                     revision = revisionToIdentityEntry.getKey();
62                 }
63             }
64         } else {
65             revision = revisions.keySet().iterator().next();
66         }
67         return QName.create(URI.create(namespace), revision, localName).toString();
68     }
69
70     @Override
71     protected Object postprocessParsedValue(final String textContent) {
72         HashMap<String, String> map = Maps.newHashMap();
73         map.put(key, textContent);
74         return map;
75     }
76
77     @Override
78     protected Object postprocessNullableDefault(final String nullableDefault) {
79         return nullableDefault == null ? null : postprocessParsedValue(nullableDefault);
80     }
81 }