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