blob: 1c07093e8a37de3170ada5e12a9b85701f863754 [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
53 */
sangho0b2b6d12015-05-20 22:16:38 -070054 public boolean createTunnel(Tunnel tunnel) {
55
56 if (tunnel.labelIds().isEmpty() || tunnel.labelIds().size() < 3) {
57 log.error("More than one router needs to specified to created a tunnel");
58 return false;
59 }
60
61 if (tunnelStore.containsKey(tunnel.id())) {
62 log.warn("The same tunnel ID exists already");
63 return false;
64 }
65
66 if (tunnelStore.containsValue(tunnel)) {
67 log.warn("The same tunnel exists already");
68 return false;
69 }
70
71 int groupId = createGroupsForTunnel(tunnel);
72 if (groupId < 0) {
73 log.error("Failed to create groups for the tunnel");
74 return false;
75 }
76
77 tunnel.setGroupId(groupId);
78 tunnelStore.put(tunnel.id(), tunnel);
79
80 return true;
sangho1e575652015-05-14 00:39:53 -070081 }
82
83 /**
84 * Removes the tunnel with the tunnel ID given.
85 *
86 * @param tunnelInfo tunnel information to delete tunnels
87 */
88 public void removeTunnel(Tunnel tunnelInfo) {
89
sangho0b2b6d12015-05-20 22:16:38 -070090 Tunnel tunnel = tunnelStore.get(tunnelInfo.id());
sangho1e575652015-05-14 00:39:53 -070091 if (tunnel != null) {
sangho0b2b6d12015-05-20 22:16:38 -070092 DeviceId deviceId = config.getDeviceId(tunnel.labelIds().get(0));
93 if (tunnel.isAllowedToRemoveGroup()) {
94 if (srManager.removeNextObjective(deviceId, tunnel.groupId())) {
95 tunnelStore.remove(tunnel.id());
96 } else {
97 log.error("Failed to remove the tunnel {}", tunnelInfo.id());
98 }
99 } else {
100 log.debug("The group is not removed because it is being used.");
101 tunnelStore.remove(tunnel.id());
102 }
sangho1e575652015-05-14 00:39:53 -0700103 } else {
104 log.warn("No tunnel found for tunnel ID {}", tunnelInfo.id());
105 }
106 }
107
108 /**
109 * Returns the tunnel with the tunnel ID given.
110 *
111 * @param tid Tunnel ID
112 * @return Tunnel reference
113 */
114 public Tunnel getTunnel(String tid) {
sangho0b2b6d12015-05-20 22:16:38 -0700115 return tunnelStore.get(tid);
sangho1e575652015-05-14 00:39:53 -0700116 }
117
118 /**
119 * Returns all tunnels.
120 *
121 * @return list of Tunnels
122 */
123 public List<Tunnel> getTunnels() {
124 List<Tunnel> tunnels = new ArrayList<>();
sangho0b2b6d12015-05-20 22:16:38 -0700125 tunnelStore.values().forEach(tunnel -> tunnels.add(
sangho1e575652015-05-14 00:39:53 -0700126 new DefaultTunnel((DefaultTunnel) tunnel)));
127
128 return tunnels;
129 }
sangho0b2b6d12015-05-20 22:16:38 -0700130
131 private int createGroupsForTunnel(Tunnel tunnel) {
132
133 List<Integer> portNumbers;
134
135 int groupId;
136
137 DeviceId deviceId = config.getDeviceId(tunnel.labelIds().get(0));
138 if (deviceId == null) {
139 log.warn("No device found for SID {}", tunnel.labelIds().get(0));
140 return -1;
141 }
142 Set<DeviceId> deviceIds = new HashSet<>();
143 int sid = tunnel.labelIds().get(1);
144 if (config.isAdjacencySid(deviceId, sid)) {
145 portNumbers = config.getPortsForAdjacencySid(deviceId, sid);
146 for (Link link: srManager.linkService.getDeviceEgressLinks(deviceId)) {
147 for (Integer port: portNumbers) {
148 if (link.src().port().toLong() == port) {
149 deviceIds.add(link.dst().deviceId());
150 }
151 }
152 }
153 } else {
154 deviceIds.add(config.getDeviceId(sid));
155 }
156
157 NeighborSet ns = new NeighborSet(deviceIds, tunnel.labelIds().get(2));
158
159 // If the tunnel reuses any existing groups, then tunnel handler
160 // should not remove the group.
161 if (srManager.hasNextObjectiveId(deviceId, ns)) {
162 tunnel.allowToRemoveGroup(false);
163 } else {
164 tunnel.allowToRemoveGroup(true);
165 }
166 groupId = srManager.getNextObjectiveId(deviceId, ns);
167
168 return groupId;
169 }
170
sangho1e575652015-05-14 00:39:53 -0700171}