Search in sources :

Example 56 with Port

use of org.onosproject.net.Port in project onos by opennetworkinglab.

the class DevicePortsListCommand method printPorts.

protected void printPorts(DeviceService service, Device device) {
    List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
    Collections.sort(ports, Comparators.PORT_COMPARATOR);
    for (Port port : ports) {
        if (!isIncluded(port)) {
            continue;
        }
        String portName = port.number().toString();
        Object portIsEnabled = port.isEnabled() ? "enabled" : "disabled";
        String portType = port.type().toString().toLowerCase();
        String annotations = annotations(port.annotations());
        print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations);
    }
}
Also used : Port(org.onosproject.net.Port) ArrayList(java.util.ArrayList)

Example 57 with Port

use of org.onosproject.net.Port in project onos by opennetworkinglab.

the class GossipDeviceStoreTest method testEvents.

// If Delegates should be called only on remote events,
// then Simple* should never call them, thus not test required.
// TODO add test for Port events when we have them
@Ignore("Ignore until Delegate spec. is clear.")
@Test
public final void testEvents() throws InterruptedException {
    final CountDownLatch addLatch = new CountDownLatch(1);
    DeviceStoreDelegate checkAdd = event -> {
        assertEquals(DEVICE_ADDED, event.type());
        assertDevice(DID1, SW1, event.subject());
        addLatch.countDown();
    };
    final CountDownLatch updateLatch = new CountDownLatch(1);
    DeviceStoreDelegate checkUpdate = event -> {
        assertEquals(DEVICE_UPDATED, event.type());
        assertDevice(DID1, SW2, event.subject());
        updateLatch.countDown();
    };
    final CountDownLatch removeLatch = new CountDownLatch(1);
    DeviceStoreDelegate checkRemove = event -> {
        assertEquals(DEVICE_REMOVED, event.type());
        assertDevice(DID1, SW2, event.subject());
        removeLatch.countDown();
    };
    DeviceDescription description = new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, HW, SW1, SN, CID);
    deviceStore.setDelegate(checkAdd);
    deviceStore.createOrUpdateDevice(PID, DID1, description);
    assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS));
    DeviceDescription description2 = new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, HW, SW2, SN, CID);
    deviceStore.unsetDelegate(checkAdd);
    deviceStore.setDelegate(checkUpdate);
    deviceStore.createOrUpdateDevice(PID, DID1, description2);
    assertTrue("Update event fired", updateLatch.await(1, TimeUnit.SECONDS));
    deviceStore.unsetDelegate(checkUpdate);
    deviceStore.setDelegate(checkRemove);
    deviceStore.removeDevice(DID1);
    assertTrue("Remove event fired", removeLatch.await(1, TimeUnit.SECONDS));
}
Also used : DeviceClockServiceAdapter(org.onosproject.net.device.DeviceClockServiceAdapter) Arrays(java.util.Arrays) EasyMock.capture(org.easymock.EasyMock.capture) BiFunction(java.util.function.BiFunction) PortNumber(org.onosproject.net.PortNumber) DeviceStore(org.onosproject.net.device.DeviceStore) EventuallyConsistentMapBuilder(org.onosproject.store.service.EventuallyConsistentMapBuilder) DEVICE_REMOVED(org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) StorageService(org.onosproject.store.service.StorageService) EasyMock.reset(org.easymock.EasyMock.reset) Port(org.onosproject.net.Port) Arrays.asList(java.util.Arrays.asList) After(org.junit.After) Map(java.util.Map) MessageSubject(org.onosproject.store.cluster.messaging.MessageSubject) SWITCH(org.onosproject.net.Device.Type.SWITCH) Assert.fail(org.junit.Assert.fail) DeviceId.deviceId(org.onosproject.net.DeviceId.deviceId) DeviceStoreDelegate(org.onosproject.net.device.DeviceStoreDelegate) DeviceDescription(org.onosproject.net.device.DeviceDescription) NodeId(org.onosproject.cluster.NodeId) AfterClass(org.junit.AfterClass) Device(org.onosproject.net.Device) Set(java.util.Set) PORT_REMOVED(org.onosproject.net.device.DeviceEvent.Type.PORT_REMOVED) Sets(com.google.common.collect.Sets) CountDownLatch(java.util.concurrent.CountDownLatch) DeviceClockService(org.onosproject.net.device.DeviceClockService) List(java.util.List) Annotations(org.onosproject.net.Annotations) SparseAnnotations(org.onosproject.net.SparseAnnotations) Assert.assertFalse(org.junit.Assert.assertFalse) EasyMock.createNiceMock(org.easymock.EasyMock.createNiceMock) DeviceEvent(org.onosproject.net.device.DeviceEvent) ClusterCommunicationService(org.onosproject.store.cluster.messaging.ClusterCommunicationService) ClusterService(org.onosproject.cluster.ClusterService) DeviceId(org.onosproject.net.DeviceId) DEVICE_AVAILABILITY_CHANGED(org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED) ChassisId(org.onlab.packet.ChassisId) Iterables(com.google.common.collect.Iterables) BeforeClass(org.junit.BeforeClass) DEVICE_ADDED(org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) KryoNamespace(org.onlab.util.KryoNamespace) Function(java.util.function.Function) ControllerNode(org.onosproject.cluster.ControllerNode) PortDescription(org.onosproject.net.device.PortDescription) Timestamp(org.onosproject.store.Timestamp) ACTIVE(org.onosproject.cluster.ControllerNode.State.ACTIVE) EasyMock.replay(org.easymock.EasyMock.replay) EventuallyConsistentMap(org.onosproject.store.service.EventuallyConsistentMap) DefaultDeviceDescription(org.onosproject.net.device.DefaultDeviceDescription) Before(org.junit.Before) IpAddress(org.onlab.packet.IpAddress) EasyMock.anyObject(org.easymock.EasyMock.anyObject) Capture(org.easymock.Capture) StaticClusterService(org.onosproject.store.cluster.StaticClusterService) MastershipRole(org.onosproject.net.MastershipRole) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ProviderId(org.onosproject.net.provider.ProviderId) MastershipBasedTimestamp(org.onosproject.store.impl.MastershipBasedTimestamp) IOException(java.io.IOException) EasyMock.expect(org.easymock.EasyMock.expect) DefaultAnnotations.union(org.onosproject.net.DefaultAnnotations.union) ClusterMessage(org.onosproject.store.cluster.messaging.ClusterMessage) PORT_ADDED(org.onosproject.net.device.DeviceEvent.Type.PORT_ADDED) TimeUnit(java.util.concurrent.TimeUnit) EasyMock.expectLastCall(org.easymock.EasyMock.expectLastCall) AtomicLong(java.util.concurrent.atomic.AtomicLong) DefaultAnnotations(org.onosproject.net.DefaultAnnotations) DefaultPortDescription(org.onosproject.net.device.DefaultPortDescription) Assert.assertNull(org.junit.Assert.assertNull) Ignore(org.junit.Ignore) MastershipServiceAdapter(org.onosproject.mastership.MastershipServiceAdapter) EasyMock.verify(org.easymock.EasyMock.verify) PORT_UPDATED(org.onosproject.net.device.DeviceEvent.Type.PORT_UPDATED) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) DEVICE_UPDATED(org.onosproject.net.device.DeviceEvent.Type.DEVICE_UPDATED) DeviceDescription(org.onosproject.net.device.DeviceDescription) DefaultDeviceDescription(org.onosproject.net.device.DefaultDeviceDescription) DeviceStoreDelegate(org.onosproject.net.device.DeviceStoreDelegate) DefaultDeviceDescription(org.onosproject.net.device.DefaultDeviceDescription) CountDownLatch(java.util.concurrent.CountDownLatch) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 58 with Port

