blob: 963019f3838817240bb06ad88baf1860919b6af0 [file] [log] [blame]
Hyunsun Moond0e932a2015-09-15 22:39:16 -07001/*
2 * Copyright 2014-2015 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 */
16package org.onosproject.cordvtn;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.google.common.collect.Sets;
Hyunsun Moon746956f2016-01-24 21:47:06 -080020import org.onlab.packet.MacAddress;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070021import org.onlab.packet.TpPort;
22import org.onosproject.core.ApplicationId;
Hyunsun Moon1f145552015-10-08 22:25:30 -070023import org.onosproject.net.DeviceId;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070024import org.onosproject.net.config.Config;
Hyunsun Moon746956f2016-01-24 21:47:06 -080025import org.slf4j.Logger;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070026
27import java.util.Set;
28
29import static com.google.common.base.Preconditions.checkNotNull;
Hyunsun Moon746956f2016-01-24 21:47:06 -080030import static org.slf4j.LoggerFactory.getLogger;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070031
32/**
Hyunsun Moon2b530322015-09-23 13:24:35 -070033 * Configuration object for CordVtn service.
Hyunsun Moond0e932a2015-09-15 22:39:16 -070034 */
35public class CordVtnConfig extends Config<ApplicationId> {
36
Hyunsun Moon746956f2016-01-24 21:47:06 -080037 protected final Logger log = getLogger(getClass());
38
Hyunsun Moon746956f2016-01-24 21:47:06 -080039 public static final String GATEWAY_MAC = "gatewayMac";
Hyunsun Moon133fd792016-02-09 01:55:48 -080040 public static final String LOCAL_MANAGEMENT_IP = "localManagementIp";
41 public static final String OVSDB_PORT = "ovsdbPort";
42 public static final String SSH_PORT = "sshPort";
43 public static final String SSH_USER = "sshUser";
44 public static final String SSH_KEY_FILE = "sshKeyFile";
45 public static final String CORDVTN_NODES = "nodes";
46
47 public static final String HOSTNAME = "hostname";
48 public static final String HOST_MANAGEMENT_IP = "hostManagementIp";
49 public static final String DATA_PLANE_IP = "dataPlaneIp";
50 public static final String DATA_PLANE_INTF = "dataPlaneIntf";
51 public static final String BRIDGE_ID = "bridgeId";
Hyunsun Moond0e932a2015-09-15 22:39:16 -070052
53 /**
Hyunsun Moon8539b042015-11-07 22:08:43 -080054 * Returns the set of nodes read from network config.
Hyunsun Moond0e932a2015-09-15 22:39:16 -070055 *
Hyunsun Moon8539b042015-11-07 22:08:43 -080056 * @return set of CordVtnNodeConfig or null
Hyunsun Moond0e932a2015-09-15 22:39:16 -070057 */
Hyunsun Moon8539b042015-11-07 22:08:43 -080058 public Set<CordVtnNodeConfig> cordVtnNodes() {
59 Set<CordVtnNodeConfig> nodes = Sets.newHashSet();
Hyunsun Moond0e932a2015-09-15 22:39:16 -070060
Hyunsun Moon8539b042015-11-07 22:08:43 -080061 JsonNode jsonNodes = object.get(CORDVTN_NODES);
62 if (jsonNodes == null) {
Hyunsun Moond0e932a2015-09-15 22:39:16 -070063 return null;
64 }
Hyunsun Moon133fd792016-02-09 01:55:48 -080065
66 jsonNodes.forEach(jsonNode -> {
67 try {
68 nodes.add(new CordVtnNodeConfig(
69 jsonNode.path(HOSTNAME).asText(),
70 NetworkAddress.valueOf(jsonNode.path(HOST_MANAGEMENT_IP).asText()),
71 NetworkAddress.valueOf(jsonNode.path(DATA_PLANE_IP).asText()),
72 jsonNode.path(DATA_PLANE_INTF).asText(),
73 DeviceId.deviceId(jsonNode.path(BRIDGE_ID).asText())));
74 } catch (IllegalArgumentException | NullPointerException e) {
75 log.error("Failed to read {}", e.toString());
76 }
77 });
Hyunsun Moond0e932a2015-09-15 22:39:16 -070078
Hyunsun Moon8539b042015-11-07 22:08:43 -080079 return nodes;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070080 }
81
82 /**
Hyunsun Moon746956f2016-01-24 21:47:06 -080083 * Returns gateway MAC address.
84 *
85 * @return mac address, or null
86 */
87 public MacAddress gatewayMac() {
88 JsonNode jsonNode = object.get(GATEWAY_MAC);
89 if (jsonNode == null) {
90 return null;
91 }
92
93 try {
94 return MacAddress.valueOf(jsonNode.asText());
95 } catch (IllegalArgumentException e) {
96 log.error("Wrong MAC address format {}", jsonNode.asText());
97 return null;
98 }
99 }
100
101 /**
Hyunsun Moon133fd792016-02-09 01:55:48 -0800102 * Returns local management network address.
103 *
104 * @return network address
105 */
106 public NetworkAddress localMgmtIp() {
107 JsonNode jsonNode = object.get(LOCAL_MANAGEMENT_IP);
108 if (jsonNode == null) {
109 return null;
110 }
111
112 try {
113 return NetworkAddress.valueOf(jsonNode.asText());
114 } catch (IllegalArgumentException e) {
115 log.error("Wrong address format {}", jsonNode.asText());
116 return null;
117 }
118 }
119
120 /**
121 * Returns the port number used for OVSDB connection.
122 *
123 * @return port number, or null
124 */
125 public TpPort ovsdbPort() {
126 JsonNode jsonNode = object.get(OVSDB_PORT);
127 if (jsonNode == null) {
128 return null;
129 }
130
131 try {
132 return TpPort.tpPort(jsonNode.asInt());
133 } catch (IllegalArgumentException e) {
134 log.error("Wrong TCP port format {}", jsonNode.asText());
135 return null;
136 }
137 }
138
139 /**
140 * Returns the port number used for SSH connection.
141 *
142 * @return port number, or null
143 */
144 public TpPort sshPort() {
145 JsonNode jsonNode = object.get(SSH_PORT);
146 if (jsonNode == null) {
147 return null;
148 }
149
150 try {
151 return TpPort.tpPort(jsonNode.asInt());
152 } catch (IllegalArgumentException e) {
153 log.error("Wrong TCP port format {}", jsonNode.asText());
154 return null;
155 }
156 }
157
158 /**
159 * Returns the user name for SSH connection.
160 *
161 * @return user name, or null
162 */
163 public String sshUser() {
164 JsonNode jsonNode = object.get(SSH_USER);
165 if (jsonNode == null) {
166 return null;
167 }
168
169 return jsonNode.asText();
170 }
171
172 /**
173 * Returns the private key file for SSH connection.
174 *
175 * @return file path, or null
176 */
177 public String sshKeyFile() {
178 JsonNode jsonNode = object.get(SSH_KEY_FILE);
179 if (jsonNode == null) {
180 return null;
181 }
182
183 return jsonNode.asText();
184 }
185
186 /**
Hyunsun Moon8539b042015-11-07 22:08:43 -0800187 * Configuration for CordVtn node.
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700188 */
Hyunsun Moon8539b042015-11-07 22:08:43 -0800189 public static class CordVtnNodeConfig {
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700190
Hyunsun Moon8539b042015-11-07 22:08:43 -0800191 private final String hostname;
Hyunsun Moon133fd792016-02-09 01:55:48 -0800192 private final NetworkAddress hostMgmtIp;
193 private final NetworkAddress dpIp;
194 private final String dpIntf;
Hyunsun Moon1f145552015-10-08 22:25:30 -0700195 private final DeviceId bridgeId;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700196
Hyunsun Moon133fd792016-02-09 01:55:48 -0800197 public CordVtnNodeConfig(String hostname, NetworkAddress hostMgmtIp, NetworkAddress dpIp,
198 String dpIntf, DeviceId bridgeId) {
Hyunsun Moon8539b042015-11-07 22:08:43 -0800199 this.hostname = checkNotNull(hostname);
Hyunsun Moon133fd792016-02-09 01:55:48 -0800200 this.hostMgmtIp = checkNotNull(hostMgmtIp);
201 this.dpIp = checkNotNull(dpIp);
202 this.dpIntf = checkNotNull(dpIntf);
Hyunsun Moon1f145552015-10-08 22:25:30 -0700203 this.bridgeId = checkNotNull(bridgeId);
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700204 }
205
206 /**
Hyunsun Moon8539b042015-11-07 22:08:43 -0800207 * Returns hostname of the node.
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700208 *
Hyunsun Moon8539b042015-11-07 22:08:43 -0800209 * @return hostname
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700210 */
Hyunsun Moon8539b042015-11-07 22:08:43 -0800211 public String hostname() {
212 return this.hostname;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700213 }
214
215 /**
Hyunsun Moon133fd792016-02-09 01:55:48 -0800216 * Returns the host management network address of the node.
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700217 *
Hyunsun Moon133fd792016-02-09 01:55:48 -0800218 * @return management network address
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700219 */
Hyunsun Moon133fd792016-02-09 01:55:48 -0800220 public NetworkAddress hostMgmtIp() {
221 return this.hostMgmtIp;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700222 }
223
224 /**
Hyunsun Moon133fd792016-02-09 01:55:48 -0800225 * Returns the data plane network address.
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700226 *
Hyunsun Moon133fd792016-02-09 01:55:48 -0800227 * @return network address
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700228 */
Hyunsun Moon133fd792016-02-09 01:55:48 -0800229 public NetworkAddress dpIp() {
230 return this.dpIp;
231 }
232
233 /**
234 * Returns the data plane interface name.
235 *
236 * @return interface name
237 */
238 public String dpIntf() {
239 return this.dpIntf;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700240 }
Hyunsun Moon1f145552015-10-08 22:25:30 -0700241
Hyunsun Moon8539b042015-11-07 22:08:43 -0800242 /**
243 * Returns integration bridge id of the node.
244 *
245 * @return device id
246 */
Hyunsun Moon1f145552015-10-08 22:25:30 -0700247 public DeviceId bridgeId() {
248 return this.bridgeId;
249 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700250 }
251}