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 / ArrayAttributeResolvingStrategy.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 org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import javax.management.openmbean.ArrayType;
17 import javax.management.openmbean.CompositeDataSupport;
18 import javax.management.openmbean.CompositeType;
19 import javax.management.openmbean.OpenType;
20 import java.lang.reflect.Array;
21 import java.util.List;
22
23 final class ArrayAttributeResolvingStrategy extends AbstractAttributeResolvingStrategy<Object, ArrayType<?>> {
24
25     private final AttributeResolvingStrategy<?, ? extends OpenType<?>> innerTypeResolvingStrategy;
26
27     private static final Logger logger = LoggerFactory.getLogger(ArrayAttributeResolvingStrategy.class);
28
29     public ArrayAttributeResolvingStrategy(AttributeResolvingStrategy<?, ? extends OpenType<?>> innerTypeResolved,
30             ArrayType<?> openType) {
31         super(openType);
32         this.innerTypeResolvingStrategy = innerTypeResolved;
33     }
34
35     @Override
36     public Optional<Object> parseAttribute(String attrName, Object value) {
37         if (value == null) {
38             return Optional.absent();
39         }
40
41         Util.checkType(value, List.class);
42         List<?> valueList = (List<?>) value;
43
44         Class<?> innerTypeClass = null;
45
46         if (innerTypeResolvingStrategy.getOpenType() instanceof CompositeType) {
47             innerTypeClass = CompositeDataSupport.class;
48         } else
49             try {
50                 innerTypeClass = Class.forName(getOpenType().getElementOpenType().getClassName());
51             } catch (ClassNotFoundException e) {
52                 throw new RuntimeException("Unable to locate class for "
53                         + getOpenType().getElementOpenType().getClassName(), e);
54             }
55
56         Object parsedArray = null;
57
58         if (getOpenType().isPrimitiveArray()) {
59             Class<?> primitiveType = getPrimitiveType(innerTypeClass);
60             parsedArray = Array.newInstance(primitiveType, valueList.size());
61         } else
62             parsedArray = Array.newInstance(innerTypeClass, valueList.size());
63
64         int i = 0;
65         for (Object innerValue : valueList) {
66             Optional<?> parsedElement = innerTypeResolvingStrategy.parseAttribute(attrName + "_" + i, innerValue);
67
68             if (!parsedElement.isPresent())
69                 continue;
70
71             Array.set(parsedArray, i, parsedElement.get());
72             // parsedArray[i] = parsedElement.get();
73             i++;
74         }
75
76         logger.debug("Attribute {} : {} parsed to type {} as {}", attrName, value, getOpenType(),
77                 toStringArray(parsedArray));
78
79         return Optional.of(parsedArray);
80     }
81
82     private static String toStringArray(Object array) {
83         StringBuilder build = new StringBuilder(array.toString());
84         build.append(" [");
85         for (int i = 0; i < Array.getLength(array); i++) {
86             build.append(Array.get(array, i).toString());
87             build.append(",");
88         }
89         build.append("]]");
90         return build.toString();
91     }
92
93     private static Class<?> getPrimitiveType(Class<?> innerTypeClass) {
94         try {
95             return (Class<?>) innerTypeClass.getField("TYPE").get(null);
96         } catch (Exception e) {
97             throw new RuntimeException("Unable to determine primitive type to " + innerTypeClass);
98         }
99     }
100
101     @Override
102     public String toString() {
103         return "ResolvedArrayTypeAttributeType [innerTypeResolved=" + innerTypeResolvingStrategy + "]";
104     }
105 }