blob: 4a498c6f8e957c1e0eb43c6db88df4a23dbc8e32 [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
18import org.slf4j.Logger;
19
20import java.util.ArrayList;
21import java.util.List;
22import java.util.HashMap;
23
24import static org.slf4j.LoggerFactory.getLogger;
25
26/**
27 * Tunnel Handler.
28 */
29public class TunnelHandler {
30 protected final Logger log = getLogger(getClass());
31
32 private final HashMap<String, Tunnel> tunnelMap;
33
34 public TunnelHandler() {
35 tunnelMap = new HashMap<>();
36 }
37
38 /**
39 * Creates a tunnel.
40 *
41 * @param tunnel tunnel reference to create a tunnel
42 */
43 public void createTunnel(Tunnel tunnel) {
44 tunnel.create();
45 tunnelMap.put(tunnel.id(), tunnel);
46 }
47
48 /**
49 * Removes the tunnel with the tunnel ID given.
50 *
51 * @param tunnelInfo tunnel information to delete tunnels
52 */
53 public void removeTunnel(Tunnel tunnelInfo) {
54
55 Tunnel tunnel = tunnelMap.get(tunnelInfo.id());
56 if (tunnel != null) {
57 tunnel.remove();
58 tunnelMap.remove(tunnel.id());
59 } else {
60 log.warn("No tunnel found for tunnel ID {}", tunnelInfo.id());
61 }
62 }
63
64 /**
65 * Returns the tunnel with the tunnel ID given.
66 *
67 * @param tid Tunnel ID
68 * @return Tunnel reference
69 */
70 public Tunnel getTunnel(String tid) {
71 return tunnelMap.get(tid);
72 }
73
74 /**
75 * Returns all tunnels.
76 *
77 * @return list of Tunnels
78 */
79 public List<Tunnel> getTunnels() {
80 List<Tunnel> tunnels = new ArrayList<>();
81 tunnelMap.values().forEach(tunnel -> tunnels.add(
82 new DefaultTunnel((DefaultTunnel) tunnel)));
83
84 return tunnels;
85 }
86}