Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 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 | |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 17 | package org.onosproject.netconf.config; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 18 | |
| 19 | import com.google.common.annotations.Beta; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.config.Config; |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame^] | 23 | import org.slf4j.Logger; |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 24 | |
| 25 | import java.util.Optional; |
| 26 | import java.util.OptionalInt; |
| 27 | |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkNotNull; |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame^] | 29 | import static org.onosproject.netconf.NetconfDeviceInfo.extractIpPortPath; |
| 30 | import static org.slf4j.LoggerFactory.getLogger; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Configuration for Netconf provider. |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame^] | 34 | * |
| 35 | * The URI for a netconf device is of the format |
| 36 | * |
| 37 | * {@code netconf:<ip>[:<port>][/<path>]} |
| 38 | * |
| 39 | * The {@code ip} and {@code port} are used to create a netconf connection |
| 40 | * to the device. The {@code path} is an optional component that is not used |
| 41 | * by the default netconf driver, but is leveragable by custom drivers. |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 42 | */ |
| 43 | @Beta |
| 44 | public class NetconfDeviceConfig extends Config<DeviceId> { |
| 45 | |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame^] | 46 | private final Logger log = getLogger(getClass()); |
| 47 | |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 48 | /** |
| 49 | * netcfg ConfigKey. |
| 50 | */ |
| 51 | public static final String CONFIG_KEY = "netconf"; |
| 52 | |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 53 | public static final String IP = "ip"; |
| 54 | public static final String PORT = "port"; |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame^] | 55 | public static final String PATH = "path"; |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 56 | public static final String USERNAME = "username"; |
| 57 | public static final String PASSWORD = "password"; |
| 58 | public static final String SSHKEY = "sshkey"; |
| 59 | public static final String SSHCLIENT = "ssh-client"; |
| 60 | public static final String CONNECT_TIMEOUT = "connect-timeout"; |
| 61 | public static final String REPLY_TIMEOUT = "reply-timeout"; |
| 62 | public static final String IDLE_TIMEOUT = "idle-timeout"; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 63 | |
| 64 | @Override |
| 65 | public boolean isValid() { |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame^] | 66 | return hasOnlyFields(IP, PORT, PATH, USERNAME, PASSWORD, SSHKEY, SSHCLIENT, |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 67 | CONNECT_TIMEOUT, REPLY_TIMEOUT, IDLE_TIMEOUT) && ip() != null; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Gets the Ip of the NETCONF device. |
| 72 | * |
| 73 | * @return ip |
| 74 | */ |
| 75 | public IpAddress ip() { |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame^] | 76 | return IpAddress.valueOf(get(IP, checkNotNull(extractIpPortPath(subject)).getLeft())); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Gets the port of the NETCONF device. |
| 81 | * |
| 82 | * @return port |
| 83 | */ |
| 84 | public int port() { |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame^] | 85 | return get(PORT, checkNotNull(extractIpPortPath(subject)).getMiddle()); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Gets the path of the NETCONF device. |
| 90 | * |
| 91 | * @return path |
| 92 | */ |
| 93 | public Optional<String> path() { |
| 94 | String val = get(PATH, ""); |
| 95 | if (val.isEmpty()) { |
| 96 | return extractIpPortPath(subject).getRight(); |
| 97 | } |
| 98 | return Optional.ofNullable(val); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Gets the username of the NETCONF device. |
| 103 | * |
| 104 | * @return username |
| 105 | */ |
| 106 | public String username() { |
| 107 | return get(USERNAME, ""); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Gets the password of the NETCONF device. |
| 112 | * |
| 113 | * @return password |
| 114 | */ |
| 115 | public String password() { |
| 116 | return get(PASSWORD, ""); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Gets the sshKey of the NETCONF device. |
| 121 | * |
| 122 | * @return sshkey |
| 123 | */ |
| 124 | public String sshKey() { |
| 125 | return get(SSHKEY, ""); |
| 126 | } |
| 127 | |
| 128 | /** |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 129 | * Gets the NETCONF SSH Client implementation. |
Yuta HIGUCHI | 5233dec | 2018-05-02 15:22:37 -0700 | [diff] [blame] | 130 | * Expecting "apache-mina" |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 131 | * |
| 132 | * @return sshClient |
| 133 | */ |
| 134 | public Optional<String> sshClient() { |
| 135 | String sshClient = get(SSHCLIENT, ""); |
| 136 | return (sshClient.isEmpty() ? Optional.empty() : Optional.ofNullable(sshClient)); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Gets the connect timeout of the SSH connection. |
| 141 | * |
| 142 | * @return connectTimeout |
| 143 | */ |
| 144 | public OptionalInt connectTimeout() { |
| 145 | int connectTimeout = get(CONNECT_TIMEOUT, 0); |
| 146 | return (connectTimeout == 0) ? OptionalInt.empty() : OptionalInt.of(connectTimeout); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Gets the reply timeout of the SSH connection. |
| 151 | * |
| 152 | * @return replyTimeout |
| 153 | */ |
| 154 | public OptionalInt replyTimeout() { |
| 155 | int replyTimeout = get(REPLY_TIMEOUT, 0); |
| 156 | return (replyTimeout == 0) ? OptionalInt.empty() : OptionalInt.of(replyTimeout); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Gets the idle timeout of the SSH connection. |
| 161 | * |
| 162 | * @return idleTimeout |
| 163 | */ |
| 164 | public OptionalInt idleTimeout() { |
| 165 | int idleTimeout = get(IDLE_TIMEOUT, 0); |
| 166 | return (idleTimeout == 0) ? OptionalInt.empty() : OptionalInt.of(idleTimeout); |
| 167 | } |
| 168 | |
| 169 | /** |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 170 | * Sets the Ip for the Device. |
| 171 | * |
| 172 | * @param ip the ip |
| 173 | * @return instance for chaining |
| 174 | */ |
| 175 | public NetconfDeviceConfig setIp(String ip) { |
| 176 | return (NetconfDeviceConfig) setOrClear(IP, ip); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Sets the Port for the Device. |
| 181 | * |
| 182 | * @param port the port |
| 183 | * @return instance for chaining |
| 184 | */ |
| 185 | public NetconfDeviceConfig setPort(int port) { |
| 186 | return (NetconfDeviceConfig) setOrClear(PORT, port); |
| 187 | } |
| 188 | |
| 189 | /** |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame^] | 190 | * Sets the path for the device. |
| 191 | * |
| 192 | * @param path the path |
| 193 | * @return instance for chaining |
| 194 | */ |
| 195 | public NetconfDeviceConfig setPath(String path) { |
| 196 | return (NetconfDeviceConfig) setOrClear(PATH, path); |
| 197 | } |
| 198 | |
| 199 | /** |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 200 | * Sets the username for the Device. |
| 201 | * |
| 202 | * @param username username |
| 203 | * @return instance for chaining |
| 204 | */ |
| 205 | public NetconfDeviceConfig setUsername(String username) { |
| 206 | return (NetconfDeviceConfig) setOrClear(USERNAME, username); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Sets the password for the Device. |
| 211 | * |
| 212 | * @param password password |
| 213 | * @return instance for chaining |
| 214 | */ |
| 215 | public NetconfDeviceConfig setPassword(String password) { |
| 216 | return (NetconfDeviceConfig) setOrClear(PASSWORD, password); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Sets the SshKey for the Device. |
| 221 | * |
| 222 | * @param sshKey sshKey as string |
| 223 | * @return instance for chaining |
| 224 | */ |
| 225 | public NetconfDeviceConfig setSshKey(String sshKey) { |
| 226 | return (NetconfDeviceConfig) setOrClear(SSHKEY, sshKey); |
| 227 | } |
| 228 | |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 229 | /** |
| 230 | * Sets the NETCONF Ssh client implementation for the Device. |
Yuta HIGUCHI | 5233dec | 2018-05-02 15:22:37 -0700 | [diff] [blame] | 231 | * Must be 'apache-mina' |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 232 | * When specified, overrides NetconfControllerImpl.sshLibrary for this device |
| 233 | * |
| 234 | * @param sshimpl sshimpl as string |
| 235 | * @return instance for chaining |
| 236 | */ |
| 237 | public NetconfDeviceConfig setSshImpl(String sshimpl) { |
| 238 | return (NetconfDeviceConfig) setOrClear(SSHCLIENT, sshimpl); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Sets the NETCONF Connect Timeout for the Device. |
| 243 | * This is the amount of time in seconds allowed for the SSH handshake to take place |
| 244 | * Minimum 1 second |
| 245 | * When specified, overrides NetconfControllerImpl.netconfConnectTimeout for this device |
| 246 | * |
| 247 | * @param connectTimeout connectTimeout as int |
| 248 | * @return instance for chaining |
| 249 | */ |
| 250 | public NetconfDeviceConfig setConnectTimeout(Integer connectTimeout) { |
| 251 | return (NetconfDeviceConfig) setOrClear(CONNECT_TIMEOUT, connectTimeout); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Sets the NETCONF Reply Timeout for the Device. |
| 256 | * This is the amount of time in seconds allowed for the NETCONF Reply to a command |
| 257 | * Minimum 1 second |
| 258 | * When specified, overrides NetconfControllerImpl.netconfReplyTimeout for this device |
| 259 | * |
| 260 | * @param replyTimeout replyTimeout as int |
| 261 | * @return instance for chaining |
| 262 | */ |
| 263 | public NetconfDeviceConfig setReplyTimeout(Integer replyTimeout) { |
| 264 | return (NetconfDeviceConfig) setOrClear(REPLY_TIMEOUT, replyTimeout); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Sets the NETCONF Idle Timeout for the Device. |
| 269 | * This is the amount of time in seconds after which the SSH connection will |
| 270 | * close if no traffic is detected |
| 271 | * Minimum 10 second |
| 272 | * When specified, overrides NetconfControllerImpl.netconfIdleTimeout for this device |
| 273 | * |
| 274 | * @param idleTimeout idleTimeout as int |
| 275 | * @return instance for chaining |
| 276 | */ |
| 277 | public NetconfDeviceConfig setIdleTimeout(Integer idleTimeout) { |
| 278 | return (NetconfDeviceConfig) setOrClear(IDLE_TIMEOUT, idleTimeout); |
| 279 | } |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 280 | } |