Java Code Examples for org.onosproject.net.DeviceId#toString()

The following examples show how to use org.onosproject.net.DeviceId#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VirtualNetworkWebResourceTest.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Tests adding of a null virtual port using POST via JSON stream.
 */
@Test
public void testPostVirtualPortNullJsonStream() {
    NetworkId networkId = networkId3;
    DeviceId deviceId = devId2;
    replay(mockVnetAdminService);

    WebTarget wt = target();
    try {
        String reqLocation = "vnets/" + networkId.toString()
                + "/devices/" + deviceId.toString() + "/ports";
        wt.path(reqLocation)
                .request(MediaType.APPLICATION_JSON_TYPE)
                .post(Entity.json(null), String.class);
        fail("POST of null virtual port did not throw an exception");
    } catch (BadRequestException ex) {
        assertThat(ex.getMessage(), containsString("HTTP 400 Bad Request"));
    }

    verify(mockVnetAdminService);
}
 
Example 2
Source File: VirtualNetworkWebResourceTest.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Tests adding of new virtual port using POST via JSON stream.
 */
@Test
public void testPostVirtualPort() {
    NetworkId networkId = networkId3;
    DeviceId deviceId = devId22;
    DefaultAnnotations annotations = DefaultAnnotations.builder().build();
    Device physDevice = new DefaultDevice(null, DeviceId.deviceId("dev1"),
                                          null, null, null, null, null, null, annotations);
    ConnectPoint cp1 = new ConnectPoint(physDevice.id(), portNumber(1));
    expect(mockVnetAdminService.createVirtualPort(networkId, deviceId, portNumber(22), cp1))
            .andReturn(vport22);

    replay(mockVnetAdminService);

    WebTarget wt = target();
    InputStream jsonStream = VirtualNetworkWebResourceTest.class
            .getResourceAsStream("post-virtual-port.json");
    String reqLocation = "vnets/" + networkId.toString()
            + "/devices/" + deviceId.toString() + "/ports";
    Response response = wt.path(reqLocation).request(MediaType.APPLICATION_JSON_TYPE)
            .post(Entity.json(jsonStream));
    assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));

    verify(mockVnetAdminService);
}
 
Example 3
Source File: TopologySimulator.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Creates simularted hosts for the specified device.
 *
 * @param deviceId   device identifier
 * @param portOffset port offset where to start attaching hosts
 */
public void createHosts(DeviceId deviceId, int portOffset) {
    String s = deviceId.toString();
    byte dByte = Byte.parseByte(s.substring(s.length() - 2), 16);
    // TODO: this limits the simulation to 256 devices & 256 hosts/device.
    byte[] macBytes = new byte[]{0, 0, 0, 0, dByte, 0};
    byte[] ipBytes = new byte[]{(byte) 192, (byte) 168, dByte, 0};

    for (int i = 0; i < hostCount; i++) {
        int port = portOffset + i + 1;
        macBytes[5] = (byte) (i + 1);
        ipBytes[3] = (byte) (i + 1);
        HostId id = hostId(MacAddress.valueOf(macBytes), VlanId.NONE);
        IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET, ipBytes);
        hostProviderService.hostDetected(id, description(id, ip, deviceId, port), false);
    }
}
 
Example 4
Source File: EncodeInstructionCodecHelper.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Encodes a extension instruction.
 *
 * @param result json node that the instruction attributes are added to
 */
private void encodeExtension(ObjectNode result) {
    final Instructions.ExtensionInstructionWrapper extensionInstruction =
            (Instructions.ExtensionInstructionWrapper) instruction;

    DeviceId deviceId = extensionInstruction.deviceId();

    ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
    DeviceService deviceService = serviceDirectory.get(DeviceService.class);
    Device device = deviceService.getDevice(deviceId);

    if (device == null) {
        throw new IllegalArgumentException("Device not found");
    }

    if (device.is(ExtensionTreatmentCodec.class)) {
        // for extension instructions, encoding device id is needed for the corresponding decoder
        result.put("deviceId", deviceId.toString());
        ExtensionTreatmentCodec treatmentCodec = device.as(ExtensionTreatmentCodec.class);
        ObjectNode node = treatmentCodec.encode(extensionInstruction.extensionInstruction(), context);
        result.set(InstructionCodec.EXTENSION, node);
    } else {
        throw new IllegalArgumentException(
                "There is no codec to encode extension for device " + deviceId.toString());
    }
}
 
