Luca Prete
Committed by Gerrit Code Review

Using VlanId String None instead of -1

Change-Id: I2597ac37285cc3f40ad1304d668564a56a5b862f
...@@ -79,7 +79,7 @@ public final class HostId extends ElementId { ...@@ -79,7 +79,7 @@ public final class HostId extends ElementId {
79 checkArgument(string.length() >= MIN_ID_LENGTH, 79 checkArgument(string.length() >= MIN_ID_LENGTH,
80 "Host ID must be at least %s characters", MIN_ID_LENGTH); 80 "Host ID must be at least %s characters", MIN_ID_LENGTH);
81 MacAddress mac = MacAddress.valueOf(string.substring(0, MAC_LENGTH)); 81 MacAddress mac = MacAddress.valueOf(string.substring(0, MAC_LENGTH));
82 - VlanId vlanId = VlanId.vlanId(Short.parseShort(string.substring(MAC_LENGTH + 1))); 82 + VlanId vlanId = VlanId.vlanId(string.substring(MAC_LENGTH + 1));
83 return new HostId(mac, vlanId); 83 return new HostId(mac, vlanId);
84 } 84 }
85 85
......
...@@ -83,7 +83,7 @@ public class ConnectPointTest { ...@@ -83,7 +83,7 @@ public class ConnectPointTest {
83 String cp = "16:3A:BD:6E:31:E4/-1/1"; 83 String cp = "16:3A:BD:6E:31:E4/-1/1";
84 84
85 ConnectPoint connectPoint = ConnectPoint.hostConnectPoint(cp); 85 ConnectPoint connectPoint = ConnectPoint.hostConnectPoint(cp);
86 - assertEquals("16:3A:BD:6E:31:E4/-1", connectPoint.hostId().toString()); 86 + assertEquals("16:3A:BD:6E:31:E4/None", connectPoint.hostId().toString());
87 assertEquals("1", connectPoint.port().toString()); 87 assertEquals("1", connectPoint.port().toString());
88 88
89 expectHostParseException(""); 89 expectHostParseException("");
......
...@@ -49,8 +49,8 @@ import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPair ...@@ -49,8 +49,8 @@ import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPair
49 public class HostToHostIntentCompilerTest extends AbstractIntentTest { 49 public class HostToHostIntentCompilerTest extends AbstractIntentTest {
50 private static final String HOST_ONE_MAC = "00:00:00:00:00:01"; 50 private static final String HOST_ONE_MAC = "00:00:00:00:00:01";
51 private static final String HOST_TWO_MAC = "00:00:00:00:00:02"; 51 private static final String HOST_TWO_MAC = "00:00:00:00:00:02";
52 - private static final String HOST_ONE_VLAN = "-1"; 52 + private static final String HOST_ONE_VLAN = "None";
53 - private static final String HOST_TWO_VLAN = "-1"; 53 + private static final String HOST_TWO_VLAN = "None";
54 private static final String HOST_ONE = HOST_ONE_MAC + "/" + HOST_ONE_VLAN; 54 private static final String HOST_ONE = HOST_ONE_MAC + "/" + HOST_ONE_VLAN;
55 private static final String HOST_TWO = HOST_TWO_MAC + "/" + HOST_TWO_VLAN; 55 private static final String HOST_TWO = HOST_TWO_MAC + "/" + HOST_TWO_VLAN;
56 56
......
...@@ -18,9 +18,9 @@ package org.onlab.packet; ...@@ -18,9 +18,9 @@ package org.onlab.packet;
18 import org.onlab.util.Identifier; 18 import org.onlab.util.Identifier;
19 19
20 /** 20 /**
21 - * Representation of a VLAN ID. 21 + * Representation of a VLAN identifier.
22 */ 22 */
23 -public class VlanId extends Identifier<Short> { 23 +public final class VlanId extends Identifier<Short> {
24 // Based on convention used elsewhere? Check and change if needed 24 // Based on convention used elsewhere? Check and change if needed
25 public static final short UNTAGGED = (short) 0xffff; 25 public static final short UNTAGGED = (short) 0xffff;
26 26
...@@ -32,30 +32,45 @@ public class VlanId extends Identifier<Short> { ...@@ -32,30 +32,45 @@ public class VlanId extends Identifier<Short> {
32 public static final VlanId NONE = VlanId.vlanId(UNTAGGED); 32 public static final VlanId NONE = VlanId.vlanId(UNTAGGED);
33 public static final VlanId ANY = VlanId.vlanId(ANY_VALUE); 33 public static final VlanId ANY = VlanId.vlanId(ANY_VALUE);
34 34
35 + private static final String STRING_NONE = "None";
36 + private static final String STRING_NUMERIC_NONE = "-1";
37 + private static final String STRING_ANY = "Any";
38 +
35 // A VLAN ID is actually 12 bits of a VLAN tag. 39 // A VLAN ID is actually 12 bits of a VLAN tag.
36 public static final short MAX_VLAN = 4095; 40 public static final short MAX_VLAN = 4095;
37 41
38 - protected VlanId() { 42 + // Constructor for serialization.
43 + private VlanId() {
39 super(UNTAGGED); 44 super(UNTAGGED);
40 } 45 }
41 46
42 - protected VlanId(short value) { 47 + // Creates a VLAN identifier for the specified VLAN number.
48 + private VlanId(short value) {
43 super(value); 49 super(value);
44 } 50 }
45 51
52 + /**
53 + * Creates a VLAN identifier for untagged VLAN.
54 + *
55 + * @return VLAN identifier
56 + */
46 public static VlanId vlanId() { 57 public static VlanId vlanId() {
47 return new VlanId(UNTAGGED); 58 return new VlanId(UNTAGGED);
48 } 59 }
49 60
61 + /**
62 + * Creates a VLAN identifier using the supplied VLAN identifier.
63 + *
64 + * @param value VLAN identifier expressed as short
65 + * @return VLAN identifier
66 + */
50 public static VlanId vlanId(short value) { 67 public static VlanId vlanId(short value) {
51 if (value == UNTAGGED) { 68 if (value == UNTAGGED) {
52 return new VlanId(); 69 return new VlanId();
53 } 70 }
54 -
55 if (value == ANY_VALUE) { 71 if (value == ANY_VALUE) {
56 return new VlanId(ANY_VALUE); 72 return new VlanId(ANY_VALUE);
57 } 73 }
58 -
59 if (value > MAX_VLAN) { 74 if (value > MAX_VLAN) {
60 throw new IllegalArgumentException( 75 throw new IllegalArgumentException(
61 "value exceeds allowed maximum VLAN ID value (4095)"); 76 "value exceeds allowed maximum VLAN ID value (4095)");
...@@ -63,6 +78,31 @@ public class VlanId extends Identifier<Short> { ...@@ -63,6 +78,31 @@ public class VlanId extends Identifier<Short> {
63 return new VlanId(value); 78 return new VlanId(value);
64 } 79 }
65 80
81 + /**
82 + * Creates a VLAN identifier Object using the supplied VLAN identifier.
83 + *
84 + * @param value VLAN identifier expressed as string
85 + * @return VLAN identifier
86 + */
87 + public static VlanId vlanId(String value) {
88 + if (value.equals(STRING_NONE) || value.equals(STRING_NUMERIC_NONE)) {
89 + return new VlanId();
90 + }
91 + if (value.equals(STRING_ANY)) {
92 + return new VlanId(ANY_VALUE);
93 + }
94 + try {
95 + return new VlanId(Short.parseShort(value));
96 + } catch (NumberFormatException e) {
97 + throw new IllegalArgumentException(e);
98 + }
99 + }
100 +
101 + /**
102 + * Returns the backing VLAN number.
103 + *
104 + * @return VLAN number
105 + */
66 public short toShort() { 106 public short toShort() {
67 return this.identifier; 107 return this.identifier;
68 } 108 }
...@@ -72,6 +112,9 @@ public class VlanId extends Identifier<Short> { ...@@ -72,6 +112,9 @@ public class VlanId extends Identifier<Short> {
72 if (this.identifier == ANY_VALUE) { 112 if (this.identifier == ANY_VALUE) {
73 return "Any"; 113 return "Any";
74 } 114 }
115 + if (this.identifier == UNTAGGED) {
116 + return "None";
117 + }
75 return String.valueOf(this.identifier); 118 return String.valueOf(this.identifier);
76 } 119 }
77 } 120 }
......
...@@ -26,12 +26,14 @@ public class VlanIdTest { ...@@ -26,12 +26,14 @@ public class VlanIdTest {
26 @Test 26 @Test
27 public void testEquality() { 27 public void testEquality() {
28 28
29 - VlanId vlan1 = VlanId.vlanId((short) -1); 29 + VlanId vlan1 = VlanId.vlanId("None");
30 - VlanId vlan2 = VlanId.vlanId((short) 100); 30 + VlanId vlan2 = VlanId.vlanId((short) -1);
31 VlanId vlan3 = VlanId.vlanId((short) 100); 31 VlanId vlan3 = VlanId.vlanId((short) 100);
32 + VlanId vlan4 = VlanId.vlanId((short) 100);
32 33
33 new EqualsTester().addEqualityGroup(VlanId.vlanId(), vlan1) 34 new EqualsTester().addEqualityGroup(VlanId.vlanId(), vlan1)
34 - .addEqualityGroup(vlan2, vlan3) 35 + .addEqualityGroup(vlan1, vlan2)
36 + .addEqualityGroup(vlan3, vlan4)
35 .addEqualityGroup(VlanId.vlanId((short) 10)); 37 .addEqualityGroup(VlanId.vlanId((short) 10));
36 38
37 } 39 }
...@@ -41,9 +43,12 @@ public class VlanIdTest { ...@@ -41,9 +43,12 @@ public class VlanIdTest {
41 // purposefully create UNTAGGED VLAN 43 // purposefully create UNTAGGED VLAN
42 VlanId vlan1 = VlanId.vlanId((short) 10); 44 VlanId vlan1 = VlanId.vlanId((short) 10);
43 VlanId vlan2 = VlanId.vlanId((short) -1); 45 VlanId vlan2 = VlanId.vlanId((short) -1);
46 + VlanId vlan3 = VlanId.vlanId("None");
44 47
45 assertEquals("incorrect VLAN value", 10, vlan1.toShort()); 48 assertEquals("incorrect VLAN value", 10, vlan1.toShort());
46 - assertEquals("invalid untagged value", VlanId.UNTAGGED, vlan2.toShort()); 49 + assertEquals("invalid untagged value", VlanId.UNTAGGED,
50 + vlan2.toShort(), vlan3.toShort());
51 +
47 } 52 }
48 53
49 @Test(expected = IllegalArgumentException.class) 54 @Test(expected = IllegalArgumentException.class)
......
...@@ -390,7 +390,7 @@ public class HostResourceTest extends ResourceTest { ...@@ -390,7 +390,7 @@ public class HostResourceTest extends ResourceTest {
390 .post(Entity.json(jsonStream)); 390 .post(Entity.json(jsonStream));
391 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED)); 391 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
392 String location = response.getLocation().getPath(); 392 String location = response.getLocation().getPath();
393 - assertThat(location, Matchers.startsWith("/hosts/11:22:33:44:55:66/-1")); 393 + assertThat(location, Matchers.startsWith("/hosts/11:22:33:44:55:66/None"));
394 } 394 }
395 } 395 }
396 396
......