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());
38 // Actual convertor keys
39 private List<Class<?>> convertorKeys = new ArrayList<>();
40 private List<Class<?>> parametrizedConvertorKeys = new ArrayList<>();
42 // Cache, that holds all registered convertors, but they can have multiple keys,
43 // based on instanceof checks in the convert method
44 private Map<Class<?>, Convertor> convertors = new HashMap<>();
45 private Map<Class<?>, ParametrizedConvertor> parametrizedConvertors = new HashMap<>();
47 private ConvertorManager() {
48 // Hiding implicit constructor
52 * Gets instance of Convertor Manager.
54 * @return the instance
56 public static ConvertorManager getInstance() {
63 * @param convertor the convertor
64 * @return if registration was successful
66 public boolean registerConvertor(final Convertor convertor) {
67 final Class<?> type = convertor.getType();
69 if (convertors.containsKey(type)) {
70 LOG.warn("Convertor for type {} is already registered", type);
74 convertorKeys.add(type);
75 convertors.put(type, convertor);
76 LOG.debug("{} is now converted by {}", type, convertor);
83 * @param convertor the convertor
84 * @return if registration was successful
86 public boolean registerConvertor(final ParametrizedConvertor convertor) {
87 final Class<?> type = convertor.getType();
89 if (parametrizedConvertors.containsKey(type)) {
90 LOG.warn("Convertor for type {} is already registered", type);
94 parametrizedConvertorKeys.add(type);
95 parametrizedConvertors.put(type, convertor);
96 LOG.debug("{} is now converted by {}", type, convertor);
101 * Lookup and use convertor by specified type, then converts source and returns converted result
103 * @param <FROM> the source type
104 * @param <TO> the result type
105 * @param source the source
106 * @return the result (can be empty, if no convertor was found)
108 @SuppressWarnings("unchecked")
109 public <FROM, TO> Optional<TO> convert(final FROM source) {
110 if (Objects.isNull(source)) {
111 LOG.trace("Cannot convert source, because it is null");
112 return Optional.empty();
115 Class<?> type = source.getClass();
117 if (source instanceof Collection) {
118 final Iterator it = ((Collection) source).iterator();
125 if (Objects.isNull(next)) {
126 LOG.trace("Cannot convert {}, because it is empty collection", type);
127 return Optional.empty();
130 type = next.getClass();
133 Convertor convertor = null;
135 if (!convertors.containsKey(type)) {
136 boolean found = false;
138 for (Class<?> key : convertorKeys) {
139 if (key.isAssignableFrom(type)) {
140 convertor = convertors.get(key);
141 convertors.put(type, convertor);
142 LOG.debug("{} is now converted by {}", type, convertor);
149 LOG.error("Convertor for {} not found", type);
150 return Optional.empty();
154 if (Objects.isNull(convertor)) {
155 convertor = convertors.get(type);
158 final Object result = convertor.convert(source);
159 return Optional.of((TO) result);
163 * Lookup and use convertor by specified type, then converts source and returns converted result
165 * @param <FROM> the source type
166 * @param <TO> the result type
167 * @param <DATA> the data type
168 * @param source the source
169 * @param data convertor data
170 * @return the result (can be empty, if no convertor was found)
172 @SuppressWarnings("unchecked")
173 public <FROM, TO, DATA extends ConvertorData> Optional<TO> convert(final FROM source, final DATA data) {
174 if (Objects.isNull(source)) {
175 LOG.trace("Cannot convert source, because it is null");
176 return Optional.empty();
179 Class<?> type = source.getClass();
181 if (source instanceof Collection) {
182 final Iterator it = ((Collection) source).iterator();
189 if (Objects.isNull(next)) {
190 LOG.trace("Cannot convert {}, because it is empty collection", type);
191 return Optional.empty();
194 type = next.getClass();
197 ParametrizedConvertor convertor = null;
199 if (!parametrizedConvertors.containsKey(type)) {
200 boolean found = false;
202 for (Class<?> key : parametrizedConvertorKeys) {
203 if (key.isAssignableFrom(type)) {
204 convertor = parametrizedConvertors.get(key);
205 parametrizedConvertors.put(type, convertor);
206 LOG.debug("{} is now converted by {}", type, convertor);
213 LOG.error("Convertor for {} not found", type);
214 return Optional.empty();
218 if (Objects.isNull(convertor)) {
219 convertor = parametrizedConvertors.get(type);
222 final Object result = convertor.convert(source, data);
223 return Optional.of((TO) result);