blob: cd290e5cdf67602bf4858c59c1749e2e1ca357ef [file] [log] [blame]
Andrea Campanella59b549d2017-04-14 21:58:16 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Andrea Campanella59b549d2017-04-14 21:58:16 +02003 *
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
17package org.onosproject.provider.rest.device.impl;
18
19import com.google.common.annotations.Beta;
20import org.apache.commons.lang3.tuple.Pair;
21import org.onlab.packet.IpAddress;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.config.Config;
fahadnaeemkhan02ffa712017-12-01 19:49:45 -080024import org.onosproject.protocol.rest.RestSBDevice.AuthenticationScheme;
Andrea Campanella59b549d2017-04-14 21:58:16 +020025
26/**
27 * Configuration to push devices to the REST provider.
28 */
29@Beta
30public class RestDeviceConfig extends Config<DeviceId> {
31
32 private static final String IP = "ip";
33 private static final String PORT = "port";
34 private static final String USERNAME = "username";
35 private static final String PASSWORD = "password";
36 private static final String PROTOCOL = "protocol";
37 private static final String URL = "url";
38 private static final String TESTURL = "testUrl";
39 private static final String MANUFACTURER = "manufacturer";
40 private static final String HWVERSION = "hwVersion";
41 private static final String SWVERSION = "swVersion";
fahadnaeemkhan02ffa712017-12-01 19:49:45 -080042 private static final String AUTHENTICATION_SCHEME = "authenticationScheme";
43 private static final String TOKEN = "token";
Andrea Campanella59b549d2017-04-14 21:58:16 +020044
45 @Override
46 public boolean isValid() {
47 return hasOnlyFields(IP, PORT, USERNAME, PASSWORD, PROTOCOL, URL,
fahadnaeemkhan02ffa712017-12-01 19:49:45 -080048 TESTURL, MANUFACTURER, HWVERSION, SWVERSION, AUTHENTICATION_SCHEME,
49 TOKEN) &&
Andrea Campanella59b549d2017-04-14 21:58:16 +020050 ip() != null;
51 }
52
53 /**
54 * Gets the Ip of the REST device.
55 *
56 * @return ip
57 */
58 public IpAddress ip() {
59 return IpAddress.valueOf(get(IP, extractIpPort().getKey()));
60 }
61
62 /**
63 * Gets the port of the REST device.
64 *
65 * @return port
66 */
67 public int port() {
68 return get(PORT, extractIpPort().getValue());
69 }
70
71 /**
72 * Gets the protocol of the REST device.
73 *
74 * @return protocol
75 */
76 public String protocol() {
77 return get(PROTOCOL, "http");
78 }
79
80 /**
81 * Gets the username of the REST device.
82 *
83 * @return username
84 */
85 public String username() {
86 return get(USERNAME, "");
87 }
88
89 /**
90 * Gets the password of the REST device.
91 *
92 * @return password
93 */
94 public String password() {
95 return get(PASSWORD, "");
96 }
97
98 /**
99 * Gets the base url of the REST device.
100 *
101 * @return base url for the device config tree
102 */
103 public String url() {
104 return get(URL, "");
105 }
106
107 /**
108 * Gets the testUrl of the REST device.
109 *
110 * @return testUrl to test the device connection
111 */
112 public String testUrl() {
113 return get(TESTURL, "");
114 }
115
116 /**
117 * Gets the manufacturer of the REST device.
118 *
119 * @return manufacturer
120 */
121 public String manufacturer() {
122 return get(MANUFACTURER, "");
123 }
124
125 /**
126 * Gets the hwversion of the REST device.
127 *
128 * @return hwversion
129 */
130 public String hwVersion() {
131 return get(HWVERSION, "");
132 }
133
134 /**
135 * Gets the swversion of the REST device.
136 *
137 * @return swversion
138 */
139 public String swVersion() {
140 return get(SWVERSION, "");
141 }
142
fahadnaeemkhan02ffa712017-12-01 19:49:45 -0800143 /**
144 * Gets the authentication type of the REST device.
145 * Default is 'basic' if username is defined, else default is no_authentication.
146 *
147 * @return authentication
148 */
149 public AuthenticationScheme authenticationScheme() {
150 // hack for backward compatibility
151 if (!hasField(AUTHENTICATION_SCHEME)) {
152 if (hasField(USERNAME)) {
153 return AuthenticationScheme.BASIC;
154 }
155 }
156 return AuthenticationScheme.valueOf(get(AUTHENTICATION_SCHEME, "NO_AUTHENTICATION").toUpperCase());
157 }
158
159 /**
160 * Gets the token of the REST device.
161 *
162 * @return token
163 */
164 public String token() {
165 return get(TOKEN, "");
166 }
167
Andrea Campanella59b549d2017-04-14 21:58:16 +0200168 private Pair<String, Integer> extractIpPort() {
169 String info = subject.toString();
170 if (info.startsWith(RestDeviceProvider.REST)) {
171 //+1 is due to length of colon separator
172 String ip = info.substring(info.indexOf(":") + 1, info.lastIndexOf(":"));
173 int port = Integer.parseInt(info.substring(info.lastIndexOf(":") + 1));
174 return Pair.of(ip, port);
175 }
176 return null;
177 }
178
179}