2 * Copyright (c) 2015, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.config.facade.xml.mapping.attributes.resolving;
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;
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;
27 final class SimpleAttributeResolvingStrategy extends AbstractAttributeResolvingStrategy<Object, SimpleType<?>> {
29 private static final Logger LOG = LoggerFactory.getLogger(SimpleAttributeResolvingStrategy.class);
31 SimpleAttributeResolvingStrategy(final SimpleType<?> simpleType) {
36 public String toString() {
37 return "ResolvedSimpleAttribute [" + getOpenType().getClassName() + "]";
41 public Optional<Object> parseAttribute(final String attrName, final Object value) throws DocumentedException {
43 return Optional.absent();
48 cls = Class.forName(getOpenType().getClassName());
49 } catch (final ClassNotFoundException e) {
50 throw new RuntimeException("Unable to locate class for " + getOpenType().getClassName(), e);
53 Util.checkType(value, String.class);
55 Resolver prefferedPlugin = RESOLVER_PLUGINS.get(cls.getCanonicalName());
56 prefferedPlugin = prefferedPlugin == null ? RESOLVER_PLUGINS.get(DEFAULT_RESOLVERS) : prefferedPlugin;
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);
63 private static final String DEFAULT_RESOLVERS = "default";
64 private static final Map<String, Resolver> RESOLVER_PLUGINS = Maps.newHashMap();
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());
76 Object resolveObject(Class<?> type, String attrName, String value) throws DocumentedException;
79 static class DefaultResolver implements Resolver {
82 public Object resolveObject(final Class<?> type, final String attrName, final String value)
83 throws DocumentedException {
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);
93 protected Object parseObject(final Class<?> type, final String value) throws DocumentedException {
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);
106 static class StringResolver extends DefaultResolver {
109 protected Object parseObject(final Class<?> type, final String value) {
114 static class BigIntegerResolver extends DefaultResolver {
117 protected Object parseObject(final Class<?> type, final String value) {
118 return new BigInteger(value);
122 static class BigDecimalResolver extends DefaultResolver {
125 protected Object parseObject(final Class<?> type, final String value) {
126 return new BigDecimal(value);
130 static class CharResolver extends DefaultResolver {
133 protected Object parseObject(final Class<?> type, final String value) {
134 return value.charAt(0);
138 static class DateResolver extends DefaultResolver {
140 protected Object parseObject(final Class<?> type, final String value) throws DocumentedException {
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);