use of org.onosproject.net.host.DefaultHostDescription in project onos by opennetworkinglab.
the class K8sSwitchingHostProvider method processPortAdded.
/**
* Processes port addition event.
*
* @param port port object used in ONOS
*/
private void processPortAdded(Port port) {
K8sPort k8sPort = portToK8sPortByName(port);
if (k8sPort == null) {
k8sPort = portToK8sPortByMac(port);
if (k8sPort == null) {
log.warn(ERR_ADD_HOST + "Kubernetes port for {} not found", port);
return;
}
}
K8sNetwork k8sNet = k8sNetworkService.network(k8sPort.networkId());
if (k8sNet == null) {
log.warn(ERR_ADD_HOST + "Kubernetes network {} not found", k8sPort.networkId());
return;
}
MacAddress mac = k8sPort.macAddress();
HostId hostId = HostId.hostId(mac);
// 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();
// update k8s port number by referring to ONOS port number
k8sNetworkService.updatePort(k8sPort.updatePortNumber(port.number()).updateState(K8sPort.State.ACTIVE));
// 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, k8sPort.networkId()).set(ANNOTATION_PORT_ID, k8sPort.portId()).set(ANNOTATION_CREATE_TIME, String.valueOf(createTime)).set(ANNOTATION_SEGMENT_ID, k8sNet.segmentId());
HostDescription hostDesc = new DefaultHostDescription(mac, VlanId.NONE, new HostLocation(connectPoint, createTime), ImmutableSet.of(k8sPort.ipAddress()), 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.host.DefaultHostDescription in project onos by opennetworkinglab.
the class Dhcp4HandlerImpl method handleDhcpAck.
/**
* Send the DHCP ack to the requester host.
* Modify Host or Route store according to the type of DHCP.
*
* @param ethernetPacketAck the packet
* @param dhcpPayload the DHCP data
*/
private void handleDhcpAck(Ethernet ethernetPacketAck, DHCP dhcpPayload) {
Optional<Interface> outInterface = getClientInterface(ethernetPacketAck, dhcpPayload);
if (!outInterface.isPresent()) {
log.warn("Can't find output interface for dhcp: {}", dhcpPayload);
return;
}
Interface outIface = outInterface.get();
ConnectPoint location = outIface.connectPoint();
if (!location.port().hasName()) {
location = translateSwitchPort(location);
}
HostLocation hostLocation = new HostLocation(location, System.currentTimeMillis());
MacAddress macAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress());
VlanId vlanId = getVlanIdFromRelayAgentOption(dhcpPayload);
if (vlanId == null) {
vlanId = outIface.vlan();
}
HostId hostId = HostId.hostId(macAddress, vlanId);
Ip4Address ip = Ip4Address.valueOf(dhcpPayload.getYourIPAddress());
if (directlyConnected(dhcpPayload)) {
// Add to host store if it connect to network directly
Set<IpAddress> ips = Sets.newHashSet(ip);
Host host = hostService.getHost(hostId);
Set<HostLocation> hostLocations = Sets.newHashSet(hostLocation);
if (host != null) {
// Dual homing support:
// if host exists, use old locations and new location
hostLocations.addAll(host.locations());
}
HostDescription desc = new DefaultHostDescription(macAddress, vlanId, hostLocations, ips, false);
// Add IP address when dhcp server give the host new ip address
providerService.hostDetected(hostId, desc, false);
} else {
// Add to route store if it does not connect to network directly
// Get gateway host IP according to host mac address
// TODO: remove relay store here
DhcpRecord record = dhcpRelayStore.getDhcpRecord(hostId).orElse(null);
if (record == null) {
log.warn("Can't find DHCP record of host {}", hostId);
return;
}
MacAddress gwMac = record.nextHop().orElse(null);
if (gwMac == null) {
log.warn("Can't find gateway mac address from record {}", record);
return;
}
HostId gwHostId = HostId.hostId(gwMac, record.vlanId());
Host gwHost = hostService.getHost(gwHostId);
if (gwHost == null) {
log.warn("Can't find gateway host {}", gwHostId);
return;
}
Ip4Address nextHopIp = gwHost.ipAddresses().stream().filter(IpAddress::isIp4).map(IpAddress::getIp4Address).findFirst().orElse(null);
if (nextHopIp == null) {
log.warn("Can't find IP address of gateway {}", gwHost);
return;
}
Route route = new Route(Route.Source.DHCP, ip.toIpPrefix(), nextHopIp);
routeStore.replaceRoute(route);
}
}
use of org.onosproject.net.host.DefaultHostDescription 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.host.DefaultHostDescription in project onos by opennetworkinglab.
the class OpenstackSwitchingHostProviderTest method testProcessPortAddedForUpdate.
/**
* Tests the process port added method for updating case.
*/
@Test
public void testProcessPortAddedForUpdate() {
org.onosproject.net.Port addedPort = new DefaultPort(DEV1, P1, true, ANNOTATIONS);
DeviceEvent addedEvent = new DeviceEvent(DeviceEvent.Type.PORT_ADDED, DEV1, addedPort);
target.portAddedHelper(addedEvent);
// org.onosproject.net.Port updatedPort = new DefaultPort(DEV1, P2, true, ANNOTATIONS);
// DeviceEvent updatedEvent = new DeviceEvent(DeviceEvent.Type.PORT_ADDED, DEV1, updatedPort);
target.portAddedHelper(addedEvent);
HostId hostId = HostId.hostId(HOST_MAC);
HostDescription hostDesc = new DefaultHostDescription(HOST_MAC, VlanId.NONE, new HostLocation(CP11, System.currentTimeMillis()), ImmutableSet.of(HOST_IP11), ANNOTATIONS);
verifyHostResult(hostId, hostDesc);
}
use of org.onosproject.net.host.DefaultHostDescription in project onos by opennetworkinglab.
the class BasicHostOperator method combine.
/**
* Generates a HostDescription containing fields from a HostDescription and
* a HostConfig.
*
* @param cfg the host config entity from network config
* @param descr a HostDescription
* @return HostDescription based on both sources
*/
public static HostDescription combine(BasicHostConfig cfg, HostDescription descr) {
if (cfg == null || descr == null) {
return descr;
}
Set<HostLocation> locations = descr.locations();
Set<HostLocation> cfgLocations = cfg.locations();
if (cfgLocations != null) {
locations = cfgLocations.stream().map(hostLocation -> new HostLocation(hostLocation, System.currentTimeMillis())).collect(Collectors.toSet());
}
Set<HostLocation> auxLocations = descr.auxLocations();
Set<HostLocation> cfgAuxLocations = cfg.auxLocations();
if (cfgAuxLocations != null) {
auxLocations = cfgAuxLocations.stream().map(hostLocation -> new HostLocation(hostLocation, System.currentTimeMillis())).collect(Collectors.toSet());
}
Set<IpAddress> ipAddresses = descr.ipAddress();
Set<IpAddress> cfgIpAddresses = cfg.ipAddresses();
if (cfgIpAddresses != null) {
ipAddresses = cfgIpAddresses;
}
SparseAnnotations sa = combine(cfg, descr.annotations());
return new DefaultHostDescription(descr.hwAddress(), descr.vlan(), locations, auxLocations, ipAddresses, descr.innerVlan(), descr.tpid(), descr.configured(), sa);
}
Aggregations