use of org.onosproject.net.Port in project onos by opennetworkinglab.

the class GossipDeviceStoreTest method testGetPorts.

@Test
public final void testGetPorts() {
    putDevice(DID1, SW1);
    putDevice(DID2, SW1);
    List<PortDescription> pds = Arrays.asList(DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).build(), DefaultPortDescription.builder().withPortNumber(P2).isEnabled(true).build());
    deviceStore.updatePorts(PID, DID1, pds);
    Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2);
    List<Port> ports = deviceStore.getPorts(DID1);
    for (Port port : ports) {
        assertTrue("Port is enabled", port.isEnabled());
        assertTrue("PortNumber is one of expected", expectedPorts.remove(port.number()));
    }
    assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty());
    assertTrue("DID2 has no ports", deviceStore.getPorts(DID2).isEmpty());
}
Also used : Port(org.onosproject.net.Port) PortDescription(org.onosproject.net.device.PortDescription) DefaultPortDescription(org.onosproject.net.device.DefaultPortDescription) PortNumber(org.onosproject.net.PortNumber) Test(org.junit.Test)

Example 59 with Port

use of org.onosproject.net.Port in project onos by opennetworkinglab.

the class FlowRuleJuniperImpl method findIpDst.

/**
 * Helper method to find the next hop IP address.
 * The logic is to check if the destination ports have an IP address
 * by checking the logical interface (e.g., for port physical ge-2/0/1,
 * a logical interface may be ge-2/0/1.0
 *
 * @param deviceId the device id of the flow rule to be installed
 * @param port     output port of the flow rule treatment
 * @return optional IPv4 address of a next hop
 */
