blob: d9a34688211d6098d2005990942deb5b31f6c05b [file] [log] [blame]
Andrea Campanella34cf65c2017-04-12 13:51:32 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Andrea Campanella34cf65c2017-04-12 13:51:32 +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
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -070017package org.onosproject.netconf.config;
Andrea Campanella34cf65c2017-04-12 13:51:32 +020018
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;
Sean Condon54d82432017-07-26 22:27:25 +010024
25import java.util.Optional;
26import java.util.OptionalInt;
27
Andrea Campanella34cf65c2017-04-12 13:51:32 +020028import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * Configuration for Netconf provider.
32 */
33@Beta
34public class NetconfDeviceConfig extends Config<DeviceId> {
35
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -070036 /**
37 * netcfg ConfigKey.
38 */
39 public static final String CONFIG_KEY = "netconf";
40
Sean Condon54d82432017-07-26 22:27:25 +010041 public static final String IP = "ip";
42 public static final String PORT = "port";
43 public static final String USERNAME = "username";
44 public static final String PASSWORD = "password";
45 public static final String SSHKEY = "sshkey";
46 public static final String SSHCLIENT = "ssh-client";
47 public static final String CONNECT_TIMEOUT = "connect-timeout";
48 public static final String REPLY_TIMEOUT = "reply-timeout";
49 public static final String IDLE_TIMEOUT = "idle-timeout";
Andrea Campanella34cf65c2017-04-12 13:51:32 +020050
51 @Override
52 public boolean isValid() {
Sean Condon54d82432017-07-26 22:27:25 +010053 return hasOnlyFields(IP, PORT, USERNAME, PASSWORD, SSHKEY, SSHCLIENT,
54 CONNECT_TIMEOUT, REPLY_TIMEOUT, IDLE_TIMEOUT) && ip() != null;
Andrea Campanella34cf65c2017-04-12 13:51:32 +020055 }
56
57 /**
58 * Gets the Ip of the NETCONF device.
59 *
60 * @return ip
61 */
62 public IpAddress ip() {
63 return IpAddress.valueOf(get(IP, checkNotNull(extractIpPort()).getKey()));
64 }
65
66 /**
67 * Gets the port of the NETCONF device.
68 *
69 * @return port
70 */
71 public int port() {
72 return get(PORT, checkNotNull(extractIpPort()).getValue());
73 }
74
75 /**
76 * Gets the username of the NETCONF device.
77 *
78 * @return username
79 */
80 public String username() {
81 return get(USERNAME, "");
82 }
83
84 /**
85 * Gets the password of the NETCONF device.
86 *
87 * @return password
88 */
89 public String password() {
90 return get(PASSWORD, "");
91 }
92
93 /**
94 * Gets the sshKey of the NETCONF device.
95 *
96 * @return sshkey
97 */
98 public String sshKey() {
99 return get(SSHKEY, "");
100 }
101
102 /**
Sean Condon54d82432017-07-26 22:27:25 +0100103 * Gets the NETCONF SSH Client implementation.
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700104 * Expecting "apache-mina"
Sean Condon54d82432017-07-26 22:27:25 +0100105 *
106 * @return sshClient
107 */
108 public Optional<String> sshClient() {
109 String sshClient = get(SSHCLIENT, "");
110 return (sshClient.isEmpty() ? Optional.empty() : Optional.ofNullable(sshClient));
111 }
112
113 /**
114 * Gets the connect timeout of the SSH connection.
115 *
116 * @return connectTimeout
117 */
118 public OptionalInt connectTimeout() {
119 int connectTimeout = get(CONNECT_TIMEOUT, 0);
120 return (connectTimeout == 0) ? OptionalInt.empty() : OptionalInt.of(connectTimeout);
121 }
122
123 /**
124 * Gets the reply timeout of the SSH connection.
125 *
126 * @return replyTimeout
127 */
128 public OptionalInt replyTimeout() {
129 int replyTimeout = get(REPLY_TIMEOUT, 0);
130 return (replyTimeout == 0) ? OptionalInt.empty() : OptionalInt.of(replyTimeout);
131 }
132
133 /**
134 * Gets the idle timeout of the SSH connection.
135 *
136 * @return idleTimeout
137 */
138 public OptionalInt idleTimeout() {
139 int idleTimeout = get(IDLE_TIMEOUT, 0);
140 return (idleTimeout == 0) ? OptionalInt.empty() : OptionalInt.of(idleTimeout);
141 }
142
143 /**
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200144 * Sets the Ip for the Device.
145 *
146 * @param ip the ip
147 * @return instance for chaining
148 */
149 public NetconfDeviceConfig setIp(String ip) {
150 return (NetconfDeviceConfig) setOrClear(IP, ip);
151 }
152
153 /**
154 * Sets the Port for the Device.
155 *
156 * @param port the port
157 * @return instance for chaining
158 */
159 public NetconfDeviceConfig setPort(int port) {
160 return (NetconfDeviceConfig) setOrClear(PORT, port);
161 }
162
163 /**
164 * Sets the username for the Device.
165 *
166 * @param username username
167 * @return instance for chaining
168 */
169 public NetconfDeviceConfig setUsername(String username) {
170 return (NetconfDeviceConfig) setOrClear(USERNAME, username);
171 }
172
173 /**
174 * Sets the password for the Device.
175 *
176 * @param password password
177 * @return instance for chaining
178 */
179 public NetconfDeviceConfig setPassword(String password) {
180 return (NetconfDeviceConfig) setOrClear(PASSWORD, password);
181 }
182
183 /**
184 * Sets the SshKey for the Device.
185 *
186 * @param sshKey sshKey as string
187 * @return instance for chaining
188 */
189 public NetconfDeviceConfig setSshKey(String sshKey) {
190 return (NetconfDeviceConfig) setOrClear(SSHKEY, sshKey);
191 }
192
Sean Condon54d82432017-07-26 22:27:25 +0100193 /**
194 * Sets the NETCONF Ssh client implementation for the Device.
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700195 * Must be 'apache-mina'
Sean Condon54d82432017-07-26 22:27:25 +0100196 * When specified, overrides NetconfControllerImpl.sshLibrary for this device
197 *
198 * @param sshimpl sshimpl as string
199 * @return instance for chaining
200 */
201 public NetconfDeviceConfig setSshImpl(String sshimpl) {
202 return (NetconfDeviceConfig) setOrClear(SSHCLIENT, sshimpl);
203 }
204
205 /**
206 * Sets the NETCONF Connect Timeout for the Device.
207 * This is the amount of time in seconds allowed for the SSH handshake to take place
208 * Minimum 1 second
209 * When specified, overrides NetconfControllerImpl.netconfConnectTimeout for this device
210 *
211 * @param connectTimeout connectTimeout as int
212 * @return instance for chaining
213 */
214 public NetconfDeviceConfig setConnectTimeout(Integer connectTimeout) {
215 return (NetconfDeviceConfig) setOrClear(CONNECT_TIMEOUT, connectTimeout);
216 }
217
218 /**
219 * Sets the NETCONF Reply Timeout for the Device.
220 * This is the amount of time in seconds allowed for the NETCONF Reply to a command
221 * Minimum 1 second
222 * When specified, overrides NetconfControllerImpl.netconfReplyTimeout for this device
223 *
224 * @param replyTimeout replyTimeout as int
225 * @return instance for chaining
226 */
227 public NetconfDeviceConfig setReplyTimeout(Integer replyTimeout) {
228 return (NetconfDeviceConfig) setOrClear(REPLY_TIMEOUT, replyTimeout);
229 }
230
231 /**
232 * Sets the NETCONF Idle Timeout for the Device.
233 * This is the amount of time in seconds after which the SSH connection will
234 * close if no traffic is detected
235 * Minimum 10 second
236 * When specified, overrides NetconfControllerImpl.netconfIdleTimeout for this device
237 *
238 * @param idleTimeout idleTimeout as int
239 * @return instance for chaining
240 */
241 public NetconfDeviceConfig setIdleTimeout(Integer idleTimeout) {
242 return (NetconfDeviceConfig) setOrClear(IDLE_TIMEOUT, idleTimeout);
243 }
244
245
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200246 private Pair<String, Integer> extractIpPort() {
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700247 // Assuming one of
248 // - netconf:ip:port
249 // - netconf:ip
250
251 // foo:schemespecifcpart
252 String info = subject.uri().getSchemeSpecificPart();
253 int portSeparator = info.lastIndexOf(':');
254 if (portSeparator == -1) {
255 // assume default port
256 return Pair.of(info, 830);
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200257 }
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -0700258 String ip = info.substring(0, portSeparator);
259 int port = Integer.parseInt(info.substring(portSeparator + 1));
260 return Pair.of(ip, port);
Andrea Campanella34cf65c2017-04-12 13:51:32 +0200261 }
262}