sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 43b5354 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
sangho | 27462c6 | 2015-05-14 00:39:53 -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 | package org.onosproject.segmentrouting; |
| 17 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 18 | import org.onosproject.net.DeviceId; |
| 19 | import org.onosproject.net.Link; |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 20 | import org.onosproject.net.link.LinkService; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 21 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 22 | import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 23 | import org.onosproject.segmentrouting.grouphandler.NeighborSet; |
| 24 | import org.onosproject.store.service.EventuallyConsistentMap; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 25 | import org.slf4j.Logger; |
| 26 | |
| 27 | import java.util.ArrayList; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 28 | import java.util.HashSet; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 29 | import java.util.List; |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 30 | import java.util.Map; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 31 | import java.util.Set; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 32 | |
| 33 | import static org.slf4j.LoggerFactory.getLogger; |
| 34 | |
| 35 | /** |
| 36 | * Tunnel Handler. |
| 37 | */ |
| 38 | public class TunnelHandler { |
| 39 | protected final Logger log = getLogger(getClass()); |
| 40 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 41 | private final DeviceConfiguration config; |
| 42 | private final EventuallyConsistentMap<String, Tunnel> tunnelStore; |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 43 | private Map<DeviceId, DefaultGroupHandler> groupHandlerMap; |
| 44 | private LinkService linkService; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 45 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 46 | /** |
| 47 | * Result of tunnel creation or removal. |
| 48 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 49 | public enum Result { |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 50 | /** |
| 51 | * Success. |
| 52 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 53 | SUCCESS, |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * More than one router needs to specified to created a tunnel. |
| 57 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 58 | WRONG_PATH, |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * The same tunnel exists already. |
| 62 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 63 | TUNNEL_EXISTS, |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * The same tunnel ID exists already. |
| 67 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 68 | ID_EXISTS, |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Tunnel not found. |
| 72 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 73 | TUNNEL_NOT_FOUND, |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Cannot remove the tunnel used by a policy. |
| 77 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 78 | TUNNEL_IN_USE, |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * Failed to create/remove groups for the tunnel. |
| 82 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 83 | INTERNAL_ERROR |
| 84 | } |
| 85 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 86 | /** |
| 87 | * Constructs tunnel handler. |
| 88 | * |
| 89 | * @param linkService link service |
| 90 | * @param deviceConfiguration device configuration |
| 91 | * @param groupHandlerMap group handler map |
| 92 | * @param tunnelStore tunnel store |
| 93 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 94 | public TunnelHandler(LinkService linkService, |
| 95 | DeviceConfiguration deviceConfiguration, |
| 96 | Map<DeviceId, DefaultGroupHandler> groupHandlerMap, |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 97 | EventuallyConsistentMap<String, Tunnel> tunnelStore) { |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 98 | this.linkService = linkService; |
| 99 | this.config = deviceConfiguration; |
| 100 | this.groupHandlerMap = groupHandlerMap; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 101 | this.tunnelStore = tunnelStore; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Creates a tunnel. |
| 106 | * |
| 107 | * @param tunnel tunnel reference to create a tunnel |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 108 | * @return WRONG_PATH if the tunnel path is wrong, ID_EXISTS if the tunnel ID |
| 109 | * exists already, TUNNEL_EXISTS if the same tunnel exists, INTERNAL_ERROR |
| 110 | * if the tunnel creation failed internally, SUCCESS if the tunnel is created |
| 111 | * successfully |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 112 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 113 | public Result createTunnel(Tunnel tunnel) { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 114 | |
| 115 | if (tunnel.labelIds().isEmpty() || tunnel.labelIds().size() < 3) { |
| 116 | log.error("More than one router needs to specified to created a tunnel"); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 117 | return Result.WRONG_PATH; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | if (tunnelStore.containsKey(tunnel.id())) { |
| 121 | log.warn("The same tunnel ID exists already"); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 122 | return Result.ID_EXISTS; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | if (tunnelStore.containsValue(tunnel)) { |
| 126 | log.warn("The same tunnel exists already"); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 127 | return Result.TUNNEL_EXISTS; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | int groupId = createGroupsForTunnel(tunnel); |
| 131 | if (groupId < 0) { |
| 132 | log.error("Failed to create groups for the tunnel"); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 133 | return Result.INTERNAL_ERROR; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | tunnel.setGroupId(groupId); |
| 137 | tunnelStore.put(tunnel.id(), tunnel); |
| 138 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 139 | return Result.SUCCESS; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Removes the tunnel with the tunnel ID given. |
| 144 | * |
| 145 | * @param tunnelInfo tunnel information to delete tunnels |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 146 | * @return TUNNEL_NOT_FOUND if the tunnel to remove does not exists, |
| 147 | * INTERNAL_ERROR if the tunnel creation failed internally, SUCCESS |
| 148 | * if the tunnel is created successfully. |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 149 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 150 | public Result removeTunnel(Tunnel tunnelInfo) { |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 151 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 152 | Tunnel tunnel = tunnelStore.get(tunnelInfo.id()); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 153 | if (tunnel != null) { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 154 | DeviceId deviceId = config.getDeviceId(tunnel.labelIds().get(0)); |
| 155 | if (tunnel.isAllowedToRemoveGroup()) { |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 156 | if (groupHandlerMap.get(deviceId).removeGroup(tunnel.groupId())) { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 157 | tunnelStore.remove(tunnel.id()); |
| 158 | } else { |
| 159 | log.error("Failed to remove the tunnel {}", tunnelInfo.id()); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 160 | return Result.INTERNAL_ERROR; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 161 | } |
| 162 | } else { |
| 163 | log.debug("The group is not removed because it is being used."); |
| 164 | tunnelStore.remove(tunnel.id()); |
| 165 | } |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 166 | } else { |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 167 | log.error("No tunnel found for tunnel ID {}", tunnelInfo.id()); |
| 168 | return Result.TUNNEL_NOT_FOUND; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 169 | } |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 170 | |
| 171 | return Result.SUCCESS; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Returns the tunnel with the tunnel ID given. |
| 176 | * |
| 177 | * @param tid Tunnel ID |
| 178 | * @return Tunnel reference |
| 179 | */ |
| 180 | public Tunnel getTunnel(String tid) { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 181 | return tunnelStore.get(tid); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Returns all tunnels. |
| 186 | * |
| 187 | * @return list of Tunnels |
| 188 | */ |
| 189 | public List<Tunnel> getTunnels() { |
| 190 | List<Tunnel> tunnels = new ArrayList<>(); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 191 | tunnelStore.values().forEach(tunnel -> tunnels.add( |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 192 | new DefaultTunnel((DefaultTunnel) tunnel))); |
| 193 | |
| 194 | return tunnels; |
| 195 | } |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 196 | |
| 197 | private int createGroupsForTunnel(Tunnel tunnel) { |
| 198 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 199 | Set<Integer> portNumbers; |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 200 | final int groupError = -1; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 201 | |
| 202 | DeviceId deviceId = config.getDeviceId(tunnel.labelIds().get(0)); |
| 203 | if (deviceId == null) { |
| 204 | log.warn("No device found for SID {}", tunnel.labelIds().get(0)); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 205 | return groupError; |
| 206 | } else if (groupHandlerMap.get(deviceId) == null) { |
| 207 | log.warn("group handler not found for {}", deviceId); |
| 208 | return groupError; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 209 | } |
| 210 | Set<DeviceId> deviceIds = new HashSet<>(); |
| 211 | int sid = tunnel.labelIds().get(1); |
| 212 | if (config.isAdjacencySid(deviceId, sid)) { |
| 213 | portNumbers = config.getPortsForAdjacencySid(deviceId, sid); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 214 | for (Link link: linkService.getDeviceEgressLinks(deviceId)) { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 215 | for (Integer port: portNumbers) { |
| 216 | if (link.src().port().toLong() == port) { |
| 217 | deviceIds.add(link.dst().deviceId()); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } else { |
| 222 | deviceIds.add(config.getDeviceId(sid)); |
| 223 | } |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 224 | // For these NeighborSet isMpls is meaningless. |
| 225 | NeighborSet ns = new NeighborSet(deviceIds, false, tunnel.labelIds().get(2)); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 226 | |
| 227 | // If the tunnel reuses any existing groups, then tunnel handler |
| 228 | // should not remove the group. |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 229 | if (groupHandlerMap.get(deviceId).hasNextObjectiveId(ns)) { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 230 | tunnel.allowToRemoveGroup(false); |
| 231 | } else { |
| 232 | tunnel.allowToRemoveGroup(true); |
| 233 | } |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 234 | |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 235 | return groupHandlerMap.get(deviceId).getNextObjectiveId(ns, null, true); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 236 | } |
| 237 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 238 | } |