private Optional<Ip4Address> findIpDst(DeviceId deviceId, Port port) {
    LinkService linkService = this.handler().get(LinkService.class);
    Set<Link> links = linkService.getEgressLinks(new ConnectPoint(deviceId, port.number()));
    DeviceService deviceService = this.handler().get(DeviceService.class);
    // Using only links with adjacency discovered by the LLDP protocol (see LinkDiscoveryJuniperImpl)
    Map<DeviceId, Port> dstPorts = links.stream().filter(l -> JuniperUtils.AK_IP.toUpperCase().equals(l.annotations().value(AnnotationKeys.LAYER))).collect(Collectors.toMap(l -> l.dst().deviceId(), l -> deviceService.getPort(l.dst().deviceId(), l.dst().port())));
    for (Map.Entry<DeviceId, Port> entry : dstPorts.entrySet()) {
        String portName = entry.getValue().annotations().value(AnnotationKeys.PORT_NAME);
        Optional<Port> childPort = deviceService.getPorts(entry.getKey()).stream().filter(p -> Strings.nullToEmpty(p.annotations().value(AnnotationKeys.PORT_NAME)).contains(portName.trim())).filter(this::isIp).findAny();
        if (childPort.isPresent()) {
            return Optional.ofNullable(Ip4Address.valueOf(childPort.get().annotations().value(JuniperUtils.AK_IP)));
        }
    }
    return Optional.empty();
}
Also used : NetconfException(org.onosproject.netconf.NetconfException) StringUtils(org.apache.commons.lang.StringUtils) JuniperUtils.commitBuilder(org.onosproject.drivers.juniper.JuniperUtils.commitBuilder) FlowRuleProgrammable(org.onosproject.net.flow.FlowRuleProgrammable) PortNumber(org.onosproject.net.PortNumber) DeviceService(org.onosproject.net.device.DeviceService) FlowEntry(org.onosproject.net.flow.FlowEntry) AnnotationKeys(org.onosproject.net.AnnotationKeys) JuniperUtils.routeAddBuilder(org.onosproject.drivers.juniper.JuniperUtils.routeAddBuilder) Link(org.onosproject.net.Link) NetconfSession(org.onosproject.netconf.NetconfSession) ConnectPoint(org.onosproject.net.ConnectPoint) HashSet(java.util.HashSet) XmlConfigParser.loadXmlString(org.onosproject.drivers.utilities.XmlConfigParser.loadXmlString) Strings(com.google.common.base.Strings) FlowRuleService(org.onosproject.net.flow.FlowRuleService) JuniperUtils.routeDeleteBuilder(org.onosproject.drivers.juniper.JuniperUtils.routeDeleteBuilder) Port(org.onosproject.net.Port) Map(java.util.Map) IPCriterion(org.onosproject.net.flow.criteria.IPCriterion) Criterion(org.onosproject.net.flow.criteria.Criterion) OutputInstruction(org.onosproject.net.flow.instructions.Instructions.OutputInstruction) Ip4Address(org.onlab.packet.Ip4Address) Instruction(org.onosproject.net.flow.instructions.Instruction) Collection(java.util.Collection) PENDING_REMOVE(org.onosproject.net.flow.FlowEntry.FlowEntryState.PENDING_REMOVE) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) Collectors(java.util.stream.Collectors) ADD(org.onosproject.drivers.juniper.JuniperUtils.OperationType.ADD) REMOVED(org.onosproject.net.flow.FlowEntry.FlowEntryState.REMOVED) Beta(com.google.common.annotations.Beta) DatastoreId(org.onosproject.netconf.DatastoreId) OperationType(org.onosproject.drivers.juniper.JuniperUtils.OperationType) FlowRule(org.onosproject.net.flow.FlowRule) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) LinkService(org.onosproject.net.link.LinkService) Optional(java.util.Optional) REMOVE(org.onosproject.drivers.juniper.JuniperUtils.OperationType.REMOVE) DeviceId(org.onosproject.net.DeviceId) Collections(java.util.Collections) JuniperUtils.rollbackBuilder(org.onosproject.drivers.juniper.JuniperUtils.rollbackBuilder) DeviceId(org.onosproject.net.DeviceId) Port(org.onosproject.net.Port) DeviceService(org.onosproject.net.device.DeviceService) XmlConfigParser.loadXmlString(org.onosproject.drivers.utilities.XmlConfigParser.loadXmlString) ConnectPoint(org.onosproject.net.ConnectPoint) LinkService(org.onosproject.net.link.LinkService) Map(java.util.Map) Link(org.onosproject.net.Link)

