Search in sources :

Example 61 with PortDescription

use of org.onosproject.net.device.PortDescription in project onos by opennetworkinglab.

the class PortDescriptionsConfig method portDescriptions.

/**
 * Retrieves all port descriptions.
 *
 * @return set of port descriptions
 */
public List<PortDescription> portDescriptions() {
    try {
        ImmutableList.Builder<PortDescription> portDescriptions = ImmutableList.builder();
        for (Iterator<Map.Entry<String, JsonNode>> it = node.fields(); it.hasNext(); ) {
            JsonNode portNode = it.next().getValue();
            long number = portNode.path(NUMBER).asLong();
            String name = portNode.path(NAME).asText(null);
            PortNumber portNumber = createPortNumber(number, name);
            DefaultPortDescription.Builder builder = DefaultPortDescription.builder().withPortNumber(portNumber);
            if (portNode.has(ENABLED)) {
                builder.isEnabled(portNode.path(ENABLED).asBoolean());
            }
            if (portNode.has(REMOVED)) {
                builder.isRemoved(portNode.path(REMOVED).asBoolean());
            }
            if (portNode.has(TYPE)) {
                builder.type(Port.Type.valueOf(portNode.path(TYPE).asText().toUpperCase()));
            }
            if (portNode.has(SPEED)) {
                builder.portSpeed(portNode.path(SPEED).asLong());
            }
            if (portNode.has(ANNOTATIONS)) {
                DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder();
                Iterator<Map.Entry<String, JsonNode>> annotationsIt = portNode.get(ANNOTATIONS).fields();
                while (annotationsIt.hasNext()) {
                    Map.Entry<String, JsonNode> entry = annotationsIt.next();
                    annotationsBuilder.set(entry.getKey(), entry.getValue().asText());
                }
                builder.annotations(annotationsBuilder.build());
            }
            portDescriptions.add(builder.build());
        }
        return portDescriptions.build();
    } catch (IllegalArgumentException e) {
        log.error(CONFIG_VALUE_ERROR, e);
        return ImmutableList.of();
    }
}
Also used : DefaultAnnotations(org.onosproject.net.DefaultAnnotations) ImmutableList(com.google.common.collect.ImmutableList) DefaultPortDescription(org.onosproject.net.device.DefaultPortDescription) PortDescription(org.onosproject.net.device.PortDescription) JsonNode(com.fasterxml.jackson.databind.JsonNode) DefaultPortDescription(org.onosproject.net.device.DefaultPortDescription) PortNumber(org.onosproject.net.PortNumber) Map(java.util.Map)

Example 62 with PortDescription

use of org.onosproject.net.device.PortDescription in project onos by opennetworkinglab.

the class SimpleDeviceStoreTest method testUpdatePorts.

