Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015-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 | |
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; |
| 20 | import org.apache.commons.lang3.tuple.Pair; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.config.Config; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | |
| 26 | /** |
| 27 | * Configuration for Netconf provider. |
| 28 | */ |
| 29 | @Beta |
| 30 | public class NetconfDeviceConfig extends Config<DeviceId> { |
| 31 | |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame^] | 32 | /** |
| 33 | * netcfg ConfigKey. |
| 34 | */ |
| 35 | public static final String CONFIG_KEY = "netconf"; |
| 36 | |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 37 | private static final String IP = "ip"; |
| 38 | private static final String PORT = "port"; |
| 39 | private static final String USERNAME = "username"; |
| 40 | private static final String PASSWORD = "password"; |
| 41 | private static final String SSHKEY = "sshkey"; |
| 42 | |
| 43 | @Override |
| 44 | public boolean isValid() { |
| 45 | return hasOnlyFields(IP, PORT, USERNAME, PASSWORD, SSHKEY) && |
| 46 | ip() != null; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Gets the Ip of the NETCONF device. |
| 51 | * |
| 52 | * @return ip |
| 53 | */ |
| 54 | public IpAddress ip() { |
| 55 | return IpAddress.valueOf(get(IP, checkNotNull(extractIpPort()).getKey())); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Gets the port of the NETCONF device. |
| 60 | * |
| 61 | * @return port |
| 62 | */ |
| 63 | public int port() { |
| 64 | return get(PORT, checkNotNull(extractIpPort()).getValue()); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Gets the username of the NETCONF device. |
| 69 | * |
| 70 | * @return username |
| 71 | */ |
| 72 | public String username() { |
| 73 | return get(USERNAME, ""); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Gets the password of the NETCONF device. |
| 78 | * |
| 79 | * @return password |
| 80 | */ |
| 81 | public String password() { |
| 82 | return get(PASSWORD, ""); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Gets the sshKey of the NETCONF device. |
| 87 | * |
| 88 | * @return sshkey |
| 89 | */ |
| 90 | public String sshKey() { |
| 91 | return get(SSHKEY, ""); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Sets the Ip for the Device. |
| 96 | * |
| 97 | * @param ip the ip |
| 98 | * @return instance for chaining |
| 99 | */ |
| 100 | public NetconfDeviceConfig setIp(String ip) { |
| 101 | return (NetconfDeviceConfig) setOrClear(IP, ip); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Sets the Port for the Device. |
| 106 | * |
| 107 | * @param port the port |
| 108 | * @return instance for chaining |
| 109 | */ |
| 110 | public NetconfDeviceConfig setPort(int port) { |
| 111 | return (NetconfDeviceConfig) setOrClear(PORT, port); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Sets the username for the Device. |
| 116 | * |
| 117 | * @param username username |
| 118 | * @return instance for chaining |
| 119 | */ |
| 120 | public NetconfDeviceConfig setUsername(String username) { |
| 121 | return (NetconfDeviceConfig) setOrClear(USERNAME, username); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Sets the password for the Device. |
| 126 | * |
| 127 | * @param password password |
| 128 | * @return instance for chaining |
| 129 | */ |
| 130 | public NetconfDeviceConfig setPassword(String password) { |
| 131 | return (NetconfDeviceConfig) setOrClear(PASSWORD, password); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Sets the SshKey for the Device. |
| 136 | * |
| 137 | * @param sshKey sshKey as string |
| 138 | * @return instance for chaining |
| 139 | */ |
| 140 | public NetconfDeviceConfig setSshKey(String sshKey) { |
| 141 | return (NetconfDeviceConfig) setOrClear(SSHKEY, sshKey); |
| 142 | } |
| 143 | |
| 144 | private Pair<String, Integer> extractIpPort() { |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame^] | 145 | // Assuming one of |
| 146 | // - netconf:ip:port |
| 147 | // - netconf:ip |
| 148 | |
| 149 | // foo:schemespecifcpart |
| 150 | String info = subject.uri().getSchemeSpecificPart(); |
| 151 | int portSeparator = info.lastIndexOf(':'); |
| 152 | if (portSeparator == -1) { |
| 153 | // assume default port |
| 154 | return Pair.of(info, 830); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 155 | } |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame^] | 156 | String ip = info.substring(0, portSeparator); |
| 157 | int port = Integer.parseInt(info.substring(portSeparator + 1)); |
| 158 | return Pair.of(ip, port); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 159 | } |
| 160 | } |