Bump upstreams for 2022.09 Chlorine
[openflowplugin.git] / extension / openflowplugin-extension-nicira / src / main / java / org / opendaylight / openflowplugin / extension / vendor / nicira / convertor / match / PktMarkConvertor.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 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.Nxm1Class;
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.PktMarkCaseValue;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.PktMarkCaseValueBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.pkt.mark.grouping.PktMarkValuesBuilder;
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.NxmNxPktMarkGrouping;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxPktMarkKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.pkt.mark.grouping.NxmNxPktMark;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.pkt.mark.grouping.NxmNxPktMarkBuilder;
38 import org.opendaylight.yangtools.yang.binding.Augmentation;
39
40 public class PktMarkConvertor implements ConvertorToOFJava<MatchEntry>, ConvertorFromOFJava<MatchEntry, MatchPath> {
41
42     @Override
43     public MatchEntry convert(final Extension extension) {
44         Optional<NxmNxPktMarkGrouping> matchGrouping = MatchUtil.PKT_MARK_RESOLVER.findExtension(extension);
45         if (!matchGrouping.isPresent()) {
46             throw new CodecPreconditionException(extension);
47         }
48         PktMarkCaseValueBuilder pktMarkCaseValueBuilder = new PktMarkCaseValueBuilder();
49         PktMarkValuesBuilder pktMarkValuesBuilder = new PktMarkValuesBuilder();
50         pktMarkValuesBuilder.setPktMark(matchGrouping.get().getNxmNxPktMark().getPktMark());
51         pktMarkValuesBuilder.setMask(matchGrouping.get().getNxmNxPktMark().getMask());
52         pktMarkCaseValueBuilder.setPktMarkValues(pktMarkValuesBuilder.build());
53         MatchEntryBuilder ofMatch = MatchUtil
54                 .createDefaultMatchEntryBuilder(org.opendaylight.yang.gen.v1.urn
55                                 .opendaylight.openflowjava.nx.match.rev140421.NxmNxPktMark.VALUE,
56                         Nxm1Class.VALUE, pktMarkCaseValueBuilder.build());
57         return ofMatch.build();
58     }
59
60     @Override
61     public ExtensionAugment<? extends Augmentation<Extension>> convert(final MatchEntry input, final MatchPath path) {
62         PktMarkCaseValue pktMarkCaseValue = (PktMarkCaseValue) input.getMatchEntryValue();
63         NxmNxPktMarkBuilder pktMarkBuilder = new NxmNxPktMarkBuilder();
64         pktMarkBuilder.setPktMark(pktMarkCaseValue.getPktMarkValues().getPktMark());
65         pktMarkBuilder.setMask(pktMarkCaseValue.getPktMarkValues().getMask());
66         return resolveAugmentation(pktMarkBuilder.build(), path, NxmNxPktMarkKey.VALUE);
67     }
68
69     private static ExtensionAugment<? extends Augmentation<Extension>> resolveAugmentation(final NxmNxPktMark value,
70             final MatchPath path, final ExtensionKey key) {
71         switch (path) {
72             case FLOWS_STATISTICS_UPDATE_MATCH:
73                 return new ExtensionAugment<>(NxAugMatchNodesNodeTableFlow.class,
74                         new NxAugMatchNodesNodeTableFlowBuilder().setNxmNxPktMark(value).build(), key);
75             case FLOWS_STATISTICS_RPC_MATCH:
76                 return new ExtensionAugment<>(NxAugMatchRpcGetFlowStats.class,
77                         new NxAugMatchRpcGetFlowStatsBuilder().setNxmNxPktMark(value).build(), key);
78             case PACKET_RECEIVED_MATCH:
79                 return new ExtensionAugment<>(NxAugMatchNotifPacketIn.class, new NxAugMatchNotifPacketInBuilder()
80                         .setNxmNxPktMark(value).build(), key);
81             case SWITCH_FLOW_REMOVED_MATCH:
82                 return new ExtensionAugment<>(NxAugMatchNotifSwitchFlowRemoved.class,
83                         new NxAugMatchNotifSwitchFlowRemovedBuilder().setNxmNxPktMark(value).build(), key);
84             case PACKET_IN_MESSAGE_MATCH:
85                 return new ExtensionAugment<>(NxAugMatchPacketInMessage.class,
86                         new NxAugMatchPacketInMessageBuilder().setNxmNxPktMark(value).build(), key);
87             default:
88                 throw new CodecPreconditionException(path);
89         }
90     }
91 }