blob: 93acab567574ddce49ad613c78aa5fdfa649ce0d [file] [log] [blame]
Andrea Campanella59b549d2017-04-14 21:58:16 +02001/*
2 * Copyright 2017-present Open Networking Laboratory
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
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;
24
25/**
26 * Configuration to push devices to the REST provider.
27 */
28@Beta
29public class RestDeviceConfig extends Config<DeviceId> {
30
31 private static final String IP = "ip";
32 private static final String PORT = "port";
33 private static final String USERNAME = "username";
34 private static final String PASSWORD = "password";
35 private static final String PROTOCOL = "protocol";
36 private static final String URL = "url";
37 private static final String TESTURL = "testUrl";
38 private static final String MANUFACTURER = "manufacturer";
39 private static final String HWVERSION = "hwVersion";
40 private static final String SWVERSION = "swVersion";
41
42 @Override
43 public boolean isValid() {
44 return hasOnlyFields(IP, PORT, USERNAME, PASSWORD, PROTOCOL, URL,
45 TESTURL, MANUFACTURER, HWVERSION, SWVERSION) &&
46 ip() != null;
47 }
48
49 /**
50 * Gets the Ip of the REST device.
51 *
52 * @return ip
53 */
54 public IpAddress ip() {
55 return IpAddress.valueOf(get(IP, extractIpPort().getKey()));
56 }
57
58 /**
59 * Gets the port of the REST device.
60 *
61 * @return port
62 */
63 public int port() {
64 return get(PORT, extractIpPort().getValue());
65 }
66
67 /**
68 * Gets the protocol of the REST device.
69 *
70 * @return protocol
71 */
72 public String protocol() {
73 return get(PROTOCOL, "http");
74 }
75
76 /**
77 * Gets the username of the REST device.
78 *
79 * @return username
80 */
81 public String username() {
82 return get(USERNAME, "");
83 }
84
85 /**
86 * Gets the password of the REST device.
87 *
88 * @return password
89 */
90 public String password() {
91 return get(PASSWORD, "");
92 }
93
94 /**
95 * Gets the base url of the REST device.
96 *
97 * @return base url for the device config tree
98 */
99 public String url() {
100 return get(URL, "");
101 }
102
103 /**
104 * Gets the testUrl of the REST device.
105 *
106 * @return testUrl to test the device connection
107 */
108 public String testUrl() {
109 return get(TESTURL, "");
110 }
111
112 /**
113 * Gets the manufacturer of the REST device.
114 *
115 * @return manufacturer
116 */
117 public String manufacturer() {
118 return get(MANUFACTURER, "");
119 }
120
121 /**
122 * Gets the hwversion of the REST device.
123 *
124 * @return hwversion
125 */
126 public String hwVersion() {
127 return get(HWVERSION, "");
128 }
129
130 /**
131 * Gets the swversion of the REST device.
132 *
133 * @return swversion
134 */
135 public String swVersion() {
136 return get(SWVERSION, "");
137 }
138
139 private Pair<String, Integer> extractIpPort() {
140 String info = subject.toString();
141 if (info.startsWith(RestDeviceProvider.REST)) {
142 //+1 is due to length of colon separator
143 String ip = info.substring(info.indexOf(":") + 1, info.lastIndexOf(":"));
144 int port = Integer.parseInt(info.substring(info.lastIndexOf(":") + 1));
145 return Pair.of(ip, port);
146 }
147 return null;
148 }
149
150}