blob: a3ee137340e41030bcee2a541bb43ef756548758 [file] [log] [blame]
sangho27462c62015-05-14 00:39:53 -07001/*
Brian O'Connor43b53542016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sangho27462c62015-05-14 00:39:53 -07003 *
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
sangho4a5c42a2015-05-20 22:16:38 -070018import org.onosproject.net.DeviceId;
19import org.onosproject.net.Link;
sanghobd812f82015-06-29 14:58:47 -070020import org.onosproject.net.link.LinkService;
Charles Chan319d1a22015-11-03 10:42:14 -080021import org.onosproject.segmentrouting.config.DeviceConfiguration;
sanghobd812f82015-06-29 14:58:47 -070022import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
sangho4a5c42a2015-05-20 22:16:38 -070023import org.onosproject.segmentrouting.grouphandler.NeighborSet;
24import org.onosproject.store.service.EventuallyConsistentMap;
sangho27462c62015-05-14 00:39:53 -070025import org.slf4j.Logger;
26
27import java.util.ArrayList;
sangho4a5c42a2015-05-20 22:16:38 -070028import java.util.HashSet;
sangho27462c62015-05-14 00:39:53 -070029import java.util.List;
sanghobd812f82015-06-29 14:58:47 -070030import java.util.Map;
sangho4a5c42a2015-05-20 22:16:38 -070031import java.util.Set;
sangho27462c62015-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
sangho4a5c42a2015-05-20 22:16:38 -070041 private final DeviceConfiguration config;
42 private final EventuallyConsistentMap<String, Tunnel> tunnelStore;
sanghobd812f82015-06-29 14:58:47 -070043 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap;
44 private LinkService linkService;
sangho27462c62015-05-14 00:39:53 -070045
Charles Chanb7f75ac2016-01-11 18:28:54 -080046 /**
47 * Result of tunnel creation or removal.
48 */
sanghobd812f82015-06-29 14:58:47 -070049 public enum Result {
Charles Chanb7f75ac2016-01-11 18:28:54 -080050 /**
51 * Success.
52 */
sanghobd812f82015-06-29 14:58:47 -070053 SUCCESS,
Charles Chanb7f75ac2016-01-11 18:28:54 -080054
55 /**
56 * More than one router needs to specified to created a tunnel.
57 */
sanghobd812f82015-06-29 14:58:47 -070058 WRONG_PATH,
Charles Chanb7f75ac2016-01-11 18:28:54 -080059
60 /**
61 * The same tunnel exists already.
62 */
sanghobd812f82015-06-29 14:58:47 -070063 TUNNEL_EXISTS,
Charles Chanb7f75ac2016-01-11 18:28:54 -080064
65 /**
66 * The same tunnel ID exists already.
67 */
sanghobd812f82015-06-29 14:58:47 -070068 ID_EXISTS,
Charles Chanb7f75ac2016-01-11 18:28:54 -080069
70 /**
71 * Tunnel not found.
72 */
sanghobd812f82015-06-29 14:58:47 -070073 TUNNEL_NOT_FOUND,
Charles Chanb7f75ac2016-01-11 18:28:54 -080074
75 /**
76 * Cannot remove the tunnel used by a policy.
77 */
sanghobd812f82015-06-29 14:58:47 -070078 TUNNEL_IN_USE,
Charles Chanb7f75ac2016-01-11 18:28:54 -080079
80 /**
81 * Failed to create/remove groups for the tunnel.
82 */
sanghobd812f82015-06-29 14:58:47 -070083 INTERNAL_ERROR
84 }
85
Charles Chanb7f75ac2016-01-11 18:28:54 -080086 /**
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 */
sanghobd812f82015-06-29 14:58:47 -070094 public TunnelHandler(LinkService linkService,
95 DeviceConfiguration deviceConfiguration,
96 Map<DeviceId, DefaultGroupHandler> groupHandlerMap,
sangho4a5c42a2015-05-20 22:16:38 -070097 EventuallyConsistentMap<String, Tunnel> tunnelStore) {
sanghobd812f82015-06-29 14:58:47 -070098 this.linkService = linkService;
99 this.config = deviceConfiguration;
100 this.groupHandlerMap = groupHandlerMap;
sangho4a5c42a2015-05-20 22:16:38 -0700101 this.tunnelStore = tunnelStore;
sangho27462c62015-05-14 00:39:53 -0700102 }
103
104 /**
105 * Creates a tunnel.
106 *
107 * @param tunnel tunnel reference to create a tunnel
sanghobd812f82015-06-29 14:58:47 -0700108 * @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
sangho27462c62015-05-14 00:39:53 -0700112 */
sanghobd812f82015-06-29 14:58:47 -0700113 public Result createTunnel(Tunnel tunnel) {
sangho4a5c42a2015-05-20 22:16:38 -0700114
115 if (tunnel.labelIds().isEmpty() || tunnel.labelIds().size() < 3) {
116 log.error("More than one router needs to specified to created a tunnel");
sanghobd812f82015-06-29 14:58:47 -0700117 return Result.WRONG_PATH;
sangho4a5c42a2015-05-20 22:16:38 -0700118 }
119
120 if (tunnelStore.containsKey(tunnel.id())) {
121 log.warn("The same tunnel ID exists already");
sanghobd812f82015-06-29 14:58:47 -0700122 return Result.ID_EXISTS;
sangho4a5c42a2015-05-20 22:16:38 -0700123 }
124
125 if (tunnelStore.containsValue(tunnel)) {
126 log.warn("The same tunnel exists already");
sanghobd812f82015-06-29 14:58:47 -0700127 return Result.TUNNEL_EXISTS;
sangho4a5c42a2015-05-20 22:16:38 -0700128 }
129
130 int groupId = createGroupsForTunnel(tunnel);
131 if (groupId < 0) {
132 log.error("Failed to create groups for the tunnel");
sanghobd812f82015-06-29 14:58:47 -0700133 return Result.INTERNAL_ERROR;
sangho4a5c42a2015-05-20 22:16:38 -0700134 }
135
136 tunnel.setGroupId(groupId);
137 tunnelStore.put(tunnel.id(), tunnel);
138
sanghobd812f82015-06-29 14:58:47 -0700139 return Result.SUCCESS;
sangho27462c62015-05-14 00:39:53 -0700140 }
141
142 /**
143 * Removes the tunnel with the tunnel ID given.
144 *
145 * @param tunnelInfo tunnel information to delete tunnels
sanghobd812f82015-06-29 14:58:47 -0700146 * @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.
sangho27462c62015-05-14 00:39:53 -0700149 */
sanghobd812f82015-06-29 14:58:47 -0700150 public Result removeTunnel(Tunnel tunnelInfo) {
sangho27462c62015-05-14 00:39:53 -0700151
sangho4a5c42a2015-05-20 22:16:38 -0700152 Tunnel tunnel = tunnelStore.get(tunnelInfo.id());
sangho27462c62015-05-14 00:39:53 -0700153 if (tunnel != null) {
sangho4a5c42a2015-05-20 22:16:38 -0700154 DeviceId deviceId = config.getDeviceId(tunnel.labelIds().get(0));
155 if (tunnel.isAllowedToRemoveGroup()) {
sanghobd812f82015-06-29 14:58:47 -0700156 if (groupHandlerMap.get(deviceId).removeGroup(tunnel.groupId())) {
sangho4a5c42a2015-05-20 22:16:38 -0700157 tunnelStore.remove(tunnel.id());
158 } else {
159 log.error("Failed to remove the tunnel {}", tunnelInfo.id());
sanghobd812f82015-06-29 14:58:47 -0700160 return Result.INTERNAL_ERROR;
sangho4a5c42a2015-05-20 22:16:38 -0700161 }
162 } else {
163 log.debug("The group is not removed because it is being used.");
164 tunnelStore.remove(tunnel.id());
165 }
sangho27462c62015-05-14 00:39:53 -0700166 } else {
sanghobd812f82015-06-29 14:58:47 -0700167 log.error("No tunnel found for tunnel ID {}", tunnelInfo.id());
168 return Result.TUNNEL_NOT_FOUND;
sangho27462c62015-05-14 00:39:53 -0700169 }
sanghobd812f82015-06-29 14:58:47 -0700170
171 return Result.SUCCESS;
sangho27462c62015-05-14 00:39:53 -0700172 }
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) {
sangho4a5c42a2015-05-20 22:16:38 -0700181 return tunnelStore.get(tid);
sangho27462c62015-05-14 00:39:53 -0700182 }
183
184 /**
185 * Returns all tunnels.
186 *
187 * @return list of Tunnels
188 */
189 public List<Tunnel> getTunnels() {
190 List<Tunnel> tunnels = new ArrayList<>();
sangho4a5c42a2015-05-20 22:16:38 -0700191 tunnelStore.values().forEach(tunnel -> tunnels.add(
sangho27462c62015-05-14 00:39:53 -0700192 new DefaultTunnel((DefaultTunnel) tunnel)));
193
194 return tunnels;
195 }
sangho4a5c42a2015-05-20 22:16:38 -0700196
197 private int createGroupsForTunnel(Tunnel tunnel) {
198
Charles Chan9bec8a32015-12-01 10:00:51 -0800199 Set<Integer> portNumbers;
sanghobd812f82015-06-29 14:58:47 -0700200 final int groupError = -1;
sangho4a5c42a2015-05-20 22:16:38 -0700201
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));
sanghobd812f82015-06-29 14:58:47 -0700205 return groupError;
206 } else if (groupHandlerMap.get(deviceId) == null) {
207 log.warn("group handler not found for {}", deviceId);
208 return groupError;
sangho4a5c42a2015-05-20 22:16:38 -0700209 }
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);
sanghobd812f82015-06-29 14:58:47 -0700214 for (Link link: linkService.getDeviceEgressLinks(deviceId)) {
sangho4a5c42a2015-05-20 22:16:38 -0700215 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 }
224
225 NeighborSet ns = new NeighborSet(deviceIds, tunnel.labelIds().get(2));
226
227 // If the tunnel reuses any existing groups, then tunnel handler
228 // should not remove the group.
sanghobd812f82015-06-29 14:58:47 -0700229 if (groupHandlerMap.get(deviceId).hasNextObjectiveId(ns)) {
sangho4a5c42a2015-05-20 22:16:38 -0700230 tunnel.allowToRemoveGroup(false);
231 } else {
232 tunnel.allowToRemoveGroup(true);
233 }
sangho4a5c42a2015-05-20 22:16:38 -0700234
Saurav Das4c35fc42015-11-20 15:27:53 -0800235 return groupHandlerMap.get(deviceId).getNextObjectiveId(ns, null);
sangho4a5c42a2015-05-20 22:16:38 -0700236 }
237
sangho27462c62015-05-14 00:39:53 -0700238}