Decouple config and netconf subsystems.
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / attributes / resolving / ArrayAttributeResolvingStrategy.java
1 /*
2  * Copyright (c) 2015 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.config.facade.xml.mapping.attributes.resolving;
10
11 import com.google.common.base.Optional;
12 import java.lang.reflect.Array;
13 import java.util.List;
14 import javax.management.openmbean.ArrayType;
15 import javax.management.openmbean.CompositeDataSupport;
16 import javax.management.openmbean.CompositeType;
17 import javax.management.openmbean.OpenDataException;
18 import javax.management.openmbean.OpenType;
19 import org.opendaylight.controller.config.facade.xml.util.Util;
20 import org.opendaylight.controller.config.util.xml.DocumentedException;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 final class ArrayAttributeResolvingStrategy extends AbstractAttributeResolvingStrategy<Object, ArrayType<?>> {
25
26     private final AttributeResolvingStrategy<?, ? extends OpenType<?>> innerTypeResolvingStrategy;
27
28     private static final Logger LOG = 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 DocumentedException {
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         // Rebuild open type. Underlying composite types might have changed
78         if (innerTypeResolvingStrategy.getOpenType() instanceof CompositeType) {
79             try {
80                 final ArrayType<?> openType = new ArrayType<Object>(getOpenType().getDimension(), innerTypeResolvingStrategy.getOpenType());
81                 setOpenType(openType);
82             } catch (OpenDataException e) {
83                 throw new IllegalStateException("An error occurred during restoration of array type " + this
84                         + " for attribute " + attrName + " from value " + value, e);
85             }
86         }
87
88         LOG.debug("Attribute {} : {} parsed to type {} as {}", attrName, value, getOpenType(),
89                 toStringArray(parsedArray));
90
91         return Optional.of(parsedArray);
92     }
93
94     private static String toStringArray(Object array) {
95         StringBuilder build = new StringBuilder(array.toString());
96         build.append(" [");
97         for (int i = 0; i < Array.getLength(array); i++) {
98             build.append(Array.get(array, i).toString());
99             build.append(",");
100         }
101         build.append("]]");
102         return build.toString();
103     }
104
105     private static Class<?> getPrimitiveType(Class<?> innerTypeClass) {
106         try {
107             return (Class<?>) innerTypeClass.getField("TYPE").get(null);
108         } catch (Exception e) {
109             throw new IllegalStateException("Unable to determine primitive type to " + innerTypeClass);
110         }
111     }
112
113     @Override
114     public String toString() {
115         return "ResolvedArrayTypeAttributeType [innerTypeResolved=" + innerTypeResolvingStrategy + "]";
116     }
117 }