2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.List;
17 import java.util.Objects;
18 import java.util.Optional;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorData;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ParametrizedConvertor;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Manages various convertors and allows to use them all in one generic way
28 public class ConvertorManager {
29 private static final Logger LOG = LoggerFactory.getLogger(ConvertorManager.class);
30 private static ConvertorManager INSTANCE;
33 INSTANCE = new ConvertorManager();
34 // All convertors are registered here
35 INSTANCE.registerConvertor(new TableFeaturesConvertor());
36 INSTANCE.registerConvertor(new TableFeaturesResponseConvertor());
39 // Actual convertor keys
40 private List<Class<?>> convertorKeys = new ArrayList<>();
41 private List<Class<?>> parametrizedConvertorKeys = new ArrayList<>();
43 // Cache, that holds all registered convertors, but they can have multiple keys,
44 // based on instanceof checks in the convert method
45 private Map<Class<?>, Convertor> convertors = new HashMap<>();
46 private Map<Class<?>, ParametrizedConvertor> parametrizedConvertors = new HashMap<>();
48 private ConvertorManager() {
49 // Hiding implicit constructor
53 * Gets instance of Convertor Manager.
55 * @return the instance
57 public static ConvertorManager getInstance() {
64 * @param convertor the convertor
65 * @return if registration was successful
67 public boolean registerConvertor(final Convertor convertor) {
68 final Class<?> type = convertor.getType();
70 if (convertors.containsKey(type)) {
71 LOG.warn("Convertor for type {} is already registered", type);
75 convertorKeys.add(type);
76 convertors.put(type, convertor);
77 LOG.debug("{} is now converted by {}", type, convertor);
84 * @param convertor the convertor
85 * @return if registration was successful
87 public boolean registerConvertor(final ParametrizedConvertor convertor) {
88 final Class<?> type = convertor.getType();
90 if (parametrizedConvertors.containsKey(type)) {
91 LOG.warn("Convertor for type {} is already registered", type);
95 parametrizedConvertorKeys.add(type);
96 parametrizedConvertors.put(type, convertor);
97 LOG.debug("{} is now converted by {}", type, convertor);
102 * Lookup and use convertor by specified type, then converts source and returns converted result
104 * @param <FROM> the source type
105 * @param <TO> the result type
106 * @param source the source
107 * @return the result (can be empty, if no convertor was found)
109 @SuppressWarnings("unchecked")
110 public <FROM, TO> Optional<TO> convert(final FROM source) {
111 if (Objects.isNull(source)) {
112 LOG.trace("Cannot convert source, because it is null");
113 return Optional.empty();
116 Class<?> type = source.getClass();
118 if (source instanceof Collection) {
119 final Iterator it = ((Collection) source).iterator();
126 if (Objects.isNull(next)) {
127 LOG.trace("Cannot convert {}, because it is empty collection", type);
128 return Optional.empty();
131 type = next.getClass();
134 Convertor convertor = null;
136 if (!convertors.containsKey(type)) {
137 boolean found = false;
139 for (Class<?> key : convertorKeys) {
140 if (key.isAssignableFrom(type)) {
141 convertor = convertors.get(key);
142 convertors.put(type, convertor);
143 LOG.debug("{} is now converted by {}", type, convertor);
150 LOG.error("Convertor for {} not found", type);
151 return Optional.empty();
155 if (Objects.isNull(convertor)) {
156 convertor = convertors.get(type);
159 final Object result = convertor.convert(source);
160 return Optional.of((TO) result);
164 * Lookup and use convertor by specified type, then converts source and returns converted result
166 * @param <FROM> the source type
167 * @param <TO> the result type
168 * @param <DATA> the data type
169 * @param source the source
170 * @param data convertor data
171 * @return the result (can be empty, if no convertor was found)
173 @SuppressWarnings("unchecked")
174 public <FROM, TO, DATA extends ConvertorData> Optional<TO> convert(final FROM source, final DATA data) {
175 if (Objects.isNull(source)) {
176 LOG.trace("Cannot convert source, because it is null");
177 return Optional.empty();
180 Class<?> type = source.getClass();
182 if (source instanceof Collection) {
183 final Iterator it = ((Collection) source).iterator();
190 if (Objects.isNull(next)) {
191 LOG.trace("Cannot convert {}, because it is empty collection", type);
192 return Optional.empty();
195 type = next.getClass();
198 ParametrizedConvertor convertor = null;
200 if (!parametrizedConvertors.containsKey(type)) {
201 boolean found = false;
203 for (Class<?> key : parametrizedConvertorKeys) {
204 if (key.isAssignableFrom(type)) {
205 convertor = parametrizedConvertors.get(key);
206 parametrizedConvertors.put(type, convertor);
207 LOG.debug("{} is now converted by {}", type, convertor);
214 LOG.error("Convertor for {} not found", type);
215 return Optional.empty();
219 if (Objects.isNull(convertor)) {
220 convertor = parametrizedConvertors.get(type);
223 final Object result = convertor.convert(source, data);
224 return Optional.of((TO) result);