2 * Copyright (c) 2013 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.common;
11 import java.math.BigInteger;
12 import java.util.HashMap;
16 * @param <FROM> source type for conversion
19 public abstract class ConvertReactor<FROM> {
21 private Map<Short, Convertor<FROM, ?>> conversionMapping;
22 private Map<InjectionKey, ResultInjector<?, ?>> injectionMapping;
24 protected ConvertReactor() {
25 conversionMapping = new HashMap<>();
26 injectionMapping = new HashMap<>();
27 initMappings(conversionMapping, injectionMapping);
31 * fill conversion and injection mappings
35 protected abstract void initMappings(Map<Short, Convertor<FROM, ?>> conversions,
36 Map<InjectionKey, ResultInjector<?, ?>> injections);
43 @SuppressWarnings("unchecked")
44 public <RESULT, TARGET> void convert(FROM source, short version, TARGET target,BigInteger datapathid) {
47 Convertor<FROM, RESULT> convertor = (Convertor<FROM, RESULT>) conversionMapping.get(version);
48 if (convertor == null) {
49 throw new IllegalArgumentException("convertor for given version ["+version+"] not found");
51 RESULT convertedItem = convertor.convert(source,datapathid);
54 InjectionKey key = new InjectionKey(version, target.getClass().getName());
55 ResultInjector<RESULT, TARGET> injection = (ResultInjector<RESULT, TARGET>) injectionMapping.get(key);
56 if (injection == null) {
57 throw new IllegalArgumentException("injector for given version and target ["+key+"] not found");
59 injection.inject(convertedItem, target);