Bug 460 - Fix warning throughout netconf subsystem
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / mapping / SimpleAttributeMappingStrategy.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.mapping;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Maps;
14 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
15
16 import javax.management.openmbean.SimpleType;
17 import java.util.Date;
18 import java.util.Map;
19
20 public class SimpleAttributeMappingStrategy extends AbstractAttributeMappingStrategy<String, SimpleType<?>> {
21
22     public SimpleAttributeMappingStrategy(SimpleType<?> openType) {
23         super(openType);
24     }
25
26     @Override
27     public Optional<String> mapAttribute(Object value) {
28         if (value == null){
29             return Optional.absent();
30         }
31
32         String expectedClass = getOpenType().getClassName();
33         String realClass = value.getClass().getName();
34         Preconditions.checkArgument(realClass.equals(expectedClass), "Type mismatch, expected " + expectedClass
35                 + " but was " + realClass);
36
37         WriterPlugin prefferedPlugin = writerPlugins.get(value.getClass().getCanonicalName());
38         prefferedPlugin = prefferedPlugin == null ? writerPlugins.get(DEFAULT_WRITER_PLUGIN) : prefferedPlugin;
39         return Optional.of(prefferedPlugin.writeObject(value));
40     }
41
42     private static final String DEFAULT_WRITER_PLUGIN = "default";
43     private static final Map<String, WriterPlugin> writerPlugins = Maps.newHashMap();
44     static {
45         writerPlugins.put(DEFAULT_WRITER_PLUGIN, new DefaultWriterPlugin());
46         writerPlugins.put(Date.class.getCanonicalName(), new DatePlugin());
47     }
48
49     /**
50      * Custom writer plugins must implement this interface.
51      */
52     static interface WriterPlugin {
53         String writeObject(Object value);
54     }
55
56     static class DefaultWriterPlugin implements WriterPlugin {
57
58         @Override
59         public String writeObject(Object value) {
60             return value.toString();
61         }
62     }
63
64     static class DatePlugin implements WriterPlugin {
65
66         @Override
67         public String writeObject(Object value) {
68             Preconditions.checkArgument(value instanceof Date, "Attribute must be Date");
69             return Util.writeDate((Date) value);
70         }
71     }
72
73 }