Add fromString method to connect point

Change-Id: If90b7582b554896250571b9290fd639dfb7057d5
This commit is contained in:
hiroki 2018-05-19 13:01:21 -07:00 committed by Thomas Vachuska
parent 55965c6aae
commit b0bc9da0b5
2 changed files with 28 additions and 0 deletions

View File

@ -144,6 +144,23 @@ public class ConnectPoint implements Comparable<ConnectPoint> {
PortNumber.portNumber(string.substring(lastSlash + 1, string.length())));
}
/**
* Parse a device connect point from a string.
* The connect point should be in the same format as toString output.
*
* @param string string to parse
* @return a ConnectPoint based on the information in the string.
*/
public static ConnectPoint fromString(String string) {
checkNotNull(string);
String[] splitted = string.split("/");
checkArgument(splitted.length == 2,
"Connect point must be in \"deviceUri/portNumber\" format");
return new ConnectPoint(DeviceId.deviceId(splitted[0]),
PortNumber.fromString(splitted[1]));
}
@Override
public int hashCode() {
return Objects.hash(elementId, portNumber);

View File

@ -71,6 +71,17 @@ public class ConnectPointTest {
expectDeviceParseException("of:0011223344556677/word");
}
@Test
public void testParseFromString() {
String cp = "netconf:127.0.0.1/[TYPE](1)";
ConnectPoint connectPoint = ConnectPoint.fromString(cp);
assertEquals("netconf:127.0.0.1", connectPoint.deviceId().toString());
assertEquals("[TYPE](1)", connectPoint.port().toString());
assertEquals(connectPoint, ConnectPoint.fromString(connectPoint.toString()));
}
/**
* Parse a device connect point and expect an exception to be thrown.
*