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