blob: c61884a6efda97f3ccc8d944f03270d671479801 [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";
Georgios Katsikas15841e22018-07-28 14:27:28 +020042 private static final String PROXY = "isProxy";
fahadnaeemkhan02ffa712017-12-01 19:49:45 -080043 private static final String AUTHENTICATION_SCHEME = "authenticationScheme";
44 private static final String TOKEN = "token";
Andrea Campanella59b549d2017-04-14 21:58:16 +020045
46 @Override
47 public boolean isValid() {
48 return hasOnlyFields(IP, PORT, USERNAME, PASSWORD, PROTOCOL, URL,
Georgios Katsikas15841e22018-07-28 14:27:28 +020049 TESTURL, MANUFACTURER, HWVERSION, SWVERSION, PROXY,
50 AUTHENTICATION_SCHEME, TOKEN) &&
Andrea Campanella59b549d2017-04-14 21:58:16 +020051 ip() != null;
52 }
53
54 /**
55 * Gets the Ip of the REST device.
56 *
57 * @return ip
58 */
59 public IpAddress ip() {
60 return IpAddress.valueOf(get(IP, extractIpPort().getKey()));
61 }
62
63 /**
64 * Gets the port of the REST device.
65 *
66 * @return port
67 */
68 public int port() {
69 return get(PORT, extractIpPort().getValue());
70 }
71
72 /**
73 * Gets the protocol of the REST device.
74 *
75 * @return protocol
76 */
77 public String protocol() {
78 return get(PROTOCOL, "http");
79 }
80
81 /**
82 * Gets the username of the REST device.
83 *
84 * @return username
85 */
86 public String username() {
87 return get(USERNAME, "");
88 }
89
90 /**
91 * Gets the password of the REST device.
92 *
93 * @return password
94 */
95 public String password() {
96 return get(PASSWORD, "");
97 }
98
99 /**
100 * Gets the base url of the REST device.
101 *
102 * @return base url for the device config tree
103 */
104 public String url() {
105 return get(URL, "");
106 }
107
108 /**
109 * Gets the testUrl of the REST device.
110 *
111 * @return testUrl to test the device connection
112 */
113 public String testUrl() {
114 return get(TESTURL, "");
115 }
116
117 /**
118 * Gets the manufacturer of the REST device.
119 *
120 * @return manufacturer
121 */
122 public String manufacturer() {
123 return get(MANUFACTURER, "");
124 }
125
126 /**
127 * Gets the hwversion of the REST device.
128 *
129 * @return hwversion
130 */
131 public String hwVersion() {
132 return get(HWVERSION, "");
133 }
134
135 /**
136 * Gets the swversion of the REST device.
137 *
138 * @return swversion
139 */
140 public String swVersion() {
141 return get(SWVERSION, "");
142 }
143
fahadnaeemkhan02ffa712017-12-01 19:49:45 -0800144 /**
Georgios Katsikas15841e22018-07-28 14:27:28 +0200145 * Gets whether the REST device is a proxy or not.
146 *
147 * @return proxy
148 */
149 public boolean isProxy() {
150 if (!hasField(PROXY)) {
151 return false;
152 }
153 return get(PROXY, false);
154 }
155
156 /**
fahadnaeemkhan02ffa712017-12-01 19:49:45 -0800157 * Gets the authentication type of the REST device.
158 * Default is 'basic' if username is defined, else default is no_authentication.
159 *
160 * @return authentication
161 */
162 public AuthenticationScheme authenticationScheme() {
163 // hack for backward compatibility
164 if (!hasField(AUTHENTICATION_SCHEME)) {
165 if (hasField(USERNAME)) {
166 return AuthenticationScheme.BASIC;
167 }
168 }
169 return AuthenticationScheme.valueOf(get(AUTHENTICATION_SCHEME, "NO_AUTHENTICATION").toUpperCase());
170 }
171
172 /**
173 * Gets the token of the REST device.
174 *
175 * @return token
176 */
177 public String token() {
178 return get(TOKEN, "");
179 }
180
Andrea Campanella59b549d2017-04-14 21:58:16 +0200181 private Pair<String, Integer> extractIpPort() {
182 String info = subject.toString();
183 if (info.startsWith(RestDeviceProvider.REST)) {
184 //+1 is due to length of colon separator
185 String ip = info.substring(info.indexOf(":") + 1, info.lastIndexOf(":"));
186 int port = Integer.parseInt(info.substring(info.lastIndexOf(":") + 1));
187 return Pair.of(ip, port);
188 }
189 return null;
190 }
191
192}