@Test
public final void testUpdatePorts() {
    putDevice(DID1, SW1);
    List<PortDescription> pds = Arrays.asList(DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).build(), DefaultPortDescription.builder().withPortNumber(P2).isEnabled(true).build());
    List<DeviceEvent> events = deviceStore.updatePorts(PID, DID1, pds);
    Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2);
    for (DeviceEvent event : events) {
        assertEquals(PORT_ADDED, event.type());
        assertDevice(DID1, SW1, event.subject());
        assertTrue("PortNumber is one of expected", expectedPorts.remove(event.port().number()));
        assertTrue("Port is enabled", event.port().isEnabled());
    }
    assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty());
    List<PortDescription> pds2 = Arrays.asList(DefaultPortDescription.builder().withPortNumber(P1).isEnabled(false).build(), DefaultPortDescription.builder().withPortNumber(P2).isEnabled(true).build(), DefaultPortDescription.builder().withPortNumber(P3).isEnabled(true).build());
    events = deviceStore.updatePorts(PID, DID1, pds2);
    assertFalse("event should be triggered", events.isEmpty());
    for (DeviceEvent event : events) {
        PortNumber num = event.port().number();
        if (P1.equals(num)) {
            assertEquals(PORT_UPDATED, event.type());
            assertDevice(DID1, SW1, event.subject());
            assertFalse("Port is disabled", event.port().isEnabled());
        } else if (P2.equals(num)) {
            fail("P2 event not expected.");
        } else if (P3.equals(num)) {
            assertEquals(PORT_ADDED, event.type());
            assertDevice(DID1, SW1, event.subject());
            assertTrue("Port is enabled", event.port().isEnabled());
        } else {
            fail("Unknown port number encountered: " + num);
        }
    }
    List<PortDescription> pds3 = Arrays.asList(DefaultPortDescription.builder().withPortNumber(P1).isEnabled(false).build(), DefaultPortDescription.builder().withPortNumber(P2).isEnabled(true).build());
    events = deviceStore.updatePorts(PID, DID1, pds3);
    assertFalse("event should be triggered", events.isEmpty());
    for (DeviceEvent event : events) {
        PortNumber num = event.port().number();
        if (P1.equals(num)) {
            fail("P1 event not expected.");
        } else if (P2.equals(num)) {
            fail("P2 event not expected.");
        } else if (P3.equals(num)) {
            assertEquals(PORT_REMOVED, event.type());
            assertDevice(DID1, SW1, event.subject());
            assertTrue("Port was enabled", event.port().isEnabled());
        } else {
            fail("Unknown port number encountered: " + num);
        }
    }
}
Also used : DeviceEvent(org.onosproject.net.device.DeviceEvent) PortDescription(org.onosproject.net.device.PortDescription) DefaultPortDescription(org.onosproject.net.device.DefaultPortDescription) PortNumber(org.onosproject.net.PortNumber) Test(org.junit.Test)

Example 63 with PortDescription

use of org.onosproject.net.device.PortDescription in project onos by opennetworkinglab.

the class SimpleDeviceStoreTest method testGetPorts.

