Search in sources :

Example 1 with Inet4ValueException

use of org.openkilda.testing.service.traffexam.networkpool.Inet4ValueException in project open-kilda by telstra.

the class TraffExamServiceImpl method initializePools.

@PostConstruct
void initializePools() {
    baseUrl = labEndpoint + "/api/" + topology.getLabId() + "/traffgen/";
    hostsPool = new ConcurrentHashMap<>();
    for (TraffGen traffGen : topology.getActiveTraffGens()) {
        URI controlEndpoint;
        try {
            controlEndpoint = new URI(traffGen.getControlEndpoint());
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException(String.format("Invalid traffGen(%s) REST endpoint address \"%s\": %s", traffGen.getName(), traffGen.getControlEndpoint(), e.getMessage()), e);
        }
        UUID id = UUID.randomUUID();
        Host host = new Host(id, traffGen.getIfaceName(), controlEndpoint, traffGen.getName());
        try {
            restTemplate.headForHeaders(makeHostUri(host).path("endpoint").build());
        } catch (RestClientException ex) {
            throw new IllegalArgumentException(String.format("The traffGen(%s) REST endpoint address \"%s\" can't be reached: %s", traffGen.getName(), traffGen.getControlEndpoint(), ex.getMessage()), ex);
        }
        hostsPool.put(id, host);
    }
    TraffGenConfig config = topology.getTraffGenConfig();
    Inet4Network network;
    try {
        network = new Inet4Network((Inet4Address) Inet4Address.getByName(config.getAddressPoolBase()), config.getAddressPoolPrefixLen());
    } catch (Inet4ValueException | UnknownHostException e) {
        throw new InputMismatchException(String.format("Invalid traffGen address pool \"%s:%s\": %s", config.getAddressPoolBase(), config.getAddressPoolPrefixLen(), e));
    }
    addressPool = new Inet4NetworkPool(network, 30);
}
Also used : Inet4Address(java.net.Inet4Address) UnknownHostException(java.net.UnknownHostException) Host(org.openkilda.testing.service.traffexam.model.Host) URISyntaxException(java.net.URISyntaxException) InputMismatchException(java.util.InputMismatchException) URI(java.net.URI) TraffGen(org.openkilda.testing.model.topology.TopologyDefinition.TraffGen) Inet4Network(org.openkilda.testing.service.traffexam.networkpool.Inet4Network) Inet4ValueException(org.openkilda.testing.service.traffexam.networkpool.Inet4ValueException) RestClientException(org.springframework.web.client.RestClientException) TraffGenConfig(org.openkilda.testing.model.topology.TopologyDefinition.TraffGenConfig) Inet4NetworkPool(org.openkilda.testing.service.traffexam.networkpool.Inet4NetworkPool) UUID(java.util.UUID) PostConstruct(javax.annotation.PostConstruct)

Example 2 with Inet4ValueException

use of org.openkilda.testing.service.traffexam.networkpool.Inet4ValueException in project open-kilda by telstra.

the class TraffExamServiceImpl method startExam.

@Override
public synchronized ExamResources startExam(Exam exam) throws NoResultsFoundException, OperationalException {
    checkHostPresence(exam.getSource());
    checkHostPresence(exam.getDest());
    Inet4Network subnet;
    try {
        subnet = addressPool.allocate();
    } catch (Inet4ValueException e) {
        throw new OperationalException("Unable to allocate subnet for exam. There is no more addresses available.");
    }
    ExamResources resources = null;
    List<HostResource> supplied = new ArrayList<>(4);
    try {
        Address sourceAddress = new Address(subnet.address(1), subnet.getPrefix(), exam.getSourceVlans());
        sourceAddress = assignAddress(exam.getSource(), sourceAddress);
        supplied.add(sourceAddress);
        Address destAddress = new Address(subnet.address(2), subnet.getPrefix(), exam.getDestVlans());
        destAddress = assignAddress(exam.getDest(), destAddress);
        supplied.add(destAddress);
        ConsumerEndpoint consumer = assignEndpoint(exam.getDest(), new ConsumerEndpoint(destAddress.getId()));
        supplied.add(consumer);
        ProducerEndpoint producer = new ProducerEndpoint(sourceAddress.getId(), new EndpointAddress(destAddress.getAddress(), consumer.getBindPort()));
        if (exam.getBandwidthLimit() != null) {
            producer.setBandwidth(exam.getBandwidthLimit());
            producer.setBurstPkt(exam.getBurstPkt());
        }
        if (exam.getTimeLimitSeconds() != null) {
            producer.setTime(exam.getTimeLimitSeconds());
        }
        producer.setUseUdp(exam.isUdp());
        producer.setBufferLength(exam.getBufferLength());
        try {
            // give consumer some time to fully roll. Probably should be fixed on service's side, this is workaround
            TimeUnit.MILLISECONDS.sleep(300);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        producer = assignEndpoint(exam.getSource(), producer);
        supplied.add(producer);
        resources = new ExamResources(subnet, producer, consumer);
    } catch (Inet4ValueException e) {
        throw new OperationalException("Insufficient resources - not enough IP address in subnet. Check addressPool configuration.");
    } finally {
        if (resources == null) {
            extendFailedToRelease(releaseResources(supplied));
            try {
                addressPool.free(subnet);
            } catch (Inet4ValueException e) {
            // Unreachable point, free throw exception only if invalid (not allocated before) address passed
            }
        }
    }
    return resources;
}
Also used : ExamResources(org.openkilda.testing.service.traffexam.model.ExamResources) Address(org.openkilda.testing.service.traffexam.model.Address) Inet4Address(java.net.Inet4Address) EndpointAddress(org.openkilda.testing.service.traffexam.model.EndpointAddress) ArrayList(java.util.ArrayList) HostResource(org.openkilda.testing.service.traffexam.model.HostResource) Inet4Network(org.openkilda.testing.service.traffexam.networkpool.Inet4Network) Inet4ValueException(org.openkilda.testing.service.traffexam.networkpool.Inet4ValueException) ConsumerEndpoint(org.openkilda.testing.service.traffexam.model.ConsumerEndpoint) ProducerEndpoint(org.openkilda.testing.service.traffexam.model.ProducerEndpoint) EndpointAddress(org.openkilda.testing.service.traffexam.model.EndpointAddress)

Aggregations

Inet4Address (java.net.Inet4Address)2 Inet4Network (org.openkilda.testing.service.traffexam.networkpool.Inet4Network)2 Inet4ValueException (org.openkilda.testing.service.traffexam.networkpool.Inet4ValueException)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 InputMismatchException (java.util.InputMismatchException)1 UUID (java.util.UUID)1 PostConstruct (javax.annotation.PostConstruct)1 TraffGen (org.openkilda.testing.model.topology.TopologyDefinition.TraffGen)1 TraffGenConfig (org.openkilda.testing.model.topology.TopologyDefinition.TraffGenConfig)1 Address (org.openkilda.testing.service.traffexam.model.Address)1 ConsumerEndpoint (org.openkilda.testing.service.traffexam.model.ConsumerEndpoint)1 EndpointAddress (org.openkilda.testing.service.traffexam.model.EndpointAddress)1 ExamResources (org.openkilda.testing.service.traffexam.model.ExamResources)1 Host (org.openkilda.testing.service.traffexam.model.Host)1 HostResource (org.openkilda.testing.service.traffexam.model.HostResource)1 ProducerEndpoint (org.openkilda.testing.service.traffexam.model.ProducerEndpoint)1 Inet4NetworkPool (org.openkilda.testing.service.traffexam.networkpool.Inet4NetworkPool)1