Merge "BUG-2243 Fixing invalid hello message handling"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / fromxml / CompositeAttributeReadingStrategy.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.Lists;
13 import com.google.common.collect.Maps;
14
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.util.xml.XmlElement;
17
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Map.Entry;
21
22 public class CompositeAttributeReadingStrategy extends AbstractAttributeReadingStrategy {
23
24     private final Map<String, AttributeReadingStrategy> innerStrategies;
25
26     public CompositeAttributeReadingStrategy(String nullableDefault,
27             Map<String, AttributeReadingStrategy> innerStrategies) {
28         super(nullableDefault);
29         this.innerStrategies = innerStrategies;
30     }
31
32     @Override
33     AttributeConfigElement readElementHook(List<XmlElement> configNodes) throws NetconfDocumentedException {
34
35         Preconditions.checkState(configNodes.size() == 1, "This element should be present only once %s", configNodes);
36
37         XmlElement complexElement = configNodes.get(0);
38
39         Map<String, Object> innerMap = Maps.newHashMap();
40
41         List<XmlElement> recognisedChildren = Lists.newArrayList();
42         for (Entry<String, AttributeReadingStrategy> innerAttrEntry : innerStrategies.entrySet()) {
43             List<XmlElement> childItem = complexElement.getChildElementsWithSameNamespace(
44                     innerAttrEntry.getKey());
45             recognisedChildren.addAll(childItem);
46
47             AttributeConfigElement resolvedInner = innerAttrEntry.getValue().readElement(childItem);
48
49             Object value = resolvedInner.getValue();
50             if(value == null) {
51                 value = resolvedInner.getDefaultValue();
52             }
53
54             innerMap.put(innerAttrEntry.getKey(), value);
55         }
56
57         complexElement.checkUnrecognisedElements(recognisedChildren);
58
59         return AttributeConfigElement.create(getNullableDefault(), innerMap);
60     }
61
62 }