sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | 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 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 46 | public enum Result { |
| 47 | SUCCESS, |
| 48 | WRONG_PATH, |
| 49 | TUNNEL_EXISTS, |
| 50 | ID_EXISTS, |
| 51 | TUNNEL_NOT_FOUND, |
| 52 | TUNNEL_IN_USE, |
| 53 | INTERNAL_ERROR |
| 54 | } |
| 55 | |
| 56 | public TunnelHandler(LinkService linkService, |
| 57 | DeviceConfiguration deviceConfiguration, |
| 58 | Map<DeviceId, DefaultGroupHandler> groupHandlerMap, |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 59 | EventuallyConsistentMap<String, Tunnel> tunnelStore) { |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 60 | this.linkService = linkService; |
| 61 | this.config = deviceConfiguration; |
| 62 | this.groupHandlerMap = groupHandlerMap; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 63 | this.tunnelStore = tunnelStore; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Creates a tunnel. |
| 68 | * |
| 69 | * @param tunnel tunnel reference to create a tunnel |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 70 | * @return WRONG_PATH if the tunnel path is wrong, ID_EXISTS if the tunnel ID |
| 71 | * exists already, TUNNEL_EXISTS if the same tunnel exists, INTERNAL_ERROR |
| 72 | * if the tunnel creation failed internally, SUCCESS if the tunnel is created |
| 73 | * successfully |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 74 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 75 | public Result createTunnel(Tunnel tunnel) { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 76 | |
| 77 | if (tunnel.labelIds().isEmpty() || tunnel.labelIds().size() < 3) { |
| 78 | log.error("More than one router needs to specified to created a tunnel"); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 79 | return Result.WRONG_PATH; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (tunnelStore.containsKey(tunnel.id())) { |
| 83 | log.warn("The same tunnel ID exists already"); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 84 | return Result.ID_EXISTS; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | if (tunnelStore.containsValue(tunnel)) { |
| 88 | log.warn("The same tunnel exists already"); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 89 | return Result.TUNNEL_EXISTS; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | int groupId = createGroupsForTunnel(tunnel); |
| 93 | if (groupId < 0) { |
| 94 | log.error("Failed to create groups for the tunnel"); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 95 | return Result.INTERNAL_ERROR; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | tunnel.setGroupId(groupId); |
| 99 | tunnelStore.put(tunnel.id(), tunnel); |
| 100 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 101 | return Result.SUCCESS; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Removes the tunnel with the tunnel ID given. |
| 106 | * |
| 107 | * @param tunnelInfo tunnel information to delete tunnels |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 108 | * @return TUNNEL_NOT_FOUND if the tunnel to remove does not exists, |
| 109 | * INTERNAL_ERROR if the tunnel creation failed internally, SUCCESS |
| 110 | * if the tunnel is created successfully. |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 111 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 112 | public Result removeTunnel(Tunnel tunnelInfo) { |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 113 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 114 | Tunnel tunnel = tunnelStore.get(tunnelInfo.id()); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 115 | if (tunnel != null) { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 116 | DeviceId deviceId = config.getDeviceId(tunnel.labelIds().get(0)); |
| 117 | if (tunnel.isAllowedToRemoveGroup()) { |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 118 | if (groupHandlerMap.get(deviceId).removeGroup(tunnel.groupId())) { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 119 | tunnelStore.remove(tunnel.id()); |
| 120 | } else { |
| 121 | log.error("Failed to remove the tunnel {}", tunnelInfo.id()); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 122 | return Result.INTERNAL_ERROR; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 123 | } |
| 124 | } else { |
| 125 | log.debug("The group is not removed because it is being used."); |
| 126 | tunnelStore.remove(tunnel.id()); |
| 127 | } |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 128 | } else { |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 129 | log.error("No tunnel found for tunnel ID {}", tunnelInfo.id()); |
| 130 | return Result.TUNNEL_NOT_FOUND; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 131 | } |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 132 | |
| 133 | return Result.SUCCESS; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Returns the tunnel with the tunnel ID given. |
| 138 | * |
| 139 | * @param tid Tunnel ID |
| 140 | * @return Tunnel reference |
| 141 | */ |
| 142 | public Tunnel getTunnel(String tid) { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 143 | return tunnelStore.get(tid); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Returns all tunnels. |
| 148 | * |
| 149 | * @return list of Tunnels |
| 150 | */ |
| 151 | public List<Tunnel> getTunnels() { |
| 152 | List<Tunnel> tunnels = new ArrayList<>(); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 153 | tunnelStore.values().forEach(tunnel -> tunnels.add( |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 154 | new DefaultTunnel((DefaultTunnel) tunnel))); |
| 155 | |
| 156 | return tunnels; |
| 157 | } |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 158 | |
| 159 | private int createGroupsForTunnel(Tunnel tunnel) { |
| 160 | |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 161 | Set<Integer> portNumbers; |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 162 | final int groupError = -1; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 163 | |
| 164 | DeviceId deviceId = config.getDeviceId(tunnel.labelIds().get(0)); |
| 165 | if (deviceId == null) { |
| 166 | log.warn("No device found for SID {}", tunnel.labelIds().get(0)); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 167 | return groupError; |
| 168 | } else if (groupHandlerMap.get(deviceId) == null) { |
| 169 | log.warn("group handler not found for {}", deviceId); |
| 170 | return groupError; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 171 | } |
| 172 | Set<DeviceId> deviceIds = new HashSet<>(); |
| 173 | int sid = tunnel.labelIds().get(1); |
| 174 | if (config.isAdjacencySid(deviceId, sid)) { |
| 175 | portNumbers = config.getPortsForAdjacencySid(deviceId, sid); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 176 | for (Link link: linkService.getDeviceEgressLinks(deviceId)) { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 177 | for (Integer port: portNumbers) { |
| 178 | if (link.src().port().toLong() == port) { |
| 179 | deviceIds.add(link.dst().deviceId()); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | } else { |
| 184 | deviceIds.add(config.getDeviceId(sid)); |
| 185 | } |
| 186 | |
| 187 | NeighborSet ns = new NeighborSet(deviceIds, tunnel.labelIds().get(2)); |
| 188 | |
| 189 | // If the tunnel reuses any existing groups, then tunnel handler |
| 190 | // should not remove the group. |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 191 | if (groupHandlerMap.get(deviceId).hasNextObjectiveId(ns)) { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 192 | tunnel.allowToRemoveGroup(false); |
| 193 | } else { |
| 194 | tunnel.allowToRemoveGroup(true); |
| 195 | } |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 196 | |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 197 | return groupHandlerMap.get(deviceId).getNextObjectiveId(ns, null); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 198 | } |
| 199 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 200 | } |