BUG-2794: Incorporated model Action api changes in OFJava to openflowplugin
[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 org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey;
11 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
12 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
13 import org.opendaylight.openflowplugin.extension.api.ConvertorFromOFJava;
14 import org.opendaylight.openflowplugin.extension.api.ExtensionAugment;
15 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
16 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmClassBase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifPacketIn;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifPacketInBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifSwitchFlowRemoved;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifSwitchFlowRemovedBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifUpdateFlowStats;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNotifUpdateFlowStatsBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match;
31 import org.opendaylight.yangtools.yang.binding.Augmentable;
32 import org.opendaylight.yangtools.yang.binding.Augmentation;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.List;
38
39 /**
40  *
41  */
42 public final class MatchExtensionHelper {
43
44     private static final Logger LOG = LoggerFactory
45             .getLogger(MatchExtensionHelper.class);
46
47     private MatchExtensionHelper() {
48         throw new IllegalAccessError("singleton enforcement");
49     }
50
51     /**
52      * @param matchEntries
53      * @param ofVersion
54      * @param matchPath
55      * @return augmentation wrapper containing augmentation depending on matchPath
56      */
57     @SuppressWarnings("unchecked")
58     public static <EXT_POINT extends Augmentable<EXT_POINT>>
59     AugmentTuple<EXT_POINT> processAllExtensions(Collection<MatchEntry> matchEntries,
60                                                  OpenflowVersion ofVersion, MatchPath matchPath) {
61         List<ExtensionList> extensionsList = new ArrayList<>();
62
63         for (MatchEntry matchEntry : matchEntries) {
64             ExtensionListBuilder extensionListBld = processExtension(matchEntry, ofVersion, matchPath);
65             if (extensionListBld == null) {
66                 continue;
67             }
68
69             extensionsList.add(extensionListBld.build());
70         }
71
72         AugmentTuple<EXT_POINT> augmentTuple = null;
73         if (!extensionsList.isEmpty()) {
74             switch (matchPath) {
75                 case FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_MATCH:
76                     GeneralAugMatchNotifUpdateFlowStatsBuilder generalExtMatchAugBld1 = new GeneralAugMatchNotifUpdateFlowStatsBuilder();
77                     generalExtMatchAugBld1.setExtensionList(extensionsList);
78                     augmentTuple = (AugmentTuple<EXT_POINT>)
79                             new AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match>(
80                                     GeneralAugMatchNotifUpdateFlowStats.class, generalExtMatchAugBld1.build());
81                     break;
82                 case PACKETRECEIVED_MATCH:
83                     GeneralAugMatchNotifPacketInBuilder generalExtMatchAugBld2 = new GeneralAugMatchNotifPacketInBuilder();
84                     generalExtMatchAugBld2.setExtensionList(extensionsList);
85                     augmentTuple = (AugmentTuple<EXT_POINT>)
86                             new AugmentTuple<Match>(GeneralAugMatchNotifPacketIn.class, generalExtMatchAugBld2.build());
87                     break;
88                 case SWITCHFLOWREMOVED_MATCH:
89                     GeneralAugMatchNotifSwitchFlowRemovedBuilder generalExtMatchAugBld3 = new GeneralAugMatchNotifSwitchFlowRemovedBuilder();
90                     generalExtMatchAugBld3.setExtensionList(extensionsList);
91                     augmentTuple = (AugmentTuple<EXT_POINT>)
92                             new AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.mod.removed.Match>(
93                                     GeneralAugMatchNotifSwitchFlowRemoved.class, generalExtMatchAugBld3.build());
94                     break;
95                 default:
96                     LOG.warn("matchPath not supported: {}", matchPath);
97             }
98         }
99
100         return augmentTuple;
101     }
102
103     /**
104      * @param ofVersion
105      * @param matchPath
106      * @param matchEntry
107      * @return
108      */
109     private static ExtensionListBuilder processExtension(MatchEntry matchEntry, OpenflowVersion ofVersion, MatchPath matchPath) {
110         ExtensionListBuilder extListBld = null;
111
112         /** TODO: EXTENSION PROPOSAL (match, OFJava to MD-SAL) */
113         MatchEntrySerializerKey<? extends OxmClassBase, ? extends MatchField> key = new MatchEntrySerializerKey<>(
114                 ofVersion.getVersion(), matchEntry.getOxmClass(), matchEntry.getOxmMatchField());
115         ConvertorFromOFJava<MatchEntry, MatchPath> convertor = OFSessionUtil.getExtensionConvertorProvider().getConverter(key);
116         if (convertor != null) {
117             ExtensionAugment<? extends Augmentation<Extension>> extensionMatch =
118                     convertor.convert(matchEntry, matchPath);
119             ExtensionBuilder extBld = new ExtensionBuilder();
120             extBld.addAugmentation(extensionMatch.getAugmentationClass(), extensionMatch.getAugmentationObject());
121
122             extListBld = new ExtensionListBuilder();
123             extListBld.setExtension(extBld.build());
124             extListBld.setExtensionKey(extensionMatch.getKey());
125         }
126
127
128         return extListBld;
129     }
130
131 }