d6e60b2a420a13b6aad60881a3a071f91f3c19d8
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / common / ConvertReactor.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common;
10
11 import java.math.BigInteger;
12 import java.util.HashMap;
13 import java.util.Map;
14
15 import com.google.common.collect.ImmutableMap;
16
17 /**
18  * @param <FROM> source type for conversion
19  *
20  */
21 public abstract class ConvertReactor<FROM> {
22     private final Map<InjectionKey, ResultInjector<?, ?>> injectionMapping;
23     private final Map<Short, Convertor<FROM, ?>> conversionMapping;
24
25     protected ConvertReactor() {
26         final Map<Short, Convertor<FROM, ?>> conversions = new HashMap<>();
27         final Map<InjectionKey, ResultInjector<?, ?>> injections = new HashMap<>();
28         initMappings(conversions, injections);
29
30         // Create optimized view of mappings
31         this.conversionMapping = ImmutableMap.copyOf(conversions);
32         this.injectionMapping = ImmutableMap.copyOf(injections);
33     }
34
35     /**
36      * fill conversion and injection mappings
37      * @param conversions convert from
38      * @param injections injection
39      */
40     protected abstract void initMappings(Map<Short, Convertor<FROM, ?>> conversions,
41             Map<InjectionKey, ResultInjector<?, ?>> injections);
42
43     /**
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
50      */
51     @SuppressWarnings("unchecked")
52     public <RESULT, TARGET> void convert(final FROM source, final short version, final TARGET target, final BigInteger datapathid) {
53
54         //lookup converter
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");
58         }
59         RESULT convertedItem = convertor.convert(source,datapathid);
60
61         //lookup injection
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");
66         }
67         injection.inject(convertedItem, target);
68     }
69
70     /**
71      * @param version openflow version
72      * @param convertedItem to be injected
73      * @param target object
74      * @return injection key
75      */
76     protected InjectionKey buildInjectionKey(final short version, final Object convertedItem, final Object target) {
77         return new InjectionKey(version, target.getClass());
78     }
79
80 }