Brian Stanke | 2b4f32d | 2016-02-03 14:22:10 -0500 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | import static com.google.common.base.Preconditions.checkState; |
| 27 | import static org.onosproject.net.DefaultAnnotations.builder; |
| 28 | |
| 29 | /** |
| 30 | * Abstraction of a device key. |
| 31 | */ |
| 32 | @Beta |
| 33 | public class DeviceKey extends AbstractAnnotated { |
| 34 | |
| 35 | // device key identifier |
| 36 | private final DeviceKeyId deviceKeyId; |
| 37 | // label of the device key |
| 38 | private String label; |
| 39 | |
| 40 | /** |
| 41 | * type of the device key. |
| 42 | */ |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 43 | public enum Type { |
Brian Stanke | 2b4f32d | 2016-02-03 14:22:10 -0500 | [diff] [blame] | 44 | COMMUNITY_NAME, USERNAME_PASSWORD, SSL_KEY |
| 45 | } |
| 46 | |
| 47 | private Type type; |
| 48 | |
| 49 | /** |
| 50 | * Constructor for serialization. |
| 51 | */ |
| 52 | DeviceKey() { |
| 53 | this.deviceKeyId = null; |
| 54 | this.label = null; |
| 55 | this.type = null; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Private constructor for a device key. |
| 60 | * |
| 61 | * @param id device key identifier |
| 62 | * @param label optional label for this device key |
| 63 | * @param type to be assigned to this device key |
| 64 | * @param annotations name/value pairs for this device key |
| 65 | */ |
| 66 | private DeviceKey(DeviceKeyId id, String label, Type type, Annotations... annotations) { |
| 67 | super(annotations); |
| 68 | checkNotNull(id, "The DeviceKeyId cannot be null."); |
| 69 | this.deviceKeyId = id; |
| 70 | this.label = label; |
| 71 | this.type = type; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the device key identifier of the device key. |
| 76 | * |
| 77 | * @return device key identifier |
| 78 | */ |
| 79 | public DeviceKeyId deviceKeyId() { |
| 80 | return deviceKeyId; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns the label of device key. |
| 85 | * |
| 86 | * @return label |
| 87 | */ |
| 88 | public String label() { |
| 89 | return label; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns the type of the device key. |
| 94 | * |
| 95 | * @return type |
| 96 | */ |
| 97 | public Type type() { |
| 98 | return type; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Method to create a device key of type CommunityName. |
| 103 | * |
| 104 | * @param id device key identifier |
| 105 | * @param label optional label for this device key |
| 106 | * @param name community name for this device key |
| 107 | * @return device key |
| 108 | */ |
| 109 | public static DeviceKey createDeviceKeyUsingCommunityName(DeviceKeyId id, String label, String name) { |
| 110 | DefaultAnnotations annotations = builder().set(AnnotationKeys.NAME, name).build(); |
| 111 | |
| 112 | return new DeviceKey(id, label, Type.COMMUNITY_NAME, annotations); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Returns a community name object from the device key. |
| 117 | * |
| 118 | * @return community name |
| 119 | */ |
| 120 | public CommunityName asCommunityName() { |
| 121 | |
| 122 | // Validate that the device key is of type COMMUNITY_NAME. |
| 123 | checkState(this.type == Type.COMMUNITY_NAME, "This device key is not a COMMUNITY_NAME type."); |
| 124 | |
| 125 | String name = annotations().value(AnnotationKeys.NAME); |
| 126 | return CommunityName.communityName(name); |
| 127 | } |
Brian Stanke | 9bf8d7c | 2016-02-11 10:17:23 -0500 | [diff] [blame] | 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 | } |
Brian Stanke | 2b4f32d | 2016-02-03 14:22:10 -0500 | [diff] [blame] | 160 | } |