@Test
public final void testGetPorts() {
    putDevice(DID1, SW1);
    putDevice(DID2, SW1);
    List<PortDescription> pds = Arrays.<PortDescription>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 64 with PortDescription

use of org.onosproject.net.device.PortDescription in project onos by opennetworkinglab.

the class SimpleDeviceStore method composePort.

/**
 * Returns a Port, merging description given from multiple Providers.
 *
 * @param device   device the port is on
 * @param number   port number
 * @param descsMap Collection of Descriptions from multiple providers
 * @return Port instance
 */
private Port composePort(Device device, PortNumber number, Map<ProviderId, DeviceDescriptions> descsMap) {
    ProviderId primary = pickPrimaryPid(descsMap);
    DeviceDescriptions primDescs = descsMap.get(primary);
    // if no primary, assume not enabled
    // TODO: revisit this default port enabled/disabled behavior
    boolean isEnabled = false;
    DefaultAnnotations annotations = DefaultAnnotations.builder().build();
    final PortDescription portDesc = primDescs.getPortDesc(number);
    if (portDesc != null) {
        isEnabled = portDesc.isEnabled();
        annotations = merge(annotations, portDesc.annotations());
    }
    for (Entry<ProviderId, DeviceDescriptions> e : descsMap.entrySet()) {
        if (e.getKey().equals(primary)) {
            continue;
        }
        // TODO: should keep track of Description timestamp
        // and only merge conflicting keys when timestamp is newer
        // Currently assuming there will never be a key conflict between
        // providers
        // annotation merging. not so efficient, should revisit later
        final PortDescription otherPortDesc = e.getValue().getPortDesc(number);
        if (otherPortDesc != null) {
            annotations = merge(annotations, otherPortDesc.annotations());
        }
    }
    return portDesc == null ? new DefaultPort(device, number, false, annotations) : new DefaultPort(device, number, isEnabled, portDesc.type(), portDesc.portSpeed(), annotations);
}
Also used : ProviderId(org.onosproject.net.provider.ProviderId) DefaultAnnotations(org.onosproject.net.DefaultAnnotations) PortDescription(org.onosproject.net.device.PortDescription) DefaultPortDescription(org.onosproject.net.device.DefaultPortDescription) DefaultPort(org.onosproject.net.DefaultPort)

Example 65 with PortDescription

use of org.onosproject.net.device.PortDescription in project onos by opennetworkinglab.

the class SimpleDeviceStore method updatePorts.

@Override
public List<DeviceEvent> updatePorts(ProviderId providerId, DeviceId deviceId, List<PortDescription> portDescriptions) {
    Device device = devices.get(deviceId);
    if (device == null) {
        log.debug("Device {} doesn't exist or hasn't been initialized yet", deviceId);
        return Collections.emptyList();
    }
    Map<ProviderId, DeviceDescriptions> descsMap = deviceDescs.get(deviceId);
    checkArgument(descsMap != null, DEVICE_NOT_FOUND, deviceId);
    List<DeviceEvent> events = new ArrayList<>();
    synchronized (descsMap) {
        DeviceDescriptions descs = descsMap.get(providerId);
        // every provider must provide DeviceDescription.
        checkArgument(descs != null, "Device description for Device ID %s from Provider %s was not found", deviceId, providerId);
        Map<PortNumber, Port> ports = getPortMap(deviceId);
        // Add new ports
        Set<PortNumber> processed = new HashSet<>();
        for (PortDescription portDescription : portDescriptions) {
            final PortNumber number = portDescription.portNumber();
            processed.add(portDescription.portNumber());
            final Port oldPort = ports.get(number);
            final Port newPort;
            // event suppression hook?
            // update description
            descs.putPortDesc(portDescription);
            newPort = composePort(device, number, descsMap);
            events.add(oldPort == null ? createPort(device, newPort, ports) : updatePort(device, oldPort, newPort, ports));
        }
        events.addAll(pruneOldPorts(device, ports, processed));
    }
    return FluentIterable.from(events).filter(notNull()).toList();
}
Also used : ProviderId(org.onosproject.net.provider.ProviderId) DeviceEvent(org.onosproject.net.device.DeviceEvent) DefaultDevice(org.onosproject.net.DefaultDevice) Device(org.onosproject.net.Device) Port(org.onosproject.net.Port) DefaultPort(org.onosproject.net.DefaultPort) ArrayList(java.util.ArrayList) PortDescription(org.onosproject.net.device.PortDescription) DefaultPortDescription(org.onosproject.net.device.DefaultPortDescription) PortNumber(org.onosproject.net.PortNumber) HashSet(java.util.HashSet)

Aggregations

PortDescription (org.onosproject.net.device.PortDescription)81 DefaultPortDescription (org.onosproject.net.device.DefaultPortDescription)41 Test (org.junit.Test)25 DefaultAnnotations (org.onosproject.net.DefaultAnnotations)25 PortNumber (org.onosproject.net.PortNumber)24 DeviceId (org.onosproject.net.DeviceId)23 ArrayList (java.util.ArrayList)22 Port (org.onosproject.net.Port)22 DeviceEvent (org.onosproject.net.device.DeviceEvent)14 ProviderId (org.onosproject.net.provider.ProviderId)13 Device (org.onosproject.net.Device)12 HierarchicalConfiguration (org.apache.commons.configuration.HierarchicalConfiguration)11 DefaultPort (org.onosproject.net.DefaultPort)9 DeviceService (org.onosproject.net.device.DeviceService)9 NetconfSession (org.onosproject.netconf.NetconfSession)8 IOException (java.io.IOException)7 HashMap (java.util.HashMap)7 OduCltPortHelper.oduCltPortDescription (org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription)7 OmsPortHelper.omsPortDescription (org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription)7 OchPortHelper.ochPortDescription (org.onosproject.net.optical.device.OchPortHelper.ochPortDescription)6