ONOS-3655 - Adding username and password support into the device key subsystem.
Change-Id: I196984479126ae3776093a0bded4b3c820a3eab8
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/key/DeviceKey.java b/incubator/api/src/main/java/org/onosproject/incubator/net/key/DeviceKey.java
index 51d042b..a9f0f5f 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/key/DeviceKey.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/key/DeviceKey.java
@@ -125,4 +125,36 @@
String name = annotations().value(AnnotationKeys.NAME);
return CommunityName.communityName(name);
}
+
+ /**
+ * Method to create a device key of type USERNAME_PASSWORD.
+ *
+ * @param id device key identifier
+ * @param label optional label for this device key
+ * @param username username for this device key
+ * @param password password for this device key
+ * @return device key
+ */
+ public static DeviceKey createDeviceKeyUsingUsernamePassword(DeviceKeyId id, String label,
+ String username, String password) {
+ DefaultAnnotations annotations = builder().set(AnnotationKeys.USERNAME, username)
+ .set(AnnotationKeys.PASSWORD, password).build();
+
+ return new DeviceKey(id, label, Type.USERNAME_PASSWORD, annotations);
+ }
+
+ /**
+ * Returns a username and password object from the device key.
+ *
+ * @return username and password
+ */
+ public UsernamePassword asUsernamePassword() {
+
+ // Validate that the device key is of type USERNAME_PASSWORD.
+ checkState(this.type == Type.USERNAME_PASSWORD, "This device key is not a USERNAME_PASSWORD type.");
+
+ String username = annotations().value(AnnotationKeys.USERNAME);
+ String password = annotations().value(AnnotationKeys.PASSWORD);
+ return UsernamePassword.usernamePassword(username, password);
+ }
}