blob: cd99d0ce3e9750ef8ef9001c3322d91f93532475 [file] [log] [blame]
tony-liuc3f9b652016-11-10 15:28:17 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
tony-liuc3f9b652016-11-10 15:28:17 +08003 *
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.tetunnel.impl;
18
tony-liuc3f9b652016-11-10 15:28:17 +080019import org.onosproject.core.ApplicationId;
20import org.onosproject.core.CoreService;
Yi Tsengfa394de2017-02-01 11:26:40 -080021import org.onosproject.core.GroupId;
tony-liuc3f9b652016-11-10 15:28:17 +080022import org.onosproject.incubator.net.tunnel.DefaultTunnel;
23import org.onosproject.incubator.net.tunnel.Tunnel;
24import org.onosproject.incubator.net.tunnel.TunnelAdminService;
25import org.onosproject.incubator.net.tunnel.TunnelId;
26import org.onosproject.incubator.net.tunnel.TunnelName;
27import org.onosproject.incubator.net.tunnel.TunnelService;
28import org.onosproject.net.DefaultAnnotations;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.provider.ProviderId;
31import org.onosproject.tetopology.management.api.TeTopology;
32import org.onosproject.tetopology.management.api.TeTopologyKey;
33import org.onosproject.tetopology.management.api.TeTopologyService;
tony-liuc3f9b652016-11-10 15:28:17 +080034import org.onosproject.tetunnel.api.TeTunnelAdminService;
35import org.onosproject.tetunnel.api.TeTunnelProviderService;
36import org.onosproject.tetunnel.api.TeTunnelService;
37import org.onosproject.tetunnel.api.TeTunnelStore;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070038import org.onosproject.tetunnel.api.lsp.TeLsp;
39import org.onosproject.tetunnel.api.lsp.TeLspKey;
40import org.onosproject.tetunnel.api.tunnel.TeTunnel;
tony-liuc3f9b652016-11-10 15:28:17 +080041import org.onosproject.tetunnel.api.tunnel.TeTunnelEndpoint;
42import org.onosproject.tetunnel.api.tunnel.TeTunnelKey;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070043import org.osgi.service.component.annotations.Activate;
44import org.osgi.service.component.annotations.Component;
45import org.osgi.service.component.annotations.Deactivate;
46import org.osgi.service.component.annotations.Reference;
47import org.osgi.service.component.annotations.ReferenceCardinality;
tony-liuc3f9b652016-11-10 15:28:17 +080048import org.slf4j.Logger;
49
50import java.util.Collection;
51import java.util.List;
52
53import static org.slf4j.LoggerFactory.getLogger;
54
55/**
56 * Implementation of TE tunnel attributes management service.
57 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070058@Component(immediate = true, service = { TeTunnelService.class, TeTunnelAdminService.class,
59 TeTunnelProviderService.class })
tony-liuc3f9b652016-11-10 15:28:17 +080060public class TeTunnelManager implements TeTunnelService, TeTunnelAdminService,
61 TeTunnelProviderService {
62
63 private static final String TE_TUNNEL_APP = "onos-app-tetunnel";
64
65 private final Logger log = getLogger(getClass());
66
Ray Milkeyd84f89b2018-08-17 14:54:17 -070067 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tony-liuc3f9b652016-11-10 15:28:17 +080068 protected CoreService coreService;
69
Ray Milkeyd84f89b2018-08-17 14:54:17 -070070 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tony-liuc3f9b652016-11-10 15:28:17 +080071 protected TeTunnelStore store;
72
Ray Milkeyd84f89b2018-08-17 14:54:17 -070073 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tony-liuc3f9b652016-11-10 15:28:17 +080074 protected TunnelService tunnelService;
75
Ray Milkeyd84f89b2018-08-17 14:54:17 -070076 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tony-liuc3f9b652016-11-10 15:28:17 +080077 protected TunnelAdminService tunnelAdminService;
78
Ray Milkeyd84f89b2018-08-17 14:54:17 -070079 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tony-liuc3f9b652016-11-10 15:28:17 +080080 protected TeTopologyService teTopologyService;
81
82 private ApplicationId appId;
83
84 @Activate
85 public void activate() {
86 appId = coreService.registerApplication(TE_TUNNEL_APP);
87
88 log.info("Started");
89 }
90
91 @Deactivate
92 public void deactivate() {
93 log.info("Stopped");
94 }
95
96 @Override
97 public TunnelId createTeTunnel(TeTunnel teTunnel) {
98 if (!store.addTeTunnel(teTunnel)) {
99 log.error("can not add teTunnel: {}", teTunnel);
100 return null;
101 }
102
103 TunnelId tunnelId = TunnelId.valueOf(teTunnel.teTunnelKey().toString());
104 Tunnel tunnel = new DefaultTunnel(ProviderId.NONE,
105 new TeTunnelEndpoint(teTunnel.srcNode(),
106 teTunnel.srcTp()),
107 new TeTunnelEndpoint(teTunnel.dstNode(),
108 teTunnel.dstTp()),
Yi Tsengfa394de2017-02-01 11:26:40 -0800109 Tunnel.Type.MPLS, new GroupId(0),
tony-liuc3f9b652016-11-10 15:28:17 +0800110 tunnelId,
111 TunnelName.tunnelName(teTunnel.name()),
112 null,
113 DefaultAnnotations.builder().build());
114 store.setTunnelId(teTunnel.teTunnelKey(), tunnelId);
115 TeTopology srcTopology = teTopologyService.teTopology(
116 teTopologyService.teNode(teTunnel.srcNode())
117 .underlayTeTopologyId());
118 if (srcTopology == null) {
119 srcTopology = teTopologyService.teTopology(teTunnel.srcNode()
120 .teTopologyKey());
121 }
122 DeviceId domainId = srcTopology.ownerId();
123 TunnelId id = tunnelService.setupTunnel(appId, domainId, tunnel, null);
124 if (id == null) {
125 log.error("can not create tunnel for te {}",
126 teTunnel.teTunnelKey());
127 store.removeTeTunnel(teTunnel.teTunnelKey());
128 return null;
129 }
130 if (!id.equals(tunnelId)) {
131 //this should not happen
132 log.error("tunnelId changed, oldId:{}, newId:{}", tunnelId, id);
133 store.setTunnelId(teTunnel.teTunnelKey(), id);
134 }
135 return id;
136 }
137
138 @Override
139 public void setTunnelId(TeTunnelKey teTunnelKey, TunnelId tunnelId) {
140 store.setTunnelId(teTunnelKey, tunnelId);
141 }
142
143 @Override
144 public void updateTeTunnel(TeTunnel teTunnel) {
145 //TODO: updateTeTunnel
146 }
147
148 @Override
149 public void updateTunnelState(TeTunnelKey key, Tunnel.State state) {
150 tunnelAdminService.updateTunnelState(
151 tunnelService.queryTunnel(getTunnelId(key)), state);
152 }
153
154 @Override
chenqinghui86320ae2016-12-01 15:40:39 +0800155 public TeLspKey teLspAdded(TeLsp lsp) {
156 if (store.addTeLsp(lsp)) {
157 return lsp.teLspKey();
158 }
159
160 return null;
161 }
162
163 @Override
164 public void teLspRemoved(TeLsp lsp) {
165 store.removeTeLsp(lsp.teLspKey());
166 }
167
168 @Override
169 public void updateTeLsp(TeLsp lsp) {
170 store.updateTeLsp(lsp);
171 }
172
173 @Override
tony-liuc3f9b652016-11-10 15:28:17 +0800174 public void removeTeTunnel(TeTunnelKey teTunnelKey) {
175 tunnelAdminService.updateTunnelState(
176 tunnelService.queryTunnel(getTunnelId(teTunnelKey)),
177 Tunnel.State.REMOVING);
178 List<TeTunnelKey> segmentTunnels =
179 getTeTunnel(teTunnelKey).segmentTunnels();
180 if (segmentTunnels == null || segmentTunnels.isEmpty()) {
181 // this is a single domain tunnel, removes it right away
182 tunnelAdminService.removeTunnel(getTunnelId(teTunnelKey));
183 }
184 }
185
186 @Override
187 public void setSegmentTunnel(TeTunnelKey e2eTunnelKey,
188 List<TeTunnelKey> segmentTunnels) {
189 TeTunnel e2eTunnel = store.getTeTunnel(e2eTunnelKey);
190 if (e2eTunnel == null) {
tony-liuf7d2a262016-10-17 16:32:24 +0800191 log.error("unknown e2eTunnelKey: {}", e2eTunnelKey);
tony-liuc3f9b652016-11-10 15:28:17 +0800192 return;
193 }
194 e2eTunnel.segmentTunnels(segmentTunnels);
195
196 for (TeTunnelKey key : segmentTunnels) {
197 TeTunnel segmentTunnel = store.getTeTunnel(key);
198 if (segmentTunnel == null) {
199 log.warn("unknown segmentTunnel: {}", key);
200 continue;
201 }
tony-liuf7d2a262016-10-17 16:32:24 +0800202 segmentTunnel.e2eTunnelKey(e2eTunnelKey);
tony-liuc3f9b652016-11-10 15:28:17 +0800203 }
204 }
205
206 @Override
207 public TeTunnel getTeTunnel(TeTunnelKey key) {
208 return store.getTeTunnel(key);
209 }
210
211 @Override
212 public TeTunnel getTeTunnel(TunnelId id) {
213 return store.getTeTunnel(id);
214 }
215
216 @Override
217 public TunnelId getTunnelId(TeTunnelKey key) {
218 return store.getTunnelId(key);
219 }
220
221 @Override
222 public Collection<TeTunnel> getTeTunnels() {
223 return store.getTeTunnels();
224 }
225
226 @Override
227 public Collection<TeTunnel> getTeTunnels(TeTunnel.Type type) {
228 return store.getTeTunnels(type);
229 }
230
231 @Override
232 public Collection<TeTunnel> getTeTunnels(TeTopologyKey key) {
233 return store.getTeTunnels(key);
234 }
235
236 @Override
chenqinghui86320ae2016-12-01 15:40:39 +0800237 public TeLsp getTeLsp(TeLspKey key) {
238 return store.getTeLsp(key);
239 }
240
241 @Override
242 public Collection<TeLsp> getTeLsps() {
243 return store.getTeLsps();
244 }
245
246 @Override
tony-liuc3f9b652016-11-10 15:28:17 +0800247 public TunnelId teTunnelAdded(TeTunnel teTunnel) {
248 //TODO teTunnelAdded
249 return null;
250 }
251
252 @Override
253 public void teTunnelRemoved(TeTunnel teTunnel) {
tony-liuf7d2a262016-10-17 16:32:24 +0800254 TeTunnelKey e2eTunnelKey = teTunnel.e2eTunnelKey();
tony-liuc3f9b652016-11-10 15:28:17 +0800255 store.removeTeTunnel(teTunnel.teTunnelKey());
256
257 // it's a segment tunnel
258 if (e2eTunnelKey != null) {
259 boolean finished = true;
260 for (TeTunnelKey key : getTeTunnel(e2eTunnelKey).segmentTunnels()) {
261 if (getTeTunnel(key) != null) {
262 // FIXME need a better way to determine whether a segment tunnel is removed.
263 finished = false;
264 }
265 }
266 if (finished) {
267 // all segment tunnels are removed
268 tunnelAdminService.removeTunnel(getTunnelId(e2eTunnelKey));
269 store.removeTeTunnel(e2eTunnelKey);
270 }
271 }
272 }
273}