9b26310f4f2bbc35dafa681709640fbac25c40dd
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / extension / MatchExtensionHelper.java
1 /**
2  * Copyright (c) 2014 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.extension;
9
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.List;
13 import java.util.Objects;
14 import java.util.Optional;
15 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey;
16 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
17 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
18 import org.opendaylight.openflowplugin.extension.api.ConvertorFromOFJava;
19 import org.opendaylight.openflowplugin.extension.api.ExtensionAugment;
20 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
21 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmClassBase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifPacketIn;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifPacketInBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifSwitchFlowRemoved;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifSwitchFlowRemovedBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifUpdateFlowStats;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifUpdateFlowStatsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchPacketInMessage;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchPacketInMessageBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchRpcOutputFlowStats;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchRpcOutputFlowStatsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
42 import org.opendaylight.yangtools.yang.binding.Augmentable;
43 import org.opendaylight.yangtools.yang.binding.Augmentation;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 public final class MatchExtensionHelper {
48
49     private static final Logger LOG = LoggerFactory
50             .getLogger(MatchExtensionHelper.class);
51
52     private MatchExtensionHelper() {
53         throw new IllegalAccessError("singleton enforcement");
54     }
55
56     /**
57      * Injects an extension.
58      *
59      * @param matchEntry match entry
60      * @param ofVersion openflow version
61      * @param matchPath match path
62      */
63     public static void injectExtension(
64             final short ofVersion,
65             final MatchEntry matchEntry,
66             final MatchBuilder matchBuilder,
67             final MatchPath matchPath) {
68
69         final ExtensionListBuilder extBuilder = processExtension(matchEntry, ofVersion, matchPath);
70
71         if (Objects.isNull(extBuilder)) {
72             LOG.warn("Convertor for {} for version {} with match path {} not found.",
73                     matchEntry.toString(),
74                     ofVersion,
75                     matchPath.name());
76         }
77
78         final GeneralAugMatchNodesNodeTableFlowBuilder builder = Optional
79                 .ofNullable(matchBuilder.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class))
80                 .map(GeneralAugMatchNodesNodeTableFlowBuilder::new)
81                 .orElse(new GeneralAugMatchNodesNodeTableFlowBuilder().setExtensionList(new ArrayList<>()));
82
83         builder.getExtensionList().add(extBuilder.build());
84         matchBuilder.addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, builder.build());
85     }
86
87     /**
88      * Processes all extensions.
89      *
90      * @param matchEntries match entries
91      * @param ofVersion openflow version
92      * @param matchPath match path
93      * @param <E> extension point
94      * @return augmentation wrapper containing augmentation depending on matchPath
95      */
96     @SuppressWarnings("unchecked")
97     public static <E extends Augmentable<E>> AugmentTuple<E> processAllExtensions(Collection<MatchEntry> matchEntries,
98             OpenflowVersion ofVersion, MatchPath matchPath) {
99         List<ExtensionList> extensionsList = new ArrayList<>();
100
101         for (MatchEntry matchEntry : matchEntries) {
102             ExtensionListBuilder extensionListBld = processExtension(matchEntry, ofVersion.getVersion(), matchPath);
103             if (extensionListBld == null) {
104                 continue;
105             }
106
107             extensionsList.add(extensionListBld.build());
108         }
109
110         AugmentTuple<E> augmentTuple = null;
111         if (!extensionsList.isEmpty()) {
112             switch (matchPath) {
113                 case FLOWS_STATISTICS_UPDATE_MATCH:
114                     GeneralAugMatchNotifUpdateFlowStatsBuilder generalExtMatchAugBld1 =
115                             new GeneralAugMatchNotifUpdateFlowStatsBuilder();
116                     generalExtMatchAugBld1.setExtensionList(extensionsList);
117                     augmentTuple = (AugmentTuple<E>)
118                             new AugmentTuple<>(
119                                     GeneralAugMatchNotifUpdateFlowStats.class, generalExtMatchAugBld1.build());
120                     break;
121                 case PACKET_RECEIVED_MATCH:
122                     GeneralAugMatchNotifPacketInBuilder generalExtMatchAugBld2 =
123                             new GeneralAugMatchNotifPacketInBuilder();
124                     generalExtMatchAugBld2.setExtensionList(extensionsList);
125                     augmentTuple = (AugmentTuple<E>)
126                             new AugmentTuple<>(GeneralAugMatchNotifPacketIn.class, generalExtMatchAugBld2.build());
127                     break;
128                 case PACKET_IN_MESSAGE_MATCH:
129                     GeneralAugMatchPacketInMessageBuilder generalExtMatchAugBld5 =
130                             new GeneralAugMatchPacketInMessageBuilder();
131                     generalExtMatchAugBld5.setExtensionList(extensionsList);
132                     augmentTuple = (AugmentTuple<E>)
133                             new AugmentTuple<>(GeneralAugMatchPacketInMessage.class, generalExtMatchAugBld5.build());
134                     break;
135                 case SWITCH_FLOW_REMOVED_MATCH:
136                     GeneralAugMatchNotifSwitchFlowRemovedBuilder generalExtMatchAugBld3 =
137                             new GeneralAugMatchNotifSwitchFlowRemovedBuilder();
138                     generalExtMatchAugBld3.setExtensionList(extensionsList);
139                     augmentTuple = (AugmentTuple<E>)
140                             new AugmentTuple<>(
141                                     GeneralAugMatchNotifSwitchFlowRemoved.class, generalExtMatchAugBld3.build());
142                     break;
143                 case FLOWS_STATISTICS_RPC_MATCH:
144                     GeneralAugMatchRpcOutputFlowStatsBuilder generalExtMatchAugBld4 =
145                            new GeneralAugMatchRpcOutputFlowStatsBuilder();
146                     generalExtMatchAugBld4.setExtensionList(extensionsList);
147                     augmentTuple = (AugmentTuple<E>)
148                             new AugmentTuple<>(
149                                     GeneralAugMatchRpcOutputFlowStats.class, generalExtMatchAugBld4.build());
150                     break;
151                 default:
152                     LOG.warn("matchPath not supported: {}", matchPath);
153             }
154         }
155
156         return augmentTuple;
157     }
158
159     /**
160      * Processes an extension.
161      *
162      * @param ofVersion openflow version
163      * @param matchPath match path
164      * @param matchEntry match entry
165      * @return an ExtensionListBuilder
166      */
167     private static ExtensionListBuilder processExtension(MatchEntry matchEntry, short ofVersion, MatchPath matchPath) {
168         ExtensionListBuilder extListBld = null;
169
170         // TODO: EXTENSION PROPOSAL (match, OFJava to MD-SAL)
171         MatchEntrySerializerKey<? extends OxmClassBase, ? extends MatchField> key = new MatchEntrySerializerKey<>(
172                 ofVersion, matchEntry.getOxmClass(), matchEntry.getOxmMatchField());
173         if (null != OFSessionUtil.getExtensionConvertorProvider()) {
174             ConvertorFromOFJava<MatchEntry, MatchPath> convertor =
175                     OFSessionUtil.getExtensionConvertorProvider().getConverter(key);
176             if (convertor != null) {
177                 ExtensionAugment<? extends Augmentation<Extension>> extensionMatch =
178                         convertor.convert(matchEntry, matchPath);
179                 ExtensionBuilder extBld = new ExtensionBuilder();
180                 extBld.addAugmentation(extensionMatch.getAugmentationClass(), extensionMatch.getAugmentationObject());
181
182                 extListBld = new ExtensionListBuilder();
183                 extListBld.setExtension(extBld.build());
184                 extListBld.setExtensionKey(extensionMatch.getKey());
185             }
186         }
187         return extListBld;
188     }
189 }