OnosXOSIntegrationManager.java
19.6 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
* Copyright 2014-2015 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.xosintegration;
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.google.common.collect.Maps;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
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.Modified;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.onlab.packet.VlanId;
import org.onlab.util.Tools;
import org.onosproject.cfg.ComponentConfigService;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.DefaultTrafficTreatment;
import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.flowobjective.DefaultForwardingObjective;
import org.onosproject.net.flowobjective.FlowObjectiveService;
import org.onosproject.net.flowobjective.ForwardingObjective;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import java.util.Dictionary;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.net.MediaType.JSON_UTF_8;
import static java.net.HttpURLConnection.HTTP_CREATED;
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static java.net.HttpURLConnection.HTTP_OK;
import static org.slf4j.LoggerFactory.getLogger;
/**
* XOS interface application.
*/
@Component(immediate = true)
@Service
public class OnosXOSIntegrationManager implements VoltTenantService {
private static final String XOS_SERVER_ADDRESS_PROPERTY_NAME =
"xosServerAddress";
private static final String XOS_SERVER_PORT_PROPERTY_NAME =
"xosServerPort";
private static final String XOS_PROVIDER_SERVICE_PROPERTY_NAME =
"xosProviderService";
private static final String TEST_XOS_SERVER_ADDRESS = "10.254.1.22";
private static final int TEST_XOS_SERVER_PORT = 8000;
private static final String XOS_TENANT_BASE_URI = "/xoslib/volttenant/";
private static final int TEST_XOS_PROVIDER_SERVICE = 1;
private static final int PRIORITY = 50000;
private static final DeviceId FABRIC_DEVICE_ID = DeviceId.deviceId("of:5e3e486e73000187");
private static final PortNumber FABRIC_OLT_CONNECT_POINT = PortNumber.portNumber(2);
private static final PortNumber FABRIC_VCPE_CONNECT_POINT = PortNumber.portNumber(3);
private static final String FABRIC_CONTROLLER_ADDRESS = "10.0.3.136";
private static final int FABRIC_SERVER_PORT = 8181;
private static final String FABRIC_BASE_URI = "/onos/cordfabric/vlans/add";
private static final DeviceId OLT_DEVICE_ID = DeviceId.deviceId("of:90e2ba82f97791e9");
private static final int OLT_UPLINK_PORT = 129;
private static final ConnectPoint FABRIC_PORT = new ConnectPoint(
DeviceId.deviceId("of:000090e2ba82f974"),
PortNumber.portNumber(2));
private final Logger log = getLogger(getClass());
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected CoreService coreService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected ComponentConfigService cfgService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected FlowObjectiveService flowObjectiveService;
@Property(name = XOS_SERVER_ADDRESS_PROPERTY_NAME,
value = TEST_XOS_SERVER_ADDRESS,
label = "XOS Server address")
protected String xosServerAddress = TEST_XOS_SERVER_ADDRESS;
@Property(name = XOS_SERVER_PORT_PROPERTY_NAME,
intValue = TEST_XOS_SERVER_PORT,
label = "XOS Server port")
protected int xosServerPort = TEST_XOS_SERVER_PORT;
@Property(name = XOS_PROVIDER_SERVICE_PROPERTY_NAME,
intValue = TEST_XOS_PROVIDER_SERVICE,
label = "XOS Provider Service")
protected int xosProviderService = TEST_XOS_PROVIDER_SERVICE;
private ApplicationId appId;
private Map<String, ConnectPoint> nodeToPort;
private Map<Long, Short> portToVlan;
private Map<ConnectPoint, String> portToSsid;
@Activate
public void activate(ComponentContext context) {
log.info("XOS app is starting");
cfgService.registerProperties(getClass());
appId = coreService.registerApplication("org.onosproject.xosintegration");
setupMap();
readComponentConfiguration(context);
log.info("XOS({}) started", appId.id());
}
@Deactivate
public void deactivate() {
cfgService.unregisterProperties(getClass(), false);
log.info("XOS({}) stopped", appId.id());
}
@Modified
public void modified(ComponentContext context) {
readComponentConfiguration(context);
}
private void setupMap() {
nodeToPort = Maps.newHashMap();
nodeToPort.put("cordcompute01.onlab.us", new ConnectPoint(FABRIC_DEVICE_ID,
PortNumber.portNumber(4)));
nodeToPort.put("cordcompute02.onlab.us", new ConnectPoint(FABRIC_DEVICE_ID,
PortNumber.portNumber(3)));
portToVlan = Maps.newHashMap();
portToVlan.putIfAbsent(1L, (short) 201);
portToVlan.putIfAbsent(6L, (short) 401);
portToSsid = Maps.newHashMap();
portToSsid.put(new ConnectPoint(OLT_DEVICE_ID, PortNumber.portNumber(1)), "0");
portToSsid.put(new ConnectPoint(FABRIC_DEVICE_ID, PortNumber.portNumber(6)), "1");
}
/**
* Converts a JSON representation of a tenant into a tenant object.
*
* @param jsonTenant JSON object representing the tenant
* @return volt tenant object
*/
private VoltTenant jsonToTenant(JsonObject jsonTenant) {
return VoltTenant.builder()
.withHumanReadableName(jsonTenant.get("humanReadableName").asString())
.withId(jsonTenant.get("id").asInt())
.withProviderService(jsonTenant.get("provider_service").asInt())
.withServiceSpecificId(jsonTenant.get("service_specific_id").asString())
.withVlanId(jsonTenant.get("vlan_id").asString())
.build();
}
/**
* Converts a tenant object into a JSON string.
*
* @param tenant volt tenant object to convert
* @return JSON string for the tenant
*/
private String tenantToJson(VoltTenant tenant) {
return "{"
+ "\"humanReadableName\": \"" + tenant.humanReadableName() + "\","
+ "\"id\": \"" + tenant.id() + "\","
+ "\"provider_service\": \"" + tenant.providerService() + "\","
+ "\"service_specific_id\": \"" + tenant.serviceSpecificId() + "\","
+ "\"vlan_id\": \"" + tenant.vlanId() + "\""
+ "}";
}
/**
* Gets a client web resource builder for the base XOS REST API
* with no additional URI.
*
* @return web resource builder
* @deprecated in Cardinal Release
*/
@Deprecated
private WebResource.Builder getClientBuilder() {
return getClientBuilder("");
}
/**
* Gets a client web resource builder for the base XOS REST API
* with an optional additional URI.
*
* @return web resource builder
* @deprecated in Cardinal Release
*/
@Deprecated
private WebResource.Builder getClientBuilder(String uri) {
String baseUrl = "http://" + xosServerAddress + ":"
+ Integer.toString(xosServerPort);
Client client = Client.create();
client.addFilter(new HTTPBasicAuthFilter("padmin@vicci.org", "letmein"));
WebResource resource = client.resource(baseUrl
+ XOS_TENANT_BASE_URI + uri);
return resource.accept(JSON_UTF_8.toString())
.type(JSON_UTF_8.toString());
}
/**
* Performs a REST GET operation on the base XOS REST URI.
*
* @return JSON string fetched by the GET operation
* @deprecated in Cardinal Release
*/
@Deprecated
private String getRest() {
return getRest("");
}
/**
* Performs a REST GET operation on the base XOS REST URI with
* an optional additional URI.
*
* @return JSON string fetched by the GET operation
* @deprecated in Cardinal Release
*/
@Deprecated
private String getRest(String uri) {
WebResource.Builder builder = getClientBuilder(uri);
ClientResponse response = builder.get(ClientResponse.class);
if (response.getStatus() != HTTP_OK) {
log.info("REST GET request returned error code {}",
response.getStatus());
}
String jsonString = response.getEntity(String.class);
log.info("JSON read:\n{}", jsonString);
return jsonString;
}
/**
* Performs a REST POST operation of a json string on the base
* XOS REST URI with an optional additional URI.
*
* @param json JSON string to post
* @deprecated in Cardinal Release
*/
@Deprecated
private String postRest(String json) {
WebResource.Builder builder = getClientBuilder();
ClientResponse response;
try {
response = builder.post(ClientResponse.class, json);
} catch (ClientHandlerException e) {
log.warn("Unable to contact REST server: {}", e.getMessage());
return "{ 'error' : 'oops no one home' }";
}
if (response.getStatus() != HTTP_CREATED) {
log.info("REST POST request returned error code {}",
response.getStatus());
}
return response.getEntity(String.class);
}
/**
* Performs a REST DELETE operation on the base
* XOS REST URI with an optional additional URI.
*
* @param uri optional additional URI
* @deprecated in Cardinal Release
*/
@Deprecated
private void deleteRest(String uri) {
WebResource.Builder builder = getClientBuilder(uri);
ClientResponse response = builder.delete(ClientResponse.class);
if (response.getStatus() != HTTP_NO_CONTENT) {
log.info("REST DELETE request returned error code {}",
response.getStatus());
}
}
/**
* Deletes the tenant with the given ID.
*
* @param tenantId ID of tenant to delete
*/
private void deleteTenant(long tenantId) {
deleteRest(Long.toString(tenantId));
}
@Override
public Set<VoltTenant> getAllTenants() {
String jsonString = getRest();
JsonArray voltTenantItems = JsonArray.readFrom(jsonString);
return IntStream.range(0, voltTenantItems.size())
.mapToObj(index -> jsonToTenant(voltTenantItems.get(index).asObject()))
.collect(Collectors.toSet());
}
@Override
public void removeTenant(long id) {
deleteTenant(id);
}
@Override
public VoltTenant addTenant(VoltTenant newTenant) {
long providerServiceId = newTenant.providerService();
if (providerServiceId == -1) {
providerServiceId = xosProviderService;
}
PortNumber onuPort = newTenant.port().port();
VlanId subscriberVlan = VlanId.vlanId(portToVlan.get(onuPort.toLong()));
VoltTenant tenantToCreate = VoltTenant.builder()
.withProviderService(providerServiceId)
.withServiceSpecificId(portToSsid.get(newTenant.port()))
.withVlanId(String.valueOf(subscriberVlan.toShort()))
.withPort(newTenant.port())
.build();
String json = tenantToJson(tenantToCreate);
provisionVlanOnPort(OLT_DEVICE_ID, OLT_UPLINK_PORT, onuPort, subscriberVlan.toShort());
//provisionDataPlane(tenantToCreate);
String retJson = postRest(json);
fetchCPELocation(tenantToCreate, retJson);
return newTenant;
}
private void fetchCPELocation(VoltTenant newTenant, String jsonString) {
JsonObject json = JsonObject.readFrom(jsonString);
if (json.get("computeNodeName") != null) {
ConnectPoint point = nodeToPort.get(json.get("computeNodeName").asString());
//ConnectPoint fromPoint = newTenant.port();
ConnectPoint oltPort = new ConnectPoint(FABRIC_DEVICE_ID, FABRIC_OLT_CONNECT_POINT);
provisionFabric(VlanId.vlanId(Short.parseShort(newTenant.vlanId())),
point, oltPort);
}
}
@Override
public VoltTenant getTenant(long id) {
String jsonString = getRest(Long.toString(id));
JsonObject jsonTenant = JsonObject.readFrom(jsonString);
if (jsonTenant.get("id") != null) {
return jsonToTenant(jsonTenant);
} else {
return null;
}
}
private void provisionVlanOnPort(DeviceId deviceId, int uplinkPort, PortNumber p, short vlanId) {
TrafficSelector upstream = DefaultTrafficSelector.builder()
.matchVlanId(VlanId.vlanId(vlanId))
.matchInPort(p)
.build();
TrafficSelector downstream = DefaultTrafficSelector.builder()
.matchVlanId(VlanId.vlanId(vlanId))
.matchInPort(PortNumber.portNumber(uplinkPort))
.build();
TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder()
.setOutput(PortNumber.portNumber(uplinkPort))
.build();
TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder()
.setOutput(p)
.build();
ForwardingObjective upFwd = DefaultForwardingObjective.builder()
.withFlag(ForwardingObjective.Flag.VERSATILE)
.withPriority(1000)
.makePermanent()
.withSelector(upstream)
.fromApp(appId)
.withTreatment(upstreamTreatment)
.add();
ForwardingObjective downFwd = DefaultForwardingObjective.builder()
.withFlag(ForwardingObjective.Flag.VERSATILE)
.withPriority(1000)
.makePermanent()
.withSelector(downstream)
.fromApp(appId)
.withTreatment(downstreamTreatment)
.add();
flowObjectiveService.forward(deviceId, upFwd);
flowObjectiveService.forward(deviceId, downFwd);
}
private void provisionDataPlane(VoltTenant tenant) {
VlanId vlan = VlanId.vlanId(Short.parseShort(tenant.vlanId()));
TrafficSelector fromGateway = DefaultTrafficSelector.builder()
.matchInPhyPort(tenant.port().port())
.build();
TrafficSelector fromFabric = DefaultTrafficSelector.builder()
.matchInPhyPort(FABRIC_PORT.port())
.matchVlanId(vlan)
.build();
TrafficTreatment toFabric = DefaultTrafficTreatment.builder()
.pushVlan()
.setVlanId(vlan)
.setOutput(FABRIC_PORT.port())
.build();
TrafficTreatment toGateway = DefaultTrafficTreatment.builder()
.popVlan()
.setOutput(tenant.port().port())
.build();
ForwardingObjective forwardToFabric = DefaultForwardingObjective.builder()
.withFlag(ForwardingObjective.Flag.VERSATILE)
.withPriority(PRIORITY)
.makePermanent()
.fromApp(appId)
.withSelector(fromGateway)
.withTreatment(toFabric)
.add();
ForwardingObjective forwardToGateway = DefaultForwardingObjective.builder()
.withFlag(ForwardingObjective.Flag.VERSATILE)
.withPriority(PRIORITY)
.makePermanent()
.fromApp(appId)
.withSelector(fromFabric)
.withTreatment(toGateway)
.add();
flowObjectiveService.forward(FABRIC_PORT.deviceId(), forwardToFabric);
flowObjectiveService.forward(FABRIC_PORT.deviceId(), forwardToGateway);
}
private void provisionFabric(VlanId vlanId, ConnectPoint point, ConnectPoint fromPoint) {
long vlan = vlanId.toShort();
JsonObject node = new JsonObject();
node.add("vlan", vlan);
if (vlan == 201) {
node.add("iptv", true);
} else {
node.add("iptv", false);
}
JsonArray array = new JsonArray();
JsonObject cp1 = new JsonObject();
JsonObject cp2 = new JsonObject();
cp1.add("device", point.deviceId().toString());
cp1.add("port", point.port().toLong());
cp2.add("device", fromPoint.deviceId().toString());
cp2.add("port", fromPoint.port().toLong());
array.add(cp1);
array.add(cp2);
node.add("ports", array);
String baseUrl = "http://" + FABRIC_CONTROLLER_ADDRESS + ":"
+ Integer.toString(FABRIC_SERVER_PORT);
Client client = Client.create();
WebResource resource = client.resource(baseUrl + FABRIC_BASE_URI);
WebResource.Builder builder = resource.accept(JSON_UTF_8.toString())
.type(JSON_UTF_8.toString());
try {
builder.post(ClientResponse.class, node.toString());
} catch (ClientHandlerException e) {
log.warn("Unable to contact fabric REST server: {}", e.getMessage());
return;
}
}
/**
* Extracts properties from the component configuration context.
*
* @param context the component context
*/
private void readComponentConfiguration(ComponentContext context) {
Dictionary<?, ?> properties = context.getProperties();
String newXosServerAddress =
Tools.get(properties, XOS_SERVER_ADDRESS_PROPERTY_NAME);
if (!isNullOrEmpty(newXosServerAddress)) {
xosServerAddress = newXosServerAddress;
}
String newXosServerPortString =
Tools.get(properties, XOS_SERVER_PORT_PROPERTY_NAME);
if (!isNullOrEmpty(newXosServerPortString)) {
xosServerPort = Integer.parseInt(newXosServerPortString);
}
String newXosProviderServiceString =
Tools.get(properties, XOS_PROVIDER_SERVICE_PROPERTY_NAME);
if (!isNullOrEmpty(newXosProviderServiceString)) {
xosProviderService = Integer.parseInt(newXosProviderServiceString);
}
}
}