Committed by
Gerrit Code Review
ONOS-3655 - Adding username and password support into the device key subsystem.
Change-Id: I196984479126ae3776093a0bded4b3c820a3eab8
Showing
6 changed files
with
235 additions
and
3 deletions
... | @@ -135,6 +135,16 @@ public final class AnnotationKeys { | ... | @@ -135,6 +135,16 @@ public final class AnnotationKeys { |
135 | public static final String MANAGEMENT_ADDRESS = "managementAddress"; | 135 | public static final String MANAGEMENT_ADDRESS = "managementAddress"; |
136 | 136 | ||
137 | /** | 137 | /** |
138 | + * Annotation key for the username. | ||
139 | + */ | ||
140 | + public static final String USERNAME = "username"; | ||
141 | + | ||
142 | + /** | ||
143 | + * Annotation key for the password. | ||
144 | + */ | ||
145 | + public static final String PASSWORD = "password"; | ||
146 | + | ||
147 | + /** | ||
138 | * Returns the value annotated object for the specified annotation key. | 148 | * Returns the value annotated object for the specified annotation key. |
139 | * The annotated value is expected to be String that can be parsed as double. | 149 | * The annotated value is expected to be String that can be parsed as double. |
140 | * If parsing fails, the returned value will be 1.0. | 150 | * If parsing fails, the returned value will be 1.0. | ... | ... |
... | @@ -19,7 +19,7 @@ package org.onosproject.incubator.net.key; | ... | @@ -19,7 +19,7 @@ package org.onosproject.incubator.net.key; |
19 | /** | 19 | /** |
20 | * Representation of an SNMP community name authentication token. | 20 | * Representation of an SNMP community name authentication token. |
21 | */ | 21 | */ |
22 | -public final class CommunityName { | 22 | +public final class CommunityName extends DeviceKey { |
23 | private final String name; | 23 | private final String name; |
24 | 24 | ||
25 | /** | 25 | /** | ... | ... |
... | @@ -125,4 +125,36 @@ public class DeviceKey extends AbstractAnnotated { | ... | @@ -125,4 +125,36 @@ public class DeviceKey extends AbstractAnnotated { |
125 | String name = annotations().value(AnnotationKeys.NAME); | 125 | String name = annotations().value(AnnotationKeys.NAME); |
126 | return CommunityName.communityName(name); | 126 | return CommunityName.communityName(name); |
127 | } | 127 | } |
128 | + | ||
129 | + /** | ||
130 | + * Method to create a device key of type USERNAME_PASSWORD. | ||
131 | + * | ||
132 | + * @param id device key identifier | ||
133 | + * @param label optional label for this device key | ||
134 | + * @param username username for this device key | ||
135 | + * @param password password for this device key | ||
136 | + * @return device key | ||
137 | + */ | ||
138 | + public static DeviceKey createDeviceKeyUsingUsernamePassword(DeviceKeyId id, String label, | ||
139 | + String username, String password) { | ||
140 | + DefaultAnnotations annotations = builder().set(AnnotationKeys.USERNAME, username) | ||
141 | + .set(AnnotationKeys.PASSWORD, password).build(); | ||
142 | + | ||
143 | + return new DeviceKey(id, label, Type.USERNAME_PASSWORD, annotations); | ||
144 | + } | ||
145 | + | ||
146 | + /** | ||
147 | + * Returns a username and password object from the device key. | ||
148 | + * | ||
149 | + * @return username and password | ||
150 | + */ | ||
151 | + public UsernamePassword asUsernamePassword() { | ||
152 | + | ||
153 | + // Validate that the device key is of type USERNAME_PASSWORD. | ||
154 | + checkState(this.type == Type.USERNAME_PASSWORD, "This device key is not a USERNAME_PASSWORD type."); | ||
155 | + | ||
156 | + String username = annotations().value(AnnotationKeys.USERNAME); | ||
157 | + String password = annotations().value(AnnotationKeys.PASSWORD); | ||
158 | + return UsernamePassword.usernamePassword(username, password); | ||
159 | + } | ||
128 | } | 160 | } | ... | ... |
1 | +/* | ||
2 | + * Copyright 2016 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | +package org.onosproject.incubator.net.key; | ||
18 | + | ||
19 | +/** | ||
20 | + * Representation of a username and password. | ||
21 | + */ | ||
22 | +public final class UsernamePassword extends DeviceKey { | ||
23 | + private final String username; | ||
24 | + private final String password; | ||
25 | + | ||
26 | + /** | ||
27 | + * Private constructor for a username and password. | ||
28 | + * | ||
29 | + * @param username user's name | ||
30 | + * @param password user's password | ||
31 | + */ | ||
32 | + private UsernamePassword(String username, String password) { | ||
33 | + this.username = username; | ||
34 | + this.password = password; | ||
35 | + } | ||
36 | + | ||
37 | + /** | ||
38 | + * Static method to construct a username / password. | ||
39 | + * | ||
40 | + * @param username user's name | ||
41 | + * @param password user's password | ||
42 | + * @return username and password | ||
43 | + */ | ||
44 | + static UsernamePassword usernamePassword(String username, String password) { | ||
45 | + return new UsernamePassword(username, password); | ||
46 | + } | ||
47 | + | ||
48 | + /** | ||
49 | + * Returns the username. | ||
50 | + * | ||
51 | + * @return username | ||
52 | + */ | ||
53 | + public String username() { | ||
54 | + return username; | ||
55 | + } | ||
56 | + | ||
57 | + /** | ||
58 | + * Returns the password. | ||
59 | + * | ||
60 | + * @return password | ||
61 | + */ | ||
62 | + public String password() { | ||
63 | + return password; | ||
64 | + } | ||
65 | +} |
... | @@ -29,9 +29,11 @@ public class DeviceKeyTest { | ... | @@ -29,9 +29,11 @@ public class DeviceKeyTest { |
29 | final String deviceKeyIdValue = "DeviceKeyId1"; | 29 | final String deviceKeyIdValue = "DeviceKeyId1"; |
30 | final String deviceKeyLabel = "DeviceKeyLabel"; | 30 | final String deviceKeyLabel = "DeviceKeyLabel"; |
31 | final String deviceKeySnmpName = "DeviceKeySnmpName"; | 31 | final String deviceKeySnmpName = "DeviceKeySnmpName"; |
32 | + final String deviceKeyUsername = "DeviceKeyUsername"; | ||
33 | + final String deviceKeyPassword = "DeviceKeyPassword"; | ||
32 | 34 | ||
33 | /** | 35 | /** |
34 | - * Checks the construction of a device key name with a null | 36 | + * Checks the construction of a community name device key with a null |
35 | * value passed into it. This will throw a NullPointerException. | 37 | * value passed into it. This will throw a NullPointerException. |
36 | */ | 38 | */ |
37 | @Test(expected = NullPointerException.class) | 39 | @Test(expected = NullPointerException.class) |
... | @@ -40,7 +42,7 @@ public class DeviceKeyTest { | ... | @@ -40,7 +42,7 @@ public class DeviceKeyTest { |
40 | } | 42 | } |
41 | 43 | ||
42 | /** | 44 | /** |
43 | - * Checks the construction of a device key name with non-null | 45 | + * Checks the construction of a community name device key name with non-null |
44 | * values passed into it. | 46 | * values passed into it. |
45 | */ | 47 | */ |
46 | @Test | 48 | @Test |
... | @@ -58,4 +60,63 @@ public class DeviceKeyTest { | ... | @@ -58,4 +60,63 @@ public class DeviceKeyTest { |
58 | assertNotNull("The communityName should not be null.", communityName); | 60 | assertNotNull("The communityName should not be null.", communityName); |
59 | assertEquals("The name should match as expected", deviceKeySnmpName, communityName.name()); | 61 | assertEquals("The name should match as expected", deviceKeySnmpName, communityName.name()); |
60 | } | 62 | } |
63 | + | ||
64 | + /** | ||
65 | + * Checks the invalid conversion a device key of type COMMUNITY_NAME to | ||
66 | + * a username / password object. | ||
67 | + */ | ||
68 | + @Test(expected = IllegalStateException.class) | ||
69 | + public void testInvalidConversionToAsUsernamePassword() { | ||
70 | + DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue); | ||
71 | + | ||
72 | + DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId, | ||
73 | + deviceKeyLabel, deviceKeySnmpName); | ||
74 | + // Attempting to convert this device key to a username / password object | ||
75 | + // should throw and IllegalStateException. | ||
76 | + UsernamePassword usernamePassword = deviceKey.asUsernamePassword(); | ||
77 | + } | ||
78 | + | ||
79 | + /** | ||
80 | + * Checks the construction of a username / password device key with a null | ||
81 | + * value passed into it. This will throw a NullPointerException. | ||
82 | + */ | ||
83 | + @Test(expected = NullPointerException.class) | ||
84 | + public void testCreateDeviceKeyUsingUsernamePasswordWithNull() { | ||
85 | + DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingUsernamePassword(null, null, null, null); | ||
86 | + } | ||
87 | + | ||
88 | + /** | ||
89 | + * Checks the construction of a username and password device key name with non-null | ||
90 | + * values passed into it. | ||
91 | + */ | ||
92 | + @Test | ||
93 | + public void testCreateDeviceKeyUsingUsernamePassword() { | ||
94 | + DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue); | ||
95 | + | ||
96 | + DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingUsernamePassword(deviceKeyId, deviceKeyLabel, | ||
97 | + deviceKeyUsername, deviceKeyPassword); | ||
98 | + assertNotNull("The deviceKey should not be null.", deviceKey); | ||
99 | + assertEquals("The deviceKeyId should match as expected", deviceKeyId, deviceKey.deviceKeyId()); | ||
100 | + assertEquals("The label should match as expected", deviceKeyLabel, deviceKey.label()); | ||
101 | + assertEquals("The type should match as expected", DeviceKey.Type.USERNAME_PASSWORD, deviceKey.type()); | ||
102 | + | ||
103 | + UsernamePassword usernamePassword = deviceKey.asUsernamePassword(); | ||
104 | + assertNotNull("The usernamePassword should not be null.", usernamePassword); | ||
105 | + assertEquals("The username should match as expected", deviceKeyUsername, usernamePassword.username()); | ||
106 | + assertEquals("The password should match as expected", deviceKeyPassword, usernamePassword.password()); | ||
107 | + } | ||
108 | + | ||
109 | + /** | ||
110 | + * Checks the invalid conversion a device key of type USERNAME_PASSWORD to | ||
111 | + * a community name object. | ||
112 | + */ | ||
113 | + @Test(expected = IllegalStateException.class) | ||
114 | + public void testInvalidConversionToAsCommunityName() { | ||
115 | + DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue); | ||
116 | + | ||
117 | + DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingUsernamePassword(deviceKeyId, deviceKeyLabel, | ||
118 | + deviceKeyUsername, deviceKeyPassword); | ||
119 | + // Attempting to convert this device key to a community name should throw and IllegalStateException. | ||
120 | + CommunityName communityName = deviceKey.asCommunityName(); | ||
121 | + } | ||
61 | } | 122 | } | ... | ... |
1 | +/* | ||
2 | + * Copyright 2016 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | +package org.onosproject.incubator.net.key; | ||
18 | + | ||
19 | +import org.junit.Test; | ||
20 | + | ||
21 | +import static org.junit.Assert.*; | ||
22 | +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; | ||
23 | + | ||
24 | +/** | ||
25 | + * Test class for UsernamePassword. | ||
26 | + */ | ||
27 | +public class UsernamePasswordTest { | ||
28 | + final String username = "username"; | ||
29 | + final String password = "password"; | ||
30 | + | ||
31 | + /** | ||
32 | + * Checks that the UsernamePassword class is immutable. | ||
33 | + */ | ||
34 | + @Test | ||
35 | + public void testImmutability() { | ||
36 | + assertThatClassIsImmutable(UsernamePassword.class); | ||
37 | + } | ||
38 | + | ||
39 | + /** | ||
40 | + * Checks the construction of a username / password object with null | ||
41 | + * values passed into it. | ||
42 | + */ | ||
43 | + @Test | ||
44 | + public void testUsernamePasswordNull() { | ||
45 | + UsernamePassword usernamePassword = UsernamePassword.usernamePassword(null, null); | ||
46 | + | ||
47 | + assertNotNull("The UsernamePassword should not be null.", usernamePassword); | ||
48 | + assertNull("The username should be null.", usernamePassword.username()); | ||
49 | + assertNull("The password should be null.", usernamePassword.password()); | ||
50 | + } | ||
51 | + | ||
52 | + /** | ||
53 | + * Checks the construction of a username / password object with non-null | ||
54 | + * values passed into it. | ||
55 | + */ | ||
56 | + @Test | ||
57 | + public void testUsernamePassword() { | ||
58 | + UsernamePassword usernamePassword = UsernamePassword.usernamePassword(username, password); | ||
59 | + | ||
60 | + assertNotNull("The usernamePassword should not be null.", usernamePassword); | ||
61 | + assertEquals("The username should match the expected value.", username, usernamePassword.username()); | ||
62 | + assertEquals("The password should match the expected value.", password, usernamePassword.password()); | ||
63 | + } | ||
64 | +} |
-
Please register or login to post a comment