Bug 5540 - FlowConvertor, FlowStatsResponseConvertor, FlowInstructionResponseConvertor
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / MatchReactorMappingFactory.java
1 /*
2  * Copyright (c) 2016 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.match;
10
11 import java.util.List;
12 import java.util.Map;
13 import org.opendaylight.openflowplugin.api.OFConstants;
14 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
15 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.InjectionKey;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ResultInjector;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.flow.FlowConvertor;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.field._case.SetFieldActionBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.aggregate._case.MultipartRequestAggregateBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlowBuilder;
26
27 /**
28  * add prepared convertors and injectors into given mappings
29  *
30  * @see MatchReactor
31  */
32 public class MatchReactorMappingFactory {
33
34     /**
35      * @param conversionMapping conversion mapping
36      */
37     public static void addMatchConvertors(final Map<Short, Convertor<Match, ?>> conversionMapping) {
38         conversionMapping.put(OFConstants.OFP_VERSION_1_3, new MatchConvertorImpl());
39         conversionMapping.put(OFConstants.OFP_VERSION_1_0, new MatchConvertorV10Impl());
40     }
41
42     /**
43      * @param injectionMapping injection mapping
44      */
45     public static void addMatchIjectors(final Map<InjectionKey, ResultInjector<?, ?>> injectionMapping) {
46         // OF-1.3|List<MatchEntries> --> FlowModInputBuilder
47         injectionMapping.put(new InjectionKey(OFConstants.OFP_VERSION_1_3, FlowModInputBuilder.class),
48                 new ResultInjector<List<MatchEntry>, FlowModInputBuilder>() {
49                     @Override
50                     public void inject(final List<MatchEntry> value,
51                                        final FlowModInputBuilder target) {
52                         target.setMatch(wrapMatchV13(value).build());
53                     }
54                 });
55
56         // OF-1.3|List<MatchEntries> --> OxmFieldsActionBuilder
57         injectionMapping.put(new InjectionKey(OFConstants.OFP_VERSION_1_3, SetFieldActionBuilder.class),
58                 new ResultInjector<List<MatchEntry>, SetFieldActionBuilder>() {
59                     @Override
60                     public void inject(final List<MatchEntry> value,
61                                        final SetFieldActionBuilder target) {
62                         target.setMatchEntry(value);
63                     }
64                 });
65
66         // OF-1.0|MatchV10Builder --> FlowModInputBuilder
67         injectionMapping.put(new InjectionKey(OFConstants.OFP_VERSION_1_0, FlowModInputBuilder.class),
68                 new ResultInjector<MatchV10, FlowModInputBuilder>() {
69                     @Override
70                     public void inject(final MatchV10 value,
71                                        final FlowModInputBuilder target) {
72                         target.setMatchV10(value);
73                     }
74                 });
75
76         // OF-1.3|List<MatchEntries> --> MultipartRequestFlowBuilder
77         injectionMapping.put(new InjectionKey(OFConstants.OFP_VERSION_1_3, MultipartRequestFlowBuilder.class),
78                 new ResultInjector<List<MatchEntry>, MultipartRequestFlowBuilder>() {
79                     @Override
80                     public void inject(final List<MatchEntry> value,
81                                        final MultipartRequestFlowBuilder target) {
82                         target.setMatch(wrapMatchV13(value).build());
83                     }
84                 });
85
86         // OF-1.0|List<MatchEntries> --> MultipartRequestFlowBuilder
87         injectionMapping.put(new InjectionKey(OFConstants.OFP_VERSION_1_0, MultipartRequestFlowBuilder.class),
88                 new ResultInjector<MatchV10, MultipartRequestFlowBuilder>() {
89                     @Override
90                     public void inject(final MatchV10 value,
91                                        final MultipartRequestFlowBuilder target) {
92                         target.setMatchV10(value);
93                     }
94                 });
95
96         // OF-1.3|List<MatchEntries> --> MultipartRequestAggregateBuilder
97         injectionMapping.put(new InjectionKey(OFConstants.OFP_VERSION_1_3, MultipartRequestAggregateBuilder.class),
98                 new ResultInjector<List<MatchEntry>, MultipartRequestAggregateBuilder>() {
99                     @Override
100                     public void inject(final List<MatchEntry> value,
101                                        final MultipartRequestAggregateBuilder target) {
102                         target.setMatch(wrapMatchV13(value).build());
103                     }
104                 });
105
106         // OF-1.0|List<MatchEntries> --> MultipartRequestAggregateBuilder
107         injectionMapping.put(new InjectionKey(OFConstants.OFP_VERSION_1_0, MultipartRequestAggregateBuilder.class),
108                 new ResultInjector<MatchV10, MultipartRequestAggregateBuilder>() {
109                     @Override
110                     public void inject(final MatchV10 value,
111                                        final MultipartRequestAggregateBuilder target) {
112                         target.setMatchV10(value);
113                     }
114                 });
115     }
116
117     /**
118      * @param value pure match
119      * @return wrapped match
120      */
121     public static MatchBuilder wrapMatchV13(final List<MatchEntry> value) {
122         MatchBuilder matchBuilder = new MatchBuilder();
123         matchBuilder.setType(FlowConvertor.DEFAULT_MATCH_TYPE);
124         if (value == null) {
125             matchBuilder.setMatchEntry(FlowConvertor.DEFAULT_MATCH_ENTRIES);
126         } else {
127             matchBuilder.setMatchEntry(value);
128         }
129         return matchBuilder;
130     }
131 }