blob: f1c02167523d53b8a00f378bb6fae73be98c8c01 [file] [log] [blame]
sangho27462c62015-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 */
16
17package org.onosproject.segmentrouting;
18
19import org.onosproject.net.DeviceId;
20import org.onosproject.net.Link;
21import org.onosproject.segmentrouting.grouphandler.NeighborSet;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import java.util.Collections;
26import java.util.HashSet;
27import java.util.List;
28import java.util.Set;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Tunnel class.
34 */
35public class DefaultTunnel implements Tunnel {
36
37 private static final Logger log = LoggerFactory
38 .getLogger(DefaultTunnel.class);
39
40 private final String tunnelId;
41 private final List<Integer> labelIds;
42 private final SegmentRoutingManager srManager;
43 private final DeviceConfiguration config;
44
45 private int groupId;
46
47 /**
48 * Creates a Tunnel reference.
49 *
50 * @param srm SegmentRoutingManager object
51 * @param tid Tunnel ID
52 * @param labelIds Label stack of the tunnel
53 */
54 public DefaultTunnel(SegmentRoutingManager srm, String tid,
55 List<Integer> labelIds) {
56 this.srManager = checkNotNull(srm);
57 this.tunnelId = checkNotNull(tid);
58 this.labelIds = Collections.unmodifiableList(labelIds);
59 this.config = srManager.deviceConfiguration;
60 this.groupId = -1;
61 }
62
63 /**
64 * Creates a Tunnel reference.
65 *
66 * @param tid Tunnel ID
67 * @param labelIds Label stack of the tunnel
68 */
69 public DefaultTunnel(String tid, List<Integer> labelIds) {
70 this.srManager = null;
71 this.tunnelId = checkNotNull(tid);
72 this.labelIds = Collections.unmodifiableList(labelIds);
73 this.config = null;
74 this.groupId = -1;
75 }
76
77 /**
78 * Creates a new DefaultTunnel reference using the tunnel reference.
79 *
80 * @param tunnel DefaultTunnel reference
81 */
82 public DefaultTunnel(DefaultTunnel tunnel) {
83 this.srManager = tunnel.srManager;
84 this.tunnelId = tunnel.tunnelId;
85 this.labelIds = tunnel.labelIds;
86 this.config = tunnel.config;
87 this.groupId = tunnel.groupId;
88 }
89
90 @Override
91 public String id() {
92 return this.tunnelId;
93 }
94
95 @Override
96 public List<Integer> labelIds() {
97 return this.labelIds;
98 }
99
100 @Override
101 public boolean create() {
102
103 if (labelIds.isEmpty() || labelIds.size() < 3) {
104 log.error("More than one router needs to specified to created a tunnel");
105 return false;
106 }
107
108 groupId = createGroupsForTunnel();
109 if (groupId < 0) {
110 log.error("Failed to create groups for the tunnel");
111 return false;
112 }
113
114 return true;
115 }
116
117 @Override
118 public boolean remove() {
119
120 DeviceId deviceId = config.getDeviceId(labelIds.get(0));
121 srManager.removeNextObjective(deviceId, groupId);
122
123 return true;
124 }
125
126 @Override
127 public int groupId() {
128 return this.groupId;
129 }
130
131 @Override
132 public DeviceId source() {
133 return config.getDeviceId(labelIds.get(0));
134 }
135
136 private int createGroupsForTunnel() {
137
138 List<Integer> portNumbers;
139
140 int groupId;
141
142 DeviceId deviceId = config.getDeviceId(labelIds.get(0));
143 if (deviceId == null) {
144 log.warn("No device found for SID {}", labelIds.get(0));
145 return -1;
146 }
147 Set<DeviceId> deviceIds = new HashSet<>();
148 int sid = labelIds.get(1);
149 if (config.isAdjacencySid(deviceId, sid)) {
150 portNumbers = config.getPortsForAdjacencySid(deviceId, sid);
151 for (Link link: srManager.linkService.getDeviceEgressLinks(deviceId)) {
152 for (Integer port: portNumbers) {
153 if (link.src().port().toLong() == port) {
154 deviceIds.add(link.dst().deviceId());
155 }
156 }
157 }
158 } else {
159 deviceIds.add(config.getDeviceId(sid));
160 }
161
162 NeighborSet ns = new NeighborSet(deviceIds, labelIds.get(2));
163 groupId = srManager.getNextObjectiveId(deviceId, ns);
164
165 return groupId;
166 }
167
168}