blob: b86adada013c897921d3423d0e954f6e31f62c95 [file] [log] [blame]
sangho1e575652015-05-14 00:39:53 -07001/*
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 */
16package org.onosproject.segmentrouting;
17
sangho0b2b6d12015-05-20 22:16:38 -070018import org.onosproject.net.DeviceId;
19import org.onosproject.net.Link;
sangho71abe1b2015-06-29 14:58:47 -070020import org.onosproject.net.link.LinkService;
Charles Chan0b4e6182015-11-03 10:42:14 -080021import org.onosproject.segmentrouting.config.DeviceConfiguration;
sangho71abe1b2015-06-29 14:58:47 -070022import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
sangho0b2b6d12015-05-20 22:16:38 -070023import org.onosproject.segmentrouting.grouphandler.NeighborSet;
24import org.onosproject.store.service.EventuallyConsistentMap;
sangho1e575652015-05-14 00:39:53 -070025import org.slf4j.Logger;
26
27import java.util.ArrayList;
sangho0b2b6d12015-05-20 22:16:38 -070028import java.util.HashSet;
sangho1e575652015-05-14 00:39:53 -070029import java.util.List;
sangho71abe1b2015-06-29 14:58:47 -070030import java.util.Map;
sangho0b2b6d12015-05-20 22:16:38 -070031import java.util.Set;
sangho1e575652015-05-14 00:39:53 -070032
33import static org.slf4j.LoggerFactory.getLogger;
34
35/**
36 * Tunnel Handler.
37 */
38public class TunnelHandler {
39 protected final Logger log = getLogger(getClass());
40
sangho0b2b6d12015-05-20 22:16:38 -070041 private final DeviceConfiguration config;
42 private final EventuallyConsistentMap<String, Tunnel> tunnelStore;
sangho71abe1b2015-06-29 14:58:47 -070043 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap;
44 private LinkService linkService;
sangho1e575652015-05-14 00:39:53 -070045
sangho71abe1b2015-06-29 14:58:47 -070046 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,
sangho0b2b6d12015-05-20 22:16:38 -070059 EventuallyConsistentMap<String, Tunnel> tunnelStore) {
sangho71abe1b2015-06-29 14:58:47 -070060 this.linkService = linkService;
61 this.config = deviceConfiguration;
62 this.groupHandlerMap = groupHandlerMap;
sangho0b2b6d12015-05-20 22:16:38 -070063 this.tunnelStore = tunnelStore;
sangho1e575652015-05-14 00:39:53 -070064 }
65
66 /**
67 * Creates a tunnel.
68 *
69 * @param tunnel tunnel reference to create a tunnel
sangho71abe1b2015-06-29 14:58:47 -070070 * @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
sangho1e575652015-05-14 00:39:53 -070074 */
sangho71abe1b2015-06-29 14:58:47 -070075 public Result createTunnel(Tunnel tunnel) {
sangho0b2b6d12015-05-20 22:16:38 -070076
77 if (tunnel.labelIds().isEmpty() || tunnel.labelIds().size() < 3) {
78 log.error("More than one router needs to specified to created a tunnel");
sangho71abe1b2015-06-29 14:58:47 -070079 return Result.WRONG_PATH;
sangho0b2b6d12015-05-20 22:16:38 -070080 }
81
82 if (tunnelStore.containsKey(tunnel.id())) {
83 log.warn("The same tunnel ID exists already");
sangho71abe1b2015-06-29 14:58:47 -070084 return Result.ID_EXISTS;
sangho0b2b6d12015-05-20 22:16:38 -070085 }
86
87 if (tunnelStore.containsValue(tunnel)) {
88 log.warn("The same tunnel exists already");
sangho71abe1b2015-06-29 14:58:47 -070089 return Result.TUNNEL_EXISTS;
sangho0b2b6d12015-05-20 22:16:38 -070090 }
91
92 int groupId = createGroupsForTunnel(tunnel);
93 if (groupId < 0) {
94 log.error("Failed to create groups for the tunnel");
sangho71abe1b2015-06-29 14:58:47 -070095 return Result.INTERNAL_ERROR;
sangho0b2b6d12015-05-20 22:16:38 -070096 }
97
98 tunnel.setGroupId(groupId);
99 tunnelStore.put(tunnel.id(), tunnel);
100
sangho71abe1b2015-06-29 14:58:47 -0700101 return Result.SUCCESS;
sangho1e575652015-05-14 00:39:53 -0700102 }
103
104 /**
105 * Removes the tunnel with the tunnel ID given.
106 *
107 * @param tunnelInfo tunnel information to delete tunnels
sangho71abe1b2015-06-29 14:58:47 -0700108 * @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.
sangho1e575652015-05-14 00:39:53 -0700111 */
sangho71abe1b2015-06-29 14:58:47 -0700112 public Result removeTunnel(Tunnel tunnelInfo) {
sangho1e575652015-05-14 00:39:53 -0700113
sangho0b2b6d12015-05-20 22:16:38 -0700114 Tunnel tunnel = tunnelStore.get(tunnelInfo.id());
sangho1e575652015-05-14 00:39:53 -0700115 if (tunnel != null) {
sangho0b2b6d12015-05-20 22:16:38 -0700116 DeviceId deviceId = config.getDeviceId(tunnel.labelIds().get(0));
117 if (tunnel.isAllowedToRemoveGroup()) {
sangho71abe1b2015-06-29 14:58:47 -0700118 if (groupHandlerMap.get(deviceId).removeGroup(tunnel.groupId())) {
sangho0b2b6d12015-05-20 22:16:38 -0700119 tunnelStore.remove(tunnel.id());
120 } else {
121 log.error("Failed to remove the tunnel {}", tunnelInfo.id());
sangho71abe1b2015-06-29 14:58:47 -0700122 return Result.INTERNAL_ERROR;
sangho0b2b6d12015-05-20 22:16:38 -0700123 }
124 } else {
125 log.debug("The group is not removed because it is being used.");
126 tunnelStore.remove(tunnel.id());
127 }
sangho1e575652015-05-14 00:39:53 -0700128 } else {
sangho71abe1b2015-06-29 14:58:47 -0700129 log.error("No tunnel found for tunnel ID {}", tunnelInfo.id());
130 return Result.TUNNEL_NOT_FOUND;
sangho1e575652015-05-14 00:39:53 -0700131 }
sangho71abe1b2015-06-29 14:58:47 -0700132
133 return Result.SUCCESS;
sangho1e575652015-05-14 00:39:53 -0700134 }
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) {
sangho0b2b6d12015-05-20 22:16:38 -0700143 return tunnelStore.get(tid);
sangho1e575652015-05-14 00:39:53 -0700144 }
145
146 /**
147 * Returns all tunnels.
148 *
149 * @return list of Tunnels
150 */
151 public List<Tunnel> getTunnels() {
152 List<Tunnel> tunnels = new ArrayList<>();
sangho0b2b6d12015-05-20 22:16:38 -0700153 tunnelStore.values().forEach(tunnel -> tunnels.add(
sangho1e575652015-05-14 00:39:53 -0700154 new DefaultTunnel((DefaultTunnel) tunnel)));
155
156 return tunnels;
157 }
sangho0b2b6d12015-05-20 22:16:38 -0700158
159 private int createGroupsForTunnel(Tunnel tunnel) {
160
161 List<Integer> portNumbers;
sangho71abe1b2015-06-29 14:58:47 -0700162 final int groupError = -1;
sangho0b2b6d12015-05-20 22:16:38 -0700163
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));
sangho71abe1b2015-06-29 14:58:47 -0700167 return groupError;
168 } else if (groupHandlerMap.get(deviceId) == null) {
169 log.warn("group handler not found for {}", deviceId);
170 return groupError;
sangho0b2b6d12015-05-20 22:16:38 -0700171 }
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);
sangho71abe1b2015-06-29 14:58:47 -0700176 for (Link link: linkService.getDeviceEgressLinks(deviceId)) {
sangho0b2b6d12015-05-20 22:16:38 -0700177 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.
sangho71abe1b2015-06-29 14:58:47 -0700191 if (groupHandlerMap.get(deviceId).hasNextObjectiveId(ns)) {
sangho0b2b6d12015-05-20 22:16:38 -0700192 tunnel.allowToRemoveGroup(false);
193 } else {
194 tunnel.allowToRemoveGroup(true);
195 }
sangho0b2b6d12015-05-20 22:16:38 -0700196
Saurav Das8a0732e2015-11-20 15:27:53 -0800197 return groupHandlerMap.get(deviceId).getNextObjectiveId(ns, null);
sangho0b2b6d12015-05-20 22:16:38 -0700198 }
199
sangho1e575652015-05-14 00:39:53 -0700200}