b86adada013c897921d3423d0e954f6e31f62c95
[onosfw.git] /
1 /*
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  */
16 package org.onosproject.segmentrouting;
17
18 import org.onosproject.net.DeviceId;
19 import org.onosproject.net.Link;
20 import org.onosproject.net.link.LinkService;
21 import org.onosproject.segmentrouting.config.DeviceConfiguration;
22 import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
23 import org.onosproject.segmentrouting.grouphandler.NeighborSet;
24 import org.onosproject.store.service.EventuallyConsistentMap;
25 import org.slf4j.Logger;
26
27 import java.util.ArrayList;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32
33 import static org.slf4j.LoggerFactory.getLogger;
34
35 /**
36  * Tunnel Handler.
37  */
38 public class TunnelHandler {
39     protected final Logger log = getLogger(getClass());
40
41     private final DeviceConfiguration config;
42     private final EventuallyConsistentMap<String, Tunnel> tunnelStore;
43     private Map<DeviceId, DefaultGroupHandler> groupHandlerMap;
44     private LinkService linkService;
45
46     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,
59                          EventuallyConsistentMap<String, Tunnel> tunnelStore) {
60         this.linkService = linkService;
61         this.config = deviceConfiguration;
62         this.groupHandlerMap = groupHandlerMap;
63         this.tunnelStore = tunnelStore;
64     }
65
66     /**
67      * Creates a tunnel.
68      *
69      * @param tunnel tunnel reference to create a tunnel
70      * @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
74      */
75     public Result createTunnel(Tunnel tunnel) {
76
77         if (tunnel.labelIds().isEmpty() || tunnel.labelIds().size() < 3) {
78             log.error("More than one router needs to specified to created a tunnel");
79             return Result.WRONG_PATH;
80         }
81
82         if (tunnelStore.containsKey(tunnel.id())) {
83             log.warn("The same tunnel ID exists already");
84             return Result.ID_EXISTS;
85         }
86
87         if (tunnelStore.containsValue(tunnel)) {
88             log.warn("The same tunnel exists already");
89             return Result.TUNNEL_EXISTS;
90         }
91
92         int groupId = createGroupsForTunnel(tunnel);
93         if (groupId < 0) {
94             log.error("Failed to create groups for the tunnel");
95             return Result.INTERNAL_ERROR;
96         }
97
98         tunnel.setGroupId(groupId);
99         tunnelStore.put(tunnel.id(), tunnel);
100
101         return Result.SUCCESS;
102     }
103
104     /**
105      * Removes the tunnel with the tunnel ID given.
106      *
107      * @param tunnelInfo tunnel information to delete tunnels
108      * @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.
111      */
112     public Result removeTunnel(Tunnel tunnelInfo) {
113
114         Tunnel tunnel = tunnelStore.get(tunnelInfo.id());
115         if (tunnel != null) {
116             DeviceId deviceId = config.getDeviceId(tunnel.labelIds().get(0));
117             if (tunnel.isAllowedToRemoveGroup()) {
118                 if (groupHandlerMap.get(deviceId).removeGroup(tunnel.groupId())) {
119                     tunnelStore.remove(tunnel.id());
120                 } else {
121                     log.error("Failed to remove the tunnel {}", tunnelInfo.id());
122                     return Result.INTERNAL_ERROR;
123                 }
124             } else {
125                 log.debug("The group is not removed because it is being used.");
126                 tunnelStore.remove(tunnel.id());
127             }
128         } else {
129             log.error("No tunnel found for tunnel ID {}", tunnelInfo.id());
130             return Result.TUNNEL_NOT_FOUND;
131         }
132
133         return Result.SUCCESS;
134     }
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) {
143         return tunnelStore.get(tid);
144     }
145
146     /**
147      * Returns all tunnels.
148      *
149      * @return list of Tunnels
150      */
151     public List<Tunnel> getTunnels() {
152         List<Tunnel> tunnels = new ArrayList<>();
153         tunnelStore.values().forEach(tunnel -> tunnels.add(
154                 new DefaultTunnel((DefaultTunnel) tunnel)));
155
156         return tunnels;
157     }
158
159     private int createGroupsForTunnel(Tunnel tunnel) {
160
161         List<Integer> portNumbers;
162         final int groupError = -1;
163
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));
167             return groupError;
168         } else if (groupHandlerMap.get(deviceId) == null) {
169             log.warn("group handler not found for {}", deviceId);
170             return groupError;
171         }
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);
176             for (Link link: linkService.getDeviceEgressLinks(deviceId)) {
177                 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.
191         if (groupHandlerMap.get(deviceId).hasNextObjectiveId(ns)) {
192             tunnel.allowToRemoveGroup(false);
193         } else {
194             tunnel.allowToRemoveGroup(true);
195         }
196
197         return groupHandlerMap.get(deviceId).getNextObjectiveId(ns, null);
198     }
199
200 }