Bug 1062 - Allow multiple service references with same name.
[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 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;
19
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;
25 import java.util.Map;
26
27 final class SimpleAttributeResolvingStrategy extends AbstractAttributeResolvingStrategy<Object, SimpleType<?>> {
28
29     private static final Logger logger = LoggerFactory.getLogger(SimpleAttributeResolvingStrategy.class);
30
31     SimpleAttributeResolvingStrategy(SimpleType<?> simpleType) {
32         super(simpleType);
33     }
34
35     @Override
36     public String toString() {
37         return "ResolvedSimpleAttribute [" + getOpenType().getClassName() + "]";
38     }
39
40     @Override
41     public Optional<Object> parseAttribute(String attrName, Object value) throws NetconfDocumentedException {
42         if (value == null) {
43             return Optional.absent();
44         }
45
46         Class<?> cls;
47         try {
48             cls = Class.forName(getOpenType().getClassName());
49         } catch (ClassNotFoundException e) {
50             throw new RuntimeException("Unable to locate class for " + getOpenType().getClassName(), e);
51         }
52
53         Util.checkType(value, String.class);
54
55         Resolver prefferedPlugin = resolverPlugins.get(cls.getCanonicalName());
56         prefferedPlugin = prefferedPlugin == null ? resolverPlugins.get(DEFAULT_RESOLVERS) : prefferedPlugin;
57
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);
61     }
62
63     private static final String DEFAULT_RESOLVERS = "default";
64     private static final Map<String, Resolver> resolverPlugins = Maps.newHashMap();
65
66     static {
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());
73     }
74
75     static interface Resolver {
76         Object resolveObject(Class<?> type, String attrName, String value) throws NetconfDocumentedException;
77     }
78
79     static class DefaultResolver implements Resolver {
80
81         @Override
82         public Object resolveObject(Class<?> type, String attrName, String value) throws NetconfDocumentedException {
83             try {
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);
90             }
91         }
92
93         protected Object parseObject(Class<?> type, String value) throws NetconfDocumentedException {
94             Method method = null;
95             try {
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);
104             }
105         }
106     }
107
108     static class StringResolver extends DefaultResolver {
109
110         @Override
111         protected Object parseObject(Class<?> type, String value) {
112             return value;
113         }
114     }
115
116     static class BigIntegerResolver extends DefaultResolver {
117
118         @Override
119         protected Object parseObject(Class<?> type, String value) {
120             return new BigInteger(value);
121         }
122     }
123
124     static class BigDecimalResolver extends DefaultResolver {
125
126         @Override
127         protected Object parseObject(Class<?> type, String value) {
128             return new BigDecimal(value);
129         }
130     }
131
132     static class CharResolver extends DefaultResolver {
133
134         @Override
135         protected Object parseObject(Class<?> type, String value)  {
136             return new Character(value.charAt(0));
137         }
138     }
139
140     static class DateResolver extends DefaultResolver {
141         @Override
142         protected Object parseObject(Class<?> type, String value) throws NetconfDocumentedException {
143             try {
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);
151             }
152         }
153     }
154
155 }