Initial code drop of netconf protocol implementation
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / resolving / SimpleAttributeResolvingStrategy.java
1 /*
2  * Copyright (c) 2013 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.netconf.confignetconfconnector.mapping.attributes.resolving;
10
11 import com.google.common.base.Optional;
12 import com.google.common.collect.Maps;
13 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 import javax.management.openmbean.SimpleType;
18 import java.lang.reflect.Method;
19 import java.math.BigInteger;
20 import java.util.Date;
21 import java.util.Map;
22
23 final class SimpleAttributeResolvingStrategy extends AbstractAttributeResolvingStrategy<Object, SimpleType<?>> {
24
25     private static final Logger logger = LoggerFactory.getLogger(SimpleAttributeResolvingStrategy.class);
26
27     SimpleAttributeResolvingStrategy(SimpleType<?> simpleType) {
28         super(simpleType);
29     }
30
31     @Override
32     public String toString() {
33         return "ResolvedSimpleAttribute [" + getOpenType().getClassName() + "]";
34     }
35
36     @Override
37     public Optional<Object> parseAttribute(String attrName, Object value) {
38         if (value == null) {
39             return Optional.absent();
40         }
41
42         Class<?> cls;
43         try {
44             cls = Class.forName(getOpenType().getClassName());
45         } catch (ClassNotFoundException e) {
46             throw new RuntimeException("Unable to locate class for " + getOpenType().getClassName(), e);
47         }
48
49         Util.checkType(value, String.class);
50
51         Resolver prefferedPlugin = resolverPlugins.get(cls.getCanonicalName());
52         prefferedPlugin = prefferedPlugin == null ? resolverPlugins.get(DEFAULT_RESOLVERS) : prefferedPlugin;
53
54         Object parsedValue = prefferedPlugin.resolveObject(cls, attrName, (String) value);
55         logger.debug("Attribute {} : {} parsed to type {} with value {}", attrName, value, getOpenType(), parsedValue);
56         return Optional.of(parsedValue);
57     }
58
59     private static final String DEFAULT_RESOLVERS = "default";
60     private static final Map<String, Resolver> resolverPlugins = Maps.newHashMap();
61
62     static {
63         resolverPlugins.put(DEFAULT_RESOLVERS, new DefaultResolver());
64         resolverPlugins.put(String.class.getCanonicalName(), new StringResolver());
65         resolverPlugins.put(Date.class.getCanonicalName(), new DateResolver());
66         resolverPlugins.put(Character.class.getCanonicalName(), new CharResolver());
67         resolverPlugins.put(BigInteger.class.getCanonicalName(), new BigIntegerResolver());
68     }
69
70     static interface Resolver {
71         Object resolveObject(Class<?> type, String attrName, String value);
72     }
73
74     static class DefaultResolver implements Resolver {
75
76         @Override
77         public Object resolveObject(Class<?> type, String attrName, String value) {
78             try {
79                 Object parsedValue = parseObject(type, value);
80                 return parsedValue;
81             } catch (Exception e) {
82                 throw new RuntimeException("Unable to resolve attribute " + attrName + " from " + value, e);
83             }
84         }
85
86         protected Object parseObject(Class<?> type, String value) throws Exception {
87             Method method = type.getMethod("valueOf", String.class);
88             Object parsedValue = method.invoke(null, value);
89             return parsedValue;
90         }
91     }
92
93     static class StringResolver extends DefaultResolver {
94
95         @Override
96         protected Object parseObject(Class<?> type, String value) throws Exception {
97             return value;
98         }
99     }
100
101     static class BigIntegerResolver extends DefaultResolver {
102
103         @Override
104         protected Object parseObject(Class<?> type, String value) throws Exception {
105             return new BigInteger(value);
106         }
107     }
108
109     static class CharResolver extends DefaultResolver {
110
111         @Override
112         protected Object parseObject(Class<?> type, String value) throws Exception {
113             return new Character(value.charAt(0));
114         }
115     }
116
117     static class DateResolver extends DefaultResolver {
118
119         @Override
120         protected Object parseObject(Class<?> type, String value) throws Exception {
121             return Util.readDate(value);
122         }
123     }
124
125 }