Decouple config and netconf subsystems.
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / attributes / fromxml / CompositeAttributeReadingStrategy.java
1 /*
2  * Copyright (c) 2015 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.base.Strings;
13 import com.google.common.collect.Lists;
14 import com.google.common.collect.Maps;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import org.opendaylight.controller.config.facade.xml.strategy.EditStrategyType;
19 import org.opendaylight.controller.config.util.xml.DocumentedException;
20 import org.opendaylight.controller.config.util.xml.XmlElement;
21 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
22
23 public class CompositeAttributeReadingStrategy extends AbstractAttributeReadingStrategy {
24
25     private final Map<String, AttributeReadingStrategy> innerStrategies;
26
27     public CompositeAttributeReadingStrategy(String nullableDefault,
28             Map<String, AttributeReadingStrategy> innerStrategies) {
29         super(nullableDefault);
30         this.innerStrategies = innerStrategies;
31     }
32
33     @Override
34     AttributeConfigElement readElementHook(List<XmlElement> configNodes) throws DocumentedException {
35
36         Preconditions.checkState(configNodes.size() == 1, "This element should be present only once %s", configNodes);
37
38         XmlElement complexElement = configNodes.get(0);
39
40         Map<String, Object> innerMap = Maps.newHashMap();
41
42         List<XmlElement> recognisedChildren = Lists.newArrayList();
43         for (Entry<String, AttributeReadingStrategy> innerAttrEntry : innerStrategies.entrySet()) {
44             List<XmlElement> childItem = complexElement.getChildElementsWithSameNamespace(
45                     innerAttrEntry.getKey());
46             recognisedChildren.addAll(childItem);
47
48             AttributeConfigElement resolvedInner = innerAttrEntry.getValue().readElement(childItem);
49
50             Object value = resolvedInner.getValue();
51             if(value == null) {
52                 value = resolvedInner.getDefaultValue();
53             }
54
55             innerMap.put(innerAttrEntry.getKey(), value);
56         }
57
58         complexElement.checkUnrecognisedElements(recognisedChildren);
59
60         String perInstanceEditStrategy = complexElement.getAttribute(XmlMappingConstants.OPERATION_ATTR_KEY,
61                 XmlMappingConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
62
63         return Strings.isNullOrEmpty(perInstanceEditStrategy) ? AttributeConfigElement.create(getNullableDefault(), innerMap) :
64                 AttributeConfigElement.create(getNullableDefault(), innerMap, EditStrategyType.valueOf(perInstanceEditStrategy));
65     }
66
67 }