/
TeTunnelRestconfProvider.java
executable file
·373 lines (317 loc) · 13.7 KB
/
TeTunnelRestconfProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.provider.te.tunnel;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.onosproject.incubator.net.tunnel.Tunnel;
import org.onosproject.incubator.net.tunnel.TunnelDescription;
import org.onosproject.incubator.net.tunnel.TunnelId;
import org.onosproject.incubator.net.tunnel.TunnelProvider;
import org.onosproject.incubator.net.tunnel.TunnelProviderRegistry;
import org.onosproject.net.DeviceId;
import org.onosproject.net.ElementId;
import org.onosproject.net.Path;
import org.onosproject.net.provider.AbstractProvider;
import org.onosproject.net.provider.ProviderId;
import org.onosproject.protocol.restconf.RestConfNotificationEventListener;
import org.onosproject.protocol.restconf.RestConfSBController;
import org.onosproject.provider.te.utils.DefaultJsonCodec;
import org.onosproject.provider.te.utils.YangCompositeEncodingImpl;
import org.onosproject.tetopology.management.api.TeTopology;
import org.onosproject.tetopology.management.api.TeTopologyKey;
import org.onosproject.tetopology.management.api.TeTopologyService;
import org.onosproject.tetunnel.api.TeTunnelProviderService;
import org.onosproject.tetunnel.api.TeTunnelService;
import org.onosproject.tetunnel.api.tunnel.DefaultTeTunnel;
import org.onosproject.tetunnel.api.tunnel.TeTunnel;
import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.IetfTe;
import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.tunnelsgrouping.Tunnels;
import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.IetfTeTypes;
import org.onosproject.yms.ych.YangCodecHandler;
import org.onosproject.yms.ych.YangCompositeEncoding;
import org.onosproject.yms.ych.YangProtocolEncodingFormat;
import org.onosproject.yms.ymsm.YmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Optional;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static org.onosproject.provider.te.utils.CodecTools.jsonToString;
import static org.onosproject.provider.te.utils.CodecTools.toJson;
import static org.onosproject.tetopology.management.api.TeTopology.BIT_MERGED;
import static org.onosproject.teyang.utils.tunnel.TunnelConverter.buildIetfTe;
import static org.onosproject.teyang.utils.tunnel.TunnelConverter.yang2TeTunnel;
import static org.onosproject.yms.ych.YangProtocolEncodingFormat.JSON;
import static org.onosproject.yms.ych.YangResourceIdentifierType.URI;
import static org.onosproject.yms.ydt.YmsOperationType.EDIT_CONFIG_REQUEST;
import static org.onosproject.yms.ydt.YmsOperationType.QUERY_REPLY;
/**
* Provider which uses RESTCONF to do cross-domain tunnel creation/deletion/
* update/deletion and so on operations on the domain networks.
*/
@Component(immediate = true)
public class TeTunnelRestconfProvider extends AbstractProvider
implements TunnelProvider {
private final Logger log = LoggerFactory.getLogger(getClass());
private static final String SCHEMA = "ietf";
private static final String IETF = "ietf";
private static final String TE = "te";
private static final int DEFAULT_INDEX = 1;
private static final String TUNNELS = "tunnels";
private static final String TUNNELS_URL = IETF + ":" + TE + "/" + TUNNELS;
private static final String MEDIA_TYPE_JSON = "json";
private static final String SHOULD_IN_ONE = "Tunnel should be setup in one topo";
private static final String PROVIDER_ID = "org.onosproject.provider.ietf";
private static final String RESTCONF_ROOT = "/onos/restconf";
private static final String TE_TUNNEL_KEY = "TeTunnelKey";
private final RestConfNotificationEventListener listener =
new InternalTunnelNotificationListener();
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected RestConfSBController controller;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected YmsService ymsService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected TeTunnelService tunnelService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected TeTunnelProviderService providerService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected TeTopologyService topologyService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected TunnelProviderRegistry tunnelProviderRegistry;
private YangCodecHandler codecHandler;
@Activate
public void activate() {
tunnelProviderRegistry.register(this);
codecHandler = ymsService.getYangCodecHandler();
codecHandler.addDeviceSchema(IetfTe.class);
codecHandler.addDeviceSchema(IetfTeTypes.class);
codecHandler.registerOverriddenCodec(new DefaultJsonCodec(ymsService),
YangProtocolEncodingFormat.JSON);
collectInitialTunnels();
subscribe();
log.info("Started");
}
@Deactivate
public void deactivate() {
tunnelProviderRegistry.unregister(this);
unsubscribe();
log.info("Stopped");
}
public TeTunnelRestconfProvider() {
super(new ProviderId(SCHEMA, PROVIDER_ID));
}
private void collectInitialTunnels() {
for (DeviceId deviceId : controller.getDevices().keySet()) {
ObjectNode jsonNodes = executeGetRequest(deviceId);
if (jsonNodes == null) {
continue;
}
ObjectNode tunnelsNode = (ObjectNode) jsonNodes.get(TUNNELS);
if (tunnelsNode == null) {
continue;
}
Tunnels teTunnels = getYangTunnelsObject(tunnelsNode);
if (teTunnels == null) {
continue;
}
updateTeTunnels(teTunnels);
}
}
private void subscribe() {
for (DeviceId deviceId : controller.getDevices().keySet()) {
try {
controller.enableNotifications(deviceId, TUNNELS_URL,
MEDIA_TYPE_JSON,
listener);
} catch (Exception e) {
log.error("Failed to subscribe for {} : {}", deviceId,
e.getMessage());
}
}
}
private void unsubscribe() {
controller.getDevices()
.keySet()
.forEach(deviceId -> controller
.removeNotificationListener(deviceId));
}
@Override
public void setupTunnel(Tunnel tunnel, Path path) {
TeTunnel teTunnel = tunnelService.getTeTunnel(tunnel.tunnelId());
long tid = teTunnel.srcNode().topologyId();
checkState(tid == teTunnel.dstNode().topologyId(), SHOULD_IN_ONE);
setupTunnel(getOwnDevice(tid), tunnel, path);
}
@Override
public void setupTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
if (!tunnel.annotations().keys().contains(TE_TUNNEL_KEY)) {
log.warn("No tunnel key info in tunnel {}", tunnel);
return;
}
String teTunnelKey = tunnel.annotations().value(TE_TUNNEL_KEY);
Optional<TeTunnel> optTunnel = tunnelService.getTeTunnels()
.stream()
.filter(t -> t.teTunnelKey().toString().equals(teTunnelKey))
.findFirst();
if (!optTunnel.isPresent()) {
log.warn("No te tunnel map to tunnel {}", tunnel);
return;
}
IetfTe ietfTe = buildIetfTe(optTunnel.get());
YangCompositeEncoding encoding = codecHandler.
encodeCompositeOperation(RESTCONF_ROOT, null, ietfTe,
JSON, EDIT_CONFIG_REQUEST);
String identifier = encoding.getResourceIdentifier();
String resourceInformation = encoding.getResourceInformation();
if (srcElement == null) {
log.error("Can't find remote device for tunnel : {}", tunnel);
return;
}
controller.post((DeviceId) srcElement, identifier,
new ByteArrayInputStream(resourceInformation.getBytes()),
MEDIA_TYPE_JSON, ObjectNode.class);
}
@Override
public void releaseTunnel(Tunnel tunnel) {
//TODO implement release tunnel method
}
@Override
public void releaseTunnel(ElementId srcElement, Tunnel tunnel) {
//TODO implement release tunnel with src method
}
@Override
public void updateTunnel(Tunnel tunnel, Path path) {
//TODO implement update tunnel method
}
@Override
public void updateTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
//TODO implement update tunnel with src method
}
@Override
public TunnelId tunnelAdded(TunnelDescription tunnel) {
//TODO implement tunnel add method when te tunnel app merged to core
return null;
}
@Override
public void tunnelRemoved(TunnelDescription tunnel) {
//TODO implement tunnel remove method when te tunnel app merged to core
}
@Override
public void tunnelUpdated(TunnelDescription tunnel) {
//TODO implement tunnel update method when te tunnel app merged to core
}
@Override
public Tunnel tunnelQueryById(TunnelId tunnelId) {
return null;
}
private ObjectNode executeGetRequest(DeviceId deviceId) {
//the request url is ietf-te:te/tunnels
//the response node will begin with tunnels
//be careful here to when get the tunnels data
InputStream resultStream =
controller.get(deviceId, TUNNELS_URL, MEDIA_TYPE_JSON);
return toJson(resultStream);
}
private Tunnels getYangTunnelsObject(ObjectNode tunnelsNode) {
checkNotNull(tunnelsNode, "Input object node should not be null");
YangCompositeEncoding yce =
new YangCompositeEncodingImpl(URI,
TUNNELS_URL,
jsonToString(tunnelsNode));
Object yo = codecHandler.decode(yce, JSON, QUERY_REPLY);
if (yo == null) {
log.error("YMS decoder returns null");
return null;
}
IetfTe ietfTe = null;
Tunnels tunnels = null;
if (yo instanceof List) {
List<Object> list = (List<Object>) yo;
ietfTe = (IetfTe) list.get(DEFAULT_INDEX);
}
if (ietfTe != null && ietfTe.te() != null) {
tunnels = ietfTe.te().tunnels();
}
return tunnels;
}
private void updateTeTunnels(Tunnels tunnels) {
TeTopologyKey key = getTopologyKey();
tunnels.tunnel().forEach(tunnel -> {
DefaultTeTunnel teTunnel = yang2TeTunnel(tunnel, key);
providerService.updateTeTunnel(teTunnel);
});
}
private TeTopologyKey getTopologyKey() {
TeTopologyKey key = null;
Optional<TeTopology> teTopology = topologyService.teTopologies()
.teTopologies()
.values()
.stream()
.filter(topology -> topology.flags().get(BIT_MERGED))
.findFirst();
if (teTopology.isPresent()) {
TeTopology topology = teTopology.get();
key = topology.teTopologyId();
}
return key;
}
private DeviceId getOwnDevice(long topologyId) {
DeviceId deviceId = null;
Optional<TeTopology> topoOpt = topologyService.teTopologies()
.teTopologies()
.values()
.stream()
.filter(tp -> tp.teTopologyId().topologyId() == topologyId)
.findFirst();
if (topoOpt.isPresent()) {
deviceId = topoOpt.get().ownerId();
}
return deviceId;
}
private class InternalTunnelNotificationListener implements
RestConfNotificationEventListener {
@Override
public void handleNotificationEvent(DeviceId deviceId, Object eventJsonString) {
ObjectNode response = toJson((String) eventJsonString);
if (response == null) {
return;
}
JsonNode teNode = response.get(TE);
if (teNode == null) {
log.error("Illegal te json object from {}", deviceId);
return;
}
JsonNode tunnelsNode = teNode.get(TUNNELS);
if (tunnelsNode == null) {
log.error("Illegal tunnel json object from {}", deviceId);
return;
}
Tunnels tunnels = getYangTunnelsObject((ObjectNode) tunnelsNode);
if (tunnels == null) {
return;
}
updateTeTunnels(tunnels);
}
}
}