blob: bc0d32967724853e2d11e687cabee424296fc88e [file] [log] [blame]
Charles Chan72f556a2015-10-05 17:50:33 -07001/*
Brian O'Connor43b53542016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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;
27import org.onosproject.net.config.Config;
Charles Chan72f556a2015-10-05 17:50:33 -070028
Charles Chan9bec8a32015-12-01 10:00:51 -080029import java.util.HashMap;
30import java.util.HashSet;
31import java.util.Map;
Charles Chan72f556a2015-10-05 17:50:33 -070032import java.util.Optional;
Charles Chan9bec8a32015-12-01 10:00:51 -080033import java.util.Set;
Charles Chan72f556a2015-10-05 17:50:33 -070034
35/**
36 * Configuration object for Segment Routing Application.
37 */
Charles Chan82ab1932016-01-30 23:22:37 -080038public class SegmentRoutingDeviceConfig extends Config<DeviceId> {
Charles Chanb7f75ac2016-01-11 18:28:54 -080039 private static final String NAME = "name";
Pier Ventreadb4ae62016-11-23 09:57:42 -080040 private static final String IP4 = "ipv4Loopback";
41 private static final String IP6 = "ipv6Loopback";
Charles Chanb7f75ac2016-01-11 18:28:54 -080042 private static final String MAC = "routerMac";
Pier Ventreadb4ae62016-11-23 09:57:42 -080043 private static final String IP4_SID = "ipv4NodeSid";
44 private static final String IP6_SID = "ipv6NodeSid";
Charles Chanb7f75ac2016-01-11 18:28:54 -080045 private static final String EDGE = "isEdgeRouter";
46 private static final String ADJSIDS = "adjacencySids";
47 private static final String ADJSID = "adjSid";
48 private static final String PORTS = "ports";
Charles Chan72f556a2015-10-05 17:50:33 -070049
Charles Chan9bec8a32015-12-01 10:00:51 -080050 @Override
51 public boolean isValid() {
Pier Ventreadb4ae62016-11-23 09:57:42 -080052 return hasOnlyFields(NAME, IP4, IP6, MAC,
53 IP4_SID, IP6_SID, EDGE,
54 ADJSIDS, ADJSID, PORTS) &&
Charles Chanb7f75ac2016-01-11 18:28:54 -080055 name() != null &&
Pier Ventreadb4ae62016-11-23 09:57:42 -080056 routerIpv4() != null &&
Charles Chanb7f75ac2016-01-11 18:28:54 -080057 routerMac() != null &&
Pier Ventreadb4ae62016-11-23 09:57:42 -080058 nodeSidIPv4() != -1 &&
Charles Chanb7f75ac2016-01-11 18:28:54 -080059 isEdgeRouter() != null &&
60 adjacencySids() != null;
Charles Chan9bec8a32015-12-01 10:00:51 -080061 }
62
63 /**
64 * Gets the name of the router.
65 *
66 * @return Optional name of the router. May be empty if not configured.
67 */
68 public Optional<String> name() {
Charles Chan72f556a2015-10-05 17:50:33 -070069 String name = get(NAME, null);
70 return name != null ? Optional.of(name) : Optional.empty();
71 }
72
Charles Chan9bec8a32015-12-01 10:00:51 -080073 /**
74 * Sets the name of the router.
75 *
76 * @param name name of the router.
77 * @return the config of the router.
78 */
Charles Chan82ab1932016-01-30 23:22:37 -080079 public SegmentRoutingDeviceConfig setName(String name) {
80 return (SegmentRoutingDeviceConfig) setOrClear(NAME, name);
Charles Chan72f556a2015-10-05 17:50:33 -070081 }
82
Charles Chan9bec8a32015-12-01 10:00:51 -080083 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -080084 * Gets the IPv4 address of the router.
Charles Chan9bec8a32015-12-01 10:00:51 -080085 *
86 * @return IP address of the router. Or null if not configured.
87 */
Pier Ventreadb4ae62016-11-23 09:57:42 -080088 public Ip4Address routerIpv4() {
89 String ip = get(IP4, null);
Charles Chan72f556a2015-10-05 17:50:33 -070090 return ip != null ? Ip4Address.valueOf(ip) : null;
91 }
92
Charles Chan9bec8a32015-12-01 10:00:51 -080093 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -080094 * Gets the IPv6 address of the router.
Charles Chan9bec8a32015-12-01 10:00:51 -080095 *
Pier Ventreadb4ae62016-11-23 09:57:42 -080096 * @return IP address of the router. Or null if not configured.
97 */
98 public Ip6Address routerIpv6() {
99 String ip = get(IP6, null);
100 return ip != null ? Ip6Address.valueOf(ip) : null;
101 }
102
103 /**
104 * Sets the IPv4 address of the router.
105 *
106 * @param ip IPv4 address of the router.
Charles Chan9bec8a32015-12-01 10:00:51 -0800107 * @return the config of the router.
108 */
Pier Ventreadb4ae62016-11-23 09:57:42 -0800109 public SegmentRoutingDeviceConfig setRouterIpv4(String ip) {
110 return (SegmentRoutingDeviceConfig) setOrClear(IP4, ip);
111 }
112
113 /**
114 * Sets the IPv6 address of the router.
115 *
116 * @param ip IPv6 address of the router.
117 * @return the config of the router.
118 */
119 public SegmentRoutingDeviceConfig setRouterIpv6(String ip) {
120 return (SegmentRoutingDeviceConfig) setOrClear(IP6, ip);
Charles Chan72f556a2015-10-05 17:50:33 -0700121 }
122
Charles Chan9bec8a32015-12-01 10:00:51 -0800123 /**
124 * Gets the MAC address of the router.
125 *
126 * @return MAC address of the router. Or null if not configured.
127 */
128 public MacAddress routerMac() {
Charles Chan72f556a2015-10-05 17:50:33 -0700129 String mac = get(MAC, null);
130 return mac != null ? MacAddress.valueOf(mac) : null;
131 }
132
Charles Chan9bec8a32015-12-01 10:00:51 -0800133 /**
134 * Sets the MAC address of the router.
135 *
136 * @param mac MAC address of the router.
137 * @return the config of the router.
138 */
Charles Chan82ab1932016-01-30 23:22:37 -0800139 public SegmentRoutingDeviceConfig setRouterMac(String mac) {
140 return (SegmentRoutingDeviceConfig) setOrClear(MAC, mac);
Charles Chan72f556a2015-10-05 17:50:33 -0700141 }
142
Charles Chan9bec8a32015-12-01 10:00:51 -0800143 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800144 * Gets the IPv4 node SID of the router.
Charles Chan9bec8a32015-12-01 10:00:51 -0800145 *
146 * @return node SID of the router. Or -1 if not configured.
147 */
Pier Ventreadb4ae62016-11-23 09:57:42 -0800148 public int nodeSidIPv4() {
149 return get(IP4_SID, -1);
Charles Chan72f556a2015-10-05 17:50:33 -0700150 }
151
Charles Chan9bec8a32015-12-01 10:00:51 -0800152 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800153 * Gets the IPv6 node SID of the router.
154 *
155 * @return node SID of the router. Or -1 if not configured.
156 */
157 public int nodeSidIPv6() {
158 return get(IP6_SID, -1);
159 }
160
161 /**
162 * Sets the node IPv4 node SID of the router.
Charles Chan9bec8a32015-12-01 10:00:51 -0800163 *
164 * @param sid node SID of the router.
165 * @return the config of the router.
166 */
Pier Ventreadb4ae62016-11-23 09:57:42 -0800167 public SegmentRoutingDeviceConfig setNodeSidIPv4(int sid) {
168 return (SegmentRoutingDeviceConfig) setOrClear(IP4_SID, sid);
169 }
170
171 /**
172 * Sets the node IPv6 node SID of the router.
173 *
174 * @param sid node SID of the router.
175 * @return the config of the router.
176 */
177 public SegmentRoutingDeviceConfig setNodeSidIPv6(int sid) {
178 return (SegmentRoutingDeviceConfig) setOrClear(IP6_SID, sid);
Charles Chan72f556a2015-10-05 17:50:33 -0700179 }
180
Charles Chan9bec8a32015-12-01 10:00:51 -0800181 /**
182 * Checks if the router is an edge router.
183 *
184 * @return true if the router is an edge router.
185 * false if the router is not an edge router.
186 * null if the value is not configured.
187 */
188 public Boolean isEdgeRouter() {
189 String isEdgeRouter = get(EDGE, null);
190 return isEdgeRouter != null ?
191 Boolean.valueOf(isEdgeRouter) :
192 null;
Charles Chan72f556a2015-10-05 17:50:33 -0700193 }
194
Charles Chan9bec8a32015-12-01 10:00:51 -0800195 /**
196 * Specifies if the router is an edge router.
197 *
198 * @param isEdgeRouter true if the router is an edge router.
199 * @return the config of the router.
200 */
Charles Chan82ab1932016-01-30 23:22:37 -0800201 public SegmentRoutingDeviceConfig setIsEdgeRouter(boolean isEdgeRouter) {
202 return (SegmentRoutingDeviceConfig) setOrClear(EDGE, isEdgeRouter);
Charles Chan72f556a2015-10-05 17:50:33 -0700203 }
204
Charles Chan9bec8a32015-12-01 10:00:51 -0800205 /**
206 * Gets the adjacency SIDs of the router.
207 *
208 * @return adjacency SIDs of the router. Or null if not configured.
209 */
210 public Map<Integer, Set<Integer>> adjacencySids() {
211 if (!object.has(ADJSIDS)) {
212 return null;
Charles Chane7c61022015-10-07 14:21:45 -0700213 }
214
Charles Chan9bec8a32015-12-01 10:00:51 -0800215 Map<Integer, Set<Integer>> adjacencySids = new HashMap<>();
216 ArrayNode adjacencySidsNode = (ArrayNode) object.path(ADJSIDS);
217 for (JsonNode adjacencySidNode : adjacencySidsNode) {
218 int asid = adjacencySidNode.path(ADJSID).asInt(-1);
219 if (asid == -1) {
220 return null;
221 }
Charles Chane7c61022015-10-07 14:21:45 -0700222
Charles Chan9bec8a32015-12-01 10:00:51 -0800223 HashSet<Integer> ports = new HashSet<>();
224 ArrayNode portsNode = (ArrayNode) adjacencySidNode.path(PORTS);
225 for (JsonNode portNode : portsNode) {
226 int port = portNode.asInt(-1);
227 if (port == -1) {
228 return null;
229 }
230 ports.add(port);
231 }
232 adjacencySids.put(asid, ports);
233 }
234
235 return ImmutableMap.copyOf(adjacencySids);
236 }
237
238 /**
239 * Sets the adjacency SIDs of the router.
240 *
241 * @param adjacencySids adjacency SIDs of the router.
242 * @return the config of the router.
243 */
Charles Chan82ab1932016-01-30 23:22:37 -0800244 public SegmentRoutingDeviceConfig setAdjacencySids(Map<Integer, Set<Integer>> adjacencySids) {
Charles Chan9bec8a32015-12-01 10:00:51 -0800245 if (adjacencySids == null) {
246 object.remove(ADJSIDS);
247 } else {
248 ArrayNode adjacencySidsNode = mapper.createArrayNode();
249
250 adjacencySids.forEach((sid, ports) -> {
251 ObjectNode adjacencySidNode = mapper.createObjectNode();
252
253 adjacencySidNode.put(ADJSID, sid);
254
255 ArrayNode portsNode = mapper.createArrayNode();
256 ports.forEach(port -> {
257 portsNode.add(port.toString());
258 });
259 adjacencySidNode.set(PORTS, portsNode);
260
261 adjacencySidsNode.add(adjacencySidNode);
Charles Chane7c61022015-10-07 14:21:45 -0700262 });
263
Charles Chan9bec8a32015-12-01 10:00:51 -0800264 object.set(ADJSIDS, adjacencySidsNode);
Charles Chan72f556a2015-10-05 17:50:33 -0700265 }
266
Charles Chan9bec8a32015-12-01 10:00:51 -0800267 return this;
Charles Chan72f556a2015-10-05 17:50:33 -0700268 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800269}