2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.segmentrouting;
18 import org.onosproject.net.DeviceId;
19 import org.onosproject.net.Link;
20 import org.onosproject.net.link.LinkService;
21 import org.onosproject.segmentrouting.config.DeviceConfiguration;
22 import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
23 import org.onosproject.segmentrouting.grouphandler.NeighborSet;
24 import org.onosproject.store.service.EventuallyConsistentMap;
25 import org.slf4j.Logger;
27 import java.util.ArrayList;
28 import java.util.HashSet;
29 import java.util.List;
33 import static org.slf4j.LoggerFactory.getLogger;
38 public class TunnelHandler {
39 protected final Logger log = getLogger(getClass());
41 private final DeviceConfiguration config;
42 private final EventuallyConsistentMap<String, Tunnel> tunnelStore;
43 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap;
44 private LinkService linkService;
56 public TunnelHandler(LinkService linkService,
57 DeviceConfiguration deviceConfiguration,
58 Map<DeviceId, DefaultGroupHandler> groupHandlerMap,
59 EventuallyConsistentMap<String, Tunnel> tunnelStore) {
60 this.linkService = linkService;
61 this.config = deviceConfiguration;
62 this.groupHandlerMap = groupHandlerMap;
63 this.tunnelStore = tunnelStore;
69 * @param tunnel tunnel reference to create a tunnel
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
75 public Result createTunnel(Tunnel tunnel) {
77 if (tunnel.labelIds().isEmpty() || tunnel.labelIds().size() < 3) {
78 log.error("More than one router needs to specified to created a tunnel");
79 return Result.WRONG_PATH;
82 if (tunnelStore.containsKey(tunnel.id())) {
83 log.warn("The same tunnel ID exists already");
84 return Result.ID_EXISTS;
87 if (tunnelStore.containsValue(tunnel)) {
88 log.warn("The same tunnel exists already");
89 return Result.TUNNEL_EXISTS;
92 int groupId = createGroupsForTunnel(tunnel);
94 log.error("Failed to create groups for the tunnel");
95 return Result.INTERNAL_ERROR;
98 tunnel.setGroupId(groupId);
99 tunnelStore.put(tunnel.id(), tunnel);
101 return Result.SUCCESS;
105 * Removes the tunnel with the tunnel ID given.
107 * @param tunnelInfo tunnel information to delete tunnels
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.
112 public Result removeTunnel(Tunnel tunnelInfo) {
114 Tunnel tunnel = tunnelStore.get(tunnelInfo.id());
115 if (tunnel != null) {
116 DeviceId deviceId = config.getDeviceId(tunnel.labelIds().get(0));
117 if (tunnel.isAllowedToRemoveGroup()) {
118 if (groupHandlerMap.get(deviceId).removeGroup(tunnel.groupId())) {
119 tunnelStore.remove(tunnel.id());
121 log.error("Failed to remove the tunnel {}", tunnelInfo.id());
122 return Result.INTERNAL_ERROR;
125 log.debug("The group is not removed because it is being used.");
126 tunnelStore.remove(tunnel.id());
129 log.error("No tunnel found for tunnel ID {}", tunnelInfo.id());
130 return Result.TUNNEL_NOT_FOUND;
133 return Result.SUCCESS;
137 * Returns the tunnel with the tunnel ID given.
139 * @param tid Tunnel ID
140 * @return Tunnel reference
142 public Tunnel getTunnel(String tid) {
143 return tunnelStore.get(tid);
147 * Returns all tunnels.
149 * @return list of Tunnels
151 public List<Tunnel> getTunnels() {
152 List<Tunnel> tunnels = new ArrayList<>();
153 tunnelStore.values().forEach(tunnel -> tunnels.add(
154 new DefaultTunnel((DefaultTunnel) tunnel)));
159 private int createGroupsForTunnel(Tunnel tunnel) {
161 List<Integer> portNumbers;
162 final int groupError = -1;
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));
168 } else if (groupHandlerMap.get(deviceId) == null) {
169 log.warn("group handler not found for {}", deviceId);
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);
176 for (Link link: linkService.getDeviceEgressLinks(deviceId)) {
177 for (Integer port: portNumbers) {
178 if (link.src().port().toLong() == port) {
179 deviceIds.add(link.dst().deviceId());
184 deviceIds.add(config.getDeviceId(sid));
187 NeighborSet ns = new NeighborSet(deviceIds, tunnel.labelIds().get(2));
189 // If the tunnel reuses any existing groups, then tunnel handler
190 // should not remove the group.
191 if (groupHandlerMap.get(deviceId).hasNextObjectiveId(ns)) {
192 tunnel.allowToRemoveGroup(false);
194 tunnel.allowToRemoveGroup(true);
197 return groupHandlerMap.get(deviceId).getNextObjectiveId(ns, null);