blob: 820bb40ac36ccb08f038a68a76af0b05c531c572 [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;
21import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
sangho0b2b6d12015-05-20 22:16:38 -070022import org.onosproject.segmentrouting.grouphandler.NeighborSet;
23import org.onosproject.store.service.EventuallyConsistentMap;
sangho1e575652015-05-14 00:39:53 -070024import org.slf4j.Logger;
25
26import java.util.ArrayList;
sangho0b2b6d12015-05-20 22:16:38 -070027import java.util.HashSet;
sangho1e575652015-05-14 00:39:53 -070028import java.util.List;
sangho71abe1b2015-06-29 14:58:47 -070029import java.util.Map;
sangho0b2b6d12015-05-20 22:16:38 -070030import java.util.Set;
sangho1e575652015-05-14 00:39:53 -070031
32import static org.slf4j.LoggerFactory.getLogger;
33
34/**
35 * Tunnel Handler.
36 */
37public class TunnelHandler {
38 protected final Logger log = getLogger(getClass());
39
sangho0b2b6d12015-05-20 22:16:38 -070040 private final DeviceConfiguration config;
41 private final EventuallyConsistentMap<String, Tunnel> tunnelStore;
sangho71abe1b2015-06-29 14:58:47 -070042 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap;
43 private LinkService linkService;
sangho1e575652015-05-14 00:39:53 -070044
sangho71abe1b2015-06-29 14:58:47 -070045 public enum Result {
46 SUCCESS,
47 WRONG_PATH,
48 TUNNEL_EXISTS,
49 ID_EXISTS,
50 TUNNEL_NOT_FOUND,
51 TUNNEL_IN_USE,
52 INTERNAL_ERROR
53 }
54
55 public TunnelHandler(LinkService linkService,
56 DeviceConfiguration deviceConfiguration,
57 Map<DeviceId, DefaultGroupHandler> groupHandlerMap,
sangho0b2b6d12015-05-20 22:16:38 -070058 EventuallyConsistentMap<String, Tunnel> tunnelStore) {
sangho71abe1b2015-06-29 14:58:47 -070059 this.linkService = linkService;
60 this.config = deviceConfiguration;
61 this.groupHandlerMap = groupHandlerMap;
sangho0b2b6d12015-05-20 22:16:38 -070062 this.tunnelStore = tunnelStore;
sangho1e575652015-05-14 00:39:53 -070063 }
64
65 /**
66 * Creates a tunnel.
67 *
68 * @param tunnel tunnel reference to create a tunnel
sangho71abe1b2015-06-29 14:58:47 -070069 * @return WRONG_PATH if the tunnel path is wrong, ID_EXISTS if the tunnel ID
70 * exists already, TUNNEL_EXISTS if the same tunnel exists, INTERNAL_ERROR
71 * if the tunnel creation failed internally, SUCCESS if the tunnel is created
72 * successfully
sangho1e575652015-05-14 00:39:53 -070073 */
sangho71abe1b2015-06-29 14:58:47 -070074 public Result createTunnel(Tunnel tunnel) {
sangho0b2b6d12015-05-20 22:16:38 -070075
76 if (tunnel.labelIds().isEmpty() || tunnel.labelIds().size() < 3) {
77 log.error("More than one router needs to specified to created a tunnel");
sangho71abe1b2015-06-29 14:58:47 -070078 return Result.WRONG_PATH;
sangho0b2b6d12015-05-20 22:16:38 -070079 }
80
81 if (tunnelStore.containsKey(tunnel.id())) {
82 log.warn("The same tunnel ID exists already");
sangho71abe1b2015-06-29 14:58:47 -070083 return Result.ID_EXISTS;
sangho0b2b6d12015-05-20 22:16:38 -070084 }
85
86 if (tunnelStore.containsValue(tunnel)) {
87 log.warn("The same tunnel exists already");
sangho71abe1b2015-06-29 14:58:47 -070088 return Result.TUNNEL_EXISTS;
sangho0b2b6d12015-05-20 22:16:38 -070089 }
90
91 int groupId = createGroupsForTunnel(tunnel);
92 if (groupId < 0) {
93 log.error("Failed to create groups for the tunnel");
sangho71abe1b2015-06-29 14:58:47 -070094 return Result.INTERNAL_ERROR;
sangho0b2b6d12015-05-20 22:16:38 -070095 }
96
97 tunnel.setGroupId(groupId);
98 tunnelStore.put(tunnel.id(), tunnel);
99
sangho71abe1b2015-06-29 14:58:47 -0700100 return Result.SUCCESS;
sangho1e575652015-05-14 00:39:53 -0700101 }
102
103 /**
104 * Removes the tunnel with the tunnel ID given.
105 *
106 * @param tunnelInfo tunnel information to delete tunnels
sangho71abe1b2015-06-29 14:58:47 -0700107 * @return TUNNEL_NOT_FOUND if the tunnel to remove does not exists,
108 * INTERNAL_ERROR if the tunnel creation failed internally, SUCCESS
109 * if the tunnel is created successfully.
sangho1e575652015-05-14 00:39:53 -0700110 */
sangho71abe1b2015-06-29 14:58:47 -0700111 public Result removeTunnel(Tunnel tunnelInfo) {
sangho1e575652015-05-14 00:39:53 -0700112
sangho0b2b6d12015-05-20 22:16:38 -0700113 Tunnel tunnel = tunnelStore.get(tunnelInfo.id());
sangho1e575652015-05-14 00:39:53 -0700114 if (tunnel != null) {
sangho0b2b6d12015-05-20 22:16:38 -0700115 DeviceId deviceId = config.getDeviceId(tunnel.labelIds().get(0));
116 if (tunnel.isAllowedToRemoveGroup()) {
sangho71abe1b2015-06-29 14:58:47 -0700117 if (groupHandlerMap.get(deviceId).removeGroup(tunnel.groupId())) {
sangho0b2b6d12015-05-20 22:16:38 -0700118 tunnelStore.remove(tunnel.id());
119 } else {
120 log.error("Failed to remove the tunnel {}", tunnelInfo.id());
sangho71abe1b2015-06-29 14:58:47 -0700121 return Result.INTERNAL_ERROR;
sangho0b2b6d12015-05-20 22:16:38 -0700122 }
123 } else {
124 log.debug("The group is not removed because it is being used.");
125 tunnelStore.remove(tunnel.id());
126 }
sangho1e575652015-05-14 00:39:53 -0700127 } else {
sangho71abe1b2015-06-29 14:58:47 -0700128 log.error("No tunnel found for tunnel ID {}", tunnelInfo.id());
129 return Result.TUNNEL_NOT_FOUND;
sangho1e575652015-05-14 00:39:53 -0700130 }
sangho71abe1b2015-06-29 14:58:47 -0700131
132 return Result.SUCCESS;
sangho1e575652015-05-14 00:39:53 -0700133 }
134
135 /**
136 * Returns the tunnel with the tunnel ID given.
137 *
138 * @param tid Tunnel ID
139 * @return Tunnel reference
140 */
141 public Tunnel getTunnel(String tid) {
sangho0b2b6d12015-05-20 22:16:38 -0700142 return tunnelStore.get(tid);
sangho1e575652015-05-14 00:39:53 -0700143 }
144
145 /**
146 * Returns all tunnels.
147 *
148 * @return list of Tunnels
149 */
150 public List<Tunnel> getTunnels() {
151 List<Tunnel> tunnels = new ArrayList<>();
sangho0b2b6d12015-05-20 22:16:38 -0700152 tunnelStore.values().forEach(tunnel -> tunnels.add(
sangho1e575652015-05-14 00:39:53 -0700153 new DefaultTunnel((DefaultTunnel) tunnel)));
154
155 return tunnels;
156 }
sangho0b2b6d12015-05-20 22:16:38 -0700157
158 private int createGroupsForTunnel(Tunnel tunnel) {
159
160 List<Integer> portNumbers;
sangho71abe1b2015-06-29 14:58:47 -0700161 final int groupError = -1;
sangho0b2b6d12015-05-20 22:16:38 -0700162
163 DeviceId deviceId = config.getDeviceId(tunnel.labelIds().get(0));
164 if (deviceId == null) {
165 log.warn("No device found for SID {}", tunnel.labelIds().get(0));
sangho71abe1b2015-06-29 14:58:47 -0700166 return groupError;
167 } else if (groupHandlerMap.get(deviceId) == null) {
168 log.warn("group handler not found for {}", deviceId);
169 return groupError;
sangho0b2b6d12015-05-20 22:16:38 -0700170 }
171 Set<DeviceId> deviceIds = new HashSet<>();
172 int sid = tunnel.labelIds().get(1);
173 if (config.isAdjacencySid(deviceId, sid)) {
174 portNumbers = config.getPortsForAdjacencySid(deviceId, sid);
sangho71abe1b2015-06-29 14:58:47 -0700175 for (Link link: linkService.getDeviceEgressLinks(deviceId)) {
sangho0b2b6d12015-05-20 22:16:38 -0700176 for (Integer port: portNumbers) {
177 if (link.src().port().toLong() == port) {
178 deviceIds.add(link.dst().deviceId());
179 }
180 }
181 }
182 } else {
183 deviceIds.add(config.getDeviceId(sid));
184 }
185
186 NeighborSet ns = new NeighborSet(deviceIds, tunnel.labelIds().get(2));
187
188 // If the tunnel reuses any existing groups, then tunnel handler
189 // should not remove the group.
sangho71abe1b2015-06-29 14:58:47 -0700190 if (groupHandlerMap.get(deviceId).hasNextObjectiveId(ns)) {
sangho0b2b6d12015-05-20 22:16:38 -0700191 tunnel.allowToRemoveGroup(false);
192 } else {
193 tunnel.allowToRemoveGroup(true);
194 }
sangho0b2b6d12015-05-20 22:16:38 -0700195
sangho71abe1b2015-06-29 14:58:47 -0700196 return groupHandlerMap.get(deviceId).getNextObjectiveId(ns);
sangho0b2b6d12015-05-20 22:16:38 -0700197 }
198
sangho1e575652015-05-14 00:39:53 -0700199}