Adjust to yangtools-2.0.0 changes
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / attributes / resolving / SimpleAttributeResolvingStrategy.java
1 /*
2  * Copyright (c) 2015, 2017 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 com.google.common.collect.Maps;
13 import java.lang.reflect.InvocationTargetException;
14 import java.lang.reflect.Method;
15 import java.math.BigDecimal;
16 import java.math.BigInteger;
17 import java.time.format.DateTimeParseException;
18 import java.util.Date;
19 import java.util.Map;
20 import javax.management.openmbean.SimpleType;
21 import org.opendaylight.controller.config.facade.xml.util.Util;
22 import org.opendaylight.controller.config.util.xml.DocumentedException;
23 import org.opendaylight.yangtools.yang.common.Revision;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 final class SimpleAttributeResolvingStrategy extends AbstractAttributeResolvingStrategy<Object, SimpleType<?>> {
28
29     private static final Logger LOG = LoggerFactory.getLogger(SimpleAttributeResolvingStrategy.class);
30
31     SimpleAttributeResolvingStrategy(final SimpleType<?> simpleType) {
32         super(simpleType);
33     }
34
35     @Override
36     public String toString() {
37         return "ResolvedSimpleAttribute [" + getOpenType().getClassName() + "]";
38     }
39
40     @Override
41     public Optional<Object> parseAttribute(final String attrName, final Object value) throws DocumentedException {
42         if (value == null) {
43             return Optional.absent();
44         }
45
46         Class<?> cls;
47         try {
48             cls = Class.forName(getOpenType().getClassName());
49         } catch (final ClassNotFoundException e) {
50             throw new RuntimeException("Unable to locate class for " + getOpenType().getClassName(), e);
51         }
52
53         Util.checkType(value, String.class);
54
55         Resolver prefferedPlugin = RESOLVER_PLUGINS.get(cls.getCanonicalName());
56         prefferedPlugin = prefferedPlugin == null ? RESOLVER_PLUGINS.get(DEFAULT_RESOLVERS) : prefferedPlugin;
57
58         Object parsedValue = prefferedPlugin.resolveObject(cls, attrName, (String) value);
59         LOG.debug("Attribute {} : {} parsed to type {} with value {}", attrName, value, getOpenType(), parsedValue);
60         return Optional.of(parsedValue);
61     }
62
63     private static final String DEFAULT_RESOLVERS = "default";
64     private static final Map<String, Resolver> RESOLVER_PLUGINS = Maps.newHashMap();
65
66     static {
67         RESOLVER_PLUGINS.put(DEFAULT_RESOLVERS, new DefaultResolver());
68         RESOLVER_PLUGINS.put(String.class.getCanonicalName(), new StringResolver());
69         RESOLVER_PLUGINS.put(Date.class.getCanonicalName(), new DateResolver());
70         RESOLVER_PLUGINS.put(Character.class.getCanonicalName(), new CharResolver());
71         RESOLVER_PLUGINS.put(BigInteger.class.getCanonicalName(), new BigIntegerResolver());
72         RESOLVER_PLUGINS.put(BigDecimal.class.getCanonicalName(), new BigDecimalResolver());
73     }
74
75     interface Resolver {
76         Object resolveObject(Class<?> type, String attrName, String value) throws DocumentedException;
77     }
78
79     static class DefaultResolver implements Resolver {
80
81         @Override
82         public Object resolveObject(final Class<?> type, final String attrName, final String value)
83                 throws DocumentedException {
84             try {
85                 return parseObject(type, value);
86             } catch (final DocumentedException e) {
87                 throw new DocumentedException("Unable to resolve attribute " + attrName + " from " + value, e,
88                         DocumentedException.ErrorType.APPLICATION, DocumentedException.ErrorTag.OPERATION_FAILED,
89                         DocumentedException.ErrorSeverity.ERROR);
90             }
91         }
92
93         protected Object parseObject(final Class<?> type, final String value) throws DocumentedException {
94             Method method = null;
95             try {
96                 method = type.getMethod("valueOf", String.class);
97                 return method.invoke(null, value);
98             } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
99                 LOG.trace("Error parsing object ", e);
100                 throw new DocumentedException("Error parsing object.", e, DocumentedException.ErrorType.APPLICATION,
101                         DocumentedException.ErrorTag.OPERATION_FAILED, DocumentedException.ErrorSeverity.ERROR);
102             }
103         }
104     }
105
106     static class StringResolver extends DefaultResolver {
107
108         @Override
109         protected Object parseObject(final Class<?> type, final String value) {
110             return value;
111         }
112     }
113
114     static class BigIntegerResolver extends DefaultResolver {
115
116         @Override
117         protected Object parseObject(final Class<?> type, final String value) {
118             return new BigInteger(value);
119         }
120     }
121
122     static class BigDecimalResolver extends DefaultResolver {
123
124         @Override
125         protected Object parseObject(final Class<?> type, final String value) {
126             return new BigDecimal(value);
127         }
128     }
129
130     static class CharResolver extends DefaultResolver {
131
132         @Override
133         protected Object parseObject(final Class<?> type, final String value) {
134             return value.charAt(0);
135         }
136     }
137
138     static class DateResolver extends DefaultResolver {
139         @Override
140         protected Object parseObject(final Class<?> type, final String value) throws DocumentedException {
141             try {
142                 return Revision.ofNullable(value).orElse(null);
143             } catch (final DateTimeParseException e) {
144                 LOG.trace("Unable parse value {} due to ", value, e);
145                 throw new DocumentedException("Unable to parse value " + value + " as date.", e,
146                         DocumentedException.ErrorType.APPLICATION, DocumentedException.ErrorTag.OPERATION_FAILED,
147                         DocumentedException.ErrorSeverity.ERROR);
148             }
149         }
150     }
151 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.