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