c477821051b08d03288d129fc29c1aaddd32eef6
[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 final 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
53         Map<String, Object> items = Maps.newHashMap();
54         Map<String, OpenType<?>> openTypes = Maps.newHashMap();
55
56         for (Object innerAttrName : innerTypes.keySet()) {
57             Preconditions.checkState(innerAttrName instanceof String, "Attribute name must be string");
58             String innerAttrNameStr = (String) innerAttrName;
59
60             AttributeResolvingStrategy<?, ? extends OpenType<?>> attributeResolvingStrategy = innerTypes
61                     .get(innerAttrName);
62
63             Object valueToParse = valueMap.get(innerAttrName);
64
65             Optional<?> parsedInnerValue = attributeResolvingStrategy.parseAttribute(innerAttrNameStr, valueToParse);
66
67             openTypes.put(innerAttrNameStr, attributeResolvingStrategy.getOpenType());
68
69             items.put(yangToJavaAttrMapping.get(innerAttrNameStr),
70                     parsedInnerValue.isPresent() ? parsedInnerValue.get() : null);
71         }
72
73         CompositeDataSupport parsedValue;
74         try {
75             parsedValue = new CompositeDataSupport(getOpenType(), items);
76         } catch (OpenDataException e) {
77             throw new RuntimeException("An error occured during restoration of composite type " + this
78                     + " for attribute " + attrName + " from value " + value, e);
79         }
80
81         logger.debug("Attribute {} : {} parsed to type {} as {}", attrName, value, getOpenType(), parsedValue);
82
83         return Optional.of(parsedValue);
84     }
85 }