blob: 2405fa658d9520dd9a357135dded5a854fa6ae2b [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;
20import org.onosproject.segmentrouting.grouphandler.NeighborSet;
21import org.onosproject.store.service.EventuallyConsistentMap;
sangho1e575652015-05-14 00:39:53 -070022import org.slf4j.Logger;
23
24import java.util.ArrayList;
sangho0b2b6d12015-05-20 22:16:38 -070025import java.util.HashSet;
sangho1e575652015-05-14 00:39:53 -070026import java.util.List;
sangho0b2b6d12015-05-20 22:16:38 -070027import java.util.Set;
sangho1e575652015-05-14 00:39:53 -070028
sangho0b2b6d12015-05-20 22:16:38 -070029import static com.google.common.base.Preconditions.checkNotNull;
sangho1e575652015-05-14 00:39:53 -070030import static org.slf4j.LoggerFactory.getLogger;
31
32/**
33 * Tunnel Handler.
34 */
35public class TunnelHandler {
36 protected final Logger log = getLogger(getClass());
37
sangho0b2b6d12015-05-20 22:16:38 -070038 private final SegmentRoutingManager srManager;
39 private final DeviceConfiguration config;
40 private final EventuallyConsistentMap<String, Tunnel> tunnelStore;
sangho1e575652015-05-14 00:39:53 -070041
sangho0b2b6d12015-05-20 22:16:38 -070042 public TunnelHandler(SegmentRoutingManager srm,
43 EventuallyConsistentMap<String, Tunnel> tunnelStore) {
44 this.srManager = checkNotNull(srm);
45 this.config = srm.deviceConfiguration;
46 this.tunnelStore = tunnelStore;
sangho1e575652015-05-14 00:39:53 -070047 }
48
49 /**
50 * Creates a tunnel.
51 *
52 * @param tunnel tunnel reference to create a tunnel
Thomas Vachuskab0ca8c52015-05-21 16:41:41 -070053 * @return true if creation succeeded
sangho1e575652015-05-14 00:39:53 -070054 */
sangho0b2b6d12015-05-20 22:16:38 -070055 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;
sangho1e575652015-05-14 00:39:53 -070082 }
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
sangho0b2b6d12015-05-20 22:16:38 -070091 Tunnel tunnel = tunnelStore.get(tunnelInfo.id());
sangho1e575652015-05-14 00:39:53 -070092 if (tunnel != null) {
sangho0b2b6d12015-05-20 22:16:38 -070093 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 }
sangho1e575652015-05-14 00:39:53 -0700104 } 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) {
sangho0b2b6d12015-05-20 22:16:38 -0700116 return tunnelStore.get(tid);
sangho1e575652015-05-14 00:39:53 -0700117 }
118
119 /**
120 * Returns all tunnels.
121 *
122 * @return list of Tunnels
123 */
124 public List<Tunnel> getTunnels() {
125 List<Tunnel> tunnels = new ArrayList<>();
sangho0b2b6d12015-05-20 22:16:38 -0700126 tunnelStore.values().forEach(tunnel -> tunnels.add(
sangho1e575652015-05-14 00:39:53 -0700127 new DefaultTunnel((DefaultTunnel) tunnel)));
128
129 return tunnels;
130 }
sangho0b2b6d12015-05-20 22:16:38 -0700131
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
sangho1e575652015-05-14 00:39:53 -0700172}