Migrate users of Optional.get()
[openflowplugin.git] / extension / openflowplugin-extension-nicira / src / main / java / org / opendaylight / openflowplugin / extension / vendor / nicira / convertor / match / RegConvertor.java
index 7aaf2abe850222d91319774399f7d0651da5fc18..c4037c0917bc6f620e1481fd38ef0edb816c0356 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.match;
 
-import java.util.Optional;
 import org.opendaylight.openflowjava.nx.codec.match.NiciraMatchCodecs;
 import org.opendaylight.openflowplugin.extension.api.ConvertorFromOFJava;
 import org.opendaylight.openflowplugin.extension.api.ConvertorToOFJava;
@@ -17,9 +16,9 @@ import org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.CodecPr
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm1Class;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.RegCaseValue;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.RegCaseValueBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.reg.grouping.RegValuesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.RegCaseValue;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.RegCaseValueBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
@@ -40,7 +39,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.ni
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg5Key;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg6Key;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg7Key;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxRegGrouping;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxReg;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxRegBuilder;
 import org.opendaylight.yangtools.yang.binding.Augmentation;
@@ -60,16 +58,16 @@ public class RegConvertor implements ConvertorToOFJava<MatchEntry>, ConvertorFro
     @Override
     public ExtensionAugment<? extends Augmentation<Extension>> convert(final MatchEntry input, final MatchPath path) {
         NxmNxRegBuilder nxRegBuilder = new NxmNxRegBuilder();
-        if (!org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg.class
-                .isAssignableFrom(input.getOxmMatchField())) {
+        if (!(input.getOxmMatchField()
+            instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg)) {
             String msg = input.getOxmMatchField()
                     + " does not implement "
                     + org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg.class;
             LOG.warn("Warning {}",msg);
             throw new IllegalStateException(msg);
         }
-        nxRegBuilder.setReg((Class<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match
-                .rev140421.NxmNxReg>) input.getOxmMatchField());
+        nxRegBuilder.setReg((org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg)
+            input.getOxmMatchField());
         RegCaseValue regCaseValue = (RegCaseValue) input.getMatchEntryValue();
         nxRegBuilder.setValue(regCaseValue.getRegValues().getValue());
 
@@ -82,72 +80,62 @@ public class RegConvertor implements ConvertorToOFJava<MatchEntry>, ConvertorFro
 
     @Override
     public MatchEntry convert(final Extension extension) {
-        Optional<NxmNxRegGrouping> matchGrouping = MatchUtil.REG_RESOLVER.findExtension(extension);
-        if (!matchGrouping.isPresent()) {
+        final var matchGrouping = MatchUtil.REG_RESOLVER.findExtension(extension);
+        if (matchGrouping.isEmpty()) {
             throw new CodecPreconditionException(extension);
         }
-        NxmNxReg nxmNxReg = matchGrouping.get().getNxmNxReg();
-        RegValuesBuilder regValuesBuilder = new RegValuesBuilder()
-            .setValue(nxmNxReg.getValue())
-            .setMask(nxmNxReg.getMask());
+        final var nxmNxReg = matchGrouping.orElseThrow().getNxmNxReg();
 
-        RegCaseValueBuilder regCaseValueBuilder = new RegCaseValueBuilder();
-        regCaseValueBuilder.setRegValues(regValuesBuilder.build());
-        return MatchUtil.createDefaultMatchEntryBuilder(nxmNxReg.getReg(),
-                Nxm1Class.class,
-                regCaseValueBuilder.build())
+        return MatchUtil.createDefaultMatchEntryBuilder(nxmNxReg.getReg(), Nxm1Class.VALUE,
+            new RegCaseValueBuilder()
+                .setRegValues(new RegValuesBuilder().setValue(nxmNxReg.getValue()).setMask(nxmNxReg.getMask()).build())
+                .build())
             .setHasMask(nxmNxReg.getMask() != null)
             .build();
     }
 
-    private static Class<? extends ExtensionKey> resolveRegKey(final Class<? extends MatchField> oxmMatchField) {
-        if (NiciraMatchCodecs.REG0_CODEC.getNxmField().isAssignableFrom(oxmMatchField)) {
-            return NxmNxReg0Key.class;
+    private static ExtensionKey resolveRegKey(final MatchField oxmMatchField) {
+        // FIXME: Use direct field value equalitity
+        if (NiciraMatchCodecs.REG0_CODEC.getNxmField().equals(oxmMatchField)) {
+            return NxmNxReg0Key.VALUE;
         }
-        if (NiciraMatchCodecs.REG1_CODEC.getNxmField().isAssignableFrom(oxmMatchField)) {
-            return NxmNxReg1Key.class;
+        if (NiciraMatchCodecs.REG1_CODEC.getNxmField().equals(oxmMatchField)) {
+            return NxmNxReg1Key.VALUE;
         }
-        if (NiciraMatchCodecs.REG2_CODEC.getNxmField().isAssignableFrom(oxmMatchField)) {
-            return NxmNxReg2Key.class;
+        if (NiciraMatchCodecs.REG2_CODEC.getNxmField().equals(oxmMatchField)) {
+            return NxmNxReg2Key.VALUE;
         }
-        if (NiciraMatchCodecs.REG3_CODEC.getNxmField().isAssignableFrom(oxmMatchField)) {
-            return NxmNxReg3Key.class;
+        if (NiciraMatchCodecs.REG3_CODEC.getNxmField().equals(oxmMatchField)) {
+            return NxmNxReg3Key.VALUE;
         }
-        if (NiciraMatchCodecs.REG4_CODEC.getNxmField().isAssignableFrom(oxmMatchField)) {
-            return NxmNxReg4Key.class;
+        if (NiciraMatchCodecs.REG4_CODEC.getNxmField().equals(oxmMatchField)) {
+            return NxmNxReg4Key.VALUE;
         }
-        if (NiciraMatchCodecs.REG5_CODEC.getNxmField().isAssignableFrom(oxmMatchField)) {
-            return NxmNxReg5Key.class;
+        if (NiciraMatchCodecs.REG5_CODEC.getNxmField().equals(oxmMatchField)) {
+            return NxmNxReg5Key.VALUE;
         }
-        if (NiciraMatchCodecs.REG6_CODEC.getNxmField().isAssignableFrom(oxmMatchField)) {
-            return NxmNxReg6Key.class;
+        if (NiciraMatchCodecs.REG6_CODEC.getNxmField().equals(oxmMatchField)) {
+            return NxmNxReg6Key.VALUE;
         }
-        if (NiciraMatchCodecs.REG7_CODEC.getNxmField().isAssignableFrom(oxmMatchField)) {
-            return NxmNxReg7Key.class;
+        if (NiciraMatchCodecs.REG7_CODEC.getNxmField().equals(oxmMatchField)) {
+            return NxmNxReg7Key.VALUE;
         }
         throw new CodecPreconditionException("There is no key for " + oxmMatchField);
     }
 
     private static ExtensionAugment<? extends Augmentation<Extension>> resolveAugmentation(final NxmNxReg nxmNxReg,
-            final MatchPath path, final Class<? extends ExtensionKey> key) {
-        switch (path) {
-            case FLOWS_STATISTICS_UPDATE_MATCH:
-                return new ExtensionAugment<>(NxAugMatchNodesNodeTableFlow.class,
-                        new NxAugMatchNodesNodeTableFlowBuilder().setNxmNxReg(nxmNxReg).build(), key);
-            case FLOWS_STATISTICS_RPC_MATCH:
-                return new ExtensionAugment<>(NxAugMatchRpcGetFlowStats.class,
-                        new NxAugMatchRpcGetFlowStatsBuilder().setNxmNxReg(nxmNxReg).build(), key);
-            case PACKET_RECEIVED_MATCH:
-                return new ExtensionAugment<>(NxAugMatchNotifPacketIn.class, new NxAugMatchNotifPacketInBuilder()
-                        .setNxmNxReg(nxmNxReg).build(), key);
-            case SWITCH_FLOW_REMOVED_MATCH:
-                return new ExtensionAugment<>(NxAugMatchNotifSwitchFlowRemoved.class,
-                        new NxAugMatchNotifSwitchFlowRemovedBuilder().setNxmNxReg(nxmNxReg).build(), key);
-            case PACKET_IN_MESSAGE_MATCH:
-                return new ExtensionAugment<>(NxAugMatchPacketInMessage.class,
-                        new NxAugMatchPacketInMessageBuilder().setNxmNxReg(nxmNxReg).build(), key);
-            default:
-                throw new CodecPreconditionException(path);
-        }
+            final MatchPath path, final ExtensionKey key) {
+        return switch (path) {
+            case FLOWS_STATISTICS_UPDATE_MATCH -> new ExtensionAugment<>(NxAugMatchNodesNodeTableFlow.class,
+                new NxAugMatchNodesNodeTableFlowBuilder().setNxmNxReg(nxmNxReg).build(), key);
+            case FLOWS_STATISTICS_RPC_MATCH -> new ExtensionAugment<>(NxAugMatchRpcGetFlowStats.class,
+                new NxAugMatchRpcGetFlowStatsBuilder().setNxmNxReg(nxmNxReg).build(), key);
+            case PACKET_RECEIVED_MATCH -> new ExtensionAugment<>(NxAugMatchNotifPacketIn.class,
+                new NxAugMatchNotifPacketInBuilder().setNxmNxReg(nxmNxReg).build(), key);
+            case SWITCH_FLOW_REMOVED_MATCH -> new ExtensionAugment<>(NxAugMatchNotifSwitchFlowRemoved.class,
+                new NxAugMatchNotifSwitchFlowRemovedBuilder().setNxmNxReg(nxmNxReg).build(), key);
+            case PACKET_IN_MESSAGE_MATCH -> new ExtensionAugment<>(NxAugMatchPacketInMessage.class,
+                new NxAugMatchPacketInMessageBuilder().setNxmNxReg(nxmNxReg).build(), key);
+        };
     }
 }