blob: 8e428e31582bbc798eb3a802a18fa86d171af760 [file] [log] [blame]
jaegonkimdcf7c822019-02-06 15:00:14 +09001/*
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 */
16package org.onosproject.workflow.model.accessinfo;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.NumericNode;
20import com.fasterxml.jackson.databind.node.TextNode;
21import com.google.common.base.MoreObjects;
22import org.onlab.packet.IpAddress;
23import org.onlab.packet.TpPort;
24import org.onosproject.workflow.api.WorkflowException;
25
26import java.util.Objects;
27
28import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * Class for NETCONF access information.
32 */
33public final class NetconfAccessInfo {
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 final String password;
44
45 /**
46 * Constructor for a given NETCONF 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 public NetconfAccessInfo(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 NetconfAccessInfo from json.
65 * @param root json root node for NetconfAccessinfo
66 * @return NETCONF access information
67 * @throws WorkflowException workflow exception
68 */
69 public static NetconfAccessInfo 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 NetconfAccessInfo(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 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 * @return password
132 */
133 public String password() {
134 return this.password;
135 }
136
137 @Override
138 public boolean equals(Object obj) {
139 if (this == obj) {
140 return true;
141 }
142
143 if (obj instanceof NetconfAccessInfo) {
144 NetconfAccessInfo that = (NetconfAccessInfo) obj;
145 return Objects.equals(remoteIp, that.remoteIp) &&
146 Objects.equals(port, that.port) &&
147 Objects.equals(user, that.user);
148 }
149 return false;
150 }
151
152 @Override
153 public int hashCode() {
154 return Objects.hash(remoteIp, port, user);
155 }
156
157 @Override
158 public String toString() {
159 return MoreObjects.toStringHelper(getClass())
160 .add("remoteIp", remoteIp)
161 .add("port", port)
162 .add("user", user)
163 .toString();
164 }
165}