blob: 1f686aeb8f252b8f341e7160a8638671ccd1d131 [file] [log] [blame]
Ray Milkey140e4782015-04-24 11:25:13 -07001/*
2 * Copyright 2014-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.xosintegration;
17
Jonathan Hart5e9a63d2015-05-19 16:21:46 -070018import com.eclipsesource.json.JsonArray;
19import com.eclipsesource.json.JsonObject;
alshabib60562282015-06-01 16:45:04 -070020import com.google.common.collect.Maps;
Jonathan Hart5e9a63d2015-05-19 16:21:46 -070021import com.sun.jersey.api.client.Client;
Jonathan Hartb4558032015-05-20 16:32:04 -070022import com.sun.jersey.api.client.ClientHandlerException;
Jonathan Hart5e9a63d2015-05-19 16:21:46 -070023import com.sun.jersey.api.client.ClientResponse;
24import com.sun.jersey.api.client.WebResource;
25import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
Ray Milkey140e4782015-04-24 11:25:13 -070026import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Deactivate;
29import org.apache.felix.scr.annotations.Modified;
30import org.apache.felix.scr.annotations.Property;
31import org.apache.felix.scr.annotations.Reference;
32import org.apache.felix.scr.annotations.ReferenceCardinality;
33import org.apache.felix.scr.annotations.Service;
Jonathan Hart5e9a63d2015-05-19 16:21:46 -070034import org.onlab.packet.VlanId;
Ray Milkey140e4782015-04-24 11:25:13 -070035import org.onlab.util.Tools;
36import org.onosproject.cfg.ComponentConfigService;
37import org.onosproject.core.ApplicationId;
38import org.onosproject.core.CoreService;
Jonathan Hart5e9a63d2015-05-19 16:21:46 -070039import org.onosproject.net.ConnectPoint;
40import org.onosproject.net.DeviceId;
41import org.onosproject.net.PortNumber;
42import org.onosproject.net.flow.DefaultTrafficSelector;
43import org.onosproject.net.flow.DefaultTrafficTreatment;
44import org.onosproject.net.flow.TrafficSelector;
45import org.onosproject.net.flow.TrafficTreatment;
46import org.onosproject.net.flowobjective.DefaultForwardingObjective;
47import org.onosproject.net.flowobjective.FlowObjectiveService;
48import org.onosproject.net.flowobjective.ForwardingObjective;
Ray Milkey140e4782015-04-24 11:25:13 -070049import org.osgi.service.component.ComponentContext;
50import org.slf4j.Logger;
51
Jonathan Hart5e9a63d2015-05-19 16:21:46 -070052import java.util.Dictionary;
alshabib60562282015-06-01 16:45:04 -070053import java.util.Map;
Jonathan Hart5e9a63d2015-05-19 16:21:46 -070054import java.util.Set;
55import java.util.stream.Collectors;
56import java.util.stream.IntStream;
Ray Milkey140e4782015-04-24 11:25:13 -070057
58import static com.google.common.base.Strings.isNullOrEmpty;
59import static com.google.common.net.MediaType.JSON_UTF_8;
Jonathan Hart64da69d2015-07-15 15:10:28 -070060import static java.net.HttpURLConnection.HTTP_CREATED;
61import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
62import static java.net.HttpURLConnection.HTTP_OK;
Ray Milkey140e4782015-04-24 11:25:13 -070063import static org.slf4j.LoggerFactory.getLogger;
64
65
66/**
67 * XOS interface application.
68 */
69@Component(immediate = true)
70@Service
71public class OnosXOSIntegrationManager implements VoltTenantService {
Ray Milkeydea98172015-05-18 10:39:39 -070072 private static final String XOS_SERVER_ADDRESS_PROPERTY_NAME =
73 "xosServerAddress";
74 private static final String XOS_SERVER_PORT_PROPERTY_NAME =
75 "xosServerPort";
76 private static final String XOS_PROVIDER_SERVICE_PROPERTY_NAME =
77 "xosProviderService";
Ray Milkey140e4782015-04-24 11:25:13 -070078
79 private static final String TEST_XOS_SERVER_ADDRESS = "10.254.1.22";
80 private static final int TEST_XOS_SERVER_PORT = 8000;
81 private static final String XOS_TENANT_BASE_URI = "/xoslib/volttenant/";
Ray Milkeydea98172015-05-18 10:39:39 -070082 private static final int TEST_XOS_PROVIDER_SERVICE = 1;
Ray Milkey140e4782015-04-24 11:25:13 -070083
Jonathan Hartb4558032015-05-20 16:32:04 -070084 private static final int PRIORITY = 50000;
Jonathan Hart5e9a63d2015-05-19 16:21:46 -070085 private static final DeviceId FABRIC_DEVICE_ID = DeviceId.deviceId("of:5e3e486e73000187");
86 private static final PortNumber FABRIC_OLT_CONNECT_POINT = PortNumber.portNumber(2);
87 private static final PortNumber FABRIC_VCPE_CONNECT_POINT = PortNumber.portNumber(3);
88 private static final String FABRIC_CONTROLLER_ADDRESS = "10.0.3.136";
89 private static final int FABRIC_SERVER_PORT = 8181;
90 private static final String FABRIC_BASE_URI = "/onos/cordfabric/vlans/add";
91
Jonathan Hart64da69d2015-07-15 15:10:28 -070092 private static final DeviceId OLT_DEVICE_ID = DeviceId.deviceId("of:90e2ba82f97791e9");
93 private static final int OLT_UPLINK_PORT = 129;
94
Jonathan Hart5e9a63d2015-05-19 16:21:46 -070095 private static final ConnectPoint FABRIC_PORT = new ConnectPoint(
96 DeviceId.deviceId("of:000090e2ba82f974"),
97 PortNumber.portNumber(2));
98
Ray Milkey140e4782015-04-24 11:25:13 -070099 private final Logger log = getLogger(getClass());
100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 protected CoreService coreService;
102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
103 protected ComponentConfigService cfgService;
Ray Milkeydea98172015-05-18 10:39:39 -0700104
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected FlowObjectiveService flowObjectiveService;
107
Ray Milkeydea98172015-05-18 10:39:39 -0700108 @Property(name = XOS_SERVER_ADDRESS_PROPERTY_NAME,
Ray Milkey140e4782015-04-24 11:25:13 -0700109 value = TEST_XOS_SERVER_ADDRESS,
110 label = "XOS Server address")
111 protected String xosServerAddress = TEST_XOS_SERVER_ADDRESS;
Ray Milkeydea98172015-05-18 10:39:39 -0700112
113 @Property(name = XOS_SERVER_PORT_PROPERTY_NAME,
Ray Milkey140e4782015-04-24 11:25:13 -0700114 intValue = TEST_XOS_SERVER_PORT,
115 label = "XOS Server port")
116 protected int xosServerPort = TEST_XOS_SERVER_PORT;
Ray Milkeydea98172015-05-18 10:39:39 -0700117
118 @Property(name = XOS_PROVIDER_SERVICE_PROPERTY_NAME,
119 intValue = TEST_XOS_PROVIDER_SERVICE,
120 label = "XOS Provider Service")
121 protected int xosProviderService = TEST_XOS_PROVIDER_SERVICE;
122
Ray Milkey140e4782015-04-24 11:25:13 -0700123 private ApplicationId appId;
alshabib60562282015-06-01 16:45:04 -0700124 private Map<String, ConnectPoint> nodeToPort;
alshabib17cde6d2015-06-05 15:03:51 -0700125 private Map<Long, Short> portToVlan;
Jonathan Hart08ad8232015-06-09 10:51:05 -0700126 private Map<ConnectPoint, String> portToSsid;
Ray Milkey140e4782015-04-24 11:25:13 -0700127
128 @Activate
129 public void activate(ComponentContext context) {
130 log.info("XOS app is starting");
131 cfgService.registerProperties(getClass());
132 appId = coreService.registerApplication("org.onosproject.xosintegration");
alshabib60562282015-06-01 16:45:04 -0700133
134 setupMap();
135
Ray Milkey140e4782015-04-24 11:25:13 -0700136 readComponentConfiguration(context);
137
138 log.info("XOS({}) started", appId.id());
139 }
140
141 @Deactivate
142 public void deactivate() {
143 cfgService.unregisterProperties(getClass(), false);
144 log.info("XOS({}) stopped", appId.id());
145 }
146
147 @Modified
148 public void modified(ComponentContext context) {
149 readComponentConfiguration(context);
150 }
151
alshabib60562282015-06-01 16:45:04 -0700152 private void setupMap() {
153 nodeToPort = Maps.newHashMap();
154
155 nodeToPort.put("cordcompute01.onlab.us", new ConnectPoint(FABRIC_DEVICE_ID,
156 PortNumber.portNumber(4)));
157
158 nodeToPort.put("cordcompute02.onlab.us", new ConnectPoint(FABRIC_DEVICE_ID,
159 PortNumber.portNumber(3)));
alshabib17cde6d2015-06-05 15:03:51 -0700160
Jonathan Hartc02437c2015-06-09 10:05:13 -0700161 portToVlan = Maps.newHashMap();
Jonathan Hart64da69d2015-07-15 15:10:28 -0700162 portToVlan.putIfAbsent(1L, (short) 201);
alshabib17cde6d2015-06-05 15:03:51 -0700163 portToVlan.putIfAbsent(6L, (short) 401);
Jonathan Hart08ad8232015-06-09 10:51:05 -0700164
165 portToSsid = Maps.newHashMap();
Jonathan Hart64da69d2015-07-15 15:10:28 -0700166 portToSsid.put(new ConnectPoint(OLT_DEVICE_ID, PortNumber.portNumber(1)), "0");
Jonathan Hart08ad8232015-06-09 10:51:05 -0700167 portToSsid.put(new ConnectPoint(FABRIC_DEVICE_ID, PortNumber.portNumber(6)), "1");
alshabib60562282015-06-01 16:45:04 -0700168 }
169
Ray Milkey140e4782015-04-24 11:25:13 -0700170 /**
171 * Converts a JSON representation of a tenant into a tenant object.
172 *
173 * @param jsonTenant JSON object representing the tenant
174 * @return volt tenant object
175 */
176 private VoltTenant jsonToTenant(JsonObject jsonTenant) {
177 return VoltTenant.builder()
178 .withHumanReadableName(jsonTenant.get("humanReadableName").asString())
179 .withId(jsonTenant.get("id").asInt())
180 .withProviderService(jsonTenant.get("provider_service").asInt())
181 .withServiceSpecificId(jsonTenant.get("service_specific_id").asString())
182 .withVlanId(jsonTenant.get("vlan_id").asString())
183 .build();
184 }
185
186 /**
187 * Converts a tenant object into a JSON string.
188 *
189 * @param tenant volt tenant object to convert
190 * @return JSON string for the tenant
191 */
192 private String tenantToJson(VoltTenant tenant) {
193 return "{"
194 + "\"humanReadableName\": \"" + tenant.humanReadableName() + "\","
195 + "\"id\": \"" + tenant.id() + "\","
196 + "\"provider_service\": \"" + tenant.providerService() + "\","
197 + "\"service_specific_id\": \"" + tenant.serviceSpecificId() + "\","
198 + "\"vlan_id\": \"" + tenant.vlanId() + "\""
199 + "}";
200 }
201
202 /**
203 * Gets a client web resource builder for the base XOS REST API
204 * with no additional URI.
205 *
206 * @return web resource builder
Sho SHIMIZUbe63b232015-06-30 10:57:58 -0700207 * @deprecated in Cardinal Release
Ray Milkey140e4782015-04-24 11:25:13 -0700208 */
Simon Hunt8483e9d2015-05-26 18:22:07 -0700209 @Deprecated
Ray Milkey140e4782015-04-24 11:25:13 -0700210 private WebResource.Builder getClientBuilder() {
211 return getClientBuilder("");
212 }
213
214 /**
215 * Gets a client web resource builder for the base XOS REST API
216 * with an optional additional URI.
217 *
218 * @return web resource builder
Sho SHIMIZUbe63b232015-06-30 10:57:58 -0700219 * @deprecated in Cardinal Release
Ray Milkey140e4782015-04-24 11:25:13 -0700220 */
Simon Hunt8483e9d2015-05-26 18:22:07 -0700221 @Deprecated
Ray Milkey140e4782015-04-24 11:25:13 -0700222 private WebResource.Builder getClientBuilder(String uri) {
223 String baseUrl = "http://" + xosServerAddress + ":"
224 + Integer.toString(xosServerPort);
225 Client client = Client.create();
226 client.addFilter(new HTTPBasicAuthFilter("padmin@vicci.org", "letmein"));
227 WebResource resource = client.resource(baseUrl
228 + XOS_TENANT_BASE_URI + uri);
229 return resource.accept(JSON_UTF_8.toString())
230 .type(JSON_UTF_8.toString());
231 }
232
233 /**
234 * Performs a REST GET operation on the base XOS REST URI.
235 *
236 * @return JSON string fetched by the GET operation
Sho SHIMIZUbe63b232015-06-30 10:57:58 -0700237 * @deprecated in Cardinal Release
Ray Milkey140e4782015-04-24 11:25:13 -0700238 */
Simon Hunt8483e9d2015-05-26 18:22:07 -0700239 @Deprecated
Ray Milkey140e4782015-04-24 11:25:13 -0700240 private String getRest() {
241 return getRest("");
242 }
243
244 /**
245 * Performs a REST GET operation on the base XOS REST URI with
246 * an optional additional URI.
247 *
248 * @return JSON string fetched by the GET operation
Sho SHIMIZUbe63b232015-06-30 10:57:58 -0700249 * @deprecated in Cardinal Release
Ray Milkey140e4782015-04-24 11:25:13 -0700250 */
Simon Hunt8483e9d2015-05-26 18:22:07 -0700251 @Deprecated
Ray Milkey140e4782015-04-24 11:25:13 -0700252 private String getRest(String uri) {
253 WebResource.Builder builder = getClientBuilder(uri);
254 ClientResponse response = builder.get(ClientResponse.class);
255
256 if (response.getStatus() != HTTP_OK) {
257 log.info("REST GET request returned error code {}",
258 response.getStatus());
259 }
260 String jsonString = response.getEntity(String.class);
261 log.info("JSON read:\n{}", jsonString);
262
263 return jsonString;
264 }
265
266 /**
267 * Performs a REST POST operation of a json string on the base
268 * XOS REST URI with an optional additional URI.
269 *
270 * @param json JSON string to post
Sho SHIMIZUbe63b232015-06-30 10:57:58 -0700271 * @deprecated in Cardinal Release
Ray Milkey140e4782015-04-24 11:25:13 -0700272 */
Simon Hunt8483e9d2015-05-26 18:22:07 -0700273 @Deprecated
alshabib60562282015-06-01 16:45:04 -0700274 private String postRest(String json) {
Ray Milkey140e4782015-04-24 11:25:13 -0700275 WebResource.Builder builder = getClientBuilder();
Jonathan Hartb4558032015-05-20 16:32:04 -0700276 ClientResponse response;
277
278 try {
279 response = builder.post(ClientResponse.class, json);
280 } catch (ClientHandlerException e) {
281 log.warn("Unable to contact REST server: {}", e.getMessage());
alshabib60562282015-06-01 16:45:04 -0700282 return "{ 'error' : 'oops no one home' }";
Jonathan Hartb4558032015-05-20 16:32:04 -0700283 }
Ray Milkey140e4782015-04-24 11:25:13 -0700284
285 if (response.getStatus() != HTTP_CREATED) {
286 log.info("REST POST request returned error code {}",
287 response.getStatus());
288 }
alshabib60562282015-06-01 16:45:04 -0700289 return response.getEntity(String.class);
Ray Milkey140e4782015-04-24 11:25:13 -0700290 }
291
292 /**
293 * Performs a REST DELETE operation on the base
294 * XOS REST URI with an optional additional URI.
295 *
296 * @param uri optional additional URI
Sho SHIMIZUbe63b232015-06-30 10:57:58 -0700297 * @deprecated in Cardinal Release
Ray Milkey140e4782015-04-24 11:25:13 -0700298 */
Simon Hunt8483e9d2015-05-26 18:22:07 -0700299 @Deprecated
Ray Milkey140e4782015-04-24 11:25:13 -0700300 private void deleteRest(String uri) {
301 WebResource.Builder builder = getClientBuilder(uri);
302 ClientResponse response = builder.delete(ClientResponse.class);
303
304 if (response.getStatus() != HTTP_NO_CONTENT) {
305 log.info("REST DELETE request returned error code {}",
306 response.getStatus());
307 }
308 }
309
310 /**
311 * Deletes the tenant with the given ID.
312 *
313 * @param tenantId ID of tenant to delete
314 */
315 private void deleteTenant(long tenantId) {
316 deleteRest(Long.toString(tenantId));
317 }
318
319 @Override
320 public Set<VoltTenant> getAllTenants() {
321 String jsonString = getRest();
322
323 JsonArray voltTenantItems = JsonArray.readFrom(jsonString);
324
325 return IntStream.range(0, voltTenantItems.size())
326 .mapToObj(index -> jsonToTenant(voltTenantItems.get(index).asObject()))
327 .collect(Collectors.toSet());
328 }
329
330 @Override
331 public void removeTenant(long id) {
332 deleteTenant(id);
333 }
334
335 @Override
336 public VoltTenant addTenant(VoltTenant newTenant) {
Ray Milkeydea98172015-05-18 10:39:39 -0700337 long providerServiceId = newTenant.providerService();
338 if (providerServiceId == -1) {
339 providerServiceId = xosProviderService;
340 }
Jonathan Hart64da69d2015-07-15 15:10:28 -0700341
342 PortNumber onuPort = newTenant.port().port();
343 VlanId subscriberVlan = VlanId.vlanId(portToVlan.get(onuPort.toLong()));
344
Ray Milkeydea98172015-05-18 10:39:39 -0700345 VoltTenant tenantToCreate = VoltTenant.builder()
346 .withProviderService(providerServiceId)
Jonathan Hart08ad8232015-06-09 10:51:05 -0700347 .withServiceSpecificId(portToSsid.get(newTenant.port()))
Jonathan Hart64da69d2015-07-15 15:10:28 -0700348 .withVlanId(String.valueOf(subscriberVlan.toShort()))
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700349 .withPort(newTenant.port())
Ray Milkeydea98172015-05-18 10:39:39 -0700350 .build();
351 String json = tenantToJson(tenantToCreate);
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700352
Jonathan Hart64da69d2015-07-15 15:10:28 -0700353
354 provisionVlanOnPort(OLT_DEVICE_ID, OLT_UPLINK_PORT, onuPort, subscriberVlan.toShort());
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700355
alshabib60562282015-06-01 16:45:04 -0700356 String retJson = postRest(json);
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700357
Jonathan Hart64da69d2015-07-15 15:10:28 -0700358 fetchCPELocation(tenantToCreate, retJson);
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700359
Ray Milkey140e4782015-04-24 11:25:13 -0700360 return newTenant;
361 }
362
alshabib60562282015-06-01 16:45:04 -0700363 private void fetchCPELocation(VoltTenant newTenant, String jsonString) {
364 JsonObject json = JsonObject.readFrom(jsonString);
365
366 if (json.get("computeNodeName") != null) {
Jonathan Hart10b98ad2015-06-02 09:53:12 -0700367 ConnectPoint point = nodeToPort.get(json.get("computeNodeName").asString());
Jonathan Hart64da69d2015-07-15 15:10:28 -0700368 //ConnectPoint fromPoint = newTenant.port();
369 ConnectPoint oltPort = new ConnectPoint(FABRIC_DEVICE_ID, FABRIC_OLT_CONNECT_POINT);
alshabib60562282015-06-01 16:45:04 -0700370
371 provisionFabric(VlanId.vlanId(Short.parseShort(newTenant.vlanId())),
Jonathan Hart64da69d2015-07-15 15:10:28 -0700372 point, oltPort);
alshabib60562282015-06-01 16:45:04 -0700373 }
374
375 }
376
Ray Milkey140e4782015-04-24 11:25:13 -0700377 @Override
378 public VoltTenant getTenant(long id) {
379 String jsonString = getRest(Long.toString(id));
380 JsonObject jsonTenant = JsonObject.readFrom(jsonString);
381 if (jsonTenant.get("id") != null) {
382 return jsonToTenant(jsonTenant);
383 } else {
384 return null;
385 }
386 }
387
Jonathan Hart64da69d2015-07-15 15:10:28 -0700388 private void provisionVlanOnPort(DeviceId deviceId, int uplinkPort, PortNumber p, short vlanId) {
389
390 TrafficSelector upstream = DefaultTrafficSelector.builder()
Jonathan Hart1468fee2015-07-16 18:50:34 -0700391 .matchVlanId(VlanId.ANY)
Jonathan Hart64da69d2015-07-15 15:10:28 -0700392 .matchInPort(p)
393 .build();
394
395 TrafficSelector downstream = DefaultTrafficSelector.builder()
396 .matchVlanId(VlanId.vlanId(vlanId))
397 .matchInPort(PortNumber.portNumber(uplinkPort))
398 .build();
399
400 TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder()
Jonathan Hart1468fee2015-07-16 18:50:34 -0700401 .setVlanId(VlanId.vlanId(vlanId))
Jonathan Hart64da69d2015-07-15 15:10:28 -0700402 .setOutput(PortNumber.portNumber(uplinkPort))
403 .build();
404
405 TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder()
Jonathan Hart1468fee2015-07-16 18:50:34 -0700406 .popVlan()
Jonathan Hart64da69d2015-07-15 15:10:28 -0700407 .setOutput(p)
408 .build();
409
410
411 ForwardingObjective upFwd = DefaultForwardingObjective.builder()
412 .withFlag(ForwardingObjective.Flag.VERSATILE)
413 .withPriority(1000)
414 .makePermanent()
415 .withSelector(upstream)
416 .fromApp(appId)
417 .withTreatment(upstreamTreatment)
418 .add();
419
420 ForwardingObjective downFwd = DefaultForwardingObjective.builder()
421 .withFlag(ForwardingObjective.Flag.VERSATILE)
422 .withPriority(1000)
423 .makePermanent()
424 .withSelector(downstream)
425 .fromApp(appId)
426 .withTreatment(downstreamTreatment)
427 .add();
428
429 flowObjectiveService.forward(deviceId, upFwd);
430 flowObjectiveService.forward(deviceId, downFwd);
431
432 }
433
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700434 private void provisionDataPlane(VoltTenant tenant) {
435 VlanId vlan = VlanId.vlanId(Short.parseShort(tenant.vlanId()));
436
437 TrafficSelector fromGateway = DefaultTrafficSelector.builder()
438 .matchInPhyPort(tenant.port().port())
439 .build();
440
441 TrafficSelector fromFabric = DefaultTrafficSelector.builder()
442 .matchInPhyPort(FABRIC_PORT.port())
443 .matchVlanId(vlan)
444 .build();
445
446 TrafficTreatment toFabric = DefaultTrafficTreatment.builder()
447 .pushVlan()
448 .setVlanId(vlan)
449 .setOutput(FABRIC_PORT.port())
450 .build();
451
452 TrafficTreatment toGateway = DefaultTrafficTreatment.builder()
453 .popVlan()
454 .setOutput(tenant.port().port())
455 .build();
456
457 ForwardingObjective forwardToFabric = DefaultForwardingObjective.builder()
458 .withFlag(ForwardingObjective.Flag.VERSATILE)
Jonathan Hartb4558032015-05-20 16:32:04 -0700459 .withPriority(PRIORITY)
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700460 .makePermanent()
461 .fromApp(appId)
462 .withSelector(fromGateway)
463 .withTreatment(toFabric)
464 .add();
465
466 ForwardingObjective forwardToGateway = DefaultForwardingObjective.builder()
467 .withFlag(ForwardingObjective.Flag.VERSATILE)
Jonathan Hartb4558032015-05-20 16:32:04 -0700468 .withPriority(PRIORITY)
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700469 .makePermanent()
470 .fromApp(appId)
471 .withSelector(fromFabric)
472 .withTreatment(toGateway)
473 .add();
474
475 flowObjectiveService.forward(FABRIC_PORT.deviceId(), forwardToFabric);
476 flowObjectiveService.forward(FABRIC_PORT.deviceId(), forwardToGateway);
477 }
478
alshabib48dd9a12015-06-05 14:45:57 -0700479 private void provisionFabric(VlanId vlanId, ConnectPoint point, ConnectPoint fromPoint) {
alshabib60562282015-06-01 16:45:04 -0700480
Jonathan Hart64da69d2015-07-15 15:10:28 -0700481 long vlan = vlanId.toShort();
alshabibc4b5d462015-06-08 23:06:24 -0700482
alshabib60562282015-06-01 16:45:04 -0700483 JsonObject node = new JsonObject();
alshabibc4b5d462015-06-08 23:06:24 -0700484 node.add("vlan", vlan);
485 if (vlan == 201) {
486 node.add("iptv", true);
487 } else {
488 node.add("iptv", false);
489 }
alshabib60562282015-06-01 16:45:04 -0700490 JsonArray array = new JsonArray();
491 JsonObject cp1 = new JsonObject();
492 JsonObject cp2 = new JsonObject();
493 cp1.add("device", point.deviceId().toString());
494 cp1.add("port", point.port().toLong());
alshabib48dd9a12015-06-05 14:45:57 -0700495 cp2.add("device", fromPoint.deviceId().toString());
496 cp2.add("port", fromPoint.port().toLong());
alshabib60562282015-06-01 16:45:04 -0700497 array.add(cp1);
498 array.add(cp2);
499 node.add("ports", array);
500
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700501
502 String baseUrl = "http://" + FABRIC_CONTROLLER_ADDRESS + ":"
503 + Integer.toString(FABRIC_SERVER_PORT);
504 Client client = Client.create();
Jonathan Hartb4558032015-05-20 16:32:04 -0700505 WebResource resource = client.resource(baseUrl + FABRIC_BASE_URI);
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700506 WebResource.Builder builder = resource.accept(JSON_UTF_8.toString())
507 .type(JSON_UTF_8.toString());
508
Jonathan Hartb4558032015-05-20 16:32:04 -0700509 try {
Jonathan Hart8e36be52015-06-05 10:54:29 -0700510 builder.post(ClientResponse.class, node.toString());
Jonathan Hartb4558032015-05-20 16:32:04 -0700511 } catch (ClientHandlerException e) {
Jonathan Hart8e36be52015-06-05 10:54:29 -0700512 log.warn("Unable to contact fabric REST server: {}", e.getMessage());
Jonathan Hartb4558032015-05-20 16:32:04 -0700513 return;
514 }
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700515 }
516
Ray Milkey140e4782015-04-24 11:25:13 -0700517 /**
518 * Extracts properties from the component configuration context.
519 *
520 * @param context the component context
521 */
522 private void readComponentConfiguration(ComponentContext context) {
523 Dictionary<?, ?> properties = context.getProperties();
524
Ray Milkeydea98172015-05-18 10:39:39 -0700525 String newXosServerAddress =
526 Tools.get(properties, XOS_SERVER_ADDRESS_PROPERTY_NAME);
Ray Milkey140e4782015-04-24 11:25:13 -0700527 if (!isNullOrEmpty(newXosServerAddress)) {
528 xosServerAddress = newXosServerAddress;
529 }
530
Ray Milkeydea98172015-05-18 10:39:39 -0700531 String newXosServerPortString =
532 Tools.get(properties, XOS_SERVER_PORT_PROPERTY_NAME);
Ray Milkey140e4782015-04-24 11:25:13 -0700533 if (!isNullOrEmpty(newXosServerPortString)) {
534 xosServerPort = Integer.parseInt(newXosServerPortString);
535 }
Ray Milkeydea98172015-05-18 10:39:39 -0700536
537 String newXosProviderServiceString =
538 Tools.get(properties, XOS_PROVIDER_SERVICE_PROPERTY_NAME);
539 if (!isNullOrEmpty(newXosProviderServiceString)) {
540 xosProviderService = Integer.parseInt(newXosProviderServiceString);
541 }
Ray Milkey140e4782015-04-24 11:25:13 -0700542 }
543}
544
545