Example 5
Source File: VirtualNetworkWebResourceTest.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Tests the result of the REST API GET when there are no virtual ports.
 */
@Test
public void testGetVirtualPortsEmptyArray() {
    NetworkId networkId = networkId4;
    DeviceId deviceId = devId2;
    expect(mockVnetService.getVirtualPorts(networkId, deviceId))
            .andReturn(ImmutableSet.of()).anyTimes();
    replay(mockVnetService);

    WebTarget wt = target();
    String location = "vnets/" + networkId.toString()
            + "/devices/" + deviceId.toString() + "/ports";
    String response = wt.path(location).request().get(String.class);
    assertThat(response, is("{\"ports\":[]}"));

    verify(mockVnetService);
}
 
Example 6
Source File: VirtualNetworkWebResourceTest.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Tests removing a virtual device with DELETE request.
 */
@Test
public void testDeleteVirtualDevice() {
    NetworkId networkId = networkId3;
    DeviceId deviceId = devId2;
    mockVnetAdminService.removeVirtualDevice(networkId, deviceId);
    expectLastCall();
    replay(mockVnetAdminService);

    WebTarget wt = target()
            .property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
    String reqLocation = "vnets/" + networkId.toString() + "/devices/" + deviceId.toString();
    Response response = wt.path(reqLocation)
            .request(MediaType.APPLICATION_JSON_TYPE)
            .delete();

    assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT));

    verify(mockVnetAdminService);
}
 
Example 7
Source File: DecodeInstructionCodecHelper.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Decodes a extension instruction.
 *
 * @return extension treatment
 */
private Instruction decodeExtension() {
    ObjectNode node = (ObjectNode) json.get(InstructionCodec.EXTENSION);
    if (node != null) {
        DeviceId deviceId = getDeviceId();

        ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
        DeviceService deviceService = serviceDirectory.get(DeviceService.class);
        Device device = deviceService.getDevice(deviceId);

        if (device == null) {
            throw new IllegalArgumentException("Device not found");
        }

        if (device.is(ExtensionTreatmentCodec.class)) {
            ExtensionTreatmentCodec treatmentCodec = device.as(ExtensionTreatmentCodec.class);
            ExtensionTreatment treatment = treatmentCodec.decode(node, context);
            return Instructions.extension(treatment, deviceId);
        } else {
            throw new IllegalArgumentException(
                    "There is no codec to decode extension for device " + deviceId.toString());
        }
    }
    return null;
}
 
Example 8
Source File: NetL3VpnTunnelHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Creates tunnel info and tunnel based on source and destination ip
 * address and configures it in the source device.
 *
 * @param sIp   source ip address
 * @param dIp   destination ip address
 * @param sInfo source device info
 */
private void createTunnelInfo(String sIp, String dIp, DeviceInfo sInfo) {
    DeviceId id = sInfo.deviceId();
    Map<DeviceId, Integer> tnlMap = store.getTunnelInfo();
    int count = 0;
    if (tnlMap.containsKey(id)) {
        count = tnlMap.get(id);
    }
    String tnlName = createTunnel(sIp, dIp);
    sInfo.addTnlName(tnlName);
    store.addTunnelInfo(id, count + 1);
    TunnelInfo tnl = new TunnelInfo(dIp, tnlName, vpnName, id.toString());
    configureDevTnl(sInfo, tnl, tnlMap);
}
 
Example 9
Source File: NetL3VpnUtil.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Returns ip address from the device id by parsing.
 *
 * @param devId device id
 * @return ip address
 */
static String getIpFromDevId(DeviceId devId) {
    String devKey = devId.toString();
    int firstInd = devKey.indexOf(COLON);
    int secInd = devKey.indexOf(COLON, firstInd + 1);
    return devKey.substring(firstInd + 1, secInd);
}
 
Example 10
Source File: AlarmId.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Instantiates a new Alarm id.
 *
 * @param id               the device id
 * @param uniqueIdentifier the unique identifier of the Alarm on that device
 */
