blob: 7959df684ea8cc05daff89fb3060617a5fd890c7 [file] [log] [blame]
Charles Chan72f556a2015-10-05 17:50:33 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Charles Chan72f556a2015-10-05 17:50:33 -07003 *
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
17package org.onosproject.segmentrouting.config;
18
Charles Chan9bec8a32015-12-01 10:00:51 -080019import com.fasterxml.jackson.databind.JsonNode;
Charles Chane7c61022015-10-07 14:21:45 -070020import com.fasterxml.jackson.databind.node.ArrayNode;
Charles Chan9bec8a32015-12-01 10:00:51 -080021import com.fasterxml.jackson.databind.node.ObjectNode;
22import com.google.common.collect.ImmutableMap;
Charles Chan72f556a2015-10-05 17:50:33 -070023import org.onlab.packet.Ip4Address;
Pier Ventreadb4ae62016-11-23 09:57:42 -080024import org.onlab.packet.Ip6Address;
Charles Chan72f556a2015-10-05 17:50:33 -070025import org.onlab.packet.MacAddress;
26import org.onosproject.net.DeviceId;
Charles Chancade1bf2017-06-07 17:26:01 -070027import org.onosproject.net.PortNumber;
Charles Chan72f556a2015-10-05 17:50:33 -070028import org.onosproject.net.config.Config;
Charles Chan72f556a2015-10-05 17:50:33 -070029
Charles Chan9bec8a32015-12-01 10:00:51 -080030import java.util.HashMap;
31import java.util.HashSet;
32import java.util.Map;
Charles Chan72f556a2015-10-05 17:50:33 -070033import java.util.Optional;
Charles Chan9bec8a32015-12-01 10:00:51 -080034import java.util.Set;
Charles Chan72f556a2015-10-05 17:50:33 -070035
36/**
37 * Configuration object for Segment Routing Application.
38 */
Charles Chan82ab1932016-01-30 23:22:37 -080039public class SegmentRoutingDeviceConfig extends Config<DeviceId> {
Charles Chanb7f75ac2016-01-11 18:28:54 -080040 private static final String NAME = "name";
Pier Ventreadb4ae62016-11-23 09:57:42 -080041 private static final String IP4 = "ipv4Loopback";
42 private static final String IP6 = "ipv6Loopback";
Charles Chanb7f75ac2016-01-11 18:28:54 -080043 private static final String MAC = "routerMac";
Pier Ventreadb4ae62016-11-23 09:57:42 -080044 private static final String IP4_SID = "ipv4NodeSid";
45 private static final String IP6_SID = "ipv6NodeSid";
Charles Chanb7f75ac2016-01-11 18:28:54 -080046 private static final String EDGE = "isEdgeRouter";
Charles Chancade1bf2017-06-07 17:26:01 -070047 /**
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 Chanb7f75ac2016-01-11 18:28:54 -080053 private static final String ADJSIDS = "adjacencySids";
Charles Chancade1bf2017-06-07 17:26:01 -070054
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 Chanb7f75ac2016-01-11 18:28:54 -080061 private static final String ADJSID = "adjSid";
Charles Chancade1bf2017-06-07 17:26:01 -070062
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 Chanb7f75ac2016-01-11 18:28:54 -080069 private static final String PORTS = "ports";
Charles Chan72f556a2015-10-05 17:50:33 -070070
Charles Chancade1bf2017-06-07 17:26:01 -070071 private static final String PAIR_DEVICE_ID = "pairDeviceId";
72 private static final String PAIR_LOCAL_PORT = "pairLocalPort";
73
Charles Chan9bec8a32015-12-01 10:00:51 -080074 @Override
75 public boolean isValid() {
Charles Chancade1bf2017-06-07 17:26:01 -070076 return hasOnlyFields(NAME, IP4, IP6, MAC, IP4_SID, IP6_SID, EDGE, ADJSIDS, ADJSID, PORTS,
77 PAIR_DEVICE_ID, PAIR_LOCAL_PORT) &&
Charles Chanb7f75ac2016-01-11 18:28:54 -080078 name() != null &&
Charles Chancade1bf2017-06-07 17:26:01 -070079 routerIpv4() != null && (!hasField(IP6) || routerIpv6() != null) &&
Charles Chanb7f75ac2016-01-11 18:28:54 -080080 routerMac() != null &&
Charles Chancade1bf2017-06-07 17:26:01 -070081 nodeSidIPv4() != -1 && (!hasField(IP6_SID) || nodeSidIPv6() != -1) &&
Charles Chanb7f75ac2016-01-11 18:28:54 -080082 isEdgeRouter() != null &&
Charles Chancade1bf2017-06-07 17:26:01 -070083 adjacencySids() != null &&
84 // pairDeviceId and pairLocalPort must be both configured or both omitted
Charles Chan3ed34d82017-06-22 18:03:14 -070085 (hasField(PAIR_DEVICE_ID) == hasField(PAIR_LOCAL_PORT)) &&
Charles Chancade1bf2017-06-07 17:26:01 -070086 (!hasField(PAIR_DEVICE_ID) || pairDeviceId() != null) &&
87 (!hasField(PAIR_LOCAL_PORT) || pairLocalPort() != null);
Charles Chan9bec8a32015-12-01 10:00:51 -080088 }
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 Chan72f556a2015-10-05 17:50:33 -070096 String name = get(NAME, null);
97 return name != null ? Optional.of(name) : Optional.empty();
98 }
99
Charles Chan9bec8a32015-12-01 10:00:51 -0800100 /**
101 * Sets the name of the router.
102 *
103 * @param name name of the router.
104 * @return the config of the router.
105 */
Charles Chan82ab1932016-01-30 23:22:37 -0800106 public SegmentRoutingDeviceConfig setName(String name) {
107 return (SegmentRoutingDeviceConfig) setOrClear(NAME, name);
Charles Chan72f556a2015-10-05 17:50:33 -0700108 }
109
Charles Chan9bec8a32015-12-01 10:00:51 -0800110 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800111 * Gets the IPv4 address of the router.
Charles Chan9bec8a32015-12-01 10:00:51 -0800112 *
113 * @return IP address of the router. Or null if not configured.
114 */
Pier Ventreadb4ae62016-11-23 09:57:42 -0800115 public Ip4Address routerIpv4() {
116 String ip = get(IP4, null);
Charles Chan72f556a2015-10-05 17:50:33 -0700117 return ip != null ? Ip4Address.valueOf(ip) : null;
118 }
119
Charles Chan9bec8a32015-12-01 10:00:51 -0800120 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800121 * Gets the IPv6 address of the router.
Charles Chan9bec8a32015-12-01 10:00:51 -0800122 *
Pier Ventreadb4ae62016-11-23 09:57:42 -0800123 * @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 Chan9bec8a32015-12-01 10:00:51 -0800134 * @return the config of the router.
135 */
Pier Ventreadb4ae62016-11-23 09:57:42 -0800136 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 Chan72f556a2015-10-05 17:50:33 -0700148 }
149
Charles Chan9bec8a32015-12-01 10:00:51 -0800150 /**
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 Chan72f556a2015-10-05 17:50:33 -0700156 String mac = get(MAC, null);
157 return mac != null ? MacAddress.valueOf(mac) : null;
158 }
159
Charles Chan9bec8a32015-12-01 10:00:51 -0800160 /**
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 Chan82ab1932016-01-30 23:22:37 -0800166 public SegmentRoutingDeviceConfig setRouterMac(String mac) {
167 return (SegmentRoutingDeviceConfig) setOrClear(MAC, mac);
Charles Chan72f556a2015-10-05 17:50:33 -0700168 }
169
Charles Chan9bec8a32015-12-01 10:00:51 -0800170 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800171 * Gets the IPv4 node SID of the router.
Charles Chan9bec8a32015-12-01 10:00:51 -0800172 *
173 * @return node SID of the router. Or -1 if not configured.
174 */
Pier Ventreadb4ae62016-11-23 09:57:42 -0800175 public int nodeSidIPv4() {
176 return get(IP4_SID, -1);
Charles Chan72f556a2015-10-05 17:50:33 -0700177 }
178
Charles Chan9bec8a32015-12-01 10:00:51 -0800179 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800180 * 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 Chan9bec8a32015-12-01 10:00:51 -0800190 *
191 * @param sid node SID of the router.
192 * @return the config of the router.
193 */
Pier Ventreadb4ae62016-11-23 09:57:42 -0800194 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 Chan72f556a2015-10-05 17:50:33 -0700206 }
207
Charles Chan9bec8a32015-12-01 10:00:51 -0800208 /**
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 Chan72f556a2015-10-05 17:50:33 -0700220 }
221
Charles Chan9bec8a32015-12-01 10:00:51 -0800222 /**
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 Chan82ab1932016-01-30 23:22:37 -0800228 public SegmentRoutingDeviceConfig setIsEdgeRouter(boolean isEdgeRouter) {
229 return (SegmentRoutingDeviceConfig) setOrClear(EDGE, isEdgeRouter);
Charles Chan72f556a2015-10-05 17:50:33 -0700230 }
231
Charles Chan9bec8a32015-12-01 10:00:51 -0800232 /**
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 Chane7c61022015-10-07 14:21:45 -0700240 }
241
Charles Chan9bec8a32015-12-01 10:00:51 -0800242 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 Chane7c61022015-10-07 14:21:45 -0700249
Charles Chan9bec8a32015-12-01 10:00:51 -0800250 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 Chan82ab1932016-01-30 23:22:37 -0800271 public SegmentRoutingDeviceConfig setAdjacencySids(Map<Integer, Set<Integer>> adjacencySids) {
Charles Chan9bec8a32015-12-01 10:00:51 -0800272 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 Chane7c61022015-10-07 14:21:45 -0700289 });
290
Charles Chan9bec8a32015-12-01 10:00:51 -0800291 object.set(ADJSIDS, adjacencySidsNode);
Charles Chan72f556a2015-10-05 17:50:33 -0700292 }
293
Charles Chan9bec8a32015-12-01 10:00:51 -0800294 return this;
Charles Chan72f556a2015-10-05 17:50:33 -0700295 }
Charles Chancade1bf2017-06-07 17:26:01 -0700296
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 Ventreadb4ae62016-11-23 09:57:42 -0800336}