Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 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; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.config.Config; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 27 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 28 | import java.util.HashMap; |
| 29 | import java.util.HashSet; |
| 30 | import java.util.Map; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 31 | import java.util.Optional; |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 32 | import java.util.Set; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Configuration object for Segment Routing Application. |
| 36 | */ |
| 37 | public class SegmentRoutingConfig extends Config<DeviceId> { |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 38 | public static final String NAME = "name"; |
| 39 | public static final String IP = "routerIp"; |
| 40 | public static final String MAC = "routerMac"; |
| 41 | public static final String SID = "nodeSid"; |
| 42 | public static final String EDGE = "isEdgeRouter"; |
| 43 | public static final String ADJSIDS = "adjacencySids"; |
| 44 | public static final String ADJSID = "adjSid"; |
| 45 | public static final String PORTS = "ports"; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 46 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 47 | @Override |
| 48 | public boolean isValid() { |
| 49 | return hasOnlyFields(NAME, IP, MAC, SID, EDGE, ADJSIDS, ADJSID, PORTS) && |
| 50 | this.name() != null && |
| 51 | this.routerIp() != null && |
| 52 | this.routerMac() != null && |
| 53 | this.nodeSid() != -1 && |
| 54 | this.isEdgeRouter() != null && |
| 55 | this.adjacencySids() != null; |
| 56 | } |
| 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 Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 64 | String name = get(NAME, null); |
| 65 | return name != null ? Optional.of(name) : Optional.empty(); |
| 66 | } |
| 67 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 68 | /** |
| 69 | * Sets the name of the router. |
| 70 | * |
| 71 | * @param name name of the router. |
| 72 | * @return the config of the router. |
| 73 | */ |
| 74 | public SegmentRoutingConfig setName(String name) { |
| 75 | return (SegmentRoutingConfig) setOrClear(NAME, name); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 78 | /** |
| 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 Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 84 | String ip = get(IP, null); |
| 85 | return ip != null ? Ip4Address.valueOf(ip) : null; |
| 86 | } |
| 87 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 88 | /** |
| 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 | */ |
| 94 | public SegmentRoutingConfig setRouterIp(String ip) { |
| 95 | return (SegmentRoutingConfig) setOrClear(IP, ip); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 98 | /** |
| 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 Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 104 | String mac = get(MAC, null); |
| 105 | return mac != null ? MacAddress.valueOf(mac) : null; |
| 106 | } |
| 107 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 108 | /** |
| 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 | */ |
| 114 | public SegmentRoutingConfig setRouterMac(String mac) { |
| 115 | return (SegmentRoutingConfig) setOrClear(MAC, mac); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 118 | /** |
| 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 Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 124 | return get(SID, -1); |
| 125 | } |
| 126 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 127 | /** |
| 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 | */ |
| 133 | public SegmentRoutingConfig setNodeSid(int sid) { |
| 134 | return (SegmentRoutingConfig) setOrClear(SID, sid); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 137 | /** |
| 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 Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 151 | /** |
| 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 | */ |
| 157 | public SegmentRoutingConfig setIsEdgeRouter(boolean isEdgeRouter) { |
| 158 | return (SegmentRoutingConfig) setOrClear(EDGE, isEdgeRouter); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 161 | /** |
| 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 Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 171 | 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 Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 178 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 179 | 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 | */ |
| 200 | public SegmentRoutingConfig setAdjacencySids(Map<Integer, Set<Integer>> adjacencySids) { |
| 201 | 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 Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 218 | }); |
| 219 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 220 | object.set(ADJSIDS, adjacencySidsNode); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 223 | return this; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 224 | } |
| 225 | } |