private AlarmId(DeviceId id, String uniqueIdentifier) {
    super(id.toString() + ":" + uniqueIdentifier);
    checkNotNull(id, "device id must not be null");
    checkNotNull(uniqueIdentifier, "unique identifier must not be null");
    checkArgument(!uniqueIdentifier.isEmpty(), "unique identifier must not be empty");
}
 
Example 11
Source File: VirtualNetworkWebResourceTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the result of the REST API GET when virtual ports are defined.
 */
@Test
public void testGetVirtualPortsArray() {
    NetworkId networkId = networkId3;
    DeviceId deviceId = dev22.id();
    vportSet.add(vport23);
    vportSet.add(vport22);
    expect(mockVnetService.getVirtualPorts(networkId, deviceId)).andReturn(vportSet).anyTimes();
    replay(mockVnetService);

    WebTarget wt = target();
    String location = "vnets/" + networkId.toString()
            + "/devices/" + deviceId.toString() + "/ports";
    String response = wt.path(location).request().get(String.class);
    assertThat(response, containsString("{\"ports\":["));

    final JsonObject result = Json.parse(response).asObject();
    assertThat(result, notNullValue());

    assertThat(result.names(), hasSize(1));
    assertThat(result.names().get(0), is("ports"));

    final JsonArray vnetJsonArray = result.get("ports").asArray();
    assertThat(vnetJsonArray, notNullValue());
    assertEquals("Virtual ports array is not the correct size.",
                 vportSet.size(), vnetJsonArray.size());

    vportSet.forEach(vport -> assertThat(vnetJsonArray, hasVport(vport)));

    verify(mockVnetService);
}
 
Example 12
Source File: DistributedLabelResourceStore.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public boolean createDevicePool(DeviceId deviceId,
                                LabelResourceId beginLabel,
                                LabelResourceId endLabel) {
    LabelResourcePool pool = new LabelResourcePool(deviceId.toString(),
                                                   beginLabel.labelId(),
                                                   endLabel.labelId());
    return this.create(pool);
}
 
Example 13
Source File: NetL3VpnTunnelHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Creates tunnel between source ip address and destination ip address
 * with pce service.
 *
 * @param srcIp source ip address
 * @param desIp destination ip address
 * @return tunnel name
 */
private String createTunnel(String srcIp, String desIp) {
    Iterable<Device> devices = devSvc.getAvailableDevices();
    DeviceId srcDevId = getId(srcIp, false, devices);
    DeviceId desDevId = getId(desIp, false, devices);
    String name = getNewName();
    boolean isCreated = pceSvc.setupPath(srcDevId, desDevId, name,
                                         null, WITH_SIGNALLING);
    if (!isCreated) {
        throw new NetL3VpnException("Tunnel is not created between " +
                                            srcDevId.toString() + " and " +
                                            desDevId.toString());
    }
    return name;
}
 
Example 14
Source File: ControllerTest.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public Driver getDriver(DeviceId deviceId) {
    switch (deviceId.toString()) {
        case NO_SUCH_DRIVER:
            return null;
        case ITEM_NOT_FOUND_DRIVER:
            throw new ItemNotFoundException();
        case DRIVER_EXISTS:
            return new TestDriver();
        default:
            throw new AssertionError();
    }
}
 
Example 15
Source File: NetconfDeviceInfo.java    From onos with Apache License 2.0 5 votes vote down vote up
public static Triple<String, Integer, Optional<String>> extractIpPortPath(DeviceId deviceId) {
    /*
     * We can expect the following formats:
     *
     * netconf:ip:port/path
     * netconf:ip:port
     */
    String string = deviceId.toString();

    /*
     * The first ':' is the separation between the scheme and the IP.
     *
     * The last ':' will represent the separator between the IP and the port.
     */
    int first = string.indexOf(':');
    int last = string.lastIndexOf(':');
    String ip = string.substring(first + 1, last);
    String port = string.substring(last + 1);
    String path = null;
    int pathSep = port.indexOf('/');
    if (pathSep != -1) {
        path = port.substring(pathSep + 1);
        port = port.substring(0, pathSep);
    }

    return Triple.of(ip, new Integer(port),
            (path == null || path.isEmpty() ? Optional.empty() : Optional.of(path)));
}
 
Example 16
Source File: NetL3VpnTunnelHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes tunnel configuration from the device by updating various
 * levels in the store.
 *
 * @param info  device info
 * @param vName VPN name
 */
