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;
15 import com.google.common.collect.ImmutableMap;
18 * @param <FROM> source type for conversion
21 public abstract class ConvertReactor<FROM> {
22 private final Map<InjectionKey, ResultInjector<?, ?>> injectionMapping;
23 private final Map<Short, Convertor<FROM, ?>> conversionMapping;
25 protected ConvertReactor() {
26 final Map<Short, Convertor<FROM, ?>> conversions = new HashMap<>();
27 final Map<InjectionKey, ResultInjector<?, ?>> injections = new HashMap<>();
28 initMappings(conversions, injections);
30 // Create optimized view of mappings
31 this.conversionMapping = ImmutableMap.copyOf(conversions);
32 this.injectionMapping = ImmutableMap.copyOf(injections);
36 * fill conversion and injection mappings
37 * @param conversions convert from
38 * @param injections injection
40 protected abstract void initMappings(Map<Short, Convertor<FROM, ?>> conversions,
41 Map<InjectionKey, ResultInjector<?, ?>> injections);
44 * @param source convert from
45 * @param version openflow version
46 * @param target convert to
47 * @param datapathid datapath id
48 * @param <RESULT> result
49 * @param <TARGET> target
51 @SuppressWarnings("unchecked")
52 public <RESULT, TARGET> void convert(final FROM source, final short version, final TARGET target, final BigInteger datapathid) {
55 Convertor<FROM, RESULT> convertor = (Convertor<FROM, RESULT>) conversionMapping.get(version);
56 if (convertor == null) {
57 throw new IllegalArgumentException("convertor for given version ["+version+"] not found");
59 RESULT convertedItem = convertor.convert(source,datapathid);
62 InjectionKey key = buildInjectionKey(version, convertedItem, target);
63 ResultInjector<RESULT, TARGET> injection = (ResultInjector<RESULT, TARGET>) injectionMapping.get(key);
64 if (injection == null) {
65 throw new IllegalArgumentException("injector for given version and target ["+key+"] not found");
67 injection.inject(convertedItem, target);
71 * @param version openflow version
72 * @param convertedItem to be injected
73 * @param target object
74 * @return injection key
76 protected InjectionKey buildInjectionKey(final short version, final Object convertedItem, final Object target) {
77 return new InjectionKey(version, target.getClass());