Example 60 with Port

use of org.onosproject.net.Port in project onos by opennetworkinglab.

the class LinkDiscoveryJuniperImpl method getLinks.

@Override
public Set<LinkDescription> getLinks() {
    DeviceId localDeviceId = this.handler().data().deviceId();
    NetconfSession session = lookupNetconfSession(localDeviceId);
    String reply;
    try {
        reply = session.get(requestBuilder(REQ_LLDP_NBR_INFO));
    } catch (NetconfException e) {
        log.warn("Failed to retrieve lldp-neighbors-information for device {}", localDeviceId);
        return ImmutableSet.of();
    }
    log.debug("Reply from device {} : {}", localDeviceId, reply);
    Set<LinkAbstraction> linkAbstractions = parseJuniperLldp(loadXmlString(reply));
    log.debug("Set of LinkAbstraction discovered {}", linkAbstractions);
    DeviceService deviceService = this.handler().get(DeviceService.class);
    Set<LinkDescription> descriptions = new HashSet<>();
    // for each lldp neighbor create two LinkDescription
    for (LinkAbstraction linkAbs : linkAbstractions) {
        // find source port by local port name
        Optional<Port> localPort = deviceService.getPorts(localDeviceId).stream().filter(port -> linkAbs.localPortName.equals(port.annotations().value(PORT_NAME))).findAny();
        if (!localPort.isPresent()) {
            log.warn("Port name {} does not exist in device {}", linkAbs.localPortName, localDeviceId);
            continue;
        }
        // find destination device by remote chassis id
        com.google.common.base.Optional<Device> dev = Iterables.tryFind(deviceService.getAvailableDevices(), input -> input.chassisId().equals(linkAbs.remoteChassisId));
        if (!dev.isPresent()) {
            log.warn("Device with chassis ID {} does not exist. Referenced by {}/{}", linkAbs.remoteChassisId, localDeviceId, linkAbs);
            continue;
        }
        Device remoteDevice = dev.get();
        // find destination port by interface index
        Optional<Port> remotePort = deviceService.getPorts(remoteDevice.id()).stream().filter(port -> {
            if (port.number().toLong() == linkAbs.remotePortIndex) {
                return true;
            }
            if (port.annotations().value(AnnotationKeys.PORT_MAC) != null && linkAbs.remotePortId != null && port.annotations().value(AnnotationKeys.PORT_MAC).equals(linkAbs.remotePortId)) {
                return true;
            }
            if (port.annotations().value(AnnotationKeys.PORT_NAME) != null && linkAbs.remotePortId != null && port.annotations().value(AnnotationKeys.PORT_NAME).equals(linkAbs.remotePortId)) {
                return true;
            }
            if (port.annotations().value(AnnotationKeys.PORT_NAME) != null && linkAbs.remotePortDescription != null && port.annotations().value(AnnotationKeys.PORT_NAME).equals(linkAbs.remotePortDescription)) {
                return true;
            }
            return false;
        }).findAny();
        if (!remotePort.isPresent()) {
            log.warn("Port does not exist in remote device {}. Referenced by {}/{}", remoteDevice.id(), localDeviceId, linkAbs);
            continue;
        }
        if (!localPort.get().isEnabled() || !remotePort.get().isEnabled()) {
            log.debug("Ports are disabled. Cannot create a link between {}/{} and {}/{}", localDeviceId, localPort.get(), remoteDevice.id(), remotePort.get());
            continue;
        }
        JuniperUtils.createOneWayLinkDescription(localDeviceId, localPort.get(), remoteDevice.id(), remotePort.get(), descriptions);
    }
    return descriptions;
}
Also used : NetconfSession(org.onosproject.netconf.NetconfSession) LinkDescription(org.onosproject.net.link.LinkDescription) NetconfException(org.onosproject.netconf.NetconfException) Iterables(com.google.common.collect.Iterables) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) Device(org.onosproject.net.Device) DeviceService(org.onosproject.net.device.DeviceService) PORT_NAME(org.onosproject.net.AnnotationKeys.PORT_NAME) Set(java.util.Set) JuniperUtils.parseJuniperLldp(org.onosproject.drivers.juniper.JuniperUtils.parseJuniperLldp) AnnotationKeys(org.onosproject.net.AnnotationKeys) LinkDiscovery(org.onosproject.net.behaviour.LinkDiscovery) NetconfSession(org.onosproject.netconf.NetconfSession) Beta(com.google.common.annotations.Beta) REQ_LLDP_NBR_INFO(org.onosproject.drivers.juniper.JuniperUtils.REQ_LLDP_NBR_INFO) HashSet(java.util.HashSet) XmlConfigParser.loadXmlString(org.onosproject.drivers.utilities.XmlConfigParser.loadXmlString) Port(org.onosproject.net.Port) LinkAbstraction(org.onosproject.drivers.juniper.JuniperUtils.LinkAbstraction) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Optional(java.util.Optional) DeviceId(org.onosproject.net.DeviceId) JuniperUtils.requestBuilder(org.onosproject.drivers.juniper.JuniperUtils.requestBuilder) LinkDescription(org.onosproject.net.link.LinkDescription) DeviceId(org.onosproject.net.DeviceId) Device(org.onosproject.net.Device) Port(org.onosproject.net.Port) DeviceService(org.onosproject.net.device.DeviceService) XmlConfigParser.loadXmlString(org.onosproject.drivers.utilities.XmlConfigParser.loadXmlString) NetconfException(org.onosproject.netconf.NetconfException) LinkAbstraction(org.onosproject.drivers.juniper.JuniperUtils.LinkAbstraction) HashSet(java.util.HashSet)

Aggregations

Port (org.onosproject.net.Port)200 DeviceService (org.onosproject.net.device.DeviceService)92 PortNumber (org.onosproject.net.PortNumber)85 ConnectPoint (org.onosproject.net.ConnectPoint)78 DeviceId (org.onosproject.net.DeviceId)76 Device (org.onosproject.net.Device)63 List (java.util.List)51 Set (java.util.Set)47 Optional (java.util.Optional)43 DefaultPort (org.onosproject.net.DefaultPort)38 Logger (org.slf4j.Logger)38 ArrayList (java.util.ArrayList)36 Collectors (java.util.stream.Collectors)35 Collections (java.util.Collections)34 Collection (java.util.Collection)33 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)33 Test (org.junit.Test)31 Ethernet (org.onlab.packet.Ethernet)31 Map (java.util.Map)29 Sets (com.google.common.collect.Sets)28