blob: 2e3f3d2ce2859d9a695e0286c7ca6a2e82c2dd47 [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
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800100 nodes.add(newNode);
Hyunsun Moon133fd792016-02-09 01:55:48 -0800101 } catch (IllegalArgumentException | NullPointerException e) {
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800102 log.error("{}", e.toString());
Hyunsun Moon133fd792016-02-09 01:55:48 -0800103 }
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800104 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700105
Hyunsun Moon8539b042015-11-07 22:08:43 -0800106 return nodes;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700107 }
108
109 /**
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800110 * Returns value of a given path. If the path is missing, show log and return
111 * null.
112 *
113 * @param path path
114 * @return value or null
115 */
116 private String getConfig(JsonNode jsonNode, String path) {
117 jsonNode = jsonNode.path(path);
118
119 if (jsonNode.isMissingNode()) {
120 log.error("{} is not configured", path);
121 return null;
122 } else {
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800123 return jsonNode.asText();
124 }
125 }
126
127 /**
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800128 * Returns private network gateway MAC address.
Hyunsun Moon746956f2016-01-24 21:47:06 -0800129 *
130 * @return mac address, or null
131 */
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800132 public MacAddress privateGatewayMac() {
133 JsonNode jsonNode = object.get(PRIVATE_GATEWAY_MAC);
Hyunsun Moon746956f2016-01-24 21:47:06 -0800134 if (jsonNode == null) {
135 return null;
136 }
137
138 try {
139 return MacAddress.valueOf(jsonNode.asText());
140 } catch (IllegalArgumentException e) {
141 log.error("Wrong MAC address format {}", jsonNode.asText());
142 return null;
143 }
144 }
145
146 /**
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800147 * Returns public network gateway IP and MAC address pairs.
148 *
149 * @return map of ip and mac address
150 */
151 public Map<IpAddress, MacAddress> publicGateways() {
152 JsonNode jsonNodes = object.get(PUBLIC_GATEWAYS);
153 if (jsonNodes == null) {
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800154 return Maps.newHashMap();
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800155 }
156
157 Map<IpAddress, MacAddress> publicGateways = Maps.newHashMap();
158 jsonNodes.forEach(jsonNode -> {
159 try {
160 publicGateways.put(
161 IpAddress.valueOf(jsonNode.path(GATEWAY_IP).asText()),
162 MacAddress.valueOf(jsonNode.path(GATEWAY_MAC).asText()));
163 } catch (IllegalArgumentException | NullPointerException e) {
164 log.error("Wrong address format {}", e.toString());
165 }
166 });
167
168 return publicGateways;
169 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700170}
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800171