CDS: Add stress test RPC to the cars model
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / attributes / resolving / SimpleAttributeResolvingStrategy.java
1 /*
2  * Copyright (c) 2015 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.config.facade.xml.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.lang.reflect.Method;
15 import java.math.BigDecimal;
16 import java.math.BigInteger;
17 import java.text.ParseException;
18 import java.util.Date;
19 import java.util.Map;
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;
25
26 final class SimpleAttributeResolvingStrategy extends AbstractAttributeResolvingStrategy<Object, SimpleType<?>> {
27
28     private static final Logger LOG = LoggerFactory.getLogger(SimpleAttributeResolvingStrategy.class);
29
30     SimpleAttributeResolvingStrategy(SimpleType<?> simpleType) {
31         super(simpleType);
32     }
33
34     @Override
35     public String toString() {
36         return "ResolvedSimpleAttribute [" + getOpenType().getClassName() + "]";
37     }
38
39     @Override
40     public Optional<Object> parseAttribute(String attrName, Object value) throws DocumentedException {
41         if (value == null) {
42             return Optional.absent();
43         }
44
45         Class<?> cls;
46         try {
47             cls = Class.forName(getOpenType().getClassName());
48         } catch (ClassNotFoundException e) {
49             throw new RuntimeException("Unable to locate class for " + getOpenType().getClassName(), e);
50         }
51
52         Util.checkType(value, String.class);
53
54         Resolver prefferedPlugin = resolverPlugins.get(cls.getCanonicalName());
55         prefferedPlugin = prefferedPlugin == null ? resolverPlugins.get(DEFAULT_RESOLVERS) : prefferedPlugin;
56
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);
60     }
61
62     private static final String DEFAULT_RESOLVERS = "default";
63     private static final Map<String, Resolver> resolverPlugins = Maps.newHashMap();
64
65     static {
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());
72     }
73
74     static interface Resolver {
75         Object resolveObject(Class<?> type, String attrName, String value) throws DocumentedException;
76     }
77
78     static class DefaultResolver implements Resolver {
79
80         @Override
81         public Object resolveObject(Class<?> type, String attrName, String value) throws DocumentedException {
82             try {
83                 return parseObject(type, value);
84             } catch (Exception e) {
85                 throw new DocumentedException("Unable to resolve attribute " + attrName + " from " + value,
86                         DocumentedException.ErrorType.application,
87                         DocumentedException.ErrorTag.operation_failed,
88                         DocumentedException.ErrorSeverity.error);
89             }
90         }
91
92         protected Object parseObject(Class<?> type, String value) throws DocumentedException {
93             Method method = null;
94             try {
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.",
100                         DocumentedException.ErrorType.application,
101                         DocumentedException.ErrorTag.operation_failed,
102                         DocumentedException.ErrorSeverity.error);
103             }
104         }
105     }
106
107     static class StringResolver extends DefaultResolver {
108
109         @Override
110         protected Object parseObject(Class<?> type, String value) {
111             return value;
112         }
113     }
114
115     static class BigIntegerResolver extends DefaultResolver {
116
117         @Override
118         protected Object parseObject(Class<?> type, String value) {
119             return new BigInteger(value);
120         }
121     }
122
123     static class BigDecimalResolver extends DefaultResolver {
124
125         @Override
126         protected Object parseObject(Class<?> type, String value) {
127             return new BigDecimal(value);
128         }
129     }
130
131     static class CharResolver extends DefaultResolver {
132
133         @Override
134         protected Object parseObject(Class<?> type, String value)  {
135             return value.charAt(0);
136         }
137     }
138
139     static class DateResolver extends DefaultResolver {
140         @Override
141         protected Object parseObject(Class<?> type, String value) throws DocumentedException {
142             try {
143                 return Util.readDate(value);
144             } catch (ParseException e) {
145                 LOG.trace("Unable parse value {} due to ",value, e);
146                 throw new DocumentedException("Unable to parse value "+value+" as date.",
147                         DocumentedException.ErrorType.application,
148                         DocumentedException.ErrorTag.operation_failed,
149                         DocumentedException.ErrorSeverity.error);
150             }
151         }
152     }
153
154 }