Merge "Refactor nsh fields to new encoding"
[openflowplugin.git] / extension / openflowplugin-extension-nicira / src / main / java / org / opendaylight / openflowplugin / extension / vendor / nicira / convertor / match / TcpDstConvertor.java
1 /*
2  * Copyright (c) 2015 Hewlett-Packard Enterprise 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.extension.vendor.nicira.convertor.match;
10
11 import com.google.common.base.Optional;
12 import org.opendaylight.openflowplugin.extension.api.ConvertorFromOFJava;
13 import org.opendaylight.openflowplugin.extension.api.ConvertorToOFJava;
14 import org.opendaylight.openflowplugin.extension.api.ExtensionAugment;
15 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
16 import org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.CodecPreconditionException;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm0Class;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.of.match.tcp.dst.grouping.TcpDstValuesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.TcpDstCaseValue;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.TcpDstCaseValueBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifPacketIn;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifPacketInBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifSwitchFlowRemoved;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifSwitchFlowRemovedBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchPacketInMessage;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchPacketInMessageBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchRpcGetFlowStats;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchRpcGetFlowStatsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfTcpDstGrouping;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfTcpDstKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.tcp.dst.grouping.NxmOfTcpDst;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.tcp.dst.grouping.NxmOfTcpDstBuilder;
39 import org.opendaylight.yangtools.yang.binding.Augmentation;
40
41 /**
42  * Convert to/from SAL flow model to openflowjava model for TcpDstCase.
43  *
44  * @author Aswin Suryanarayanan.
45  */
46 public class TcpDstConvertor implements ConvertorToOFJava<MatchEntry>, ConvertorFromOFJava<MatchEntry, MatchPath> {
47
48     @Override
49     public ExtensionAugment<? extends Augmentation<Extension>> convert(MatchEntry input, MatchPath path) {
50         TcpDstCaseValue tcpDstCaseValue = (TcpDstCaseValue) input.getMatchEntryValue();
51         NxmOfTcpDstBuilder tcpDstBuilder = new NxmOfTcpDstBuilder();
52         tcpDstBuilder.setPort(tcpDstCaseValue.getTcpDstValues().getPort());
53         tcpDstBuilder.setMask(tcpDstCaseValue.getTcpDstValues().getMask());
54         return resolveAugmentation(tcpDstBuilder.build(), path,
55                 NxmOfTcpDstKey.class);
56     }
57
58     @Override
59     public MatchEntry convert(Extension extension) {
60         Optional<NxmOfTcpDstGrouping> matchGrouping = MatchUtil.TCP_DST_RESOLVER.getExtension(extension);
61         if (!matchGrouping.isPresent()) {
62             throw new CodecPreconditionException(extension);
63         }
64         TcpDstCaseValueBuilder tcpDstCaseValueBuilder = new TcpDstCaseValueBuilder();
65         TcpDstValuesBuilder tcpDstValuesBuilder = new TcpDstValuesBuilder();
66         tcpDstValuesBuilder.setPort(matchGrouping.get().getNxmOfTcpDst().getPort());
67         tcpDstValuesBuilder.setMask(matchGrouping.get().getNxmOfTcpDst().getMask());
68         tcpDstCaseValueBuilder.setTcpDstValues(tcpDstValuesBuilder.build());
69         MatchEntryBuilder ofMatch = MatchUtil
70                 .createDefaultMatchEntryBuilder(org.opendaylight.yang.gen.v1.urn
71                                                 .opendaylight.openflowjava.nx.match.rev140421.NxmOfTcpDst.class,
72                                                 Nxm0Class.class, tcpDstCaseValueBuilder.build());
73         ofMatch.setHasMask(true);
74         return ofMatch.build();
75     }
76
77     private static ExtensionAugment<? extends Augmentation<Extension>> resolveAugmentation(NxmOfTcpDst value,
78                                                                    MatchPath path, Class<? extends ExtensionKey> key) {
79         switch (path) {
80             case FLOWS_STATISTICS_UPDATE_MATCH:
81                 return new ExtensionAugment<>(NxAugMatchNodesNodeTableFlow.class,
82                         new NxAugMatchNodesNodeTableFlowBuilder().setNxmOfTcpDst(value).build(), key);
83             case FLOWS_STATISTICS_RPC_MATCH:
84                 return new ExtensionAugment<>(NxAugMatchRpcGetFlowStats.class,
85                         new NxAugMatchRpcGetFlowStatsBuilder().setNxmOfTcpDst(value).build(), key);
86             case PACKET_RECEIVED_MATCH:
87                 return new ExtensionAugment<>(NxAugMatchNotifPacketIn.class, new NxAugMatchNotifPacketInBuilder()
88                         .setNxmOfTcpDst(value).build(), key);
89             case SWITCH_FLOW_REMOVED_MATCH:
90                 return new ExtensionAugment<>(NxAugMatchNotifSwitchFlowRemoved.class,
91                         new NxAugMatchNotifSwitchFlowRemovedBuilder().setNxmOfTcpDst(value).build(), key);
92             case PACKET_IN_MESSAGE_MATCH:
93                 return new ExtensionAugment<>(NxAugMatchPacketInMessage.class,
94                         new NxAugMatchPacketInMessageBuilder().setNxmOfTcpDst(value).build(), key);
95             default:
96                 throw new CodecPreconditionException(path);
97         }
98     }
99 }