2 * Copyright (c) 2013 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.netconf.confignetconfconnector.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.netconf.api.NetconfDocumentedException;
22 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
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(SimpleType<?> simpleType) {
35 public String toString() {
36 return "ResolvedSimpleAttribute [" + getOpenType().getClassName() + "]";
40 public Optional<Object> parseAttribute(String attrName, Object value) throws NetconfDocumentedException {
42 return Optional.absent();
47 cls = Class.forName(getOpenType().getClassName());
48 } catch (ClassNotFoundException e) {
49 throw new RuntimeException("Unable to locate class for " + getOpenType().getClassName(), e);
52 Util.checkType(value, String.class);
54 Resolver prefferedPlugin = resolverPlugins.get(cls.getCanonicalName());
55 prefferedPlugin = prefferedPlugin == null ? resolverPlugins.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> resolverPlugins = Maps.newHashMap();
66 resolverPlugins.put(DEFAULT_RESOLVERS, new DefaultResolver());
67 resolverPlugins.put(String.class.getCanonicalName(), new StringResolver());
68 resolverPlugins.put(Date.class.getCanonicalName(), new DateResolver());
69 resolverPlugins.put(Character.class.getCanonicalName(), new CharResolver());
70 resolverPlugins.put(BigInteger.class.getCanonicalName(), new BigIntegerResolver());
71 resolverPlugins.put(BigDecimal.class.getCanonicalName(), new BigDecimalResolver());
74 static interface Resolver {
75 Object resolveObject(Class<?> type, String attrName, String value) throws NetconfDocumentedException;
78 static class DefaultResolver implements Resolver {
81 public Object resolveObject(Class<?> type, String attrName, String value) throws NetconfDocumentedException {
83 return parseObject(type, value);
84 } catch (Exception e) {
85 throw new NetconfDocumentedException("Unable to resolve attribute " + attrName + " from " + value,
86 NetconfDocumentedException.ErrorType.application,
87 NetconfDocumentedException.ErrorTag.operation_failed,
88 NetconfDocumentedException.ErrorSeverity.error);
92 protected Object parseObject(Class<?> type, String value) throws NetconfDocumentedException {
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 NetconfDocumentedException("Error parsing object.",
100 NetconfDocumentedException.ErrorType.application,
101 NetconfDocumentedException.ErrorTag.operation_failed,
102 NetconfDocumentedException.ErrorSeverity.error);
107 static class StringResolver extends DefaultResolver {
110 protected Object parseObject(Class<?> type, String value) {
115 static class BigIntegerResolver extends DefaultResolver {
118 protected Object parseObject(Class<?> type, String value) {
119 return new BigInteger(value);
123 static class BigDecimalResolver extends DefaultResolver {
126 protected Object parseObject(Class<?> type, String value) {
127 return new BigDecimal(value);
131 static class CharResolver extends DefaultResolver {
134 protected Object parseObject(Class<?> type, String value) {
135 return value.charAt(0);
139 static class DateResolver extends DefaultResolver {
141 protected Object parseObject(Class<?> type, String value) throws NetconfDocumentedException {
143 return Util.readDate(value);
144 } catch (ParseException e) {
145 LOG.trace("Unable parse value {} due to ",value, e);
146 throw new NetconfDocumentedException("Unable to parse value "+value+" as date.",
147 NetconfDocumentedException.ErrorType.application,
148 NetconfDocumentedException.ErrorTag.operation_failed,
149 NetconfDocumentedException.ErrorSeverity.error);