Brian Stanke | 2b4f32d | 2016-02-03 14:22:10 -0500 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Brian Stanke | 2b4f32d | 2016-02-03 14:22:10 -0500 | [diff] [blame] | 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 | |
Claudine Chiu | 31ad527 | 2016-02-17 20:56:24 +0000 | [diff] [blame] | 17 | package org.onosproject.net.key; |
Brian Stanke | 2b4f32d | 2016-02-03 14:22:10 -0500 | [diff] [blame] | 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import org.onosproject.net.AbstractAnnotated; |
| 21 | import org.onosproject.net.AnnotationKeys; |
| 22 | import org.onosproject.net.Annotations; |
| 23 | import org.onosproject.net.DefaultAnnotations; |
| 24 | |
Jordan Halterman | 0d89ea3 | 2017-06-13 10:42:36 -0700 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkArgument; |
Brian Stanke | 2b4f32d | 2016-02-03 14:22:10 -0500 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | import static com.google.common.base.Preconditions.checkState; |
| 28 | import static org.onosproject.net.DefaultAnnotations.builder; |
| 29 | |
| 30 | /** |
| 31 | * Abstraction of a device key. |
| 32 | */ |
| 33 | @Beta |
| 34 | public class DeviceKey extends AbstractAnnotated { |
| 35 | |
Jordan Halterman | 0d89ea3 | 2017-06-13 10:42:36 -0700 | [diff] [blame] | 36 | private static final int LABEL_MAX_LENGTH = 1024; |
| 37 | |
Brian Stanke | 2b4f32d | 2016-02-03 14:22:10 -0500 | [diff] [blame] | 38 | // device key identifier |
| 39 | private final DeviceKeyId deviceKeyId; |
| 40 | // label of the device key |
| 41 | private String label; |
| 42 | |
| 43 | /** |
| 44 | * type of the device key. |
| 45 | */ |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 46 | public enum Type { |
Brian Stanke | 2b4f32d | 2016-02-03 14:22:10 -0500 | [diff] [blame] | 47 | COMMUNITY_NAME, USERNAME_PASSWORD, SSL_KEY |
| 48 | } |
| 49 | |
| 50 | private Type type; |
| 51 | |
| 52 | /** |
| 53 | * Constructor for serialization. |
| 54 | */ |
| 55 | DeviceKey() { |
| 56 | this.deviceKeyId = null; |
| 57 | this.label = null; |
| 58 | this.type = null; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Private constructor for a device key. |
| 63 | * |
| 64 | * @param id device key identifier |
| 65 | * @param label optional label for this device key |
| 66 | * @param type to be assigned to this device key |
| 67 | * @param annotations name/value pairs for this device key |
| 68 | */ |
| 69 | private DeviceKey(DeviceKeyId id, String label, Type type, Annotations... annotations) { |
| 70 | super(annotations); |
| 71 | checkNotNull(id, "The DeviceKeyId cannot be null."); |
Jordan Halterman | 0d89ea3 | 2017-06-13 10:42:36 -0700 | [diff] [blame] | 72 | if (label != null) { |
| 73 | checkArgument(label.length() <= LABEL_MAX_LENGTH, "label exceeds maximum length " + LABEL_MAX_LENGTH); |
| 74 | } |
Brian Stanke | 2b4f32d | 2016-02-03 14:22:10 -0500 | [diff] [blame] | 75 | this.deviceKeyId = id; |
| 76 | this.label = label; |
| 77 | this.type = type; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns the device key identifier of the device key. |
| 82 | * |
| 83 | * @return device key identifier |
| 84 | */ |
| 85 | public DeviceKeyId deviceKeyId() { |
| 86 | return deviceKeyId; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns the label of device key. |
| 91 | * |
| 92 | * @return label |
| 93 | */ |
| 94 | public String label() { |
| 95 | return label; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Returns the type of the device key. |
| 100 | * |
| 101 | * @return type |
| 102 | */ |
| 103 | public Type type() { |
| 104 | return type; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Method to create a device key of type CommunityName. |
| 109 | * |
| 110 | * @param id device key identifier |
| 111 | * @param label optional label for this device key |
| 112 | * @param name community name for this device key |
| 113 | * @return device key |
| 114 | */ |
| 115 | public static DeviceKey createDeviceKeyUsingCommunityName(DeviceKeyId id, String label, String name) { |
| 116 | DefaultAnnotations annotations = builder().set(AnnotationKeys.NAME, name).build(); |
| 117 | |
| 118 | return new DeviceKey(id, label, Type.COMMUNITY_NAME, annotations); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns a community name object from the device key. |
| 123 | * |
| 124 | * @return community name |
| 125 | */ |
| 126 | public CommunityName asCommunityName() { |
| 127 | |
| 128 | // Validate that the device key is of type COMMUNITY_NAME. |
| 129 | checkState(this.type == Type.COMMUNITY_NAME, "This device key is not a COMMUNITY_NAME type."); |
| 130 | |
| 131 | String name = annotations().value(AnnotationKeys.NAME); |
| 132 | return CommunityName.communityName(name); |
| 133 | } |
Brian Stanke | 9bf8d7c | 2016-02-11 10:17:23 -0500 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * Method to create a device key of type USERNAME_PASSWORD. |
| 137 | * |
Andrea Campanella | bddba16 | 2017-02-14 10:05:25 -0800 | [diff] [blame] | 138 | * @param id device key identifier |
| 139 | * @param label optional label for this device key |
| 140 | * @param username username for accessing this device |
| 141 | * @param password password for accessing this device |
Brian Stanke | 9bf8d7c | 2016-02-11 10:17:23 -0500 | [diff] [blame] | 142 | * @return device key |
| 143 | */ |
| 144 | public static DeviceKey createDeviceKeyUsingUsernamePassword(DeviceKeyId id, String label, |
| 145 | String username, String password) { |
| 146 | DefaultAnnotations annotations = builder().set(AnnotationKeys.USERNAME, username) |
| 147 | .set(AnnotationKeys.PASSWORD, password).build(); |
| 148 | |
| 149 | return new DeviceKey(id, label, Type.USERNAME_PASSWORD, annotations); |
| 150 | } |
| 151 | |
| 152 | /** |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 153 | * Method to create a device key of type SSL_KEY. |
| 154 | * |
Andrea Campanella | bddba16 | 2017-02-14 10:05:25 -0800 | [diff] [blame] | 155 | * @param id device key identifier |
| 156 | * @param label optional label for this device key |
| 157 | * @param username username for accessing this device |
| 158 | * @param password password for accessing this device SSH key |
| 159 | * @param sshkey SSH key for accessing this device |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 160 | * @return device key |
| 161 | */ |
| 162 | public static DeviceKey createDeviceKeyUsingSshKey(DeviceKeyId id, String label, |
Andrea Campanella | bddba16 | 2017-02-14 10:05:25 -0800 | [diff] [blame] | 163 | String username, String password, String sshkey) { |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 164 | DefaultAnnotations annotations = builder().set(AnnotationKeys.USERNAME, username) |
| 165 | .set(AnnotationKeys.PASSWORD, password) |
| 166 | .set(AnnotationKeys.SSHKEY, sshkey).build(); |
| 167 | |
| 168 | return new DeviceKey(id, label, Type.SSL_KEY, annotations); |
| 169 | } |
| 170 | |
| 171 | /** |
Brian Stanke | 9bf8d7c | 2016-02-11 10:17:23 -0500 | [diff] [blame] | 172 | * Returns a username and password object from the device key. |
| 173 | * |
| 174 | * @return username and password |
| 175 | */ |
| 176 | public UsernamePassword asUsernamePassword() { |
| 177 | |
| 178 | // Validate that the device key is of type USERNAME_PASSWORD. |
| 179 | checkState(this.type == Type.USERNAME_PASSWORD, "This device key is not a USERNAME_PASSWORD type."); |
| 180 | |
| 181 | String username = annotations().value(AnnotationKeys.USERNAME); |
| 182 | String password = annotations().value(AnnotationKeys.PASSWORD); |
| 183 | return UsernamePassword.usernamePassword(username, password); |
| 184 | } |
Brian Stanke | 2b4f32d | 2016-02-03 14:22:10 -0500 | [diff] [blame] | 185 | } |