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