use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class OpenstackSwitchingHostProvider method processPortAdded.
/**
* Processes port addition event.
* Once a port addition event is detected, it tries to create a host instance
* with openstack augmented host information such as networkId, portId,
* createTime, segmentId and notifies to host provider.
*
* @param port port object used in ONOS
*/
void processPortAdded(Port port) {
// TODO check the node state is COMPLETE
org.openstack4j.model.network.Port osPort = osNetworkService.port(port);
if (osPort == null) {
log.warn(ERR_ADD_HOST + "OpenStack port for {} not found", port);
return;
}
Network osNet = osNetworkService.network(osPort.getNetworkId());
if (osNet == null) {
log.warn(ERR_ADD_HOST + "OpenStack network {} not found", osPort.getNetworkId());
return;
}
if (osPort.getFixedIps().isEmpty()) {
log.warn(ERR_ADD_HOST + "no fixed IP for port {}", osPort.getId());
return;
}
MacAddress mac = MacAddress.valueOf(osPort.getMacAddress());
HostId hostId = HostId.hostId(mac);
/* typically one openstack port should only be bound to one fix IP address;
however, openstack4j binds multiple fixed IPs to one port, this might
be a defect of openstack4j implementation */
// TODO: we need to find a way to bind multiple ports from multiple
// openstack networks into one host sooner or later
Set<IpAddress> fixedIps = osPort.getFixedIps().stream().map(ip -> IpAddress.valueOf(ip.getIpAddress())).collect(Collectors.toSet());
// connect point is the combination of switch ID with port number where
// the host is attached to
ConnectPoint connectPoint = new ConnectPoint(port.element().id(), port.number());
long createTime = System.currentTimeMillis();
// we check whether the host already attached to same locations
Host host = hostService.getHost(hostId);
// build host annotations to include a set of meta info from neutron
DefaultAnnotations.Builder annotations = DefaultAnnotations.builder().set(ANNOTATION_NETWORK_ID, osPort.getNetworkId()).set(ANNOTATION_PORT_ID, osPort.getId()).set(ANNOTATION_CREATE_TIME, String.valueOf(createTime));
// FLAT typed network does not require segment ID
Type netType = osNetworkService.networkType(osNet.getId());
if (netType != FLAT) {
annotations.set(ANNOTATION_SEGMENT_ID, osNet.getProviderSegID());
}
// build host description object
HostDescription hostDesc = new DefaultHostDescription(mac, VlanId.NONE, new HostLocation(connectPoint, createTime), fixedIps, annotations.build());
if (host != null) {
Set<HostLocation> locations = host.locations().stream().filter(l -> l.deviceId().equals(connectPoint.deviceId())).filter(l -> l.port().equals(connectPoint.port())).collect(Collectors.toSet());
// therefore, we simply add this into the location list
if (locations.isEmpty()) {
hostProviderService.addLocationToHost(hostId, new HostLocation(connectPoint, createTime));
}
// the hostDetected method invocation in turn triggers host Update event
if (locations.size() == 1) {
hostProviderService.hostDetected(hostId, hostDesc, false);
}
} else {
hostProviderService.hostDetected(hostId, hostDesc, false);
}
}
use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class OpenstackSwitchingHostProvider method processPortRemoved.
/**
* Processes port removal event.
* Once a port removal event is detected, it tries to look for a host
* instance through host provider by giving connect point information,
* and vanishes it.
*
* @param port ONOS port
*/
private void processPortRemoved(Port port) {
ConnectPoint connectPoint = new ConnectPoint(port.element().id(), port.number());
Set<Host> hosts = hostService.getConnectedHosts(connectPoint);
hosts.forEach(h -> {
Optional<HostLocation> hostLocation = h.locations().stream().filter(l -> l.deviceId().equals(port.element().id())).filter(l -> l.port().equals(port.number())).findAny();
// if the host contains only one filtered location, we remove the host
if (h.locations().size() == 1) {
hostProviderService.hostVanished(h.id());
}
// host location
if (h.locations().size() > 1 && hostLocation.isPresent()) {
hostProviderService.removeLocationFromHost(h.id(), hostLocation.get());
}
});
}
use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class SimpleDeviceStoreTest method testGetPort.
@Test
public final void testGetPort() {
putDevice(DID1, SW1);
putDevice(DID2, SW1);
List<PortDescription> pds = Arrays.asList(DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).build(), DefaultPortDescription.builder().withPortNumber(P2).isEnabled(false).build());
deviceStore.updatePorts(PID, DID1, pds);
Port port1 = deviceStore.getPort(DID1, P1);
assertEquals(P1, port1.number());
assertTrue("Port is enabled", port1.isEnabled());
Port port2 = deviceStore.getPort(DID1, P2);
assertEquals(P2, port2.number());
assertFalse("Port is disabled", port2.isEnabled());
Port port3 = deviceStore.getPort(DID1, P3);
assertNull("P3 not expected", port3);
}
use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class SimpleDeviceStore method removeDevice.
@Override
public DeviceEvent removeDevice(DeviceId deviceId) {
Map<ProviderId, DeviceDescriptions> descs = getOrCreateDeviceDescriptions(deviceId);
synchronized (descs) {
Device device = devices.remove(deviceId);
// should DEVICE_REMOVED carry removed ports?
Map<PortNumber, Port> ports = devicePorts.get(deviceId);
if (ports != null) {
ports.clear();
}
availableDevices.remove(deviceId);
descs.clear();
return device == null ? null : new DeviceEvent(DEVICE_REMOVED, device, null);
}
}
use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class SimpleDeviceStore method updatePortStatus.
@Override
public DeviceEvent updatePortStatus(ProviderId providerId, DeviceId deviceId, PortDescription portDescription) {
Device device = devices.get(deviceId);
checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
synchronized (descsMap) {
DeviceDescriptions descs = descsMap.get(providerId);
// assuming all providers must give DeviceDescription first
checkArgument(descs != null, "Device description for Device ID %s from Provider %s was not found", deviceId, providerId);
ConcurrentMap<PortNumber, Port> ports = getPortMap(deviceId);
final PortNumber number = portDescription.portNumber();
final Port oldPort = ports.get(number);
final Port newPort;
// update description
descs.putPortDesc(portDescription);
newPort = composePort(device, number, descsMap);
if (oldPort == null) {
return createPort(device, newPort, ports);
} else {
return updatePort(device, oldPort, newPort, ports);
}
}
}
Aggregations