Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | package org.onosproject.segmentrouting.config; |
| 18 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.JsonNode; |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import com.google.common.collect.ImmutableMap; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 23 | import org.onlab.packet.Ip4Address; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 24 | import org.onlab.packet.Ip6Address; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onosproject.net.DeviceId; |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 27 | import org.onosproject.net.PortNumber; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 28 | import org.onosproject.net.config.Config; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 29 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 30 | import java.util.HashMap; |
| 31 | import java.util.HashSet; |
| 32 | import java.util.Map; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 33 | import java.util.Optional; |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 34 | import java.util.Set; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Configuration object for Segment Routing Application. |
| 38 | */ |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 39 | public class SegmentRoutingDeviceConfig extends Config<DeviceId> { |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 40 | private static final String NAME = "name"; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 41 | private static final String IP4 = "ipv4Loopback"; |
| 42 | private static final String IP6 = "ipv6Loopback"; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 43 | private static final String MAC = "routerMac"; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 44 | private static final String IP4_SID = "ipv4NodeSid"; |
| 45 | private static final String IP6_SID = "ipv6NodeSid"; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 46 | private static final String EDGE = "isEdgeRouter"; |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Adjancency SIDs config. |
| 49 | * |
| 50 | * @deprecated in Loon (1.11). We are not using and do not plan to use it. |
| 51 | */ |
| 52 | @Deprecated |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 53 | private static final String ADJSIDS = "adjacencySids"; |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Adjancency SID config. |
| 57 | * |
| 58 | * @deprecated in Loon (1.11). We are not using and do not plan to use it. |
| 59 | */ |
| 60 | @Deprecated |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 61 | private static final String ADJSID = "adjSid"; |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Adjancency port config. |
| 65 | * |
| 66 | * @deprecated in Loon (1.11). We are not using and do not plan to use it. |
| 67 | */ |
| 68 | @Deprecated |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 69 | private static final String PORTS = "ports"; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 70 | |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 71 | private static final String PAIR_DEVICE_ID = "pairDeviceId"; |
| 72 | private static final String PAIR_LOCAL_PORT = "pairLocalPort"; |
| 73 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 74 | @Override |
| 75 | public boolean isValid() { |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 76 | return hasOnlyFields(NAME, IP4, IP6, MAC, IP4_SID, IP6_SID, EDGE, ADJSIDS, ADJSID, PORTS, |
| 77 | PAIR_DEVICE_ID, PAIR_LOCAL_PORT) && |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 78 | name() != null && |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 79 | routerIpv4() != null && (!hasField(IP6) || routerIpv6() != null) && |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 80 | routerMac() != null && |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 81 | nodeSidIPv4() != -1 && (!hasField(IP6_SID) || nodeSidIPv6() != -1) && |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 82 | isEdgeRouter() != null && |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 83 | adjacencySids() != null && |
| 84 | // pairDeviceId and pairLocalPort must be both configured or both omitted |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 85 | (hasField(PAIR_DEVICE_ID) == hasField(PAIR_LOCAL_PORT)) && |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 86 | (!hasField(PAIR_DEVICE_ID) || pairDeviceId() != null) && |
| 87 | (!hasField(PAIR_LOCAL_PORT) || pairLocalPort() != null); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Gets the name of the router. |
| 92 | * |
| 93 | * @return Optional name of the router. May be empty if not configured. |
| 94 | */ |
| 95 | public Optional<String> name() { |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 96 | String name = get(NAME, null); |
| 97 | return name != null ? Optional.of(name) : Optional.empty(); |
| 98 | } |
| 99 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 100 | /** |
| 101 | * Sets the name of the router. |
| 102 | * |
| 103 | * @param name name of the router. |
| 104 | * @return the config of the router. |
| 105 | */ |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 106 | public SegmentRoutingDeviceConfig setName(String name) { |
| 107 | return (SegmentRoutingDeviceConfig) setOrClear(NAME, name); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 110 | /** |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 111 | * Gets the IPv4 address of the router. |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 112 | * |
| 113 | * @return IP address of the router. Or null if not configured. |
| 114 | */ |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 115 | public Ip4Address routerIpv4() { |
| 116 | String ip = get(IP4, null); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 117 | return ip != null ? Ip4Address.valueOf(ip) : null; |
| 118 | } |
| 119 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 120 | /** |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 121 | * Gets the IPv6 address of the router. |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 122 | * |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 123 | * @return IP address of the router. Or null if not configured. |
| 124 | */ |
| 125 | public Ip6Address routerIpv6() { |
| 126 | String ip = get(IP6, null); |
| 127 | return ip != null ? Ip6Address.valueOf(ip) : null; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Sets the IPv4 address of the router. |
| 132 | * |
| 133 | * @param ip IPv4 address of the router. |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 134 | * @return the config of the router. |
| 135 | */ |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 136 | public SegmentRoutingDeviceConfig setRouterIpv4(String ip) { |
| 137 | return (SegmentRoutingDeviceConfig) setOrClear(IP4, ip); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Sets the IPv6 address of the router. |
| 142 | * |
| 143 | * @param ip IPv6 address of the router. |
| 144 | * @return the config of the router. |
| 145 | */ |
| 146 | public SegmentRoutingDeviceConfig setRouterIpv6(String ip) { |
| 147 | return (SegmentRoutingDeviceConfig) setOrClear(IP6, ip); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 150 | /** |
| 151 | * Gets the MAC address of the router. |
| 152 | * |
| 153 | * @return MAC address of the router. Or null if not configured. |
| 154 | */ |
| 155 | public MacAddress routerMac() { |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 156 | String mac = get(MAC, null); |
| 157 | return mac != null ? MacAddress.valueOf(mac) : null; |
| 158 | } |
| 159 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 160 | /** |
| 161 | * Sets the MAC address of the router. |
| 162 | * |
| 163 | * @param mac MAC address of the router. |
| 164 | * @return the config of the router. |
| 165 | */ |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 166 | public SegmentRoutingDeviceConfig setRouterMac(String mac) { |
| 167 | return (SegmentRoutingDeviceConfig) setOrClear(MAC, mac); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 170 | /** |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 171 | * Gets the IPv4 node SID of the router. |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 172 | * |
| 173 | * @return node SID of the router. Or -1 if not configured. |
| 174 | */ |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 175 | public int nodeSidIPv4() { |
| 176 | return get(IP4_SID, -1); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 179 | /** |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 180 | * Gets the IPv6 node SID of the router. |
| 181 | * |
| 182 | * @return node SID of the router. Or -1 if not configured. |
| 183 | */ |
| 184 | public int nodeSidIPv6() { |
| 185 | return get(IP6_SID, -1); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Sets the node IPv4 node SID of the router. |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 190 | * |
| 191 | * @param sid node SID of the router. |
| 192 | * @return the config of the router. |
| 193 | */ |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 194 | public SegmentRoutingDeviceConfig setNodeSidIPv4(int sid) { |
| 195 | return (SegmentRoutingDeviceConfig) setOrClear(IP4_SID, sid); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Sets the node IPv6 node SID of the router. |
| 200 | * |
| 201 | * @param sid node SID of the router. |
| 202 | * @return the config of the router. |
| 203 | */ |
| 204 | public SegmentRoutingDeviceConfig setNodeSidIPv6(int sid) { |
| 205 | return (SegmentRoutingDeviceConfig) setOrClear(IP6_SID, sid); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 208 | /** |
| 209 | * Checks if the router is an edge router. |
| 210 | * |
| 211 | * @return true if the router is an edge router. |
| 212 | * false if the router is not an edge router. |
| 213 | * null if the value is not configured. |
| 214 | */ |
| 215 | public Boolean isEdgeRouter() { |
| 216 | String isEdgeRouter = get(EDGE, null); |
| 217 | return isEdgeRouter != null ? |
| 218 | Boolean.valueOf(isEdgeRouter) : |
| 219 | null; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 222 | /** |
| 223 | * Specifies if the router is an edge router. |
| 224 | * |
| 225 | * @param isEdgeRouter true if the router is an edge router. |
| 226 | * @return the config of the router. |
| 227 | */ |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 228 | public SegmentRoutingDeviceConfig setIsEdgeRouter(boolean isEdgeRouter) { |
| 229 | return (SegmentRoutingDeviceConfig) setOrClear(EDGE, isEdgeRouter); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 232 | /** |
| 233 | * Gets the adjacency SIDs of the router. |
| 234 | * |
| 235 | * @return adjacency SIDs of the router. Or null if not configured. |
| 236 | */ |
| 237 | public Map<Integer, Set<Integer>> adjacencySids() { |
| 238 | if (!object.has(ADJSIDS)) { |
| 239 | return null; |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 242 | Map<Integer, Set<Integer>> adjacencySids = new HashMap<>(); |
| 243 | ArrayNode adjacencySidsNode = (ArrayNode) object.path(ADJSIDS); |
| 244 | for (JsonNode adjacencySidNode : adjacencySidsNode) { |
| 245 | int asid = adjacencySidNode.path(ADJSID).asInt(-1); |
| 246 | if (asid == -1) { |
| 247 | return null; |
| 248 | } |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 249 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 250 | HashSet<Integer> ports = new HashSet<>(); |
| 251 | ArrayNode portsNode = (ArrayNode) adjacencySidNode.path(PORTS); |
| 252 | for (JsonNode portNode : portsNode) { |
| 253 | int port = portNode.asInt(-1); |
| 254 | if (port == -1) { |
| 255 | return null; |
| 256 | } |
| 257 | ports.add(port); |
| 258 | } |
| 259 | adjacencySids.put(asid, ports); |
| 260 | } |
| 261 | |
| 262 | return ImmutableMap.copyOf(adjacencySids); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Sets the adjacency SIDs of the router. |
| 267 | * |
| 268 | * @param adjacencySids adjacency SIDs of the router. |
| 269 | * @return the config of the router. |
| 270 | */ |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 271 | public SegmentRoutingDeviceConfig setAdjacencySids(Map<Integer, Set<Integer>> adjacencySids) { |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 272 | if (adjacencySids == null) { |
| 273 | object.remove(ADJSIDS); |
| 274 | } else { |
| 275 | ArrayNode adjacencySidsNode = mapper.createArrayNode(); |
| 276 | |
| 277 | adjacencySids.forEach((sid, ports) -> { |
| 278 | ObjectNode adjacencySidNode = mapper.createObjectNode(); |
| 279 | |
| 280 | adjacencySidNode.put(ADJSID, sid); |
| 281 | |
| 282 | ArrayNode portsNode = mapper.createArrayNode(); |
| 283 | ports.forEach(port -> { |
| 284 | portsNode.add(port.toString()); |
| 285 | }); |
| 286 | adjacencySidNode.set(PORTS, portsNode); |
| 287 | |
| 288 | adjacencySidsNode.add(adjacencySidNode); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 289 | }); |
| 290 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 291 | object.set(ADJSIDS, adjacencySidsNode); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 294 | return this; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 295 | } |
Charles Chan | cade1bf | 2017-06-07 17:26:01 -0700 | [diff] [blame] | 296 | |
| 297 | /** |
| 298 | * Gets the pair device id. |
| 299 | * |
| 300 | * @return pair device id; Or null if not configured. |
| 301 | */ |
| 302 | public DeviceId pairDeviceId() { |
| 303 | String pairDeviceId = get(PAIR_DEVICE_ID, null); |
| 304 | return pairDeviceId != null ? DeviceId.deviceId(pairDeviceId) : null; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Sets the pair device id. |
| 309 | * |
| 310 | * @param deviceId pair device id |
| 311 | * @return this configuration |
| 312 | */ |
| 313 | public SegmentRoutingDeviceConfig setPairDeviceId(DeviceId deviceId) { |
| 314 | return (SegmentRoutingDeviceConfig) setOrClear(PAIR_DEVICE_ID, deviceId.toString()); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Gets the pair local port. |
| 319 | * |
| 320 | * @return pair local port; Or null if not configured. |
| 321 | */ |
| 322 | public PortNumber pairLocalPort() { |
| 323 | long pairLocalPort = get(PAIR_LOCAL_PORT, -1L); |
| 324 | return pairLocalPort != -1L ? PortNumber.portNumber(pairLocalPort) : null; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Sets the pair local port. |
| 329 | * |
| 330 | * @param portNumber pair local port |
| 331 | * @return this configuration |
| 332 | */ |
| 333 | public SegmentRoutingDeviceConfig setPairLocalPort(PortNumber portNumber) { |
| 334 | return (SegmentRoutingDeviceConfig) setOrClear(PAIR_LOCAL_PORT, portNumber.toLong()); |
| 335 | } |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 336 | } |