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.text.ParseException;
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.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 final class SimpleAttributeResolvingStrategy extends AbstractAttributeResolvingStrategy<Object, SimpleType<?>> {
28 private static final Logger LOG = LoggerFactory.getLogger(SimpleAttributeResolvingStrategy.class);
30 SimpleAttributeResolvingStrategy(final SimpleType<?> simpleType) {
35 public String toString() {
36 return "ResolvedSimpleAttribute [" + getOpenType().getClassName() + "]";
40 public Optional<Object> parseAttribute(final String attrName, final Object value) throws DocumentedException {
42 return Optional.absent();
47 cls = Class.forName(getOpenType().getClassName());
48 } catch (final ClassNotFoundException e) {
49 throw new RuntimeException("Unable to locate class for " + getOpenType().getClassName(), e);
52 Util.checkType(value, String.class);
54 Resolver prefferedPlugin = RESOLVER_PLUGINS.get(cls.getCanonicalName());
55 prefferedPlugin = prefferedPlugin == null ? RESOLVER_PLUGINS.get(DEFAULT_RESOLVERS) : prefferedPlugin;
57 Object parsedValue = prefferedPlugin.resolveObject(cls, attrName, (String) value);
58 LOG.debug("Attribute {} : {} parsed to type {} with value {}", attrName, value, getOpenType(), parsedValue);
59 return Optional.of(parsedValue);
62 private static final String DEFAULT_RESOLVERS = "default";
63 private static final Map<String, Resolver> RESOLVER_PLUGINS = Maps.newHashMap();
66 RESOLVER_PLUGINS.put(DEFAULT_RESOLVERS, new DefaultResolver());
67 RESOLVER_PLUGINS.put(String.class.getCanonicalName(), new StringResolver());
68 RESOLVER_PLUGINS.put(Date.class.getCanonicalName(), new DateResolver());
69 RESOLVER_PLUGINS.put(Character.class.getCanonicalName(), new CharResolver());
70 RESOLVER_PLUGINS.put(BigInteger.class.getCanonicalName(), new BigIntegerResolver());
71 RESOLVER_PLUGINS.put(BigDecimal.class.getCanonicalName(), new BigDecimalResolver());
75 Object resolveObject(Class<?> type, String attrName, String value) throws DocumentedException;
78 static class DefaultResolver implements Resolver {
81 public Object resolveObject(final Class<?> type, final String attrName, final String value)
82 throws DocumentedException {
84 return parseObject(type, value);
85 } catch (final DocumentedException e) {
86 throw new DocumentedException("Unable to resolve attribute " + attrName + " from " + value, e,
87 DocumentedException.ErrorType.APPLICATION, DocumentedException.ErrorTag.OPERATION_FAILED,
88 DocumentedException.ErrorSeverity.ERROR);
92 protected Object parseObject(final Class<?> type, final String value) throws DocumentedException {
95 method = type.getMethod("valueOf", String.class);
96 return method.invoke(null, value);
97 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
98 LOG.trace("Error parsing object ", e);
99 throw new DocumentedException("Error parsing object.", e, DocumentedException.ErrorType.APPLICATION,
100 DocumentedException.ErrorTag.OPERATION_FAILED, DocumentedException.ErrorSeverity.ERROR);
105 static class StringResolver extends DefaultResolver {
108 protected Object parseObject(final Class<?> type, final String value) {
113 static class BigIntegerResolver extends DefaultResolver {
116 protected Object parseObject(final Class<?> type, final String value) {
117 return new BigInteger(value);
121 static class BigDecimalResolver extends DefaultResolver {
124 protected Object parseObject(final Class<?> type, final String value) {
125 return new BigDecimal(value);
129 static class CharResolver extends DefaultResolver {
132 protected Object parseObject(final Class<?> type, final String value) {
133 return value.charAt(0);
137 static class DateResolver extends DefaultResolver {
139 protected Object parseObject(final Class<?> type, final String value) throws DocumentedException {
141 return Util.readDate(value);
142 } catch (final ParseException e) {
143 LOG.trace("Unable parse value {} due to ", value, e);
144 throw new DocumentedException("Unable to parse value " + value + " as date.", e,
145 DocumentedException.ErrorType.APPLICATION, DocumentedException.ErrorTag.OPERATION_FAILED,
146 DocumentedException.ErrorSeverity.ERROR);