13b23249fc448cc9ce300b199b1023a7e58cb438
[openflowplugin.git] / extension / openflowplugin-extension-nicira / src / main / java / org / opendaylight / openflowplugin / extension / vendor / nicira / convertor / match / CtTpSrcConvertor.java
1 /*
2  * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. 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.Nxm1Class;
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.nx.match.ct.tp.src.grouping.CtTpSrcValuesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.CtTpSrcCaseValue;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.CtTpSrcCaseValueBuilder;
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.NxmNxCtTpSrcGrouping;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxCtTpSrcKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.ct.tp.src.grouping.NxmNxCtTpSrc;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.ct.tp.src.grouping.NxmNxCtTpSrcBuilder;
39 import org.opendaylight.yangtools.yang.binding.Augmentation;
40
41 public class CtTpSrcConvertor implements ConvertorToOFJava<MatchEntry>, ConvertorFromOFJava<MatchEntry, MatchPath> {
42
43     @Override
44     public MatchEntry convert(Extension extension) {
45         Optional<NxmNxCtTpSrcGrouping> matchGrouping = MatchUtil.CT_TP_SRC_RESOLVER.getExtension(extension);
46         if (!matchGrouping.isPresent()) {
47             throw new CodecPreconditionException(extension);
48         }
49         CtTpSrcCaseValueBuilder ctTpSrcCaseValueBuilder = new CtTpSrcCaseValueBuilder();
50         CtTpSrcValuesBuilder ctTpSrcValuesBuilder = new CtTpSrcValuesBuilder();
51         ctTpSrcValuesBuilder.setCtTpSrc(matchGrouping.get().getNxmNxCtTpSrc().getCtTpSrc());
52         ctTpSrcCaseValueBuilder.setCtTpSrcValues(ctTpSrcValuesBuilder.build());
53         MatchEntryBuilder ofMatch = MatchUtil
54                 .createDefaultMatchEntryBuilder(org.opendaylight.yang.gen.v1.urn
55                                 .opendaylight.openflowjava.nx.match.rev140421.NxmNxCtTpSrc.class,
56                         Nxm1Class.class, ctTpSrcCaseValueBuilder.build());
57         return ofMatch.build();
58     }
59
60     @Override
61     public ExtensionAugment<? extends Augmentation<Extension>> convert(MatchEntry input, MatchPath path) {
62         CtTpSrcCaseValue ctTpSrcCaseValue = (CtTpSrcCaseValue) input.getMatchEntryValue();
63         NxmNxCtTpSrcBuilder ctTpSrcBuilder = new NxmNxCtTpSrcBuilder();
64         ctTpSrcBuilder.setCtTpSrc(ctTpSrcCaseValue.getCtTpSrcValues().getCtTpSrc());
65         return resolveAugmentation(ctTpSrcBuilder.build(), path,
66                 NxmNxCtTpSrcKey.class);
67     }
68
69     private static ExtensionAugment<? extends Augmentation<Extension>> resolveAugmentation(NxmNxCtTpSrc value,
70                                                                    MatchPath path, Class<? extends ExtensionKey> key) {
71         switch (path) {
72             case FLOWS_STATISTICS_UPDATE_MATCH:
73                 return new ExtensionAugment<>(NxAugMatchNodesNodeTableFlow.class,
74                         new NxAugMatchNodesNodeTableFlowBuilder().setNxmNxCtTpSrc(value).build(), key);
75             case FLOWS_STATISTICS_RPC_MATCH:
76                 return new ExtensionAugment<>(NxAugMatchRpcGetFlowStats.class,
77                         new NxAugMatchRpcGetFlowStatsBuilder().setNxmNxCtTpSrc(value).build(), key);
78             case PACKET_RECEIVED_MATCH:
79                 return new ExtensionAugment<>(NxAugMatchNotifPacketIn.class, new NxAugMatchNotifPacketInBuilder()
80                         .setNxmNxCtTpSrc(value).build(), key);
81             case SWITCH_FLOW_REMOVED_MATCH:
82                 return new ExtensionAugment<>(NxAugMatchNotifSwitchFlowRemoved.class,
83                         new NxAugMatchNotifSwitchFlowRemovedBuilder().setNxmNxCtTpSrc(value).build(), key);
84             case PACKET_IN_MESSAGE_MATCH:
85                 return new ExtensionAugment<>(NxAugMatchPacketInMessage.class,
86                         new NxAugMatchPacketInMessageBuilder().setNxmNxCtTpSrc(value).build(), key);
87             default:
88                 throw new CodecPreconditionException(path);
89         }
90     }
91 }