private void deleteFromDevice(DeviceInfo info, String vName) {
    Map<DeviceId, Integer> map = store.getTunnelInfo();
    DeviceId id = info.deviceId();
    Integer count = map.get(id);
    int tnlCount = info.tnlNames().size();
    int upCount = count - tnlCount;
    ModelIdLevel level;
    TunnelInfo tnlInfo = new TunnelInfo(null, null, vName, id.toString());
    if (upCount == 0) {
        if (map.size() == 1) {
            level = DEVICES;
        } else {
            level = DEVICE;
        }
    } else {
        if (map.size() > 1) {
            level = TNL_POL;
        } else {
            return;
        }
    }
    tnlInfo.level(level);
    ModelObjectData mod = info.processDeleteTnl(driSvc, tnlInfo);
    deleteFromStore(mod);
    info.tnlNames(null);
    info.setTnlPolCreated(false);
    if (upCount == 0) {
        store.removeTunnelInfo(id);
    } else {
        store.addTunnelInfo(id, upCount);
    }
}
 
Example 17
Source File: OvsdbControllerConfig.java    From onos with Apache License 2.0 5 votes vote down vote up
private static boolean dpidMatches(OvsdbBridge bridge, DeviceId deviceId) {
    checkArgument(bridge.datapathId().isPresent());

    String bridgeDpid = "of:" + bridge.datapathId().get();
    String ofDpid = deviceId.toString();
    return bridgeDpid.equals(ofDpid);
}
 
Example 18
Source File: DistributedLabelResourceStore.java    From onos with Apache License 2.0 4 votes vote down vote up
private boolean internalRelease(LabelResourceRequest request) {
    DeviceId deviceId = request.deviceId();
    Collection<LabelResource> release = request.releaseCollection();
    Versioned<LabelResourcePool> poolOld = resourcePool.get(deviceId);
    if (poolOld == null) {
        log.info("the label resource pool of device id {} not allocated");
        return false;
    }
    LabelResourcePool pool = poolOld.value();
    if (pool == null) {
        log.info("the label resource pool of device id {} does not exist");
        return false;
    }
    Set<LabelResource> storeSet = new HashSet<>(pool.releaseLabelId());
    LabelResource labelResource = null;
    long realReleasedNum = 0;
    for (Iterator<LabelResource> it = release.iterator(); it.hasNext();) {
        labelResource = it.next();
        if (labelResource.labelResourceId().labelId() < pool.beginLabel()
                .labelId()
                || labelResource.labelResourceId().labelId() > pool
                        .endLabel().labelId()) {
            continue;
        }
        if (pool.currentUsedMaxLabelId().labelId() > labelResource
                .labelResourceId().labelId()
                || !storeSet.contains(labelResource)) {
            storeSet.add(labelResource);
            realReleasedNum++;
        }
    }
    long beginNum = pool.beginLabel().labelId();
    long endNum = pool.endLabel().labelId();
    long totalNum = pool.totalNum();
    long usedNum = pool.usedNum() - realReleasedNum;
    long current = pool.currentUsedMaxLabelId().labelId();
    ImmutableSet<LabelResource> s = ImmutableSet.copyOf(storeSet);
    LabelResourcePool newPool = new LabelResourcePool(deviceId.toString(),
                                                      beginNum, endNum,
                                                      totalNum, usedNum,
                                                      current, s);
    resourcePool.put(deviceId, newPool);
    log.info("success to release label resource");
    return true;
}
 
Example 19
Source File: AlarmTopovMessageHandler.java    From onos with Apache License 2.0 4 votes vote down vote up
private void addDeviceBadge(Highlights h, DeviceId devId, int n) {
    DeviceHighlight dh = new DeviceHighlight(devId.toString());
    dh.setBadge(createBadge(n));
    h.add(dh);
}
 
Example 20
Source File: TopologyViewMessageHandlerBase.java    From onos with Apache License 2.0 4 votes vote down vote up
private String friendlyDevice(DeviceId deviceId) {
    Device device = services.device().getDevice(deviceId);
    Annotations annot = device.annotations();
    String name = annot.value(AnnotationKeys.NAME);
    return isNullOrEmpty(name) ? deviceId.toString() : name;
}