blob: 8122bd049470c1d472a93caf3593d18384d26bee [file] [log] [blame]
Brian Stanke2b4f32d2016-02-03 14:22:10 -05001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Brian Stanke2b4f32d2016-02-03 14:22:10 -05003 *
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 Chiu31ad5272016-02-17 20:56:24 +000017package org.onosproject.net.key;
Brian Stanke2b4f32d2016-02-03 14:22:10 -050018
19import com.google.common.annotations.Beta;
20import org.onosproject.net.AbstractAnnotated;
21import org.onosproject.net.AnnotationKeys;
22import org.onosproject.net.Annotations;
23import org.onosproject.net.DefaultAnnotations;
24
Jordan Halterman0d89ea32017-06-13 10:42:36 -070025import static com.google.common.base.Preconditions.checkArgument;
Brian Stanke2b4f32d2016-02-03 14:22:10 -050026import static com.google.common.base.Preconditions.checkNotNull;
27import static com.google.common.base.Preconditions.checkState;
28import static org.onosproject.net.DefaultAnnotations.builder;
29
30/**
31 * Abstraction of a device key.
32 */
33@Beta
34public class DeviceKey extends AbstractAnnotated {
35
Jordan Halterman0d89ea32017-06-13 10:42:36 -070036 private static final int LABEL_MAX_LENGTH = 1024;
37
Brian Stanke2b4f32d2016-02-03 14:22:10 -050038 // 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 Stankee312fc72016-02-16 15:07:13 -050046 public enum Type {
Brian Stanke2b4f32d2016-02-03 14:22:10 -050047 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 Halterman0d89ea32017-06-13 10:42:36 -070072 if (label != null) {
73 checkArgument(label.length() <= LABEL_MAX_LENGTH, "label exceeds maximum length " + LABEL_MAX_LENGTH);
74 }
Brian Stanke2b4f32d2016-02-03 14:22:10 -050075 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 Stanke9bf8d7c2016-02-11 10:17:23 -0500134
135 /**
136 * Method to create a device key of type USERNAME_PASSWORD.
137 *
Andrea Campanellabddba162017-02-14 10:05:25 -0800138 * @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 Stanke9bf8d7c2016-02-11 10:17:23 -0500142 * @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 Ranjan7c2ee3c2017-02-13 05:10:08 -0600153 * Method to create a device key of type SSL_KEY.
154 *
Andrea Campanellabddba162017-02-14 10:05:25 -0800155 * @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 Ranjan7c2ee3c2017-02-13 05:10:08 -0600160 * @return device key
161 */
162 public static DeviceKey createDeviceKeyUsingSshKey(DeviceKeyId id, String label,
Andrea Campanellabddba162017-02-14 10:05:25 -0800163 String username, String password, String sshkey) {
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600164 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 Stanke9bf8d7c2016-02-11 10:17:23 -0500172 * 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 Stanke2b4f32d2016-02-03 14:22:10 -0500185}