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.action.ActionConvertor;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.ActionResponseConvertor;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorData;
23 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ParametrizedConvertor;
24 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchResponseConvertor;
25 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchV10ResponseConvertor;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Manages various convertors and allows to use them all in one generic way
32 public class ConvertorManager {
33 private static final Logger LOG = LoggerFactory.getLogger(ConvertorManager.class);
34 private static ConvertorManager INSTANCE;
37 INSTANCE = new ConvertorManager();
38 // All convertors are registered here
39 INSTANCE.registerConvertor(new TableFeaturesConvertor());
40 INSTANCE.registerConvertor(new TableFeaturesResponseConvertor());
41 INSTANCE.registerConvertor(new MeterConvertor());
42 INSTANCE.registerConvertor(new MeterStatsResponseConvertor());
43 INSTANCE.registerConvertor(new MeterConfigStatsResponseConvertor());
44 INSTANCE.registerConvertor(new PortConvertor());
45 // TODO: Add MatchConvertor
46 INSTANCE.registerConvertor(new MatchResponseConvertor());
47 INSTANCE.registerConvertor(new MatchV10ResponseConvertor());
48 INSTANCE.registerConvertor(new ActionConvertor());
49 INSTANCE.registerConvertor(new ActionResponseConvertor());
52 // Actual convertor keys
53 private List<Class<?>> convertorKeys = new ArrayList<>();
54 private List<Class<?>> parametrizedConvertorKeys = new ArrayList<>();
56 // Cache, that holds all registered convertors, but they can have multiple keys,
57 // based on instanceof checks in the convert method
58 private Map<Class<?>, Convertor> convertors = new HashMap<>();
59 private Map<Class<?>, ParametrizedConvertor> parametrizedConvertors = new HashMap<>();
61 private ConvertorManager() {
62 // Hiding implicit constructor
66 * Gets instance of Convertor Manager.
68 * @return the instance
70 public static ConvertorManager getInstance() {
77 * @param convertor the convertor
78 * @return if registration was successful
80 public boolean registerConvertor(final Convertor convertor) {
81 final Class<?> type = convertor.getType();
83 if (convertors.containsKey(type)) {
84 LOG.warn("Convertor for type {} is already registered", type);
88 convertorKeys.add(type);
89 convertors.put(type, convertor);
90 LOG.debug("{} is now converted by {}", type, convertor);
97 * @param convertor the convertor
98 * @return if registration was successful
100 public boolean registerConvertor(final ParametrizedConvertor convertor) {
101 final Class<?> type = convertor.getType();
103 if (parametrizedConvertors.containsKey(type)) {
104 LOG.warn("Convertor for type {} is already registered", type);
108 parametrizedConvertorKeys.add(type);
109 parametrizedConvertors.put(type, convertor);
110 LOG.debug("{} is now converted by {}", type, convertor);
115 * Lookup and use convertor by specified type, then converts source and returns converted result
117 * @param <FROM> the source type
118 * @param <TO> the result type
119 * @param source the source
120 * @return the result (can be empty, if no convertor was found)
122 @SuppressWarnings("unchecked")
123 public <FROM, TO> Optional<TO> convert(final FROM source) {
124 if (Objects.isNull(source)) {
125 LOG.trace("Cannot convert source, because it is null");
126 return Optional.empty();
129 Class<?> type = source.getClass();
131 if (source instanceof Collection) {
132 final Iterator it = ((Collection) source).iterator();
139 if (Objects.isNull(next)) {
140 LOG.trace("Cannot convert {}, because it is empty collection", type);
141 return Optional.empty();
144 type = next.getClass();
147 Convertor convertor = null;
149 if (!convertors.containsKey(type)) {
150 boolean found = false;
152 for (Class<?> key : convertorKeys) {
153 if (key.isAssignableFrom(type)) {
154 convertor = convertors.get(key);
155 convertors.put(type, convertor);
156 LOG.debug("{} is now converted by {}", type, convertor);
163 LOG.error("Convertor for {} not found", type);
164 return Optional.empty();
168 if (Objects.isNull(convertor)) {
169 convertor = convertors.get(type);
172 final Object result = convertor.convert(source);
173 return Optional.of((TO) result);
177 * Lookup and use convertor by specified type, then converts source and returns converted result
179 * @param <FROM> the source type
180 * @param <TO> the result type
181 * @param <DATA> the data type
182 * @param source the source
183 * @param data convertor data
184 * @return the result (can be empty, if no convertor was found)
186 @SuppressWarnings("unchecked")
187 public <FROM, TO, DATA extends ConvertorData> Optional<TO> convert(final FROM source, final DATA data) {
188 if (Objects.isNull(source)) {
189 LOG.trace("Cannot convert source, because it is null");
190 return Optional.empty();
193 Class<?> type = source.getClass();
195 if (source instanceof Collection) {
196 final Iterator it = ((Collection) source).iterator();
203 if (Objects.isNull(next)) {
204 LOG.trace("Cannot convert {}, because it is empty collection", type);
205 return Optional.empty();
208 type = next.getClass();
211 ParametrizedConvertor convertor = null;
213 if (!parametrizedConvertors.containsKey(type)) {
214 boolean found = false;
216 for (Class<?> key : parametrizedConvertorKeys) {
217 if (key.isAssignableFrom(type)) {
218 convertor = parametrizedConvertors.get(key);
219 parametrizedConvertors.put(type, convertor);
220 LOG.debug("{} is now converted by {}", type, convertor);
227 LOG.error("Convertor for {} not found", type);
228 return Optional.empty();
232 if (Objects.isNull(convertor)) {
233 convertor = parametrizedConvertors.get(type);
236 final Object result = convertor.convert(source, data);
237 return Optional.of((TO) result);