Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.xosintegration; |
| 17 | |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 18 | import com.eclipsesource.json.JsonArray; |
| 19 | import com.eclipsesource.json.JsonObject; |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 21 | import com.sun.jersey.api.client.Client; |
Jonathan Hart | b455803 | 2015-05-20 16:32:04 -0700 | [diff] [blame] | 22 | import com.sun.jersey.api.client.ClientHandlerException; |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 23 | import com.sun.jersey.api.client.ClientResponse; |
| 24 | import com.sun.jersey.api.client.WebResource; |
| 25 | import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Modified; |
| 30 | import org.apache.felix.scr.annotations.Property; |
| 31 | import org.apache.felix.scr.annotations.Reference; |
| 32 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 33 | import org.apache.felix.scr.annotations.Service; |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 34 | import org.onlab.packet.VlanId; |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 35 | import org.onlab.util.Tools; |
| 36 | import org.onosproject.cfg.ComponentConfigService; |
| 37 | import org.onosproject.core.ApplicationId; |
| 38 | import org.onosproject.core.CoreService; |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 39 | import org.onosproject.net.ConnectPoint; |
| 40 | import org.onosproject.net.DeviceId; |
| 41 | import org.onosproject.net.PortNumber; |
| 42 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 43 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 44 | import org.onosproject.net.flow.TrafficSelector; |
| 45 | import org.onosproject.net.flow.TrafficTreatment; |
| 46 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 47 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 48 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 49 | import org.osgi.service.component.ComponentContext; |
| 50 | import org.slf4j.Logger; |
| 51 | |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 52 | import java.util.Dictionary; |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 53 | import java.util.Map; |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 54 | import java.util.Set; |
| 55 | import java.util.stream.Collectors; |
| 56 | import java.util.stream.IntStream; |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 57 | |
| 58 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 59 | import static com.google.common.net.MediaType.JSON_UTF_8; |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 60 | import static java.net.HttpURLConnection.*; |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 61 | import static org.slf4j.LoggerFactory.getLogger; |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * XOS interface application. |
| 66 | */ |
| 67 | @Component(immediate = true) |
| 68 | @Service |
| 69 | public class OnosXOSIntegrationManager implements VoltTenantService { |
Ray Milkey | dea9817 | 2015-05-18 10:39:39 -0700 | [diff] [blame] | 70 | 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 Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 76 | |
| 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 Milkey | dea9817 | 2015-05-18 10:39:39 -0700 | [diff] [blame] | 80 | private static final int TEST_XOS_PROVIDER_SERVICE = 1; |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 81 | |
Jonathan Hart | b455803 | 2015-05-20 16:32:04 -0700 | [diff] [blame] | 82 | private static final int PRIORITY = 50000; |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 83 | 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 Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 94 | 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 Milkey | dea9817 | 2015-05-18 10:39:39 -0700 | [diff] [blame] | 99 | |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 101 | protected FlowObjectiveService flowObjectiveService; |
| 102 | |
Ray Milkey | dea9817 | 2015-05-18 10:39:39 -0700 | [diff] [blame] | 103 | @Property(name = XOS_SERVER_ADDRESS_PROPERTY_NAME, |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 104 | value = TEST_XOS_SERVER_ADDRESS, |
| 105 | label = "XOS Server address") |
| 106 | protected String xosServerAddress = TEST_XOS_SERVER_ADDRESS; |
Ray Milkey | dea9817 | 2015-05-18 10:39:39 -0700 | [diff] [blame] | 107 | |
| 108 | @Property(name = XOS_SERVER_PORT_PROPERTY_NAME, |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 109 | intValue = TEST_XOS_SERVER_PORT, |
| 110 | label = "XOS Server port") |
| 111 | protected int xosServerPort = TEST_XOS_SERVER_PORT; |
Ray Milkey | dea9817 | 2015-05-18 10:39:39 -0700 | [diff] [blame] | 112 | |
| 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 Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 118 | private ApplicationId appId; |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 119 | private Map<String, ConnectPoint> nodeToPort; |
alshabib | 17cde6d | 2015-06-05 15:03:51 -0700 | [diff] [blame] | 120 | private Map<Long, Short> portToVlan; |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 121 | |
| 122 | @Activate |
| 123 | public void activate(ComponentContext context) { |
| 124 | log.info("XOS app is starting"); |
| 125 | cfgService.registerProperties(getClass()); |
| 126 | appId = coreService.registerApplication("org.onosproject.xosintegration"); |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 127 | |
| 128 | setupMap(); |
| 129 | |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 130 | readComponentConfiguration(context); |
| 131 | |
| 132 | log.info("XOS({}) started", appId.id()); |
| 133 | } |
| 134 | |
| 135 | @Deactivate |
| 136 | public void deactivate() { |
| 137 | cfgService.unregisterProperties(getClass(), false); |
| 138 | log.info("XOS({}) stopped", appId.id()); |
| 139 | } |
| 140 | |
| 141 | @Modified |
| 142 | public void modified(ComponentContext context) { |
| 143 | readComponentConfiguration(context); |
| 144 | } |
| 145 | |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 146 | private void setupMap() { |
| 147 | nodeToPort = Maps.newHashMap(); |
| 148 | |
| 149 | nodeToPort.put("cordcompute01.onlab.us", new ConnectPoint(FABRIC_DEVICE_ID, |
| 150 | PortNumber.portNumber(4))); |
| 151 | |
| 152 | nodeToPort.put("cordcompute02.onlab.us", new ConnectPoint(FABRIC_DEVICE_ID, |
| 153 | PortNumber.portNumber(3))); |
alshabib | 17cde6d | 2015-06-05 15:03:51 -0700 | [diff] [blame] | 154 | |
| 155 | portToVlan.putIfAbsent(2L, (short) 201); |
| 156 | portToVlan.putIfAbsent(6L, (short) 401); |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 159 | /** |
| 160 | * Converts a JSON representation of a tenant into a tenant object. |
| 161 | * |
| 162 | * @param jsonTenant JSON object representing the tenant |
| 163 | * @return volt tenant object |
| 164 | */ |
| 165 | private VoltTenant jsonToTenant(JsonObject jsonTenant) { |
| 166 | return VoltTenant.builder() |
| 167 | .withHumanReadableName(jsonTenant.get("humanReadableName").asString()) |
| 168 | .withId(jsonTenant.get("id").asInt()) |
| 169 | .withProviderService(jsonTenant.get("provider_service").asInt()) |
| 170 | .withServiceSpecificId(jsonTenant.get("service_specific_id").asString()) |
| 171 | .withVlanId(jsonTenant.get("vlan_id").asString()) |
| 172 | .build(); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Converts a tenant object into a JSON string. |
| 177 | * |
| 178 | * @param tenant volt tenant object to convert |
| 179 | * @return JSON string for the tenant |
| 180 | */ |
| 181 | private String tenantToJson(VoltTenant tenant) { |
| 182 | return "{" |
| 183 | + "\"humanReadableName\": \"" + tenant.humanReadableName() + "\"," |
| 184 | + "\"id\": \"" + tenant.id() + "\"," |
| 185 | + "\"provider_service\": \"" + tenant.providerService() + "\"," |
| 186 | + "\"service_specific_id\": \"" + tenant.serviceSpecificId() + "\"," |
| 187 | + "\"vlan_id\": \"" + tenant.vlanId() + "\"" |
| 188 | + "}"; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Gets a client web resource builder for the base XOS REST API |
| 193 | * with no additional URI. |
| 194 | * |
| 195 | * @return web resource builder |
| 196 | */ |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 197 | @Deprecated |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 198 | private WebResource.Builder getClientBuilder() { |
| 199 | return getClientBuilder(""); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Gets a client web resource builder for the base XOS REST API |
| 204 | * with an optional additional URI. |
| 205 | * |
| 206 | * @return web resource builder |
| 207 | */ |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 208 | @Deprecated |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 209 | private WebResource.Builder getClientBuilder(String uri) { |
| 210 | String baseUrl = "http://" + xosServerAddress + ":" |
| 211 | + Integer.toString(xosServerPort); |
| 212 | Client client = Client.create(); |
| 213 | client.addFilter(new HTTPBasicAuthFilter("padmin@vicci.org", "letmein")); |
| 214 | WebResource resource = client.resource(baseUrl |
| 215 | + XOS_TENANT_BASE_URI + uri); |
| 216 | return resource.accept(JSON_UTF_8.toString()) |
| 217 | .type(JSON_UTF_8.toString()); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Performs a REST GET operation on the base XOS REST URI. |
| 222 | * |
| 223 | * @return JSON string fetched by the GET operation |
| 224 | */ |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 225 | @Deprecated |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 226 | private String getRest() { |
| 227 | return getRest(""); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Performs a REST GET operation on the base XOS REST URI with |
| 232 | * an optional additional URI. |
| 233 | * |
| 234 | * @return JSON string fetched by the GET operation |
| 235 | */ |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 236 | @Deprecated |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 237 | private String getRest(String uri) { |
| 238 | WebResource.Builder builder = getClientBuilder(uri); |
| 239 | ClientResponse response = builder.get(ClientResponse.class); |
| 240 | |
| 241 | if (response.getStatus() != HTTP_OK) { |
| 242 | log.info("REST GET request returned error code {}", |
| 243 | response.getStatus()); |
| 244 | } |
| 245 | String jsonString = response.getEntity(String.class); |
| 246 | log.info("JSON read:\n{}", jsonString); |
| 247 | |
| 248 | return jsonString; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Performs a REST POST operation of a json string on the base |
| 253 | * XOS REST URI with an optional additional URI. |
| 254 | * |
| 255 | * @param json JSON string to post |
| 256 | */ |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 257 | @Deprecated |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 258 | private String postRest(String json) { |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 259 | WebResource.Builder builder = getClientBuilder(); |
Jonathan Hart | b455803 | 2015-05-20 16:32:04 -0700 | [diff] [blame] | 260 | ClientResponse response; |
| 261 | |
| 262 | try { |
| 263 | response = builder.post(ClientResponse.class, json); |
| 264 | } catch (ClientHandlerException e) { |
| 265 | log.warn("Unable to contact REST server: {}", e.getMessage()); |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 266 | return "{ 'error' : 'oops no one home' }"; |
Jonathan Hart | b455803 | 2015-05-20 16:32:04 -0700 | [diff] [blame] | 267 | } |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 268 | |
| 269 | if (response.getStatus() != HTTP_CREATED) { |
| 270 | log.info("REST POST request returned error code {}", |
| 271 | response.getStatus()); |
| 272 | } |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 273 | return response.getEntity(String.class); |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Performs a REST DELETE operation on the base |
| 278 | * XOS REST URI with an optional additional URI. |
| 279 | * |
| 280 | * @param uri optional additional URI |
| 281 | */ |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 282 | @Deprecated |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 283 | private void deleteRest(String uri) { |
| 284 | WebResource.Builder builder = getClientBuilder(uri); |
| 285 | ClientResponse response = builder.delete(ClientResponse.class); |
| 286 | |
| 287 | if (response.getStatus() != HTTP_NO_CONTENT) { |
| 288 | log.info("REST DELETE request returned error code {}", |
| 289 | response.getStatus()); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Deletes the tenant with the given ID. |
| 295 | * |
| 296 | * @param tenantId ID of tenant to delete |
| 297 | */ |
| 298 | private void deleteTenant(long tenantId) { |
| 299 | deleteRest(Long.toString(tenantId)); |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public Set<VoltTenant> getAllTenants() { |
| 304 | String jsonString = getRest(); |
| 305 | |
| 306 | JsonArray voltTenantItems = JsonArray.readFrom(jsonString); |
| 307 | |
| 308 | return IntStream.range(0, voltTenantItems.size()) |
| 309 | .mapToObj(index -> jsonToTenant(voltTenantItems.get(index).asObject())) |
| 310 | .collect(Collectors.toSet()); |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public void removeTenant(long id) { |
| 315 | deleteTenant(id); |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public VoltTenant addTenant(VoltTenant newTenant) { |
Ray Milkey | dea9817 | 2015-05-18 10:39:39 -0700 | [diff] [blame] | 320 | long providerServiceId = newTenant.providerService(); |
| 321 | if (providerServiceId == -1) { |
| 322 | providerServiceId = xosProviderService; |
| 323 | } |
| 324 | VoltTenant tenantToCreate = VoltTenant.builder() |
| 325 | .withProviderService(providerServiceId) |
| 326 | .withServiceSpecificId(newTenant.serviceSpecificId()) |
| 327 | .withVlanId(newTenant.vlanId()) |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 328 | .withPort(newTenant.port()) |
Ray Milkey | dea9817 | 2015-05-18 10:39:39 -0700 | [diff] [blame] | 329 | .build(); |
| 330 | String json = tenantToJson(tenantToCreate); |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 331 | |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 332 | //provisionDataPlane(tenantToCreate); |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 333 | |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 334 | String retJson = postRest(json); |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 335 | |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 336 | fetchCPELocation(newTenant, retJson); |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 337 | |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 338 | return newTenant; |
| 339 | } |
| 340 | |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 341 | private void fetchCPELocation(VoltTenant newTenant, String jsonString) { |
| 342 | JsonObject json = JsonObject.readFrom(jsonString); |
| 343 | |
| 344 | if (json.get("computeNodeName") != null) { |
Jonathan Hart | 10b98ad | 2015-06-02 09:53:12 -0700 | [diff] [blame] | 345 | ConnectPoint point = nodeToPort.get(json.get("computeNodeName").asString()); |
alshabib | 48dd9a1 | 2015-06-05 14:45:57 -0700 | [diff] [blame] | 346 | ConnectPoint fromPoint = newTenant.port(); |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 347 | |
| 348 | provisionFabric(VlanId.vlanId(Short.parseShort(newTenant.vlanId())), |
alshabib | 48dd9a1 | 2015-06-05 14:45:57 -0700 | [diff] [blame] | 349 | point, fromPoint); |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | } |
| 353 | |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 354 | @Override |
| 355 | public VoltTenant getTenant(long id) { |
| 356 | String jsonString = getRest(Long.toString(id)); |
| 357 | JsonObject jsonTenant = JsonObject.readFrom(jsonString); |
| 358 | if (jsonTenant.get("id") != null) { |
| 359 | return jsonToTenant(jsonTenant); |
| 360 | } else { |
| 361 | return null; |
| 362 | } |
| 363 | } |
| 364 | |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 365 | private void provisionDataPlane(VoltTenant tenant) { |
| 366 | VlanId vlan = VlanId.vlanId(Short.parseShort(tenant.vlanId())); |
| 367 | |
| 368 | TrafficSelector fromGateway = DefaultTrafficSelector.builder() |
| 369 | .matchInPhyPort(tenant.port().port()) |
| 370 | .build(); |
| 371 | |
| 372 | TrafficSelector fromFabric = DefaultTrafficSelector.builder() |
| 373 | .matchInPhyPort(FABRIC_PORT.port()) |
| 374 | .matchVlanId(vlan) |
| 375 | .build(); |
| 376 | |
| 377 | TrafficTreatment toFabric = DefaultTrafficTreatment.builder() |
| 378 | .pushVlan() |
| 379 | .setVlanId(vlan) |
| 380 | .setOutput(FABRIC_PORT.port()) |
| 381 | .build(); |
| 382 | |
| 383 | TrafficTreatment toGateway = DefaultTrafficTreatment.builder() |
| 384 | .popVlan() |
| 385 | .setOutput(tenant.port().port()) |
| 386 | .build(); |
| 387 | |
| 388 | ForwardingObjective forwardToFabric = DefaultForwardingObjective.builder() |
| 389 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
Jonathan Hart | b455803 | 2015-05-20 16:32:04 -0700 | [diff] [blame] | 390 | .withPriority(PRIORITY) |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 391 | .makePermanent() |
| 392 | .fromApp(appId) |
| 393 | .withSelector(fromGateway) |
| 394 | .withTreatment(toFabric) |
| 395 | .add(); |
| 396 | |
| 397 | ForwardingObjective forwardToGateway = DefaultForwardingObjective.builder() |
| 398 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
Jonathan Hart | b455803 | 2015-05-20 16:32:04 -0700 | [diff] [blame] | 399 | .withPriority(PRIORITY) |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 400 | .makePermanent() |
| 401 | .fromApp(appId) |
| 402 | .withSelector(fromFabric) |
| 403 | .withTreatment(toGateway) |
| 404 | .add(); |
| 405 | |
| 406 | flowObjectiveService.forward(FABRIC_PORT.deviceId(), forwardToFabric); |
| 407 | flowObjectiveService.forward(FABRIC_PORT.deviceId(), forwardToGateway); |
| 408 | } |
| 409 | |
alshabib | 48dd9a1 | 2015-06-05 14:45:57 -0700 | [diff] [blame] | 410 | private void provisionFabric(VlanId vlanId, ConnectPoint point, ConnectPoint fromPoint) { |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 411 | //String json = "{\"vlan\":" + vlanId + ",\"ports\":["; |
| 412 | //json += "{\"device\":\"" + FABRIC_DEVICE_ID.toString() + "\",\"port\":\"" |
| 413 | // + FABRIC_OLT_CONNECT_POINT.toString() + "\"},"; |
| 414 | //json += "{\"device\":\"" + FABRIC_DEVICE_ID.toString() + "\",\"port\":\"" |
| 415 | // + FABRIC_VCPE_CONNECT_POINT.toString() + "\"}"; |
| 416 | //json += "]}"; |
| 417 | |
| 418 | JsonObject node = new JsonObject(); |
alshabib | 17cde6d | 2015-06-05 15:03:51 -0700 | [diff] [blame] | 419 | node.add("vlan", portToVlan.get(fromPoint.port().toLong())); |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 420 | JsonArray array = new JsonArray(); |
| 421 | JsonObject cp1 = new JsonObject(); |
| 422 | JsonObject cp2 = new JsonObject(); |
| 423 | cp1.add("device", point.deviceId().toString()); |
| 424 | cp1.add("port", point.port().toLong()); |
alshabib | 48dd9a1 | 2015-06-05 14:45:57 -0700 | [diff] [blame] | 425 | cp2.add("device", fromPoint.deviceId().toString()); |
| 426 | cp2.add("port", fromPoint.port().toLong()); |
alshabib | 6056228 | 2015-06-01 16:45:04 -0700 | [diff] [blame] | 427 | array.add(cp1); |
| 428 | array.add(cp2); |
| 429 | node.add("ports", array); |
| 430 | |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 431 | |
| 432 | String baseUrl = "http://" + FABRIC_CONTROLLER_ADDRESS + ":" |
| 433 | + Integer.toString(FABRIC_SERVER_PORT); |
| 434 | Client client = Client.create(); |
Jonathan Hart | b455803 | 2015-05-20 16:32:04 -0700 | [diff] [blame] | 435 | WebResource resource = client.resource(baseUrl + FABRIC_BASE_URI); |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 436 | WebResource.Builder builder = resource.accept(JSON_UTF_8.toString()) |
| 437 | .type(JSON_UTF_8.toString()); |
| 438 | |
Jonathan Hart | b455803 | 2015-05-20 16:32:04 -0700 | [diff] [blame] | 439 | try { |
Jonathan Hart | 8e36be5 | 2015-06-05 10:54:29 -0700 | [diff] [blame] | 440 | builder.post(ClientResponse.class, node.toString()); |
Jonathan Hart | b455803 | 2015-05-20 16:32:04 -0700 | [diff] [blame] | 441 | } catch (ClientHandlerException e) { |
Jonathan Hart | 8e36be5 | 2015-06-05 10:54:29 -0700 | [diff] [blame] | 442 | log.warn("Unable to contact fabric REST server: {}", e.getMessage()); |
Jonathan Hart | b455803 | 2015-05-20 16:32:04 -0700 | [diff] [blame] | 443 | return; |
| 444 | } |
Jonathan Hart | 5e9a63d | 2015-05-19 16:21:46 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 447 | /** |
| 448 | * Extracts properties from the component configuration context. |
| 449 | * |
| 450 | * @param context the component context |
| 451 | */ |
| 452 | private void readComponentConfiguration(ComponentContext context) { |
| 453 | Dictionary<?, ?> properties = context.getProperties(); |
| 454 | |
Ray Milkey | dea9817 | 2015-05-18 10:39:39 -0700 | [diff] [blame] | 455 | String newXosServerAddress = |
| 456 | Tools.get(properties, XOS_SERVER_ADDRESS_PROPERTY_NAME); |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 457 | if (!isNullOrEmpty(newXosServerAddress)) { |
| 458 | xosServerAddress = newXosServerAddress; |
| 459 | } |
| 460 | |
Ray Milkey | dea9817 | 2015-05-18 10:39:39 -0700 | [diff] [blame] | 461 | String newXosServerPortString = |
| 462 | Tools.get(properties, XOS_SERVER_PORT_PROPERTY_NAME); |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 463 | if (!isNullOrEmpty(newXosServerPortString)) { |
| 464 | xosServerPort = Integer.parseInt(newXosServerPortString); |
| 465 | } |
Ray Milkey | dea9817 | 2015-05-18 10:39:39 -0700 | [diff] [blame] | 466 | |
| 467 | String newXosProviderServiceString = |
| 468 | Tools.get(properties, XOS_PROVIDER_SERVICE_PROPERTY_NAME); |
| 469 | if (!isNullOrEmpty(newXosProviderServiceString)) { |
| 470 | xosProviderService = Integer.parseInt(newXosProviderServiceString); |
| 471 | } |
Ray Milkey | 140e478 | 2015-04-24 11:25:13 -0700 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
| 475 | |