ONOS-3931: move "org.onosproject.incubator.net.key" to "org.onosproject.net.key"
Change-Id: I90da894317583ce8d6cfb238e933ff28ad2c83ca
diff --git a/core/api/src/main/java/org/onosproject/net/key/CommunityName.java b/core/api/src/main/java/org/onosproject/net/key/CommunityName.java
new file mode 100644
index 0000000..3c3e29e
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/key/CommunityName.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.key;
+
+/**
+ * Representation of an SNMP community name authentication token.
+ */
+public final class CommunityName extends DeviceKey {
+ private final String name;
+
+ /**
+ * Private constructor for a community name.
+ *
+ * @param name community name
+ */
+ private CommunityName(String name) {
+ this.name = name;
+ }
+
+ /**
+ * Static method to construct a community name.
+ *
+ * @param name community name
+ * @return community name
+ */
+ static CommunityName communityName(String name) {
+ return new CommunityName(name);
+ }
+
+ /**
+ * Returns the community name.
+ *
+ * @return name
+ */
+ public String name() {
+ return name;
+ }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/key/DeviceKey.java b/core/api/src/main/java/org/onosproject/net/key/DeviceKey.java
new file mode 100644
index 0000000..3f4a9fd
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/key/DeviceKey.java
@@ -0,0 +1,160 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.key;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.net.AbstractAnnotated;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.Annotations;
+import org.onosproject.net.DefaultAnnotations;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkState;
+import static org.onosproject.net.DefaultAnnotations.builder;
+
+/**
+ * Abstraction of a device key.
+ */
+@Beta
+public class DeviceKey extends AbstractAnnotated {
+
+ // device key identifier
+ private final DeviceKeyId deviceKeyId;
+ // label of the device key
+ private String label;
+
+ /**
+ * type of the device key.
+ */
+ public enum Type {
+ COMMUNITY_NAME, USERNAME_PASSWORD, SSL_KEY
+ }
+
+ private Type type;
+
+ /**
+ * Constructor for serialization.
+ */
+ DeviceKey() {
+ this.deviceKeyId = null;
+ this.label = null;
+ this.type = null;
+ }
+
+ /**
+ * Private constructor for a device key.
+ *
+ * @param id device key identifier
+ * @param label optional label for this device key
+ * @param type to be assigned to this device key
+ * @param annotations name/value pairs for this device key
+ */
+ private DeviceKey(DeviceKeyId id, String label, Type type, Annotations... annotations) {
+ super(annotations);
+ checkNotNull(id, "The DeviceKeyId cannot be null.");
+ this.deviceKeyId = id;
+ this.label = label;
+ this.type = type;
+ }
+
+ /**
+ * Returns the device key identifier of the device key.
+ *
+ * @return device key identifier
+ */
+ public DeviceKeyId deviceKeyId() {
+ return deviceKeyId;
+ }
+
+ /**
+ * Returns the label of device key.
+ *
+ * @return label
+ */
+ public String label() {
+ return label;
+ }
+
+ /**
+ * Returns the type of the device key.
+ *
+ * @return type
+ */
+ public Type type() {
+ return type;
+ }
+
+ /**
+ * Method to create a device key of type CommunityName.
+ *
+ * @param id device key identifier
+ * @param label optional label for this device key
+ * @param name community name for this device key
+ * @return device key
+ */
+ public static DeviceKey createDeviceKeyUsingCommunityName(DeviceKeyId id, String label, String name) {
+ DefaultAnnotations annotations = builder().set(AnnotationKeys.NAME, name).build();
+
+ return new DeviceKey(id, label, Type.COMMUNITY_NAME, annotations);
+ }
+
+ /**
+ * Returns a community name object from the device key.
+ *
+ * @return community name
+ */
+ public CommunityName asCommunityName() {
+
+ // Validate that the device key is of type COMMUNITY_NAME.
+ checkState(this.type == Type.COMMUNITY_NAME, "This device key is not a COMMUNITY_NAME type.");
+
+ 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);
+ }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/key/DeviceKeyAdminService.java b/core/api/src/main/java/org/onosproject/net/key/DeviceKeyAdminService.java
new file mode 100644
index 0000000..9e4f168
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/key/DeviceKeyAdminService.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.key;
+
+import com.google.common.annotations.Beta;
+
+/**
+ * Service for managing device keys.
+ */
+@Beta
+public interface DeviceKeyAdminService extends DeviceKeyService {
+
+ /**
+ * Adds a new device key to the store.
+ *
+ * @param deviceKey device key to be stored
+ */
+ void addKey(DeviceKey deviceKey);
+
+ /**
+ * Removes a device key from the store using the device
+ * key identifier.
+ *
+ * @param id device key identifier used to identify the device key
+ */
+ void removeKey(DeviceKeyId id);
+}
+
diff --git a/core/api/src/main/java/org/onosproject/net/key/DeviceKeyEvent.java b/core/api/src/main/java/org/onosproject/net/key/DeviceKeyEvent.java
new file mode 100644
index 0000000..2cd1d57
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/key/DeviceKeyEvent.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.key;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.event.AbstractEvent;
+
+/**
+ * Describes device key events.
+ */
+@Beta
+public class DeviceKeyEvent extends AbstractEvent<DeviceKeyEvent.Type, DeviceKey> {
+
+ /**
+ * Type of device key events.
+ */
+ public enum Type {
+ /**
+ * Signals that a new device key has been added.
+ */
+ DEVICE_KEY_ADDED,
+
+ /**
+ * Signals that a device key has been updated or changed state.
+ */
+ DEVICE_KEY_UPDATED,
+
+ /**
+ * Signals that a device key has been removed.
+ */
+ DEVICE_KEY_REMOVED
+ }
+
+ /**
+ * Creates an event of a given type, and for the specified device key.
+ *
+ * @param type device key event type
+ * @param deviceKey event device key subject
+ */
+ public DeviceKeyEvent(Type type, DeviceKey deviceKey) {
+ super(type, deviceKey);
+ }
+
+ /**
+ * Creates an event of a given type, for the specified device key, and
+ * the current time.
+ *
+ * @param type device key event type
+ * @param deviceKey event device key subject
+ * @param time occurrence time
+ */
+ public DeviceKeyEvent(Type type, DeviceKey deviceKey, long time) {
+ super(type, deviceKey, time);
+ }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/key/DeviceKeyId.java b/core/api/src/main/java/org/onosproject/net/key/DeviceKeyId.java
new file mode 100644
index 0000000..12c1d95
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/key/DeviceKeyId.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.key;
+
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Device key Id definition.
+ */
+public final class DeviceKeyId {
+ private final String identifier;
+
+ /**
+ * Constructor for serialization.
+ */
+ private DeviceKeyId() {
+ this.identifier = null;
+ }
+
+ /**
+ * Constructs the ID corresponding to a given string value.
+ *
+ * @param value the underlying value of this ID
+ */
+ private DeviceKeyId(String value) {
+ this.identifier = checkNotNull(value, "Device Key Id cannot be null.");
+ }
+
+ /**
+ * Static method to construct a device key identifier.
+ *
+ * @param id for the device key identifier
+ * @return device key identifier
+ */
+ public static final DeviceKeyId deviceKeyId(String id) {
+ return new DeviceKeyId(id);
+ }
+
+ /**
+ * Returns the identifier of the device key identifier.
+ *
+ * @return identifier
+ */
+ public String id() {
+ return identifier;
+ }
+
+ /**
+ * Returns the hashcode of the identifier.
+ *
+ * @return hashcode
+ */
+ @Override
+ public int hashCode() {
+ return identifier.hashCode();
+ }
+
+ /**
+ * Compares two device key identifiers for equality.
+ *
+ * @param obj to compare against
+ * @return true if the objects are equal, false otherwise.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof DeviceKeyId) {
+ final DeviceKeyId that = (DeviceKeyId) obj;
+ return this.getClass() == that.getClass() &&
+ Objects.equals(this.identifier, that.identifier);
+ }
+ return false;
+ }
+
+ /**
+ * Returns a string representation of a DeviceKeyId.
+ *
+ * @return string
+ */
+ public String toString() {
+ return identifier;
+ }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/key/DeviceKeyListener.java b/core/api/src/main/java/org/onosproject/net/key/DeviceKeyListener.java
new file mode 100644
index 0000000..f7f8080
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/key/DeviceKeyListener.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.key;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.event.EventListener;
+
+/**
+ * Listener for device key related events.
+ */
+@Beta
+public interface DeviceKeyListener extends EventListener<DeviceKeyEvent> {
+}
diff --git a/core/api/src/main/java/org/onosproject/net/key/DeviceKeyService.java b/core/api/src/main/java/org/onosproject/net/key/DeviceKeyService.java
new file mode 100644
index 0000000..b87530f
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/key/DeviceKeyService.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.key;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.event.ListenerService;
+
+import java.util.Collection;
+
+/**
+ * Service for querying device keys.
+ */
+@Beta
+public interface DeviceKeyService extends ListenerService<DeviceKeyEvent, DeviceKeyListener> {
+
+ /**
+ * Returns all device keys.
+ *
+ * @return Collection of device keys
+ */
+ Collection<DeviceKey> getDeviceKeys();
+
+ /**
+ * Returns the device key using a device key identifier.
+ *
+ * @param deviceKeyId device key identifier
+ * @return device key
+ */
+ DeviceKey getDeviceKey(DeviceKeyId deviceKeyId);
+}
+
diff --git a/core/api/src/main/java/org/onosproject/net/key/DeviceKeyStore.java b/core/api/src/main/java/org/onosproject/net/key/DeviceKeyStore.java
new file mode 100644
index 0000000..c9b1194
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/key/DeviceKeyStore.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.key;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.store.Store;
+
+import java.util.Collection;
+
+/**
+ * Manages inventory of device keys; not intended for direct use.
+ */
+@Beta
+public interface DeviceKeyStore extends Store<DeviceKeyEvent, DeviceKeyStoreDelegate> {
+ /**
+ * Creates or updates a device key.
+ *
+ * @param deviceKey device key
+ */
+ void createOrUpdateDeviceKey(DeviceKey deviceKey);
+
+ /**
+ * Deletes a device key by a specific device key identifier.
+ *
+ * @param deviceKeyId device key unique identifier
+ */
+ void deleteDeviceKey(DeviceKeyId deviceKeyId);
+
+ /**
+ * Returns all device keys.
+ *
+ * @return set of device keys
+ */
+ Collection<DeviceKey> getDeviceKeys();
+
+ /**
+ * Returns the device key matching a device key identifier.
+ *
+ * @param deviceKeyId device key unique identifier
+ * @return device key
+ */
+ DeviceKey getDeviceKey(DeviceKeyId deviceKeyId);
+}
diff --git a/core/api/src/main/java/org/onosproject/net/key/DeviceKeyStoreDelegate.java b/core/api/src/main/java/org/onosproject/net/key/DeviceKeyStoreDelegate.java
new file mode 100644
index 0000000..e78912b
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/key/DeviceKeyStoreDelegate.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.key;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.store.StoreDelegate;
+
+/**
+ * Device key store delegate abstraction.
+ */
+@Beta
+public interface DeviceKeyStoreDelegate extends StoreDelegate<DeviceKeyEvent> {
+}
diff --git a/core/api/src/main/java/org/onosproject/net/key/UsernamePassword.java b/core/api/src/main/java/org/onosproject/net/key/UsernamePassword.java
new file mode 100644
index 0000000..d496fc35
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/key/UsernamePassword.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.key;
+
+/**
+ * Representation of a username and password.
+ */
+public final class UsernamePassword extends DeviceKey {
+ private final String username;
+ private final String password;
+
+ /**
+ * Private constructor for a username and password.
+ *
+ * @param username user's name
+ * @param password user's password
+ */
+ private UsernamePassword(String username, String password) {
+ this.username = username;
+ this.password = password;
+ }
+
+ /**
+ * Static method to construct a username / password.
+ *
+ * @param username user's name
+ * @param password user's password
+ * @return username and password
+ */
+ static UsernamePassword usernamePassword(String username, String password) {
+ return new UsernamePassword(username, password);
+ }
+
+ /**
+ * Returns the username.
+ *
+ * @return username
+ */
+ public String username() {
+ return username;
+ }
+
+ /**
+ * Returns the password.
+ *
+ * @return password
+ */
+ public String password() {
+ return password;
+ }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/key/package-info.java b/core/api/src/main/java/org/onosproject/net/key/package-info.java
new file mode 100644
index 0000000..0e88963
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/key/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Device key data model and services.
+ * This subsystem and its interfaces will change in the upcoming release.
+ */
+package org.onosproject.net.key;
\ No newline at end of file
diff --git a/core/api/src/test/java/org/onosproject/net/key/CommunityNameTest.java b/core/api/src/test/java/org/onosproject/net/key/CommunityNameTest.java
new file mode 100644
index 0000000..5e2d4a9
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/key/CommunityNameTest.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.key;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+
+/**
+ * Test class for CommunityName.
+ */
+public class CommunityNameTest {
+
+ final String cName = "CommunityName";
+
+ /**
+ * Checks that the CommunityName class is immutable.
+ */
+ @Test
+ public void testImmutability() {
+ assertThatClassIsImmutable(CommunityName.class);
+ }
+
+ /**
+ * Checks the construction of a community name object with a null
+ * value passed into it.
+ */
+ @Test
+ public void testCommunityNameNull() {
+ CommunityName communityName = CommunityName.communityName(null);
+
+ assertNotNull("The CommunityName should not be null.", communityName);
+ assertNull("The name should be null.", communityName.name());
+ }
+
+ /**
+ * Checks the construction of a community name object with a non-null
+ * value passed into it.
+ */
+ @Test
+ public void testCommunityName() {
+ CommunityName communityName = CommunityName.communityName(cName);
+
+ assertNotNull("The CommunityName should not be null.", communityName);
+ assertEquals("The name should match the expected value.", cName, communityName.name());
+ }
+}
diff --git a/core/api/src/test/java/org/onosproject/net/key/DeviceKeyIdTest.java b/core/api/src/test/java/org/onosproject/net/key/DeviceKeyIdTest.java
new file mode 100644
index 0000000..6744460
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/key/DeviceKeyIdTest.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.key;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+
+/**
+ * Test class for DeviceDeyId.
+ */
+public class DeviceKeyIdTest {
+
+ final String deviceKeyIdValue1 = "DeviceKeyId1";
+ final String deviceKeyIdValue2 = "DeviceKeyId2";
+
+ /**
+ * Checks that the DeviceKeyId class is immutable.
+ */
+ @Test
+ public void testImmutability() {
+ assertThatClassIsImmutable(DeviceKeyId.class);
+ }
+
+ /**
+ * Checks the construction of a DeviceKeyId object throws an
+ * IllegalArgumentException when the input identifier is null.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testConstructionUsingNullId() {
+ DeviceKeyId.deviceKeyId(null);
+ }
+
+ /**
+ * Checks the construction of a DeviceKeyId object.
+ */
+ @Test
+ public void testConstruction() {
+ DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue1);
+ assertNotNull("The deviceKeyId should not be null.", deviceKeyId);
+ assertEquals("The id should match the expected value.",
+ deviceKeyIdValue1, deviceKeyId.id());
+ }
+
+ /**
+ * Tests the equality of device key identifiers.
+ */
+ @Test
+ public void testEquality() {
+ DeviceKeyId deviceKeyId1 = DeviceKeyId.deviceKeyId(deviceKeyIdValue1);
+ DeviceKeyId deviceKeyId2 = DeviceKeyId.deviceKeyId(deviceKeyIdValue1);
+ DeviceKeyId deviceKeyId3 = DeviceKeyId.deviceKeyId(deviceKeyIdValue2);
+
+ new EqualsTester().addEqualityGroup(deviceKeyId1, deviceKeyId2)
+ .addEqualityGroup(deviceKeyId3)
+ .testEquals();
+ }
+}
diff --git a/core/api/src/test/java/org/onosproject/net/key/DeviceKeyTest.java b/core/api/src/test/java/org/onosproject/net/key/DeviceKeyTest.java
new file mode 100644
index 0000000..344a425
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/key/DeviceKeyTest.java
@@ -0,0 +1,122 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.key;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+/**
+ * Test class for DeviceKey.
+ */
+public class DeviceKeyTest {
+
+ final String deviceKeyIdValue = "DeviceKeyId1";
+ final String deviceKeyLabel = "DeviceKeyLabel";
+ final String deviceKeySnmpName = "DeviceKeySnmpName";
+ final String deviceKeyUsername = "DeviceKeyUsername";
+ final String deviceKeyPassword = "DeviceKeyPassword";
+
+ /**
+ * Checks the construction of a community name device key with a null
+ * value passed into it. This will throw a NullPointerException.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testCreateDeviceKeyUsingCommunityNameWithNull() {
+ DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(null, null, null);
+ }
+
+ /**
+ * Checks the construction of a community name device key name with non-null
+ * values passed into it.
+ */
+ @Test
+ public void testCreateDeviceKeyUsingCommunityName() {
+ DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue);
+
+ DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId,
+ deviceKeyLabel, deviceKeySnmpName);
+ assertNotNull("The deviceKey should not be null.", deviceKey);
+ assertEquals("The deviceKeyId should match as expected", deviceKeyId, deviceKey.deviceKeyId());
+ assertEquals("The label should match as expected", deviceKeyLabel, deviceKey.label());
+ assertEquals("The type should match as expected", DeviceKey.Type.COMMUNITY_NAME, deviceKey.type());
+
+ CommunityName communityName = deviceKey.asCommunityName();
+ assertNotNull("The communityName should not be null.", communityName);
+ assertEquals("The name should match as expected", deviceKeySnmpName, communityName.name());
+ }
+
+ /**
+ * Checks the invalid conversion a device key of type COMMUNITY_NAME to
+ * a username / password object.
+ */
+ @Test(expected = IllegalStateException.class)
+ public void testInvalidConversionToAsUsernamePassword() {
+ DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue);
+
+ DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId,
+ deviceKeyLabel, deviceKeySnmpName);
+ // Attempting to convert this device key to a username / password object
+ // should throw and IllegalStateException.
+ UsernamePassword usernamePassword = deviceKey.asUsernamePassword();
+ }
+
+ /**
+ * Checks the construction of a username / password device key with a null
+ * value passed into it. This will throw a NullPointerException.
+ */
+ @Test(expected = NullPointerException.class)
+ public void testCreateDeviceKeyUsingUsernamePasswordWithNull() {
+ DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingUsernamePassword(null, null, null, null);
+ }
+
+ /**
+ * Checks the construction of a username and password device key name with non-null
+ * values passed into it.
+ */
+ @Test
+ public void testCreateDeviceKeyUsingUsernamePassword() {
+ DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue);
+
+ DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingUsernamePassword(deviceKeyId, deviceKeyLabel,
+ deviceKeyUsername, deviceKeyPassword);
+ assertNotNull("The deviceKey should not be null.", deviceKey);
+ assertEquals("The deviceKeyId should match as expected", deviceKeyId, deviceKey.deviceKeyId());
+ assertEquals("The label should match as expected", deviceKeyLabel, deviceKey.label());
+ assertEquals("The type should match as expected", DeviceKey.Type.USERNAME_PASSWORD, deviceKey.type());
+
+ UsernamePassword usernamePassword = deviceKey.asUsernamePassword();
+ assertNotNull("The usernamePassword should not be null.", usernamePassword);
+ assertEquals("The username should match as expected", deviceKeyUsername, usernamePassword.username());
+ assertEquals("The password should match as expected", deviceKeyPassword, usernamePassword.password());
+ }
+
+ /**
+ * Checks the invalid conversion a device key of type USERNAME_PASSWORD to
+ * a community name object.
+ */
+ @Test(expected = IllegalStateException.class)
+ public void testInvalidConversionToAsCommunityName() {
+ DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue);
+
+ DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingUsernamePassword(deviceKeyId, deviceKeyLabel,
+ deviceKeyUsername, deviceKeyPassword);
+ // Attempting to convert this device key to a community name should throw and IllegalStateException.
+ CommunityName communityName = deviceKey.asCommunityName();
+ }
+}
diff --git a/core/api/src/test/java/org/onosproject/net/key/UsernamePasswordTest.java b/core/api/src/test/java/org/onosproject/net/key/UsernamePasswordTest.java
new file mode 100644
index 0000000..d8acafb
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/key/UsernamePasswordTest.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.key;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+
+/**
+ * Test class for UsernamePassword.
+ */
+public class UsernamePasswordTest {
+ final String username = "username";
+ final String password = "password";
+
+ /**
+ * Checks that the UsernamePassword class is immutable.
+ */
+ @Test
+ public void testImmutability() {
+ assertThatClassIsImmutable(UsernamePassword.class);
+ }
+
+ /**
+ * Checks the construction of a username / password object with null
+ * values passed into it.
+ */
+ @Test
+ public void testUsernamePasswordNull() {
+ UsernamePassword usernamePassword = UsernamePassword.usernamePassword(null, null);
+
+ assertNotNull("The UsernamePassword should not be null.", usernamePassword);
+ assertNull("The username should be null.", usernamePassword.username());
+ assertNull("The password should be null.", usernamePassword.password());
+ }
+
+ /**
+ * Checks the construction of a username / password object with non-null
+ * values passed into it.
+ */
+ @Test
+ public void testUsernamePassword() {
+ UsernamePassword usernamePassword = UsernamePassword.usernamePassword(username, password);
+
+ assertNotNull("The usernamePassword should not be null.", usernamePassword);
+ assertEquals("The username should match the expected value.", username, usernamePassword.username());
+ assertEquals("The password should match the expected value.", password, usernamePassword.password());
+ }
+}