blob: 1135abdfb127d3244173f5969da7fc1d16632ae6 [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;
Andrea Campanella34cf65c2017-04-12 13:51:32 +020020import org.onlab.packet.IpAddress;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.config.Config;
David K. Bainbridge56e90232018-12-18 23:25:08 -080023import org.slf4j.Logger;
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;
David K. Bainbridge56e90232018-12-18 23:25:08 -080029import static org.onosproject.netconf.NetconfDeviceInfo.extractIpPortPath;
30import static org.slf4j.LoggerFactory.getLogger;
Andrea Campanella34cf65c2017-04-12 13:51:32 +020031
32/**
33 * Configuration for Netconf provider.
David K. Bainbridge56e90232018-12-18 23:25:08 -080034 *
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 Campanella34cf65c2017-04-12 13:51:32 +020042 */
43@Beta
44public class NetconfDeviceConfig extends Config<DeviceId> {
45
David K. Bainbridge56e90232018-12-18 23:25:08 -080046 private final Logger log = getLogger(getClass());
47
Yuta HIGUCHIb6e0e912017-05-18 20:13:52 -070048 /**
49 * netcfg ConfigKey.
50 */
51 public static final String CONFIG_KEY = "netconf";
52
Sean Condon54d82432017-07-26 22:27:25 +010053 public static final String IP = "ip";
54 public static final String PORT = "port";
David K. Bainbridge56e90232018-12-18 23:25:08 -080055 public static final String PATH = "path";
Sean Condon54d82432017-07-26 22:27:25 +010056 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 Campanella34cf65c2017-04-12 13:51:32 +020063
64 @Override
65 public boolean isValid() {
David K. Bainbridge56e90232018-12-18 23:25:08 -080066 return hasOnlyFields(IP, PORT, PATH, USERNAME, PASSWORD, SSHKEY, SSHCLIENT,
Sean Condon54d82432017-07-26 22:27:25 +010067 CONNECT_TIMEOUT, REPLY_TIMEOUT, IDLE_TIMEOUT) && ip() != null;
Andrea Campanella34cf65c2017-04-12 13:51:32 +020068 }
69
70 /**
71 * Gets the Ip of the NETCONF device.
72 *
73 * @return ip
74 */
75 public IpAddress ip() {
David K. Bainbridge56e90232018-12-18 23:25:08 -080076 return IpAddress.valueOf(get(IP, checkNotNull(extractIpPortPath(subject)).getLeft()));
Andrea Campanella34cf65c2017-04-12 13:51:32 +020077 }
78
79 /**
80 * Gets the port of the NETCONF device.
81 *
82 * @return port
83 */
84 public int port() {
David K. Bainbridge56e90232018-12-18 23:25:08 -080085 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 Campanella34cf65c2017-04-12 13:51:32 +020099 }
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 Condon54d82432017-07-26 22:27:25 +0100129 * Gets the NETCONF SSH Client implementation.
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700130 * Expecting "apache-mina"
Sean Condon54d82432017-07-26 22:27:25 +0100131 *
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 Campanella34cf65c2017-04-12 13:51:32 +0200170 * 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. Bainbridge56e90232018-12-18 23:25:08 -0800190 * 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 Campanella34cf65c2017-04-12 13:51:32 +0200200 * 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 Condon54d82432017-07-26 22:27:25 +0100229 /**
230 * Sets the NETCONF Ssh client implementation for the Device.
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700231 * Must be 'apache-mina'
Sean Condon54d82432017-07-26 22:27:25 +0100232 * 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 Campanella34cf65c2017-04-12 13:51:32 +0200280}