blob: 1b29a5e252127ed205fc4812c94402159b81955f [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 Moon32f3b8e2016-03-02 19:27:26 -080021import org.onlab.packet.Ip4Address;
Hyunsun Moonae39ae82016-02-17 15:02:06 -080022import org.onlab.packet.IpAddress;
Hyunsun Moon746956f2016-01-24 21:47:06 -080023import org.onlab.packet.MacAddress;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070024import org.onlab.packet.TpPort;
25import org.onosproject.core.ApplicationId;
Hyunsun Moon1f145552015-10-08 22:25:30 -070026import org.onosproject.net.DeviceId;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070027import org.onosproject.net.config.Config;
Hyunsun Moon746956f2016-01-24 21:47:06 -080028import org.slf4j.Logger;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070029
Hyunsun Moonae39ae82016-02-17 15:02:06 -080030import java.util.Map;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070031import java.util.Set;
32
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";
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080048
Hyunsun Moon133fd792016-02-09 01:55:48 -080049 public static final String SSH_PORT = "sshPort";
50 public static final String SSH_USER = "sshUser";
51 public static final String SSH_KEY_FILE = "sshKeyFile";
Hyunsun Moon133fd792016-02-09 01:55:48 -080052
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080053 public static final String CORDVTN_NODES = "nodes";
Hyunsun Moon133fd792016-02-09 01:55:48 -080054 public static final String HOSTNAME = "hostname";
55 public static final String HOST_MANAGEMENT_IP = "hostManagementIp";
56 public static final String DATA_PLANE_IP = "dataPlaneIp";
57 public static final String DATA_PLANE_INTF = "dataPlaneIntf";
58 public static final String BRIDGE_ID = "bridgeId";
Hyunsun Moond0e932a2015-09-15 22:39:16 -070059
60 /**
Hyunsun Moon8539b042015-11-07 22:08:43 -080061 * Returns the set of nodes read from network config.
Hyunsun Moond0e932a2015-09-15 22:39:16 -070062 *
Hyunsun Moonaf520d32016-03-07 16:37:17 -080063 * @return set of CordVtnNodeConfig or empty set
Hyunsun Moond0e932a2015-09-15 22:39:16 -070064 */
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080065 public Set<CordVtnNode> cordVtnNodes() {
Hyunsun Moond0e932a2015-09-15 22:39:16 -070066
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080067 Set<CordVtnNode> nodes = Sets.newHashSet();
Hyunsun Moon8539b042015-11-07 22:08:43 -080068 JsonNode jsonNodes = object.get(CORDVTN_NODES);
69 if (jsonNodes == null) {
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080070 log.debug("No CORD VTN nodes found");
Hyunsun Moonaf520d32016-03-07 16:37:17 -080071 return nodes;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070072 }
Hyunsun Moon133fd792016-02-09 01:55:48 -080073
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080074 for (JsonNode jsonNode : jsonNodes) {
Hyunsun Moon133fd792016-02-09 01:55:48 -080075 try {
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080076 NetworkAddress hostMgmt = NetworkAddress.valueOf(getConfig(jsonNode, HOST_MANAGEMENT_IP));
77 NetworkAddress localMgmt = NetworkAddress.valueOf(getConfig(object, LOCAL_MANAGEMENT_IP));
78 if (hostMgmt.prefix().contains(localMgmt.prefix()) ||
79 localMgmt.prefix().contains(hostMgmt.prefix())) {
80 log.error("hostMamt and localMgmt cannot be overlapped, skip this node");
81 continue;
82 }
83
84 Ip4Address hostMgmtIp = hostMgmt.ip().getIp4Address();
85 SshAccessInfo sshInfo = new SshAccessInfo(
86 hostMgmtIp,
87 TpPort.tpPort(Integer.parseInt(getConfig(object, SSH_PORT))),
88 getConfig(object, SSH_USER), getConfig(object, SSH_KEY_FILE));
89
90 String hostname = getConfig(jsonNode, HOSTNAME);
91 CordVtnNode newNode = new CordVtnNode(
92 hostname, hostMgmt, localMgmt,
93 NetworkAddress.valueOf(getConfig(jsonNode, DATA_PLANE_IP)),
94 TpPort.tpPort(Integer.parseInt(getConfig(object, OVSDB_PORT))),
95 sshInfo,
96 DeviceId.deviceId(getConfig(jsonNode, BRIDGE_ID)),
Hyunsun Moonaf520d32016-03-07 16:37:17 -080097 getConfig(jsonNode, DATA_PLANE_INTF),
98 CordVtnNodeState.noState());
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080099
100 log.info("Successfully read {} from the config", hostname);
101 nodes.add(newNode);
Hyunsun Moon133fd792016-02-09 01:55:48 -0800102 } catch (IllegalArgumentException | NullPointerException e) {
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800103 log.error("{}", e.toString());
Hyunsun Moon133fd792016-02-09 01:55:48 -0800104 }
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800105 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700106
Hyunsun Moon8539b042015-11-07 22:08:43 -0800107 return nodes;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700108 }
109
110 /**
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800111 * Returns value of a given path. If the path is missing, show log and return
112 * null.
113 *
114 * @param path path
115 * @return value or null
116 */
117 private String getConfig(JsonNode jsonNode, String path) {
118 jsonNode = jsonNode.path(path);
119
120 if (jsonNode.isMissingNode()) {
121 log.error("{} is not configured", path);
122 return null;
123 } else {
124 log.debug("{} : {}", path, jsonNode.asText());
125 return jsonNode.asText();
126 }
127 }
128
129 /**
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800130 * Returns private network gateway MAC address.
Hyunsun Moon746956f2016-01-24 21:47:06 -0800131 *
132 * @return mac address, or null
133 */
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800134 public MacAddress privateGatewayMac() {
135 JsonNode jsonNode = object.get(PRIVATE_GATEWAY_MAC);
Hyunsun Moon746956f2016-01-24 21:47:06 -0800136 if (jsonNode == null) {
137 return null;
138 }
139
140 try {
141 return MacAddress.valueOf(jsonNode.asText());
142 } catch (IllegalArgumentException e) {
143 log.error("Wrong MAC address format {}", jsonNode.asText());
144 return null;
145 }
146 }
147
148 /**
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800149 * Returns public network gateway IP and MAC address pairs.
150 *
151 * @return map of ip and mac address
152 */
153 public Map<IpAddress, MacAddress> publicGateways() {
154 JsonNode jsonNodes = object.get(PUBLIC_GATEWAYS);
155 if (jsonNodes == null) {
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800156 return Maps.newHashMap();
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800157 }
158
159 Map<IpAddress, MacAddress> publicGateways = Maps.newHashMap();
160 jsonNodes.forEach(jsonNode -> {
161 try {
162 publicGateways.put(
163 IpAddress.valueOf(jsonNode.path(GATEWAY_IP).asText()),
164 MacAddress.valueOf(jsonNode.path(GATEWAY_MAC).asText()));
165 } catch (IllegalArgumentException | NullPointerException e) {
166 log.error("Wrong address format {}", e.toString());
167 }
168 });
169
170 return publicGateways;
171 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700172}
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800173