Use ByteBuf.readRetainedSlice()
[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 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common;
9
10 import com.google.common.collect.ImmutableMap;
11 import java.util.HashMap;
12 import java.util.Map;
13 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
14 import org.opendaylight.yangtools.yang.common.Uint8;
15
16 /**
17  * Base class for a conversion reactor.
18  *
19  * @param <F> source type for conversion
20  */
21 public abstract class ConvertReactor<F> {
22     private final Map<ConvertorKey, ResultInjector<?, ?>> injectionMapping;
23     private final Map<Uint8, ConvertReactorConvertor<F, ?>> conversionMapping;
24
25     protected ConvertReactor() {
26         final Map<Uint8, ConvertReactorConvertor<F, ?>> conversions = new HashMap<>();
27         final Map<ConvertorKey, 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      *
38      * @param conversions convert from
39      * @param injections injection
40      */
41     protected abstract void initMappings(Map<Uint8, ConvertReactorConvertor<F, ?>> conversions,
42             Map<ConvertorKey, ResultInjector<?, ?>> injections);
43
44     /**
45      * Converts a source to a target.
46      *
47      * @param <R> result
48      * @param <T> target
49      * @param source convert from
50      * @param version openflow version
51      * @param target convert to
52      * @param convertorExecutor the convertor executor
53      */
54     @SuppressWarnings("unchecked")
55     public <R, T> void convert(final F source, final Uint8 version, final T target,
56             final ConvertorExecutor convertorExecutor) {
57
58         //lookup converter
59         ConvertReactorConvertor<F, R> convertor = (ConvertReactorConvertor<F, R>) conversionMapping.get(version);
60         if (convertor == null) {
61             throw new IllegalArgumentException("convertor for given version [" + version + "] not found");
62         }
63         R convertedItem = convertor.convert(source, convertorExecutor);
64
65         //lookup injection
66         ConvertorKey key = buildInjectionKey(version, convertedItem, target);
67         ResultInjector<R, T> injection = (ResultInjector<R, T>) injectionMapping.get(key);
68         if (injection == null) {
69             throw new IllegalArgumentException("injector for given version and target [" + key + "] not found");
70         }
71         injection.inject(convertedItem, target);
72     }
73
74     /**
75      * Builds an injection key.
76      *
77      * @param version openflow version
78      * @param convertedItem to be injected
79      * @param target object
80      * @return injection key
81      */
82     protected ConvertorKey buildInjectionKey(final Uint8 version, final Object convertedItem, final Object target) {
83         return new ConvertorKey(version, target.getClass());
84     }
85
86 }