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; |
| 20 | import org.onosproject.segmentrouting.grouphandler.NeighborSet; |
| 21 | import org.onosproject.store.service.EventuallyConsistentMap; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 22 | import org.slf4j.Logger; |
| 23 | |
| 24 | import java.util.ArrayList; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 25 | import java.util.HashSet; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 26 | import java.util.List; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 27 | import java.util.Set; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 28 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 29 | import static com.google.common.base.Preconditions.checkNotNull; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 30 | import static org.slf4j.LoggerFactory.getLogger; |
| 31 | |
| 32 | /** |
| 33 | * Tunnel Handler. |
| 34 | */ |
| 35 | public class TunnelHandler { |
| 36 | protected final Logger log = getLogger(getClass()); |
| 37 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 38 | private final SegmentRoutingManager srManager; |
| 39 | private final DeviceConfiguration config; |
| 40 | private final EventuallyConsistentMap<String, Tunnel> tunnelStore; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 41 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 42 | public TunnelHandler(SegmentRoutingManager srm, |
| 43 | EventuallyConsistentMap<String, Tunnel> tunnelStore) { |
| 44 | this.srManager = checkNotNull(srm); |
| 45 | this.config = srm.deviceConfiguration; |
| 46 | this.tunnelStore = tunnelStore; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Creates a tunnel. |
| 51 | * |
| 52 | * @param tunnel tunnel reference to create a tunnel |
Thomas Vachuska | d62989f | 2015-05-21 16:41:41 -0700 | [diff] [blame] | 53 | * @return true if creation succeeded |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 54 | */ |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 55 | public boolean createTunnel(Tunnel tunnel) { |
| 56 | |
| 57 | if (tunnel.labelIds().isEmpty() || tunnel.labelIds().size() < 3) { |
| 58 | log.error("More than one router needs to specified to created a tunnel"); |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | if (tunnelStore.containsKey(tunnel.id())) { |
| 63 | log.warn("The same tunnel ID exists already"); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | if (tunnelStore.containsValue(tunnel)) { |
| 68 | log.warn("The same tunnel exists already"); |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | int groupId = createGroupsForTunnel(tunnel); |
| 73 | if (groupId < 0) { |
| 74 | log.error("Failed to create groups for the tunnel"); |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | tunnel.setGroupId(groupId); |
| 79 | tunnelStore.put(tunnel.id(), tunnel); |
| 80 | |
| 81 | return true; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Removes the tunnel with the tunnel ID given. |
| 86 | * |
| 87 | * @param tunnelInfo tunnel information to delete tunnels |
| 88 | */ |
| 89 | public void removeTunnel(Tunnel tunnelInfo) { |
| 90 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 91 | Tunnel tunnel = tunnelStore.get(tunnelInfo.id()); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 92 | if (tunnel != null) { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 93 | DeviceId deviceId = config.getDeviceId(tunnel.labelIds().get(0)); |
| 94 | if (tunnel.isAllowedToRemoveGroup()) { |
| 95 | if (srManager.removeNextObjective(deviceId, tunnel.groupId())) { |
| 96 | tunnelStore.remove(tunnel.id()); |
| 97 | } else { |
| 98 | log.error("Failed to remove the tunnel {}", tunnelInfo.id()); |
| 99 | } |
| 100 | } else { |
| 101 | log.debug("The group is not removed because it is being used."); |
| 102 | tunnelStore.remove(tunnel.id()); |
| 103 | } |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 104 | } else { |
| 105 | log.warn("No tunnel found for tunnel ID {}", tunnelInfo.id()); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns the tunnel with the tunnel ID given. |
| 111 | * |
| 112 | * @param tid Tunnel ID |
| 113 | * @return Tunnel reference |
| 114 | */ |
| 115 | public Tunnel getTunnel(String tid) { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 116 | return tunnelStore.get(tid); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns all tunnels. |
| 121 | * |
| 122 | * @return list of Tunnels |
| 123 | */ |
| 124 | public List<Tunnel> getTunnels() { |
| 125 | List<Tunnel> tunnels = new ArrayList<>(); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 126 | tunnelStore.values().forEach(tunnel -> tunnels.add( |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 127 | new DefaultTunnel((DefaultTunnel) tunnel))); |
| 128 | |
| 129 | return tunnels; |
| 130 | } |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 131 | |
| 132 | private int createGroupsForTunnel(Tunnel tunnel) { |
| 133 | |
| 134 | List<Integer> portNumbers; |
| 135 | |
| 136 | int groupId; |
| 137 | |
| 138 | DeviceId deviceId = config.getDeviceId(tunnel.labelIds().get(0)); |
| 139 | if (deviceId == null) { |
| 140 | log.warn("No device found for SID {}", tunnel.labelIds().get(0)); |
| 141 | return -1; |
| 142 | } |
| 143 | Set<DeviceId> deviceIds = new HashSet<>(); |
| 144 | int sid = tunnel.labelIds().get(1); |
| 145 | if (config.isAdjacencySid(deviceId, sid)) { |
| 146 | portNumbers = config.getPortsForAdjacencySid(deviceId, sid); |
| 147 | for (Link link: srManager.linkService.getDeviceEgressLinks(deviceId)) { |
| 148 | for (Integer port: portNumbers) { |
| 149 | if (link.src().port().toLong() == port) { |
| 150 | deviceIds.add(link.dst().deviceId()); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } else { |
| 155 | deviceIds.add(config.getDeviceId(sid)); |
| 156 | } |
| 157 | |
| 158 | NeighborSet ns = new NeighborSet(deviceIds, tunnel.labelIds().get(2)); |
| 159 | |
| 160 | // If the tunnel reuses any existing groups, then tunnel handler |
| 161 | // should not remove the group. |
| 162 | if (srManager.hasNextObjectiveId(deviceId, ns)) { |
| 163 | tunnel.allowToRemoveGroup(false); |
| 164 | } else { |
| 165 | tunnel.allowToRemoveGroup(true); |
| 166 | } |
| 167 | groupId = srManager.getNextObjectiveId(deviceId, ns); |
| 168 | |
| 169 | return groupId; |
| 170 | } |
| 171 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 172 | } |