Merge "Bug 616 - Eliminate the use of xtend in md-sal/topology-lldp-discovery"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / resolving / CompositeAttributeResolvingStrategy.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.resolving;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Maps;
14 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 import javax.management.openmbean.CompositeDataSupport;
19 import javax.management.openmbean.CompositeType;
20 import javax.management.openmbean.OpenDataException;
21 import javax.management.openmbean.OpenType;
22 import java.util.Map;
23
24 class CompositeAttributeResolvingStrategy extends
25         AbstractAttributeResolvingStrategy<CompositeDataSupport, CompositeType> {
26     private final Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerTypes;
27     private final Map<String, String> yangToJavaAttrMapping;
28
29     private static final Logger logger = LoggerFactory.getLogger(CompositeAttributeResolvingStrategy.class);
30
31     CompositeAttributeResolvingStrategy(Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerTypes,
32             CompositeType openType, Map<String, String> yangToJavaAttrMapping) {
33         super(openType);
34         this.innerTypes = innerTypes;
35         this.yangToJavaAttrMapping = yangToJavaAttrMapping;
36     }
37
38     @Override
39     public String toString() {
40         return "ResolvedCompositeAttribute [" + innerTypes + "]";
41     }
42
43     @Override
44     public Optional<CompositeDataSupport> parseAttribute(String attrName, Object value) {
45
46         if (value == null) {
47             return Optional.absent();
48         }
49
50         Util.checkType(value, Map.class);
51         Map<?, ?> valueMap = (Map<?, ?>) value;
52         valueMap = preprocessValueMap(valueMap);
53
54         Map<String, Object> items = Maps.newHashMap();
55         Map<String, OpenType<?>> openTypes = Maps.newHashMap();
56
57         for (Object innerAttrName : innerTypes.keySet()) {
58             Preconditions.checkState(innerAttrName instanceof String, "Attribute name must be string");
59             String innerAttrNameStr = (String) innerAttrName;
60
61             AttributeResolvingStrategy<?, ? extends OpenType<?>> attributeResolvingStrategy = innerTypes
62                     .get(innerAttrName);
63
64             Object valueToParse = valueMap.get(innerAttrName);
65
66             Optional<?> parsedInnerValue = attributeResolvingStrategy.parseAttribute(innerAttrNameStr, valueToParse);
67
68             openTypes.put(innerAttrNameStr, attributeResolvingStrategy.getOpenType());
69
70             items.put(yangToJavaAttrMapping.get(innerAttrNameStr),
71                     parsedInnerValue.isPresent() ? parsedInnerValue.get() : null);
72         }
73
74         CompositeDataSupport parsedValue;
75         try {
76             parsedValue = new CompositeDataSupport(getOpenType(), items);
77         } catch (OpenDataException e) {
78             throw new RuntimeException("An error occured during restoration of composite type " + this
79                     + " for attribute " + attrName + " from value " + value, e);
80         }
81
82         logger.debug("Attribute {} : {} parsed to type {} as {}", attrName, value, getOpenType(), parsedValue);
83
84         return Optional.of(parsedValue);
85     }
86
87     protected Map<?, ?> preprocessValueMap(Map<?, ?> valueMap) {
88         return valueMap;
89     }
90 }