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