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