blob: 1dd8d8de142fd1095e3502e3ea309620841a822d [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;
20import com.sun.jersey.api.client.Client;
Jonathan Hartb4558032015-05-20 16:32:04 -070021import com.sun.jersey.api.client.ClientHandlerException;
Jonathan Hart5e9a63d2015-05-19 16:21:46 -070022import com.sun.jersey.api.client.ClientResponse;
23import com.sun.jersey.api.client.WebResource;
24import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
Ray Milkey140e4782015-04-24 11:25:13 -070025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Modified;
29import org.apache.felix.scr.annotations.Property;
30import org.apache.felix.scr.annotations.Reference;
31import org.apache.felix.scr.annotations.ReferenceCardinality;
32import org.apache.felix.scr.annotations.Service;
Jonathan Hart5e9a63d2015-05-19 16:21:46 -070033import org.onlab.packet.VlanId;
Ray Milkey140e4782015-04-24 11:25:13 -070034import org.onlab.util.Tools;
35import org.onosproject.cfg.ComponentConfigService;
36import org.onosproject.core.ApplicationId;
37import org.onosproject.core.CoreService;
Jonathan Hart5e9a63d2015-05-19 16:21:46 -070038import org.onosproject.net.ConnectPoint;
39import org.onosproject.net.DeviceId;
40import org.onosproject.net.PortNumber;
41import org.onosproject.net.flow.DefaultTrafficSelector;
42import org.onosproject.net.flow.DefaultTrafficTreatment;
43import org.onosproject.net.flow.TrafficSelector;
44import org.onosproject.net.flow.TrafficTreatment;
45import org.onosproject.net.flowobjective.DefaultForwardingObjective;
46import org.onosproject.net.flowobjective.FlowObjectiveService;
47import org.onosproject.net.flowobjective.ForwardingObjective;
Ray Milkey140e4782015-04-24 11:25:13 -070048import org.osgi.service.component.ComponentContext;
49import org.slf4j.Logger;
50
Jonathan Hart5e9a63d2015-05-19 16:21:46 -070051import java.util.Dictionary;
52import java.util.Set;
53import java.util.stream.Collectors;
54import java.util.stream.IntStream;
Ray Milkey140e4782015-04-24 11:25:13 -070055
56import static com.google.common.base.Strings.isNullOrEmpty;
57import static com.google.common.net.MediaType.JSON_UTF_8;
58import static java.net.HttpURLConnection.HTTP_CREATED;
59import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
60import static java.net.HttpURLConnection.HTTP_OK;
61import static org.slf4j.LoggerFactory.getLogger;
62
63
64/**
65 * XOS interface application.
66 */
67@Component(immediate = true)
68@Service
69public class OnosXOSIntegrationManager implements VoltTenantService {
Ray Milkeydea98172015-05-18 10:39:39 -070070 private static final String XOS_SERVER_ADDRESS_PROPERTY_NAME =
71 "xosServerAddress";
72 private static final String XOS_SERVER_PORT_PROPERTY_NAME =
73 "xosServerPort";
74 private static final String XOS_PROVIDER_SERVICE_PROPERTY_NAME =
75 "xosProviderService";
Ray Milkey140e4782015-04-24 11:25:13 -070076
77 private static final String TEST_XOS_SERVER_ADDRESS = "10.254.1.22";
78 private static final int TEST_XOS_SERVER_PORT = 8000;
79 private static final String XOS_TENANT_BASE_URI = "/xoslib/volttenant/";
Ray Milkeydea98172015-05-18 10:39:39 -070080 private static final int TEST_XOS_PROVIDER_SERVICE = 1;
Ray Milkey140e4782015-04-24 11:25:13 -070081
Jonathan Hartb4558032015-05-20 16:32:04 -070082 private static final int PRIORITY = 50000;
Jonathan Hart5e9a63d2015-05-19 16:21:46 -070083 private static final DeviceId FABRIC_DEVICE_ID = DeviceId.deviceId("of:5e3e486e73000187");
84 private static final PortNumber FABRIC_OLT_CONNECT_POINT = PortNumber.portNumber(2);
85 private static final PortNumber FABRIC_VCPE_CONNECT_POINT = PortNumber.portNumber(3);
86 private static final String FABRIC_CONTROLLER_ADDRESS = "10.0.3.136";
87 private static final int FABRIC_SERVER_PORT = 8181;
88 private static final String FABRIC_BASE_URI = "/onos/cordfabric/vlans/add";
89
90 private static final ConnectPoint FABRIC_PORT = new ConnectPoint(
91 DeviceId.deviceId("of:000090e2ba82f974"),
92 PortNumber.portNumber(2));
93
Ray Milkey140e4782015-04-24 11:25:13 -070094 private final Logger log = getLogger(getClass());
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected CoreService coreService;
97 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
98 protected ComponentConfigService cfgService;
Ray Milkeydea98172015-05-18 10:39:39 -070099
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 protected FlowObjectiveService flowObjectiveService;
102
Ray Milkeydea98172015-05-18 10:39:39 -0700103 @Property(name = XOS_SERVER_ADDRESS_PROPERTY_NAME,
Ray Milkey140e4782015-04-24 11:25:13 -0700104 value = TEST_XOS_SERVER_ADDRESS,
105 label = "XOS Server address")
106 protected String xosServerAddress = TEST_XOS_SERVER_ADDRESS;
Ray Milkeydea98172015-05-18 10:39:39 -0700107
108 @Property(name = XOS_SERVER_PORT_PROPERTY_NAME,
Ray Milkey140e4782015-04-24 11:25:13 -0700109 intValue = TEST_XOS_SERVER_PORT,
110 label = "XOS Server port")
111 protected int xosServerPort = TEST_XOS_SERVER_PORT;
Ray Milkeydea98172015-05-18 10:39:39 -0700112
113 @Property(name = XOS_PROVIDER_SERVICE_PROPERTY_NAME,
114 intValue = TEST_XOS_PROVIDER_SERVICE,
115 label = "XOS Provider Service")
116 protected int xosProviderService = TEST_XOS_PROVIDER_SERVICE;
117
Ray Milkey140e4782015-04-24 11:25:13 -0700118 private ApplicationId appId;
119
120 @Activate
121 public void activate(ComponentContext context) {
122 log.info("XOS app is starting");
123 cfgService.registerProperties(getClass());
124 appId = coreService.registerApplication("org.onosproject.xosintegration");
125 readComponentConfiguration(context);
126
127 log.info("XOS({}) started", appId.id());
128 }
129
130 @Deactivate
131 public void deactivate() {
132 cfgService.unregisterProperties(getClass(), false);
133 log.info("XOS({}) stopped", appId.id());
134 }
135
136 @Modified
137 public void modified(ComponentContext context) {
138 readComponentConfiguration(context);
139 }
140
141 /**
142 * Converts a JSON representation of a tenant into a tenant object.
143 *
144 * @param jsonTenant JSON object representing the tenant
145 * @return volt tenant object
146 */
147 private VoltTenant jsonToTenant(JsonObject jsonTenant) {
148 return VoltTenant.builder()
149 .withHumanReadableName(jsonTenant.get("humanReadableName").asString())
150 .withId(jsonTenant.get("id").asInt())
151 .withProviderService(jsonTenant.get("provider_service").asInt())
152 .withServiceSpecificId(jsonTenant.get("service_specific_id").asString())
153 .withVlanId(jsonTenant.get("vlan_id").asString())
154 .build();
155 }
156
157 /**
158 * Converts a tenant object into a JSON string.
159 *
160 * @param tenant volt tenant object to convert
161 * @return JSON string for the tenant
162 */
163 private String tenantToJson(VoltTenant tenant) {
164 return "{"
165 + "\"humanReadableName\": \"" + tenant.humanReadableName() + "\","
166 + "\"id\": \"" + tenant.id() + "\","
167 + "\"provider_service\": \"" + tenant.providerService() + "\","
168 + "\"service_specific_id\": \"" + tenant.serviceSpecificId() + "\","
169 + "\"vlan_id\": \"" + tenant.vlanId() + "\""
170 + "}";
171 }
172
173 /**
174 * Gets a client web resource builder for the base XOS REST API
175 * with no additional URI.
176 *
177 * @return web resource builder
178 */
Simon Hunt8483e9d2015-05-26 18:22:07 -0700179 @Deprecated
Ray Milkey140e4782015-04-24 11:25:13 -0700180 private WebResource.Builder getClientBuilder() {
181 return getClientBuilder("");
182 }
183
184 /**
185 * Gets a client web resource builder for the base XOS REST API
186 * with an optional additional URI.
187 *
188 * @return web resource builder
189 */
Simon Hunt8483e9d2015-05-26 18:22:07 -0700190 @Deprecated
Ray Milkey140e4782015-04-24 11:25:13 -0700191 private WebResource.Builder getClientBuilder(String uri) {
192 String baseUrl = "http://" + xosServerAddress + ":"
193 + Integer.toString(xosServerPort);
194 Client client = Client.create();
195 client.addFilter(new HTTPBasicAuthFilter("padmin@vicci.org", "letmein"));
196 WebResource resource = client.resource(baseUrl
197 + XOS_TENANT_BASE_URI + uri);
198 return resource.accept(JSON_UTF_8.toString())
199 .type(JSON_UTF_8.toString());
200 }
201
202 /**
203 * Performs a REST GET operation on the base XOS REST URI.
204 *
205 * @return JSON string fetched by the GET operation
206 */
Simon Hunt8483e9d2015-05-26 18:22:07 -0700207 @Deprecated
Ray Milkey140e4782015-04-24 11:25:13 -0700208 private String getRest() {
209 return getRest("");
210 }
211
212 /**
213 * Performs a REST GET operation on the base XOS REST URI with
214 * an optional additional URI.
215 *
216 * @return JSON string fetched by the GET operation
217 */
Simon Hunt8483e9d2015-05-26 18:22:07 -0700218 @Deprecated
Ray Milkey140e4782015-04-24 11:25:13 -0700219 private String getRest(String uri) {
220 WebResource.Builder builder = getClientBuilder(uri);
221 ClientResponse response = builder.get(ClientResponse.class);
222
223 if (response.getStatus() != HTTP_OK) {
224 log.info("REST GET request returned error code {}",
225 response.getStatus());
226 }
227 String jsonString = response.getEntity(String.class);
228 log.info("JSON read:\n{}", jsonString);
229
230 return jsonString;
231 }
232
233 /**
234 * Performs a REST POST operation of a json string on the base
235 * XOS REST URI with an optional additional URI.
236 *
237 * @param json JSON string to post
238 */
Simon Hunt8483e9d2015-05-26 18:22:07 -0700239 @Deprecated
Ray Milkey140e4782015-04-24 11:25:13 -0700240 private void postRest(String json) {
241 WebResource.Builder builder = getClientBuilder();
Jonathan Hartb4558032015-05-20 16:32:04 -0700242 ClientResponse response;
243
244 try {
245 response = builder.post(ClientResponse.class, json);
246 } catch (ClientHandlerException e) {
247 log.warn("Unable to contact REST server: {}", e.getMessage());
248 return;
249 }
Ray Milkey140e4782015-04-24 11:25:13 -0700250
251 if (response.getStatus() != HTTP_CREATED) {
252 log.info("REST POST request returned error code {}",
253 response.getStatus());
254 }
255 }
256
257 /**
258 * Performs a REST DELETE operation on the base
259 * XOS REST URI with an optional additional URI.
260 *
261 * @param uri optional additional URI
262 */
Simon Hunt8483e9d2015-05-26 18:22:07 -0700263 @Deprecated
Ray Milkey140e4782015-04-24 11:25:13 -0700264 private void deleteRest(String uri) {
265 WebResource.Builder builder = getClientBuilder(uri);
266 ClientResponse response = builder.delete(ClientResponse.class);
267
268 if (response.getStatus() != HTTP_NO_CONTENT) {
269 log.info("REST DELETE request returned error code {}",
270 response.getStatus());
271 }
272 }
273
274 /**
275 * Deletes the tenant with the given ID.
276 *
277 * @param tenantId ID of tenant to delete
278 */
279 private void deleteTenant(long tenantId) {
280 deleteRest(Long.toString(tenantId));
281 }
282
283 @Override
284 public Set<VoltTenant> getAllTenants() {
285 String jsonString = getRest();
286
287 JsonArray voltTenantItems = JsonArray.readFrom(jsonString);
288
289 return IntStream.range(0, voltTenantItems.size())
290 .mapToObj(index -> jsonToTenant(voltTenantItems.get(index).asObject()))
291 .collect(Collectors.toSet());
292 }
293
294 @Override
295 public void removeTenant(long id) {
296 deleteTenant(id);
297 }
298
299 @Override
300 public VoltTenant addTenant(VoltTenant newTenant) {
Ray Milkeydea98172015-05-18 10:39:39 -0700301 long providerServiceId = newTenant.providerService();
302 if (providerServiceId == -1) {
303 providerServiceId = xosProviderService;
304 }
305 VoltTenant tenantToCreate = VoltTenant.builder()
306 .withProviderService(providerServiceId)
307 .withServiceSpecificId(newTenant.serviceSpecificId())
308 .withVlanId(newTenant.vlanId())
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700309 .withPort(newTenant.port())
Ray Milkeydea98172015-05-18 10:39:39 -0700310 .build();
311 String json = tenantToJson(tenantToCreate);
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700312
313 provisionDataPlane(tenantToCreate);
314
Ray Milkey140e4782015-04-24 11:25:13 -0700315 postRest(json);
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700316
317 provisionFabric(VlanId.vlanId(Short.parseShort(newTenant.vlanId())));
318
Ray Milkey140e4782015-04-24 11:25:13 -0700319 return newTenant;
320 }
321
322 @Override
323 public VoltTenant getTenant(long id) {
324 String jsonString = getRest(Long.toString(id));
325 JsonObject jsonTenant = JsonObject.readFrom(jsonString);
326 if (jsonTenant.get("id") != null) {
327 return jsonToTenant(jsonTenant);
328 } else {
329 return null;
330 }
331 }
332
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700333 private void provisionDataPlane(VoltTenant tenant) {
334 VlanId vlan = VlanId.vlanId(Short.parseShort(tenant.vlanId()));
335
336 TrafficSelector fromGateway = DefaultTrafficSelector.builder()
337 .matchInPhyPort(tenant.port().port())
338 .build();
339
340 TrafficSelector fromFabric = DefaultTrafficSelector.builder()
341 .matchInPhyPort(FABRIC_PORT.port())
342 .matchVlanId(vlan)
343 .build();
344
345 TrafficTreatment toFabric = DefaultTrafficTreatment.builder()
346 .pushVlan()
347 .setVlanId(vlan)
348 .setOutput(FABRIC_PORT.port())
349 .build();
350
351 TrafficTreatment toGateway = DefaultTrafficTreatment.builder()
352 .popVlan()
353 .setOutput(tenant.port().port())
354 .build();
355
356 ForwardingObjective forwardToFabric = DefaultForwardingObjective.builder()
357 .withFlag(ForwardingObjective.Flag.VERSATILE)
Jonathan Hartb4558032015-05-20 16:32:04 -0700358 .withPriority(PRIORITY)
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700359 .makePermanent()
360 .fromApp(appId)
361 .withSelector(fromGateway)
362 .withTreatment(toFabric)
363 .add();
364
365 ForwardingObjective forwardToGateway = DefaultForwardingObjective.builder()
366 .withFlag(ForwardingObjective.Flag.VERSATILE)
Jonathan Hartb4558032015-05-20 16:32:04 -0700367 .withPriority(PRIORITY)
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700368 .makePermanent()
369 .fromApp(appId)
370 .withSelector(fromFabric)
371 .withTreatment(toGateway)
372 .add();
373
374 flowObjectiveService.forward(FABRIC_PORT.deviceId(), forwardToFabric);
375 flowObjectiveService.forward(FABRIC_PORT.deviceId(), forwardToGateway);
376 }
377
378 private void provisionFabric(VlanId vlanId) {
379 String json = "{\"vlan\":" + vlanId + ",\"ports\":[";
380 json += "{\"device\":\"" + FABRIC_DEVICE_ID.toString() + "\",\"port\":\""
381 + FABRIC_OLT_CONNECT_POINT.toString() + "\"},";
382 json += "{\"device\":\"" + FABRIC_DEVICE_ID.toString() + "\",\"port\":\""
383 + FABRIC_VCPE_CONNECT_POINT.toString() + "\"}";
384 json += "]}";
385
386 String baseUrl = "http://" + FABRIC_CONTROLLER_ADDRESS + ":"
387 + Integer.toString(FABRIC_SERVER_PORT);
388 Client client = Client.create();
Jonathan Hartb4558032015-05-20 16:32:04 -0700389 WebResource resource = client.resource(baseUrl + FABRIC_BASE_URI);
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700390 WebResource.Builder builder = resource.accept(JSON_UTF_8.toString())
391 .type(JSON_UTF_8.toString());
392
Jonathan Hartb4558032015-05-20 16:32:04 -0700393 try {
394 builder.post(ClientResponse.class, json);
395 } catch (ClientHandlerException e) {
396 log.warn("Unable to contact fabric REST server:", e.getMessage());
397 return;
398 }
Jonathan Hart5e9a63d2015-05-19 16:21:46 -0700399 }
400
Ray Milkey140e4782015-04-24 11:25:13 -0700401 /**
402 * Extracts properties from the component configuration context.
403 *
404 * @param context the component context
405 */
406 private void readComponentConfiguration(ComponentContext context) {
407 Dictionary<?, ?> properties = context.getProperties();
408
Ray Milkeydea98172015-05-18 10:39:39 -0700409 String newXosServerAddress =
410 Tools.get(properties, XOS_SERVER_ADDRESS_PROPERTY_NAME);
Ray Milkey140e4782015-04-24 11:25:13 -0700411 if (!isNullOrEmpty(newXosServerAddress)) {
412 xosServerAddress = newXosServerAddress;
413 }
414
Ray Milkeydea98172015-05-18 10:39:39 -0700415 String newXosServerPortString =
416 Tools.get(properties, XOS_SERVER_PORT_PROPERTY_NAME);
Ray Milkey140e4782015-04-24 11:25:13 -0700417 if (!isNullOrEmpty(newXosServerPortString)) {
418 xosServerPort = Integer.parseInt(newXosServerPortString);
419 }
Ray Milkeydea98172015-05-18 10:39:39 -0700420
421 String newXosProviderServiceString =
422 Tools.get(properties, XOS_PROVIDER_SERVICE_PROPERTY_NAME);
423 if (!isNullOrEmpty(newXosProviderServiceString)) {
424 xosProviderService = Integer.parseInt(newXosProviderServiceString);
425 }
Ray Milkey140e4782015-04-24 11:25:13 -0700426 }
427}
428
429