use of org.onosproject.incubator.net.virtual.VirtualDevice in project onos by opennetworkinglab.
the class VirtualPortCodec method decode.
@Override
public VirtualPort decode(ObjectNode json, CodecContext context) {
if (json == null || !json.isObject()) {
return null;
}
NetworkId nId = NetworkId.networkId(Long.parseLong(extractMember(NETWORK_ID, json)));
DeviceId dId = DeviceId.deviceId(extractMember(DEVICE_ID, json));
VirtualNetworkService vnetService = context.getService(VirtualNetworkService.class);
Set<VirtualDevice> vDevs = vnetService.getVirtualDevices(nId);
VirtualDevice vDev = vDevs.stream().filter(virtualDevice -> virtualDevice.id().equals(dId)).findFirst().orElse(null);
nullIsIllegal(vDev, dId.toString() + INVALID_VIRTUAL_DEVICE);
PortNumber portNum = PortNumber.portNumber(extractMember(PORT_NUM, json));
DeviceId physDId = DeviceId.deviceId(extractMember(PHYS_DEVICE_ID, json));
PortNumber physPortNum = PortNumber.portNumber(extractMember(PHYS_PORT_NUM, json));
ConnectPoint realizedBy = new ConnectPoint(physDId, physPortNum);
return new DefaultVirtualPort(nId, vDev, portNum, realizedBy);
}
use of org.onosproject.incubator.net.virtual.VirtualDevice in project onos by opennetworkinglab.
the class VirtualDeviceCompleter method getSortedVirtualDevices.
/**
* Returns the list of virtual devices sorted using the network identifier.
*
* @param networkId network id
* @return sorted virtual device list
*/
private List<VirtualDevice> getSortedVirtualDevices(long networkId) {
VirtualNetworkService service = getService(VirtualNetworkService.class);
List<VirtualDevice> virtualDevices = new ArrayList<>();
virtualDevices.addAll(service.getVirtualDevices(NetworkId.networkId(networkId)));
Collections.sort(virtualDevices, Comparators.VIRTUAL_DEVICE_COMPARATOR);
return virtualDevices;
}
use of org.onosproject.incubator.net.virtual.VirtualDevice in project onos by opennetworkinglab.
the class VirtualNetworkMastershipManager method balanceRoles.
@Override
public void balanceRoles() {
// FIXME: More advanced logic for balancing virtual network roles.
List<ControllerNode> nodes = clusterService.getNodes().stream().filter(n -> clusterService.getState(n.id()).equals(ControllerNode.State.ACTIVE)).collect(Collectors.toList());
nodes.sort(Comparator.comparing(ControllerNode::id));
// Pick a node using network Id,
NodeId masterNode = nodes.get((int) ((networkId.id() - 1) % nodes.size())).id();
List<CompletableFuture<Void>> setRoleFutures = Lists.newLinkedList();
for (VirtualDevice device : manager.getVirtualDevices(networkId)) {
setRoleFutures.add(setRole(masterNode, device.id(), MastershipRole.MASTER));
}
CompletableFuture<Void> balanceRolesFuture = CompletableFuture.allOf(setRoleFutures.toArray(new CompletableFuture[setRoleFutures.size()]));
Futures.getUnchecked(balanceRolesFuture);
}
use of org.onosproject.incubator.net.virtual.VirtualDevice in project onos by opennetworkinglab.
the class VirtualNetworkWebResource method createVirtualDevice.
/**
* Creates a virtual device from the JSON input stream.
*
* @param networkId network identifier
* @param stream virtual device JSON stream
* @return status of the request - CREATED if the JSON is correct,
* BAD_REQUEST if the JSON is invalid
* @onos.rsModel VirtualDevice
*/
@POST
@Path("{networkId}/devices")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createVirtualDevice(@PathParam("networkId") long networkId, InputStream stream) {
try {
ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
final VirtualDevice vdevReq = codec(VirtualDevice.class).decode(jsonTree, this);
JsonNode specifiedNetworkId = jsonTree.get("networkId");
if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) {
throw new IllegalArgumentException(INVALID_FIELD + "networkId");
}
final VirtualDevice vdevRes = vnetAdminService.createVirtualDevice(vdevReq.networkId(), vdevReq.id());
UriBuilder locationBuilder = uriInfo.getBaseUriBuilder().path("vnets").path(specifiedNetworkId.asText()).path("devices").path(vdevRes.id().toString());
return Response.created(locationBuilder.build()).build();
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
use of org.onosproject.incubator.net.virtual.VirtualDevice in project onos by opennetworkinglab.
the class DistributedVirtualNetworkStore method bindPort.
@Override
public void bindPort(NetworkId networkId, DeviceId deviceId, PortNumber portNumber, ConnectPoint realizedBy) {
Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId);
Optional<VirtualPort> virtualPortOptional = virtualPortSet.stream().filter(p -> p.element().id().equals(deviceId) && p.number().equals(portNumber)).findFirst();
checkState(virtualPortOptional.isPresent(), "The virtual port has not been added.");
VirtualDevice device = deviceIdVirtualDeviceMap.get(new VirtualDeviceId(networkId, deviceId));
checkNotNull(device, "The device has not been created for deviceId: " + deviceId);
VirtualPort vPort = virtualPortOptional.get();
virtualPortSet.remove(vPort);
vPort = new DefaultVirtualPort(networkId, device, portNumber, realizedBy);
virtualPortSet.add(vPort);
networkIdVirtualPortSetMap.put(networkId, virtualPortSet);
notifyDelegate(new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_UPDATED, networkId, device, vPort));
}
Aggregations