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 | |
| 17 | package org.onosproject.provider.netconf.device.impl; |
| 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; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
| 27 | /** |
| 28 | * Configuration for Netconf provider. |
| 29 | */ |
| 30 | @Beta |
| 31 | public class NetconfDeviceConfig extends Config<DeviceId> { |
| 32 | |
| 33 | private static final String IP = "ip"; |
| 34 | private static final String PORT = "port"; |
| 35 | private static final String USERNAME = "username"; |
| 36 | private static final String PASSWORD = "password"; |
| 37 | private static final String SSHKEY = "sshkey"; |
| 38 | |
| 39 | @Override |
| 40 | public boolean isValid() { |
| 41 | return hasOnlyFields(IP, PORT, USERNAME, PASSWORD, SSHKEY) && |
| 42 | ip() != null; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Gets the Ip of the NETCONF device. |
| 47 | * |
| 48 | * @return ip |
| 49 | */ |
| 50 | public IpAddress ip() { |
| 51 | return IpAddress.valueOf(get(IP, checkNotNull(extractIpPort()).getKey())); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Gets the port of the NETCONF device. |
| 56 | * |
| 57 | * @return port |
| 58 | */ |
| 59 | public int port() { |
| 60 | return get(PORT, checkNotNull(extractIpPort()).getValue()); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Gets the username of the NETCONF device. |
| 65 | * |
| 66 | * @return username |
| 67 | */ |
| 68 | public String username() { |
| 69 | return get(USERNAME, ""); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Gets the password of the NETCONF device. |
| 74 | * |
| 75 | * @return password |
| 76 | */ |
| 77 | public String password() { |
| 78 | return get(PASSWORD, ""); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Gets the sshKey of the NETCONF device. |
| 83 | * |
| 84 | * @return sshkey |
| 85 | */ |
| 86 | public String sshKey() { |
| 87 | return get(SSHKEY, ""); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Sets the Ip for the Device. |
| 92 | * |
| 93 | * @param ip the ip |
| 94 | * @return instance for chaining |
| 95 | */ |
| 96 | public NetconfDeviceConfig setIp(String ip) { |
| 97 | return (NetconfDeviceConfig) setOrClear(IP, ip); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Sets the Port for the Device. |
| 102 | * |
| 103 | * @param port the port |
| 104 | * @return instance for chaining |
| 105 | */ |
| 106 | public NetconfDeviceConfig setPort(int port) { |
| 107 | return (NetconfDeviceConfig) setOrClear(PORT, port); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Sets the username for the Device. |
| 112 | * |
| 113 | * @param username username |
| 114 | * @return instance for chaining |
| 115 | */ |
| 116 | public NetconfDeviceConfig setUsername(String username) { |
| 117 | return (NetconfDeviceConfig) setOrClear(USERNAME, username); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Sets the password for the Device. |
| 122 | * |
| 123 | * @param password password |
| 124 | * @return instance for chaining |
| 125 | */ |
| 126 | public NetconfDeviceConfig setPassword(String password) { |
| 127 | return (NetconfDeviceConfig) setOrClear(PASSWORD, password); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Sets the SshKey for the Device. |
| 132 | * |
| 133 | * @param sshKey sshKey as string |
| 134 | * @return instance for chaining |
| 135 | */ |
| 136 | public NetconfDeviceConfig setSshKey(String sshKey) { |
| 137 | return (NetconfDeviceConfig) setOrClear(SSHKEY, sshKey); |
| 138 | } |
| 139 | |
| 140 | private Pair<String, Integer> extractIpPort() { |
| 141 | String info = subject.toString(); |
| 142 | if (info.startsWith(NetconfDeviceProvider.SCHEME_NAME)) { |
| 143 | //+1 is due to length of colon separator |
| 144 | String ip = info.substring(info.indexOf(":") + 1, info.lastIndexOf(":")); |
| 145 | int port = Integer.parseInt(info.substring(info.lastIndexOf(":") + 1)); |
| 146 | return Pair.of(ip, port); |
| 147 | } |
| 148 | return null; |
| 149 | } |
| 150 | } |