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 REST access information. |
| 32 | */ |
| 33 | public final class RestAccessInfo { |
| 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 | |
| 40 | private final IpAddress remoteIp; |
| 41 | private final TpPort port; |
| 42 | private final String user; |
| 43 | private String password; |
| 44 | |
| 45 | /** |
| 46 | * Creates a new REST access information. |
| 47 | * |
| 48 | * @param remoteIp remote ip address |
| 49 | * @param port port number |
| 50 | * @param user user name |
| 51 | * @param password password |
| 52 | */ |
| 53 | private RestAccessInfo(IpAddress remoteIp, |
| 54 | TpPort port, |
| 55 | String user, |
| 56 | String password) { |
| 57 | this.remoteIp = checkNotNull(remoteIp); |
| 58 | this.port = checkNotNull(port); |
| 59 | this.user = checkNotNull(user); |
| 60 | this.password = checkNotNull(password); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Builds RestAccessInfo from json. |
| 65 | * @param root json root node for RestAccessinfo |
| 66 | * @return REST access information |
| 67 | * @throws WorkflowException workflow exception |
| 68 | */ |
| 69 | public static RestAccessInfo valueOf(JsonNode root) throws WorkflowException { |
| 70 | |
| 71 | JsonNode node = root.at(ptr(REMOTE_IP)); |
| 72 | if (node == null || !(node instanceof TextNode)) { |
| 73 | throw new WorkflowException("invalid remoteIp for " + root); |
| 74 | } |
| 75 | IpAddress remoteIp = IpAddress.valueOf(node.asText()); |
| 76 | |
| 77 | node = root.at(ptr(PORT)); |
| 78 | if (node == null || !(node instanceof NumericNode)) { |
| 79 | throw new WorkflowException("invalid port for " + root); |
| 80 | } |
| 81 | TpPort tpPort = TpPort.tpPort(node.asInt()); |
| 82 | |
| 83 | node = root.at(ptr(USER)); |
| 84 | if (node == null || !(node instanceof TextNode)) { |
| 85 | throw new WorkflowException("invalid user for " + root); |
| 86 | } |
| 87 | String strUser = node.asText(); |
| 88 | |
| 89 | node = root.at(ptr(PASSWORD)); |
| 90 | if (node == null || !(node instanceof TextNode)) { |
| 91 | throw new WorkflowException("invalid password for " + root); |
| 92 | } |
| 93 | String strPassword = node.asText(); |
| 94 | |
| 95 | return new RestAccessInfo(remoteIp, tpPort, strUser, strPassword); |
| 96 | } |
| 97 | |
| 98 | private static String ptr(String field) { |
| 99 | return "/" + field; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns the remote IP address. |
| 104 | * |
| 105 | * @return ip address |
| 106 | */ |
| 107 | public IpAddress remoteIp() { |
| 108 | return this.remoteIp; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns the port number. |
| 113 | * |
| 114 | * @return REST port |
| 115 | */ |
| 116 | public TpPort port() { |
| 117 | return this.port; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns the user name. |
| 122 | * |
| 123 | * @return user name |
| 124 | */ |
| 125 | public String user() { |
| 126 | return this.user; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns the password. |
| 131 | * |
| 132 | * @return password |
| 133 | */ |
| 134 | public String password() { |
| 135 | return this.password; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | @Override |
| 140 | public boolean equals(Object obj) { |
| 141 | if (this == obj) { |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | if (obj instanceof RestAccessInfo) { |
| 146 | RestAccessInfo that = (RestAccessInfo) obj; |
| 147 | return Objects.equals(remoteIp, that.remoteIp) && |
| 148 | Objects.equals(port, that.port) && |
| 149 | Objects.equals(user, that.user); |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public int hashCode() { |
| 156 | return Objects.hash(remoteIp, port, user); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public String toString() { |
| 161 | return MoreObjects.toStringHelper(getClass()) |
| 162 | .add("remoteIp", remoteIp) |
| 163 | .add("port", port) |
| 164 | .add("user", user) |
| 165 | .toString(); |
| 166 | } |
| 167 | } |