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.text.ParseException;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import javax.management.openmbean.SimpleType;
21 import java.lang.reflect.Method;
22 import java.math.BigDecimal;
23 import java.math.BigInteger;
24 import java.util.Date;
27 final class SimpleAttributeResolvingStrategy extends AbstractAttributeResolvingStrategy<Object, SimpleType<?>> {
29 private static final Logger logger = LoggerFactory.getLogger(SimpleAttributeResolvingStrategy.class);
31 SimpleAttributeResolvingStrategy(SimpleType<?> simpleType) {
36 public String toString() {
37 return "ResolvedSimpleAttribute [" + getOpenType().getClassName() + "]";
41 public Optional<Object> parseAttribute(String attrName, Object value) throws NetconfDocumentedException {
43 return Optional.absent();
48 cls = Class.forName(getOpenType().getClassName());
49 } catch (ClassNotFoundException e) {
50 throw new RuntimeException("Unable to locate class for " + getOpenType().getClassName(), e);
53 Util.checkType(value, String.class);
55 Resolver prefferedPlugin = resolverPlugins.get(cls.getCanonicalName());
56 prefferedPlugin = prefferedPlugin == null ? resolverPlugins.get(DEFAULT_RESOLVERS) : prefferedPlugin;
58 Object parsedValue = prefferedPlugin.resolveObject(cls, attrName, (String) value);
59 logger.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> resolverPlugins = Maps.newHashMap();
67 resolverPlugins.put(DEFAULT_RESOLVERS, new DefaultResolver());
68 resolverPlugins.put(String.class.getCanonicalName(), new StringResolver());
69 resolverPlugins.put(Date.class.getCanonicalName(), new DateResolver());
70 resolverPlugins.put(Character.class.getCanonicalName(), new CharResolver());
71 resolverPlugins.put(BigInteger.class.getCanonicalName(), new BigIntegerResolver());
72 resolverPlugins.put(BigDecimal.class.getCanonicalName(), new BigDecimalResolver());
75 static interface Resolver {
76 Object resolveObject(Class<?> type, String attrName, String value) throws NetconfDocumentedException;
79 static class DefaultResolver implements Resolver {
82 public Object resolveObject(Class<?> type, String attrName, String value) throws NetconfDocumentedException {
84 return parseObject(type, value);
85 } catch (Exception e) {
86 throw new NetconfDocumentedException("Unable to resolve attribute " + attrName + " from " + value,
87 NetconfDocumentedException.ErrorType.application,
88 NetconfDocumentedException.ErrorTag.operation_failed,
89 NetconfDocumentedException.ErrorSeverity.error);
93 protected Object parseObject(Class<?> type, String value) throws NetconfDocumentedException {
96 method = type.getMethod("valueOf", String.class);
97 return method.invoke(null, value);
98 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
99 logger.trace("Error parsing object {}",e);
100 throw new NetconfDocumentedException("Error parsing object.",
101 NetconfDocumentedException.ErrorType.application,
102 NetconfDocumentedException.ErrorTag.operation_failed,
103 NetconfDocumentedException.ErrorSeverity.error);
108 static class StringResolver extends DefaultResolver {
111 protected Object parseObject(Class<?> type, String value) {
116 static class BigIntegerResolver extends DefaultResolver {
119 protected Object parseObject(Class<?> type, String value) {
120 return new BigInteger(value);
124 static class BigDecimalResolver extends DefaultResolver {
127 protected Object parseObject(Class<?> type, String value) {
128 return new BigDecimal(value);
132 static class CharResolver extends DefaultResolver {
135 protected Object parseObject(Class<?> type, String value) {
136 return new Character(value.charAt(0));
140 static class DateResolver extends DefaultResolver {
142 protected Object parseObject(Class<?> type, String value) throws NetconfDocumentedException {
144 return Util.readDate(value);
145 } catch (ParseException e) {
146 logger.trace("Unable parse value {} due to {}",value, e);
147 throw new NetconfDocumentedException("Unable to parse value "+value+" as date.",
148 NetconfDocumentedException.ErrorType.application,
149 NetconfDocumentedException.ErrorTag.operation_failed,
150 NetconfDocumentedException.ErrorSeverity.error);