Switch TerminationReason to use Uint8 79/85979/6
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 26 Nov 2019 15:13:59 +0000 (16:13 +0100)
committerRobert Varga <nite@hq.sk>
Wed, 30 Dec 2020 20:49:49 +0000 (20:49 +0000)
As binding has switched to more expressive Uint types, embrace them
to allow smaller number of conversions.

Change-Id: I353750e1ed9fb8904b1f20be44f25d9c09592213
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
pcep/api/src/main/java/org/opendaylight/protocol/pcep/TerminationReason.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/PCEPSessionImpl.java
pcep/impl/src/test/java/org/opendaylight/protocol/pcep/impl/PCEPSessionImplTest.java

index 3d07b79ef7f5ca07327ac0aab68a3aed135d910f..f53421419f05710c2308d25501d48db1b4742966 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.protocol.pcep;
 import static com.google.common.base.Verify.verify;
 
 import java.util.Arrays;
+import org.opendaylight.yangtools.yang.common.Uint8;
 
 public enum TerminationReason {
     UNKNOWN(1),
@@ -25,19 +26,20 @@ public enum TerminationReason {
         final TerminationReason[] reasons = TerminationReason.values();
         verify(reasons.length > 0);
 
-        final short highest = Arrays.stream(reasons).map(TerminationReason::getShortValue).max(Short::compareTo).get();
+        final short highest = Arrays.stream(reasons).map(TerminationReason::getUintValue).max(Uint8::compareTo).get()
+                .toJava();
         final TerminationReason[] init = new TerminationReason[highest + 1];
         for (TerminationReason reason : reasons) {
-            init[reason.getShortValue()] = reason;
+            init[reason.getUintValue().toJava()] = reason;
         }
 
         REASONS = init;
     }
 
-    private short value;
+    private final Uint8 value;
 
     TerminationReason(final int value) {
-        this.value = (short) value;
+        this.value = Uint8.valueOf(value);
     }
 
     /**
@@ -45,7 +47,7 @@ public enum TerminationReason {
      *
      * @return short value
      */
-    public short getShortValue() {
+    public Uint8 getUintValue() {
         return value;
     }
 
@@ -55,7 +57,8 @@ public enum TerminationReason {
      * @param valueArg corresponding to Termination reason
      * @return corresponding TerminationReason item
      */
-    public static TerminationReason forValue(final short valueArg) {
-        return valueArg < 0 || valueArg >= REASONS.length ? null : REASONS[valueArg];
+    public static TerminationReason forValue(final Uint8 valueArg) {
+        final short value = valueArg.toJava();
+        return value >= REASONS.length ? null : REASONS[value];
     }
 }
index ef17f90f4db52420a0f770a5c3dfcfc482f1516d..3e5a10fa9c06a46b1a4aecbf5256f316d4732cd6 100644 (file)
@@ -50,7 +50,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.typ
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.keepalive.message.KeepaliveMessageBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.Open;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.open.Tlvs;
-import org.opendaylight.yangtools.yang.common.Uint8;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -249,7 +248,7 @@ public class PCEPSessionImpl extends SimpleChannelInboundHandler<Message> implem
             LOG.info("Closing PCEP session with reason {}: {}", reason, this);
             sendMessage(new CloseBuilder().setCCloseMessage(
                     new CCloseMessageBuilder().setCClose(new CCloseBuilder()
-                        .setReason(Uint8.valueOf(reason.getShortValue())).build()).build()).build());
+                        .setReason(reason.getUintValue()).build()).build()).build());
         } else {
             LOG.info("Closing PCEP session: {}", this);
         }
@@ -348,7 +347,7 @@ public class PCEPSessionImpl extends SimpleChannelInboundHandler<Message> implem
              */
             close();
             this.listener.onSessionTerminated(this, new PCEPCloseTermination(TerminationReason
-                    .forValue(((CloseMessage) msg).getCCloseMessage().getCClose().getReason().toJava())));
+                    .forValue(((CloseMessage) msg).getCCloseMessage().getCClose().getReason())));
         } else {
             // This message needs to be handled by the user
             if (msg instanceof PcerrMessage) {
index 6efb76b25749c13d8c03296b1fa0f1beec64f111..25488bb5a2570c14cdc669210dd23b76a5dd7039 100644 (file)
@@ -95,7 +95,7 @@ public class PCEPSessionImplTest extends AbstractPCEPSessionTest {
         Assert.assertTrue(this.msgsSend.get(1) instanceof CloseMessage);
         final CloseMessage closeMsg = (CloseMessage) this.msgsSend.get(1);
         Assert.assertEquals(TerminationReason.TOO_MANY_UNKNOWN_MSGS,
-            TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason().toJava()));
+            TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason()));
         Mockito.verify(this.channel, Mockito.times(1)).close();
     }
 
@@ -113,7 +113,7 @@ public class PCEPSessionImplTest extends AbstractPCEPSessionTest {
         Assert.assertTrue(this.msgsSend.get(0) instanceof CloseMessage);
         final CloseMessage closeMsg = (CloseMessage) this.msgsSend.get(0);
         Assert.assertEquals(TerminationReason.UNKNOWN,
-            TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason().toJava()));
+            TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason()));
         Mockito.verify(this.channel, Mockito.times(1)).close();
     }