jaegonkim | dcf7c82 | 2019-02-06 15:00:14 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 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 | package org.onosproject.workflow.model.accessinfo; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.NumericNode; |
| 20 | import com.fasterxml.jackson.databind.node.TextNode; |
| 21 | import com.google.common.base.MoreObjects; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.TpPort; |
| 24 | import org.onosproject.workflow.api.WorkflowException; |
| 25 | |
| 26 | import java.util.Objects; |
| 27 | |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | |
| 30 | /** |
| 31 | * Class for SSH access information. |
| 32 | */ |
| 33 | public final class SshAccessInfo { |
| 34 | |
| 35 | private static final String REMOTE_IP = "remoteIp"; |
| 36 | private static final String PORT = "port"; |
| 37 | private static final String USER = "user"; |
| 38 | private static final String PASSWORD = "password"; |
| 39 | private static final String KEYFILE = "keyfile"; |
| 40 | |
| 41 | private final IpAddress remoteIp; |
| 42 | private final TpPort port; |
| 43 | private final String user; |
| 44 | private String password; |
| 45 | private final String privateKey; |
| 46 | |
| 47 | /** |
| 48 | * Constructor for SSH access information. |
| 49 | * |
| 50 | * @param remoteIp ssh remote ip address |
| 51 | * @param port ssh port number |
| 52 | * @param user user name |
| 53 | * @param password password |
| 54 | * @param privateKey path of ssh private key |
| 55 | */ |
| 56 | private SshAccessInfo(IpAddress remoteIp, TpPort port, String user, String password, String privateKey) { |
| 57 | this.remoteIp = checkNotNull(remoteIp); |
| 58 | this.port = checkNotNull(port); |
| 59 | this.user = checkNotNull(user); |
| 60 | this.password = password; |
| 61 | this.privateKey = checkNotNull(privateKey); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Builds SshAccessInfo from json. |
| 66 | * @param root json root node for SshAccessinfo |
| 67 | * @return SSH access information |
| 68 | * @throws WorkflowException workflow exception |
| 69 | */ |
| 70 | public static SshAccessInfo valueOf(JsonNode root) throws WorkflowException { |
| 71 | |
| 72 | JsonNode node = root.at(ptr(REMOTE_IP)); |
| 73 | if (node == null || !(node instanceof TextNode)) { |
| 74 | throw new WorkflowException("invalid remoteIp for " + root); |
| 75 | } |
| 76 | IpAddress sshIp = IpAddress.valueOf(node.asText()); |
| 77 | |
| 78 | node = root.at(ptr(PORT)); |
| 79 | if (node == null || !(node instanceof NumericNode)) { |
| 80 | throw new WorkflowException("invalid port for " + root); |
| 81 | } |
| 82 | TpPort sshPort = TpPort.tpPort(node.asInt()); |
| 83 | |
| 84 | node = root.at(ptr(USER)); |
| 85 | if (node == null || !(node instanceof TextNode)) { |
| 86 | throw new WorkflowException("invalid user for " + root); |
| 87 | } |
| 88 | String sshUser = node.asText(); |
| 89 | |
| 90 | node = root.at(ptr(PASSWORD)); |
| 91 | if (node == null || !(node instanceof TextNode)) { |
| 92 | throw new WorkflowException("invalid password for " + root); |
| 93 | } |
| 94 | String sshPassword = node.asText(); |
| 95 | |
| 96 | node = root.at(ptr(KEYFILE)); |
| 97 | if (node == null || !(node instanceof TextNode)) { |
| 98 | throw new WorkflowException("invalid keyfile for " + root); |
| 99 | } |
| 100 | String sshKeyfile = node.asText(); |
| 101 | |
| 102 | return new SshAccessInfo(sshIp, sshPort, sshUser, sshPassword, sshKeyfile); |
| 103 | } |
| 104 | |
| 105 | private static String ptr(String field) { |
| 106 | return "/" + field; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns the remote IP address. |
| 111 | * |
| 112 | * @return ip address |
| 113 | */ |
| 114 | public IpAddress remoteIp() { |
| 115 | return this.remoteIp; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns the port number. |
| 120 | * |
| 121 | * @return ssh port |
| 122 | */ |
| 123 | public TpPort port() { |
| 124 | return this.port; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Returns the user name. |
| 129 | * |
| 130 | * @return user name |
| 131 | */ |
| 132 | public String user() { |
| 133 | return this.user; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Returns the password. |
| 138 | * @return password |
| 139 | */ |
| 140 | public String password() { |
| 141 | return this.password; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns the private key path. |
| 146 | * |
| 147 | * @return privateKey |
| 148 | */ |
| 149 | public String privateKey() { |
| 150 | return privateKey; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public boolean equals(Object obj) { |
| 155 | if (this == obj) { |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | if (obj instanceof SshAccessInfo) { |
| 160 | SshAccessInfo that = (SshAccessInfo) obj; |
| 161 | return Objects.equals(remoteIp, that.remoteIp) && |
| 162 | Objects.equals(port, that.port) && |
| 163 | Objects.equals(user, that.user) && |
| 164 | Objects.equals(privateKey, that.privateKey); |
| 165 | } |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public int hashCode() { |
| 171 | return Objects.hash(remoteIp, port, user, privateKey); |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public String toString() { |
| 176 | return MoreObjects.toStringHelper(getClass()) |
| 177 | .add("remoteIp", remoteIp) |
| 178 | .add("port", port) |
| 179 | .add("user", user) |
| 180 | .add("privateKey", privateKey) |
| 181 | .toString(); |
| 182 | } |
| 183 | } |