blob: ec8343246b534fcbb22a079efb8c37842889c0a4 [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;
Hyunsun Moonae39ae82016-02-17 15:02:06 -080019import com.google.common.collect.Maps;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070020import com.google.common.collect.Sets;
Hyunsun Moonae39ae82016-02-17 15:02:06 -080021import org.onlab.packet.IpAddress;
Hyunsun Moon746956f2016-01-24 21:47:06 -080022import org.onlab.packet.MacAddress;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070023import org.onlab.packet.TpPort;
24import org.onosproject.core.ApplicationId;
Hyunsun Moon1f145552015-10-08 22:25:30 -070025import org.onosproject.net.DeviceId;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070026import org.onosproject.net.config.Config;
Hyunsun Moon746956f2016-01-24 21:47:06 -080027import org.slf4j.Logger;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070028
Hyunsun Moonae39ae82016-02-17 15:02:06 -080029import java.util.Map;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070030import java.util.Set;
31
32import static com.google.common.base.Preconditions.checkNotNull;
Hyunsun Moon746956f2016-01-24 21:47:06 -080033import static org.slf4j.LoggerFactory.getLogger;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070034
35/**
Hyunsun Moon2b530322015-09-23 13:24:35 -070036 * Configuration object for CordVtn service.
Hyunsun Moond0e932a2015-09-15 22:39:16 -070037 */
38public class CordVtnConfig extends Config<ApplicationId> {
39
Hyunsun Moon746956f2016-01-24 21:47:06 -080040 protected final Logger log = getLogger(getClass());
41
Hyunsun Moonae39ae82016-02-17 15:02:06 -080042 public static final String PRIVATE_GATEWAY_MAC = "privateGatewayMac";
43 public static final String PUBLIC_GATEWAYS = "publicGateways";
44 public static final String GATEWAY_IP = "gatewayIp";
Hyunsun Moon746956f2016-01-24 21:47:06 -080045 public static final String GATEWAY_MAC = "gatewayMac";
Hyunsun Moon133fd792016-02-09 01:55:48 -080046 public static final String LOCAL_MANAGEMENT_IP = "localManagementIp";
47 public static final String OVSDB_PORT = "ovsdbPort";
48 public static final String SSH_PORT = "sshPort";
49 public static final String SSH_USER = "sshUser";
50 public static final String SSH_KEY_FILE = "sshKeyFile";
51 public static final String CORDVTN_NODES = "nodes";
52
53 public static final String HOSTNAME = "hostname";
54 public static final String HOST_MANAGEMENT_IP = "hostManagementIp";
55 public static final String DATA_PLANE_IP = "dataPlaneIp";
56 public static final String DATA_PLANE_INTF = "dataPlaneIntf";
57 public static final String BRIDGE_ID = "bridgeId";
Hyunsun Moond0e932a2015-09-15 22:39:16 -070058
59 /**
Hyunsun Moon8539b042015-11-07 22:08:43 -080060 * Returns the set of nodes read from network config.
Hyunsun Moond0e932a2015-09-15 22:39:16 -070061 *
Hyunsun Moon8539b042015-11-07 22:08:43 -080062 * @return set of CordVtnNodeConfig or null
Hyunsun Moond0e932a2015-09-15 22:39:16 -070063 */
Hyunsun Moon8539b042015-11-07 22:08:43 -080064 public Set<CordVtnNodeConfig> cordVtnNodes() {
65 Set<CordVtnNodeConfig> nodes = Sets.newHashSet();
Hyunsun Moond0e932a2015-09-15 22:39:16 -070066
Hyunsun Moon8539b042015-11-07 22:08:43 -080067 JsonNode jsonNodes = object.get(CORDVTN_NODES);
68 if (jsonNodes == null) {
Hyunsun Moond0e932a2015-09-15 22:39:16 -070069 return null;
70 }
Hyunsun Moon133fd792016-02-09 01:55:48 -080071
72 jsonNodes.forEach(jsonNode -> {
73 try {
74 nodes.add(new CordVtnNodeConfig(
75 jsonNode.path(HOSTNAME).asText(),
76 NetworkAddress.valueOf(jsonNode.path(HOST_MANAGEMENT_IP).asText()),
77 NetworkAddress.valueOf(jsonNode.path(DATA_PLANE_IP).asText()),
78 jsonNode.path(DATA_PLANE_INTF).asText(),
79 DeviceId.deviceId(jsonNode.path(BRIDGE_ID).asText())));
80 } catch (IllegalArgumentException | NullPointerException e) {
81 log.error("Failed to read {}", e.toString());
82 }
83 });
Hyunsun Moond0e932a2015-09-15 22:39:16 -070084
Hyunsun Moon8539b042015-11-07 22:08:43 -080085 return nodes;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070086 }
87
88 /**
Hyunsun Moonae39ae82016-02-17 15:02:06 -080089 * Returns private network gateway MAC address.
Hyunsun Moon746956f2016-01-24 21:47:06 -080090 *
91 * @return mac address, or null
92 */
Hyunsun Moonae39ae82016-02-17 15:02:06 -080093 public MacAddress privateGatewayMac() {
94 JsonNode jsonNode = object.get(PRIVATE_GATEWAY_MAC);
Hyunsun Moon746956f2016-01-24 21:47:06 -080095 if (jsonNode == null) {
96 return null;
97 }
98
99 try {
100 return MacAddress.valueOf(jsonNode.asText());
101 } catch (IllegalArgumentException e) {
102 log.error("Wrong MAC address format {}", jsonNode.asText());
103 return null;
104 }
105 }
106
107 /**
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800108 * Returns public network gateway IP and MAC address pairs.
109 *
110 * @return map of ip and mac address
111 */
112 public Map<IpAddress, MacAddress> publicGateways() {
113 JsonNode jsonNodes = object.get(PUBLIC_GATEWAYS);
114 if (jsonNodes == null) {
115 return null;
116 }
117
118 Map<IpAddress, MacAddress> publicGateways = Maps.newHashMap();
119 jsonNodes.forEach(jsonNode -> {
120 try {
121 publicGateways.put(
122 IpAddress.valueOf(jsonNode.path(GATEWAY_IP).asText()),
123 MacAddress.valueOf(jsonNode.path(GATEWAY_MAC).asText()));
124 } catch (IllegalArgumentException | NullPointerException e) {
125 log.error("Wrong address format {}", e.toString());
126 }
127 });
128
129 return publicGateways;
130 }
131
132 /**
Hyunsun Moon133fd792016-02-09 01:55:48 -0800133 * Returns local management network address.
134 *
135 * @return network address
136 */
137 public NetworkAddress localMgmtIp() {
138 JsonNode jsonNode = object.get(LOCAL_MANAGEMENT_IP);
139 if (jsonNode == null) {
140 return null;
141 }
142
143 try {
144 return NetworkAddress.valueOf(jsonNode.asText());
145 } catch (IllegalArgumentException e) {
146 log.error("Wrong address format {}", jsonNode.asText());
147 return null;
148 }
149 }
150
151 /**
152 * Returns the port number used for OVSDB connection.
153 *
154 * @return port number, or null
155 */
156 public TpPort ovsdbPort() {
157 JsonNode jsonNode = object.get(OVSDB_PORT);
158 if (jsonNode == null) {
159 return null;
160 }
161
162 try {
163 return TpPort.tpPort(jsonNode.asInt());
164 } catch (IllegalArgumentException e) {
165 log.error("Wrong TCP port format {}", jsonNode.asText());
166 return null;
167 }
168 }
169
170 /**
171 * Returns the port number used for SSH connection.
172 *
173 * @return port number, or null
174 */
175 public TpPort sshPort() {
176 JsonNode jsonNode = object.get(SSH_PORT);
177 if (jsonNode == null) {
178 return null;
179 }
180
181 try {
182 return TpPort.tpPort(jsonNode.asInt());
183 } catch (IllegalArgumentException e) {
184 log.error("Wrong TCP port format {}", jsonNode.asText());
185 return null;
186 }
187 }
188
189 /**
190 * Returns the user name for SSH connection.
191 *
192 * @return user name, or null
193 */
194 public String sshUser() {
195 JsonNode jsonNode = object.get(SSH_USER);
196 if (jsonNode == null) {
197 return null;
198 }
199
200 return jsonNode.asText();
201 }
202
203 /**
204 * Returns the private key file for SSH connection.
205 *
206 * @return file path, or null
207 */
208 public String sshKeyFile() {
209 JsonNode jsonNode = object.get(SSH_KEY_FILE);
210 if (jsonNode == null) {
211 return null;
212 }
213
214 return jsonNode.asText();
215 }
216
217 /**
Hyunsun Moon8539b042015-11-07 22:08:43 -0800218 * Configuration for CordVtn node.
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700219 */
Hyunsun Moon8539b042015-11-07 22:08:43 -0800220 public static class CordVtnNodeConfig {
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700221
Hyunsun Moon8539b042015-11-07 22:08:43 -0800222 private final String hostname;
Hyunsun Moon133fd792016-02-09 01:55:48 -0800223 private final NetworkAddress hostMgmtIp;
224 private final NetworkAddress dpIp;
225 private final String dpIntf;
Hyunsun Moon1f145552015-10-08 22:25:30 -0700226 private final DeviceId bridgeId;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700227
Hyunsun Moon133fd792016-02-09 01:55:48 -0800228 public CordVtnNodeConfig(String hostname, NetworkAddress hostMgmtIp, NetworkAddress dpIp,
229 String dpIntf, DeviceId bridgeId) {
Hyunsun Moon8539b042015-11-07 22:08:43 -0800230 this.hostname = checkNotNull(hostname);
Hyunsun Moon133fd792016-02-09 01:55:48 -0800231 this.hostMgmtIp = checkNotNull(hostMgmtIp);
232 this.dpIp = checkNotNull(dpIp);
233 this.dpIntf = checkNotNull(dpIntf);
Hyunsun Moon1f145552015-10-08 22:25:30 -0700234 this.bridgeId = checkNotNull(bridgeId);
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700235 }
236
237 /**
Hyunsun Moon8539b042015-11-07 22:08:43 -0800238 * Returns hostname of the node.
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700239 *
Hyunsun Moon8539b042015-11-07 22:08:43 -0800240 * @return hostname
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700241 */
Hyunsun Moon8539b042015-11-07 22:08:43 -0800242 public String hostname() {
243 return this.hostname;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700244 }
245
246 /**
Hyunsun Moon133fd792016-02-09 01:55:48 -0800247 * Returns the host management network address of the node.
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700248 *
Hyunsun Moon133fd792016-02-09 01:55:48 -0800249 * @return management network address
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700250 */
Hyunsun Moon133fd792016-02-09 01:55:48 -0800251 public NetworkAddress hostMgmtIp() {
252 return this.hostMgmtIp;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700253 }
254
255 /**
Hyunsun Moon133fd792016-02-09 01:55:48 -0800256 * Returns the data plane network address.
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700257 *
Hyunsun Moon133fd792016-02-09 01:55:48 -0800258 * @return network address
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700259 */
Hyunsun Moon133fd792016-02-09 01:55:48 -0800260 public NetworkAddress dpIp() {
261 return this.dpIp;
262 }
263
264 /**
265 * Returns the data plane interface name.
266 *
267 * @return interface name
268 */
269 public String dpIntf() {
270 return this.dpIntf;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700271 }
Hyunsun Moon1f145552015-10-08 22:25:30 -0700272
Hyunsun Moon8539b042015-11-07 22:08:43 -0800273 /**
274 * Returns integration bridge id of the node.
275 *
276 * @return device id
277 */
Hyunsun Moon1f145552015-10-08 22:25:30 -0700278 public DeviceId bridgeId() {
279 return this.bridgeId;
280 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700281 }
282}