blob: 21ba1e640b6e247ddc7e5bb78fa7e6c0a5224cda [file] [log] [blame]
Charles Chand6832882015-10-05 17:50:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Charles Chand6832882015-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 Chan531a78b2015-12-01 10:00:51 -080019import com.fasterxml.jackson.databind.JsonNode;
Charles Chan4636be02015-10-07 14:21:45 -070020import com.fasterxml.jackson.databind.node.ArrayNode;
Charles Chan531a78b2015-12-01 10:00:51 -080021import com.fasterxml.jackson.databind.node.ObjectNode;
22import com.google.common.collect.ImmutableMap;
Charles Chand6832882015-10-05 17:50:33 -070023import org.onlab.packet.Ip4Address;
24import org.onlab.packet.MacAddress;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.config.Config;
Charles Chand6832882015-10-05 17:50:33 -070027
Charles Chan531a78b2015-12-01 10:00:51 -080028import java.util.HashMap;
29import java.util.HashSet;
30import java.util.Map;
Charles Chand6832882015-10-05 17:50:33 -070031import java.util.Optional;
Charles Chan531a78b2015-12-01 10:00:51 -080032import java.util.Set;
Charles Chand6832882015-10-05 17:50:33 -070033
34/**
35 * Configuration object for Segment Routing Application.
36 */
Charles Chan5270ed02016-01-30 23:22:37 -080037public class SegmentRoutingDeviceConfig extends Config<DeviceId> {
Charles Chane849c192016-01-11 18:28:54 -080038 private static final String NAME = "name";
39 private static final String IP = "routerIp";
40 private static final String MAC = "routerMac";
41 private static final String SID = "nodeSid";
42 private static final String EDGE = "isEdgeRouter";
43 private static final String ADJSIDS = "adjacencySids";
44 private static final String ADJSID = "adjSid";
45 private static final String PORTS = "ports";
Charles Chand6832882015-10-05 17:50:33 -070046
Charles Chan531a78b2015-12-01 10:00:51 -080047 @Override
48 public boolean isValid() {
49 return hasOnlyFields(NAME, IP, MAC, SID, EDGE, ADJSIDS, ADJSID, PORTS) &&
Charles Chane849c192016-01-11 18:28:54 -080050 name() != null &&
51 routerIp() != null &&
52 routerMac() != null &&
53 nodeSid() != -1 &&
54 isEdgeRouter() != null &&
55 adjacencySids() != null;
Charles Chan531a78b2015-12-01 10:00:51 -080056 }
57
58 /**
59 * Gets the name of the router.
60 *
61 * @return Optional name of the router. May be empty if not configured.
62 */
63 public Optional<String> name() {
Charles Chand6832882015-10-05 17:50:33 -070064 String name = get(NAME, null);
65 return name != null ? Optional.of(name) : Optional.empty();
66 }
67
Charles Chan531a78b2015-12-01 10:00:51 -080068 /**
69 * Sets the name of the router.
70 *
71 * @param name name of the router.
72 * @return the config of the router.
73 */
Charles Chan5270ed02016-01-30 23:22:37 -080074 public SegmentRoutingDeviceConfig setName(String name) {
75 return (SegmentRoutingDeviceConfig) setOrClear(NAME, name);
Charles Chand6832882015-10-05 17:50:33 -070076 }
77
Charles Chan531a78b2015-12-01 10:00:51 -080078 /**
79 * Gets the IP address of the router.
80 *
81 * @return IP address of the router. Or null if not configured.
82 */
83 public Ip4Address routerIp() {
Charles Chand6832882015-10-05 17:50:33 -070084 String ip = get(IP, null);
85 return ip != null ? Ip4Address.valueOf(ip) : null;
86 }
87
Charles Chan531a78b2015-12-01 10:00:51 -080088 /**
89 * Sets the IP address of the router.
90 *
91 * @param ip IP address of the router.
92 * @return the config of the router.
93 */
Charles Chan5270ed02016-01-30 23:22:37 -080094 public SegmentRoutingDeviceConfig setRouterIp(String ip) {
95 return (SegmentRoutingDeviceConfig) setOrClear(IP, ip);
Charles Chand6832882015-10-05 17:50:33 -070096 }
97
Charles Chan531a78b2015-12-01 10:00:51 -080098 /**
99 * Gets the MAC address of the router.
100 *
101 * @return MAC address of the router. Or null if not configured.
102 */
103 public MacAddress routerMac() {
Charles Chand6832882015-10-05 17:50:33 -0700104 String mac = get(MAC, null);
105 return mac != null ? MacAddress.valueOf(mac) : null;
106 }
107
Charles Chan531a78b2015-12-01 10:00:51 -0800108 /**
109 * Sets the MAC address of the router.
110 *
111 * @param mac MAC address of the router.
112 * @return the config of the router.
113 */
Charles Chan5270ed02016-01-30 23:22:37 -0800114 public SegmentRoutingDeviceConfig setRouterMac(String mac) {
115 return (SegmentRoutingDeviceConfig) setOrClear(MAC, mac);
Charles Chand6832882015-10-05 17:50:33 -0700116 }
117
Charles Chan531a78b2015-12-01 10:00:51 -0800118 /**
119 * Gets the node SID of the router.
120 *
121 * @return node SID of the router. Or -1 if not configured.
122 */
123 public int nodeSid() {
Charles Chand6832882015-10-05 17:50:33 -0700124 return get(SID, -1);
125 }
126
Charles Chan531a78b2015-12-01 10:00:51 -0800127 /**
128 * Sets the node SID of the router.
129 *
130 * @param sid node SID of the router.
131 * @return the config of the router.
132 */
Charles Chan5270ed02016-01-30 23:22:37 -0800133 public SegmentRoutingDeviceConfig setNodeSid(int sid) {
134 return (SegmentRoutingDeviceConfig) setOrClear(SID, sid);
Charles Chand6832882015-10-05 17:50:33 -0700135 }
136
Charles Chan531a78b2015-12-01 10:00:51 -0800137 /**
138 * Checks if the router is an edge router.
139 *
140 * @return true if the router is an edge router.
141 * false if the router is not an edge router.
142 * null if the value is not configured.
143 */
144 public Boolean isEdgeRouter() {
145 String isEdgeRouter = get(EDGE, null);
146 return isEdgeRouter != null ?
147 Boolean.valueOf(isEdgeRouter) :
148 null;
Charles Chand6832882015-10-05 17:50:33 -0700149 }
150
Charles Chan531a78b2015-12-01 10:00:51 -0800151 /**
152 * Specifies if the router is an edge router.
153 *
154 * @param isEdgeRouter true if the router is an edge router.
155 * @return the config of the router.
156 */
Charles Chan5270ed02016-01-30 23:22:37 -0800157 public SegmentRoutingDeviceConfig setIsEdgeRouter(boolean isEdgeRouter) {
158 return (SegmentRoutingDeviceConfig) setOrClear(EDGE, isEdgeRouter);
Charles Chand6832882015-10-05 17:50:33 -0700159 }
160
Charles Chan531a78b2015-12-01 10:00:51 -0800161 /**
162 * Gets the adjacency SIDs of the router.
163 *
164 * @return adjacency SIDs of the router. Or null if not configured.
165 */
166 public Map<Integer, Set<Integer>> adjacencySids() {
167 if (!object.has(ADJSIDS)) {
168 return null;
Charles Chan4636be02015-10-07 14:21:45 -0700169 }
170
Charles Chan531a78b2015-12-01 10:00:51 -0800171 Map<Integer, Set<Integer>> adjacencySids = new HashMap<>();
172 ArrayNode adjacencySidsNode = (ArrayNode) object.path(ADJSIDS);
173 for (JsonNode adjacencySidNode : adjacencySidsNode) {
174 int asid = adjacencySidNode.path(ADJSID).asInt(-1);
175 if (asid == -1) {
176 return null;
177 }
Charles Chan4636be02015-10-07 14:21:45 -0700178
Charles Chan531a78b2015-12-01 10:00:51 -0800179 HashSet<Integer> ports = new HashSet<>();
180 ArrayNode portsNode = (ArrayNode) adjacencySidNode.path(PORTS);
181 for (JsonNode portNode : portsNode) {
182 int port = portNode.asInt(-1);
183 if (port == -1) {
184 return null;
185 }
186 ports.add(port);
187 }
188 adjacencySids.put(asid, ports);
189 }
190
191 return ImmutableMap.copyOf(adjacencySids);
192 }
193
194 /**
195 * Sets the adjacency SIDs of the router.
196 *
197 * @param adjacencySids adjacency SIDs of the router.
198 * @return the config of the router.
199 */
Charles Chan5270ed02016-01-30 23:22:37 -0800200 public SegmentRoutingDeviceConfig setAdjacencySids(Map<Integer, Set<Integer>> adjacencySids) {
Charles Chan531a78b2015-12-01 10:00:51 -0800201 if (adjacencySids == null) {
202 object.remove(ADJSIDS);
203 } else {
204 ArrayNode adjacencySidsNode = mapper.createArrayNode();
205
206 adjacencySids.forEach((sid, ports) -> {
207 ObjectNode adjacencySidNode = mapper.createObjectNode();
208
209 adjacencySidNode.put(ADJSID, sid);
210
211 ArrayNode portsNode = mapper.createArrayNode();
212 ports.forEach(port -> {
213 portsNode.add(port.toString());
214 });
215 adjacencySidNode.set(PORTS, portsNode);
216
217 adjacencySidsNode.add(adjacencySidNode);
Charles Chan4636be02015-10-07 14:21:45 -0700218 });
219
Charles Chan531a78b2015-12-01 10:00:51 -0800220 object.set(ADJSIDS, adjacencySidsNode);
Charles Chand6832882015-10-05 17:50:33 -0700221 }
222
Charles Chan531a78b2015-12-01 10:00:51 -0800223 return this;
Charles Chand6832882015-10-05 17:50:33 -0700224 }
225}