Bump upstreams for 2022.09 Chlorine
[openflowplugin.git] / extension / openflowplugin-extension-nicira / src / main / java / org / opendaylight / openflowplugin / extension / vendor / nicira / convertor / match / RegConvertor.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.match;
9
10 import java.util.Optional;
11 import org.opendaylight.openflowjava.nx.codec.match.NiciraMatchCodecs;
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.MatchField;
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.aug.nx.match.RegCaseValue;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.RegCaseValueBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.reg.grouping.RegValuesBuilder;
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.NxmNxReg0Key;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg1Key;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg3Key;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg4Key;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg5Key;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg6Key;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg7Key;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxRegGrouping;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxReg;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxRegBuilder;
46 import org.opendaylight.yangtools.yang.binding.Augmentation;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * Convert to/from SAL flow model to openflowjava model for NxmNxReg.
52  *
53  * @author msunal
54  */
55 public class RegConvertor implements ConvertorToOFJava<MatchEntry>, ConvertorFromOFJava<MatchEntry, MatchPath> {
56
57     private static final Logger LOG = LoggerFactory.getLogger(RegConvertor.class);
58
59     @SuppressWarnings("unchecked")
60     @Override
61     public ExtensionAugment<? extends Augmentation<Extension>> convert(final MatchEntry input, final MatchPath path) {
62         NxmNxRegBuilder nxRegBuilder = new NxmNxRegBuilder();
63         if (!(input.getOxmMatchField()
64             instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg)) {
65             String msg = input.getOxmMatchField()
66                     + " does not implement "
67                     + org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg.class;
68             LOG.warn("Warning {}",msg);
69             throw new IllegalStateException(msg);
70         }
71         nxRegBuilder.setReg((org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg)
72             input.getOxmMatchField());
73         RegCaseValue regCaseValue = (RegCaseValue) input.getMatchEntryValue();
74         nxRegBuilder.setValue(regCaseValue.getRegValues().getValue());
75
76         if (input.getHasMask()) {
77             nxRegBuilder.setMask(regCaseValue.getRegValues().getMask());
78         }
79
80         return resolveAugmentation(nxRegBuilder.build(), path, resolveRegKey(input.getOxmMatchField()));
81     }
82
83     @Override
84     public MatchEntry convert(final Extension extension) {
85         Optional<NxmNxRegGrouping> matchGrouping = MatchUtil.REG_RESOLVER.findExtension(extension);
86         if (!matchGrouping.isPresent()) {
87             throw new CodecPreconditionException(extension);
88         }
89         NxmNxReg nxmNxReg = matchGrouping.get().getNxmNxReg();
90         RegValuesBuilder regValuesBuilder = new RegValuesBuilder()
91             .setValue(nxmNxReg.getValue())
92             .setMask(nxmNxReg.getMask());
93
94         RegCaseValueBuilder regCaseValueBuilder = new RegCaseValueBuilder();
95         regCaseValueBuilder.setRegValues(regValuesBuilder.build());
96         return MatchUtil.createDefaultMatchEntryBuilder(nxmNxReg.getReg(),
97                 Nxm1Class.VALUE,
98                 regCaseValueBuilder.build())
99             .setHasMask(nxmNxReg.getMask() != null)
100             .build();
101     }
102
103     private static ExtensionKey resolveRegKey(final MatchField oxmMatchField) {
104         // FIXME: Use direct field value equalitity
105         if (NiciraMatchCodecs.REG0_CODEC.getNxmField().equals(oxmMatchField)) {
106             return NxmNxReg0Key.VALUE;
107         }
108         if (NiciraMatchCodecs.REG1_CODEC.getNxmField().equals(oxmMatchField)) {
109             return NxmNxReg1Key.VALUE;
110         }
111         if (NiciraMatchCodecs.REG2_CODEC.getNxmField().equals(oxmMatchField)) {
112             return NxmNxReg2Key.VALUE;
113         }
114         if (NiciraMatchCodecs.REG3_CODEC.getNxmField().equals(oxmMatchField)) {
115             return NxmNxReg3Key.VALUE;
116         }
117         if (NiciraMatchCodecs.REG4_CODEC.getNxmField().equals(oxmMatchField)) {
118             return NxmNxReg4Key.VALUE;
119         }
120         if (NiciraMatchCodecs.REG5_CODEC.getNxmField().equals(oxmMatchField)) {
121             return NxmNxReg5Key.VALUE;
122         }
123         if (NiciraMatchCodecs.REG6_CODEC.getNxmField().equals(oxmMatchField)) {
124             return NxmNxReg6Key.VALUE;
125         }
126         if (NiciraMatchCodecs.REG7_CODEC.getNxmField().equals(oxmMatchField)) {
127             return NxmNxReg7Key.VALUE;
128         }
129         throw new CodecPreconditionException("There is no key for " + oxmMatchField);
130     }
131
132     private static ExtensionAugment<? extends Augmentation<Extension>> resolveAugmentation(final NxmNxReg nxmNxReg,
133             final MatchPath path, final ExtensionKey key) {
134         switch (path) {
135             case FLOWS_STATISTICS_UPDATE_MATCH:
136                 return new ExtensionAugment<>(NxAugMatchNodesNodeTableFlow.class,
137                         new NxAugMatchNodesNodeTableFlowBuilder().setNxmNxReg(nxmNxReg).build(), key);
138             case FLOWS_STATISTICS_RPC_MATCH:
139                 return new ExtensionAugment<>(NxAugMatchRpcGetFlowStats.class,
140                         new NxAugMatchRpcGetFlowStatsBuilder().setNxmNxReg(nxmNxReg).build(), key);
141             case PACKET_RECEIVED_MATCH:
142                 return new ExtensionAugment<>(NxAugMatchNotifPacketIn.class, new NxAugMatchNotifPacketInBuilder()
143                         .setNxmNxReg(nxmNxReg).build(), key);
144             case SWITCH_FLOW_REMOVED_MATCH:
145                 return new ExtensionAugment<>(NxAugMatchNotifSwitchFlowRemoved.class,
146                         new NxAugMatchNotifSwitchFlowRemovedBuilder().setNxmNxReg(nxmNxReg).build(), key);
147             case PACKET_IN_MESSAGE_MATCH:
148                 return new ExtensionAugment<>(NxAugMatchPacketInMessage.class,
149                         new NxAugMatchPacketInMessageBuilder().setNxmNxReg(nxmNxReg).build(), key);
150             default:
151                 throw new CodecPreconditionException(path);
152         }
153     }
154 }