0b8752deb97ec46d95a7da2b6b912a411155ed85
[openflowplugin.git] / extension / openflowplugin-extension-nicira / src / main / java / org / opendaylight / openflowplugin / extension / vendor / nicira / convertor / match / TunIPv4SrcConvertor.java
1 /*
2  * Copyright (c) 2015 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
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.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm1Class;
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.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.tun.ipv4.src.grouping.TunIpv4SrcValuesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.TunIpv4SrcCaseValue;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.TunIpv4SrcCaseValueBuilder;
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.NxmNxTunIpv4SrcGrouping;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxTunIpv4SrcKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.ipv4.src.grouping.NxmNxTunIpv4Src;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.tun.ipv4.src.grouping.NxmNxTunIpv4SrcBuilder;
39 import org.opendaylight.yangtools.yang.binding.Augmentation;
40
41 public class TunIPv4SrcConvertor implements ConvertorToOFJava<MatchEntry>, ConvertorFromOFJava<MatchEntry, MatchPath> {
42     private static ExtensionAugment<? extends Augmentation<Extension>> resolveAugmentation(NxmNxTunIpv4Src value,
43             MatchPath path, Class<? extends ExtensionKey> key) {
44         switch (path) {
45             case FLOWS_STATISTICS_UPDATE_MATCH:
46                 return new ExtensionAugment<>(NxAugMatchNodesNodeTableFlow.class,
47                         new NxAugMatchNodesNodeTableFlowBuilder().setNxmNxTunIpv4Src(value).build(), key);
48             case FLOWS_STATISTICS_RPC_MATCH:
49                 return new ExtensionAugment<>(NxAugMatchRpcGetFlowStats.class,
50                         new NxAugMatchRpcGetFlowStatsBuilder().setNxmNxTunIpv4Src(value).build(), key);
51             case PACKET_RECEIVED_MATCH:
52                 return new ExtensionAugment<>(NxAugMatchNotifPacketIn.class,
53                         new NxAugMatchNotifPacketInBuilder().setNxmNxTunIpv4Src(value).build(), key);
54             case SWITCH_FLOW_REMOVED_MATCH:
55                 return new ExtensionAugment<>(NxAugMatchNotifSwitchFlowRemoved.class,
56                         new NxAugMatchNotifSwitchFlowRemovedBuilder().setNxmNxTunIpv4Src(value).build(), key);
57             case PACKET_IN_MESSAGE_MATCH:
58                 return new ExtensionAugment<>(NxAugMatchPacketInMessage.class,
59                         new NxAugMatchPacketInMessageBuilder().setNxmNxTunIpv4Src(value).build(), key);
60             default:
61                 throw new CodecPreconditionException(path);
62         }
63     }
64
65     @Override
66     public ExtensionAugment<? extends Augmentation<Extension>> convert(
67             MatchEntry input, MatchPath path) {
68         TunIpv4SrcCaseValue tunIpv4SrcCaseValue = (TunIpv4SrcCaseValue) input.getMatchEntryValue();
69         return resolveAugmentation(new NxmNxTunIpv4SrcBuilder()
70                 .setIpv4Address(MatchUtil.longToIpv4Address(tunIpv4SrcCaseValue.getTunIpv4SrcValues().getValue()))
71                 .build(), path, NxmNxTunIpv4SrcKey.class);
72     }
73
74     @Override
75     public MatchEntry convert(Extension extension) {
76         Optional<NxmNxTunIpv4SrcGrouping> matchGrouping = MatchUtil.TUN_IPV4_SRC_RESOLVER.getExtension(extension);
77         if (!matchGrouping.isPresent()) {
78             throw new CodecPreconditionException(extension);
79         }
80         Ipv4Address value = matchGrouping.get().getNxmNxTunIpv4Src().getIpv4Address();
81
82         TunIpv4SrcCaseValueBuilder tunIpv4SrcCaseValueBuilder = new TunIpv4SrcCaseValueBuilder();
83         tunIpv4SrcCaseValueBuilder.setTunIpv4SrcValues(new TunIpv4SrcValuesBuilder()
84                 .setValue(MatchUtil.ipv4ToLong(value)).build());
85         return MatchUtil.createDefaultMatchEntryBuilder(
86                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxTunIpv4Src.class,
87                 Nxm1Class.class, tunIpv4SrcCaseValueBuilder.build()).build();
88     }
89 }