<commit_before>/*
 * Copyright (c) 2015 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openbaton.clients.interfaces.client.openstack;

import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
import org.jclouds.Constants;
import org.jclouds.ContextBuilder;
import org.jclouds.collect.PagedIterable;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.domain.Credentials;
import org.jclouds.io.Payload;
import org.jclouds.io.payloads.ByteArrayPayload;
import org.jclouds.io.payloads.InputStreamPayload;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.openstack.glance.v1_0.GlanceApi;
import org.jclouds.openstack.glance.v1_0.domain.ContainerFormat;
import org.jclouds.openstack.glance.v1_0.domain.DiskFormat;
import org.jclouds.openstack.glance.v1_0.domain.Image;
import org.jclouds.openstack.glance.v1_0.domain.ImageDetails;
import org.jclouds.openstack.glance.v1_0.features.ImageApi;
import org.jclouds.openstack.glance.v1_0.options.CreateImageOptions;
import org.jclouds.openstack.glance.v1_0.options.UpdateImageOptions;
import org.jclouds.openstack.keystone.v2_0.config.CredentialTypes;
import org.jclouds.openstack.keystone.v2_0.config.KeystoneProperties;
import org.jclouds.openstack.keystone.v2_0.domain.Access;
import org.jclouds.openstack.keystone.v2_0.domain.Endpoint;
import org.jclouds.openstack.neutron.v2.NeutronApi;
import org.jclouds.openstack.neutron.v2.domain.*;
import org.jclouds.openstack.neutron.v2.domain.Network.CreateNetwork;
import org.jclouds.openstack.neutron.v2.domain.Network.UpdateNetwork;
import org.jclouds.openstack.neutron.v2.domain.Subnet.CreateSubnet;
import org.jclouds.openstack.neutron.v2.domain.Subnet.UpdateSubnet;
import org.jclouds.openstack.neutron.v2.extensions.RouterApi;
import org.jclouds.openstack.neutron.v2.features.NetworkApi;
import org.jclouds.openstack.neutron.v2.features.PortApi;
import org.jclouds.openstack.neutron.v2.features.SubnetApi;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.openstack.nova.v2_0.domain.Address;
import org.jclouds.openstack.nova.v2_0.domain.FloatingIP;
import org.jclouds.openstack.nova.v2_0.domain.RebootType;
import org.jclouds.openstack.nova.v2_0.features.FlavorApi;
import org.jclouds.openstack.nova.v2_0.features.ServerApi;
import org.jclouds.openstack.nova.v2_0.options.CreateServerOptions;
import org.jclouds.openstack.v2_0.domain.Resource;
import org.jclouds.scriptbuilder.ScriptBuilder;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.openbaton.catalogue.mano.common.DeploymentFlavour;
import org.openbaton.catalogue.nfvo.*;
import org.openbaton.catalogue.nfvo.Network;
import org.openbaton.catalogue.nfvo.Subnet;
import org.openbaton.plugin.PluginStarter;
import org.openbaton.vim.drivers.exceptions.VimDriverException;
import org.openbaton.vim.drivers.interfaces.VimDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.*;
import java.util.concurrent.TimeoutException;
import java.util.regex.Pattern;

import static org.jclouds.scriptbuilder.domain.Statements.exec;

/**
 * Created by mpa on 06.05.15.
 */
public class OpenstackClient extends VimDriver {

    private static final Pattern PATTERN = Pattern.compile(
            "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
    Iterable<Module> modules;
    //private KeystoneApi keystoneApi;
    Properties overrides;
    private Logger log = LoggerFactory.getLogger(this.getClass());

    public OpenstackClient() throws RemoteException {
        super();
        init();
    }

    public static boolean validate(final String ip) {
        return PATTERN.matcher(ip).matches();
    }

    public static void main(String[] args) throws NoSuchMethodException, IOException, InstantiationException, TimeoutException, IllegalAccessException, InvocationTargetException {
        if (args.length == 4)
            PluginStarter.registerPlugin(OpenstackClient.class, args[0], args[1], Integer.parseInt(args[2]), Integer.parseInt(args[3]));
        else
            PluginStarter.registerPlugin(OpenstackClient.class, "openstack", "localhost", 5672, 10);
    }

    public void init() {
        modules = ImmutableSet.<Module>of(new SLF4JLoggingModule());
        overrides = new Properties();
        overrides.setProperty(KeystoneProperties.CREDENTIAL_TYPE, CredentialTypes.PASSWORD_CREDENTIALS);
        overrides.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, "true");
        overrides.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, "true");
        //NovaApi novaApi = ContextBuilder.newBuilder("openstack-nova").endpoint(vimInstance.getAuthUrl()).credentials(vimInstance.getTenant() + ":" + vimInstance.getUsername(), vimInstance.getPassword()).modules(modules).overrides(overrides).buildApi(NovaApi.class);
        //NeutronApi neutronApi = ContextBuilder.newBuilder("openstack-neutron").endpoint(vimInstance.getAuthUrl()).credentials(vimInstance.getTenant() + ":" + vimInstance.getUsername(), vimInstance.getPassword()).modules(modules).overrides(overrides).buildApi(NeutronApi.class);
        //GlanceApi glanceApi = ContextBuilder.newBuilder("openstack-glance").endpoint(vimInstance.getAuthUrl()).credentials(vimInstance.getTenant() + ":" + vimInstance.getUsername(), vimInstance.getPassword()).modules(modules).overrides(overrides).buildApi(GlanceApi.class);
        //this.keystoneApi = ContextBuilder.newBuilder("openstack-keystone").endpoint(vimInstance.getAuthUrl()).credentials(vimInstance.getTenant() + ":" + vimInstance.getUsername(), vimInstance.getPassword()).modules(modules).overrides(overrides).buildApi(KeystoneApi.class);
        //this.tenantId = keystoneApi.getTenantApi().get().getByName(vimInstance.getTenant()).getId();
        //String tenantId = getTenantId(vimInstance);
    }

    private String getZone(VimInstance vimInstance) {
        NovaApi novaApi = ContextBuilder.newBuilder("openstack-nova").endpoint(vimInstance.getAuthUrl()).credentials(vimInstance.getTenant() + ":" + vimInstance.getUsername(), vimInstance.getPassword()).modules(modules).overrides(overrides).buildApi(NovaApi.class);
        Set<String> zones = novaApi.getConfiguredRegions();
        log.debug("Available openstack environment zones: " + zones);
        String zone = null;
        if (vimInstance.getLocation() != null) {
            if (vimInstance.getLocation().getName() != null) {
                log.debug("Finding Location of openstack environment: " + vimInstance.getLocation().getName());
                for (String region : zones) {
                    if (region.equals(vimInstance.getLocation().getName())) {
                        zone = region;
                        log.debug("Found Location of openstack environment: " + zone);
                        break;
                    }
                }
                if (zone == null) {
                    log.warn("Not found Location '" + vimInstance.getLocation().getName() + "'of openstack environment. Selecting a random one...");
                }
            } else {
                log.warn("Location of openstack environment is not defined properly -> Missing Name of the Location. Selecting a random one...");
            }
        } else {
            log.debug("Location of openstack environment is not defined. Selecting a random one...");
        }
        if (zone == null) {
            log.debug("Selecting a random Location of openstack environment from: " + zones);
            zone = zones.iterator().next();
            log.debug("Selected Location of openstack environment: '" + zone + "'");
        }
        return zone;
    }

    @Override
    public Server launchInstance(VimInstance vimInstance, String name, String imageId, String flavorId, String keypair, Set<String> network, Set<String> secGroup, String userData) throws VimDriverException {
        try {
            NovaApi novaApi = ContextBuilder.newBuilder("openstack-nova").endpoint(vimInstance.getAuthUrl()).credentials(vimInstance.getTenant() + ":" + vimInstance.getUsername(), vimInstance.getPassword()).modules(modules).overrides(overrides).buildApi(NovaApi.class);
            ServerApi serverApi = novaApi.getServerApi(getZone(vimInstance));
            String script = new ScriptBuilder().addStatement(exec(userData)).render(OsFamily.UNIX);
            CreateServerOptions options = CreateServerOptions.Builder.keyPairName(keypair).networks(network).securityGroupNames(secGroup).userData(script.getBytes());
            String extId = serverApi.create(name, imageId, flavorId, options).getId();
            Server server = getServerById(vimInstance, extId);
            log.debug("Created Server: " + server);
            return server;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new VimDriverException(e.getMessage());
        }
    }

    public Server launchInstanceAndWait(VimInstance vimInstance, String name, String imageId, String flavorId, String keypair, Set<String> network, Set<String> secGroup, String userData) throws VimDriverException {
        return launchInstanceAndWait(vimInstance, name, imageId, flavorId, keypair, network, secGroup, userData, null);
    }

    @Override
    public Server launchInstanceAndWait(VimInstance vimInstance, String name, String imageId, String flavorId, String keypair, Set<String> network, Set<String> secGroup, String userData, Map<String, String> floatingIp) throws VimDriverException {
        boolean bootCompleted = false;
        log.info("Deploying VM on VimInstance: " + vimInstance.getName());
        Server server = launchInstance(vimInstance, name, imageId, flavorId, keypair, network, secGroup, userData);
        log.info("Deployed VM ( " + server.getName() + " ) with extId: " + server.getExtId() + " in status " + server.getStatus());
        while (bootCompleted == false) {
            log.debug("Waiting for VM with hostname: " + name + " to finish the launch");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            server = getServerById(vimInstance, server.getExtId());
            if (server.getStatus().equals("ACTIVE")) {
                log.debug("Finished deployment of VM with hostname: " + name);
                bootCompleted = true;
            }
            if (server.getStatus().equals("ERROR")) {
                log.error("Failed to launch VM with hostname: " + name + " -> Went into ERROR");
                throw new VimDriverException(server.getExtendedStatus());
            }
        }
        if (floatingIp != null && floatingIp.size() > 0) {
            log.debug("Assigning FloatingIPs to VM with hostname: " + name);
            log.debug("FloatingIPs are: " + floatingIp);
            if (listFreeFloatingIps(vimInstance).size() >= floatingIp.size()) {
                for (Map.Entry<String, String> fip : floatingIp.entrySet()) {
                    associateFloatingIpToNetwork(vimInstance, server, fip);
                    log.info("Assigned FloatingIPs to VM with hostname: " + name + " -> FloatingIPs: " + server.getFloatingIps());
                }
            } else {
                log.error("Cannot assign FloatingIPs to VM with hostname: " + name + ". No FloatingIPs left...");
            }
        }
        return server;
    }

    private String getNetworkIdByName(VimInstance vimInstance, String key) throws VimDriverException {
        for (Network n : this.listNetworks(vimInstance)) {
            if (n.getName().equals(key))
                return n.getExtId();
        }
        return null;
    }

    public void rebootServer(VimInstance vimInstance, String extId, RebootType type) throws VimDriverException {
        try {
            NovaApi novaApi = ContextBuilder.newBuilder("openstack-nova").endpoint(vimInstance.getAuthUrl()).credentials(vimInstance.getTenant() + ":" + vimInstance.getUsername(), vimInstance.getPassword()).modules(modules).overrides(overrides).buildApi(NovaApi.class);
            ServerApi serverApi = novaApi.getServerApi(getZone(vimInstance));
            serverApi.reboot(extId, type);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new VimDriverException(e.getMessage());
        }
    }

    public void deleteServerById(VimInstance vimInstance, String extId) throws VimDriverException {
        try {
            NovaApi novaApi = ContextBuilder.newBuilder("openstack-nova").endpoint(vimInstance.getAuthUrl()).credentials(vimInstance.getTenant() + ":" + vimInstance.getUsername(), vimInstance.getPassword()).modules(modules).overrides(overrides).buildApi(NovaApi.class);
            ServerApi serverApi = novaApi.getServerApi(getZone(vimInstance));
            serverApi.delete(extId);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new VimDriverException(e.getMessage());
        }
    }

    @Override
    public void deleteServerByIdAndWait(VimInstance vimInstance, String extId) throws VimDriverException {
        boolean deleteCompleted = false;
        log.debug("Deleting VM with ExtId: " + extId);
        deleteServerById(vimInstance, extId);
        while (deleteCompleted == false) {
            log.debug("Waiting until VM with ExtId: " + extId + " is deleted...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                getServerById(vimInstance, extId);
            } catch (NullPointerException e) {
                deleteCompleted = true;
                log.info("Deleted VM with ExtId: " + extId);
            }
        }
    }

    @Override
    public List<NFVImage> listImages(VimInstance vimInstance) throws VimDriverException {
        log.debug("Listing images for VimInstance with name: " + vimInstance.getName());
        try {
            GlanceApi glanceApi = ContextBuilder.newBuilder("openstack-glance").endpoint(vimInstance.getAuthUrl()).credentials(vimInstance.getTenant() + ":" + vimInstance.getUsername(), vimInstance.getPassword()).modules(modules).overrides(overrides).buildApi(GlanceApi.class);
            ImageApi imageApi = glanceApi.getImageApi(getZone(vimInstance));
            List<NFVImage> images = new ArrayList<NFVImage>();
            for (ImageDetails jcloudsImage : imageApi.listInDetail().concat()) {
                NFVImage image = new NFVImage();<commit_msg>fixed problem of limited iamge list<commit_after>/*
 * Copyright (c) 2015 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openbaton.clients.interfaces.client.openstack;

import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
import org.jclouds.Constants;
import org.jclouds.ContextBuilder;
import org.jclouds.collect.PagedIterable;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.domain.Credentials;
import org.jclouds.io.Payload;
import org.jclouds.io.payloads.ByteArrayPayload;
import org.jclouds.io.payloads.InputStreamPayload;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.openstack.glance.v1_0.GlanceApi;
import org.jclouds.openstack.glance.v1_0.domain.ContainerFormat;
import org.jclouds.openstack.glance.v1_0.domain.DiskFormat;
import org.jclouds.openstack.glance.v1_0.domain.Image;
import org.jclouds.openstack.glance.v1_0.domain.ImageDetails;
import org.jclouds.openstack.glance.v1_0.features.ImageApi;
import org.jclouds.openstack.glance.v1_0.options.CreateImageOptions;
import org.jclouds.openstack.glance.v1_0.options.ListImageOptions;
import org.jclouds.openstack.glance.v1_0.options.UpdateImageOptions;
import org.jclouds.openstack.keystone.v2_0.config.CredentialTypes;
import org.jclouds.openstack.keystone.v2_0.config.KeystoneProperties;
import org.jclouds.openstack.keystone.v2_0.domain.Access;
import org.jclouds.openstack.keystone.v2_0.domain.Endpoint;
import org.jclouds.openstack.neutron.v2.NeutronApi;
import org.jclouds.openstack.neutron.v2.domain.*;
import org.jclouds.openstack.neutron.v2.domain.Network.CreateNetwork;
import org.jclouds.openstack.neutron.v2.domain.Network.UpdateNetwork;
import org.jclouds.openstack.neutron.v2.domain.Subnet.CreateSubnet;
import org.jclouds.openstack.neutron.v2.domain.Subnet.UpdateSubnet;
import org.jclouds.openstack.neutron.v2.extensions.RouterApi;
import org.jclouds.openstack.neutron.v2.features.NetworkApi;
import org.jclouds.openstack.neutron.v2.features.PortApi;
import org.jclouds.openstack.neutron.v2.features.SubnetApi;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.openstack.nova.v2_0.domain.Address;
import org.jclouds.openstack.nova.v2_0.domain.FloatingIP;
import org.jclouds.openstack.nova.v2_0.domain.RebootType;
import org.jclouds.openstack.nova.v2_0.features.FlavorApi;
import org.jclouds.openstack.nova.v2_0.features.ServerApi;
import org.jclouds.openstack.nova.v2_0.options.CreateServerOptions;
import org.jclouds.openstack.v2_0.domain.Resource;
import org.jclouds.scriptbuilder.ScriptBuilder;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.openbaton.catalogue.mano.common.DeploymentFlavour;
import org.openbaton.catalogue.nfvo.*;
import org.openbaton.catalogue.nfvo.Network;
import org.openbaton.catalogue.nfvo.Subnet;
import org.openbaton.plugin.PluginStarter;
import org.openbaton.vim.drivers.exceptions.VimDriverException;
import org.openbaton.vim.drivers.interfaces.VimDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.*;
import java.util.concurrent.TimeoutException;
import java.util.regex.Pattern;

import static org.jclouds.scriptbuilder.domain.Statements.exec;

/**
 * Created by mpa on 06.05.15.
 */
public class OpenstackClient extends VimDriver {

    private static final Pattern PATTERN = Pattern.compile(
            "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
    Iterable<Module> modules;
    //private KeystoneApi keystoneApi;
    Properties overrides;
    private Logger log = LoggerFactory.getLogger(this.getClass());

    public OpenstackClient() throws RemoteException {
        super();
        init();
    }

    public static boolean validate(final String ip) {
        return PATTERN.matcher(ip).matches();
    }

    public static void main(String[] args) throws NoSuchMethodException, IOException, InstantiationException, TimeoutException, IllegalAccessException, InvocationTargetException {
        if (args.length == 4)
            PluginStarter.registerPlugin(OpenstackClient.class, args[0], args[1], Integer.parseInt(args[2]), Integer.parseInt(args[3]));
        else
            PluginStarter.registerPlugin(OpenstackClient.class, "openstack", "localhost", 5672, 10);
    }

    public void init() {
        modules = ImmutableSet.<Module>of(new SLF4JLoggingModule());
        overrides = new Properties();
        overrides.setProperty(KeystoneProperties.CREDENTIAL_TYPE, CredentialTypes.PASSWORD_CREDENTIALS);
        overrides.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, "true");
        overrides.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, "true");
        //NovaApi novaApi = ContextBuilder.newBuilder("openstack-nova").endpoint(vimInstance.getAuthUrl()).credentials(vimInstance.getTenant() + ":" + vimInstance.getUsername(), vimInstance.getPassword()).modules(modules).overrides(overrides).buildApi(NovaApi.class);
        //NeutronApi neutronApi = ContextBuilder.newBuilder("openstack-neutron").endpoint(vimInstance.getAuthUrl()).credentials(vimInstance.getTenant() + ":" + vimInstance.getUsername(), vimInstance.getPassword()).modules(modules).overrides(overrides).buildApi(NeutronApi.class);
        //GlanceApi glanceApi = ContextBuilder.newBuilder("openstack-glance").endpoint(vimInstance.getAuthUrl()).credentials(vimInstance.getTenant() + ":" + vimInstance.getUsername(), vimInstance.getPassword()).modules(modules).overrides(overrides).buildApi(GlanceApi.class);
        //this.keystoneApi = ContextBuilder.newBuilder("openstack-keystone").endpoint(vimInstance.getAuthUrl()).credentials(vimInstance.getTenant() + ":" + vimInstance.getUsername(), vimInstance.getPassword()).modules(modules).overrides(overrides).buildApi(KeystoneApi.class);
        //this.tenantId = keystoneApi.getTenantApi().get().getByName(vimInstance.getTenant()).getId();
        //String tenantId = getTenantId(vimInstance);
    }

    private String getZone(VimInstance vimInstance) {
        NovaApi novaApi = ContextBuilder.newBuilder("openstack-nova").endpoint(vimInstance.getAuthUrl()).credentials(vimInstance.getTenant() + ":" + vimInstance.getUsername(), vimInstance.getPassword()).modules(modules).overrides(overrides).buildApi(NovaApi.class);
        Set<String> zones = novaApi.getConfiguredRegions();
        log.debug("Available openstack environment zones: " + zones);
        String zone = null;
        if (vimInstance.getLocation() != null) {
            if (vimInstance.getLocation().getName() != null) {
                log.debug("Finding Location of openstack environment: " + vimInstance.getLocation().getName());
                for (String region : zones) {
                    if (region.equals(vimInstance.getLocation().getName())) {
                        zone = region;
                        log.debug("Found Location of openstack environment: " + zone);
                        break;
                    }
                }
                if (zone == null) {
                    log.warn("Not found Location '" + vimInstance.getLocation().getName() + "'of openstack environment. Selecting a random one...");
                }
            } else {
                log.warn("Location of openstack environment is not defined properly -> Missing Name of the Location. Selecting a random one...");
            }
        } else {
            log.debug("Location of openstack environment is not defined. Selecting a random one...");
        }
        if (zone == null) {
            log.debug("Selecting a random Location of openstack environment from: " + zones);
            zone = zones.iterator().next();
            log.debug("Selected Location of openstack environment: '" + zone + "'");
        }
        return zone;
    }

    @Override
    public Server launchInstance(VimInstance vimInstance, String name, String imageId, String flavorId, String keypair, Set<String> network, Set<String> secGroup, String userData) throws VimDriverException {
        try {
            NovaApi novaApi = ContextBuilder.newBuilder("openstack-nova").endpoint(vimInstance.getAuthUrl()).credentials(vimInstance.getTenant() + ":" + vimInstance.getUsername(), vimInstance.getPassword()).modules(modules).overrides(overrides).buildApi(NovaApi.class);
            ServerApi serverApi = novaApi.getServerApi(getZone(vimInstance));
            String script = new ScriptBuilder().addStatement(exec(userData)).render(OsFamily.UNIX);
            CreateServerOptions options = CreateServerOptions.Builder.keyPairName(keypair).networks(network).securityGroupNames(secGroup).userData(script.getBytes());
            String extId = serverApi.create(name, imageId, flavorId, options).getId();
            Server server = getServerById(vimInstance, extId);
            log.debug("Created Server: " + server);
            return server;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new VimDriverException(e.getMessage());
        }
    }

    public Server launchInstanceAndWait(VimInstance vimInstance, String name, String imageId, String flavorId, String keypair, Set<String> network, Set<String> secGroup, String userData) throws VimDriverException {
        return launchInstanceAndWait(vimInstance, name, imageId, flavorId, keypair, network, secGroup, userData, null);
    }

    @Override
    public Server launchInstanceAndWait(VimInstance vimInstance, String name, String imageId, String flavorId, String keypair, Set<String> network, Set<String> secGroup, String userData, Map<String, String> floatingIp) throws VimDriverException {
        boolean bootCompleted = false;
        log.info("Deploying VM on VimInstance: " + vimInstance.getName());
        Server server = launchInstance(vimInstance, name, imageId, flavorId, keypair, network, secGroup, userData);
        log.info("Deployed VM ( " + server.getName() + " ) with extId: " + server.getExtId() + " in status " + server.getStatus());
        while (bootCompleted == false) {
            log.debug("Waiting for VM with hostname: " + name + " to finish the launch");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            server = getServerById(vimInstance, server.getExtId());
            if (server.getStatus().equals("ACTIVE")) {
                log.debug("Finished deployment of VM with hostname: " + name);
                bootCompleted = true;
            }
            if (server.getStatus().equals("ERROR")) {
                log.error("Failed to launch VM with hostname: " + name + " -> Went into ERROR");
                throw new VimDriverException(server.getExtendedStatus());
            }
        }
        if (floatingIp != null && floatingIp.size() > 0) {
            log.debug("Assigning FloatingIPs to VM with hostname: " + name);
            log.debug("FloatingIPs are: " + floatingIp);
            if (listFreeFloatingIps(vimInstance).size() >= floatingIp.size()) {
                for (Map.Entry<String, String> fip : floatingIp.entrySet()) {
                    associateFloatingIpToNetwork(vimInstance, server, fip);
                    log.info("Assigned FloatingIPs to VM with hostname: " + name + " -> FloatingIPs: " + server.getFloatingIps());
                }
            } else {
                log.error("Cannot assign FloatingIPs to VM with hostname: " + name + ". No FloatingIPs left...");
            }
        }
        return server;
    }

    private String getNetworkIdByName(VimInstance vimInstance, String key) throws VimDriverException {
        for (Network n : this.listNetworks(vimInstance)) {
            if (n.getName().equals(key))
                return n.getExtId();
        }
        return null;
    }

    public void rebootServer(VimInstance vimInstance, String extId, RebootType type) throws VimDriverException {
        try {
            NovaApi novaApi = ContextBuilder.newBuilder("openstack-nova").endpoint(vimInstance.getAuthUrl()).credentials(vimInstance.getTenant() + ":" + vimInstance.getUsername(), vimInstance.getPassword()).modules(modules).overrides(overrides).buildApi(NovaApi.class);
            ServerApi serverApi = novaApi.getServerApi(getZone(vimInstance));
            serverApi.reboot(extId, type);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new VimDriverException(e.getMessage());
        }
    }

    public void deleteServerById(VimInstance vimInstance, String extId) throws VimDriverException {
        try {
            NovaApi novaApi = ContextBuilder.newBuilder("openstack-nova").endpoint(vimInstance.getAuthUrl()).credentials(vimInstance.getTenant() + ":" + vimInstance.getUsername(), vimInstance.getPassword()).modules(modules).overrides(overrides).buildApi(NovaApi.class);
            ServerApi serverApi = novaApi.getServerApi(getZone(vimInstance));
            serverApi.delete(extId);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new VimDriverException(e.getMessage());
        }
    }

    @Override
    public void deleteServerByIdAndWait(VimInstance vimInstance, String extId) throws VimDriverException {
        boolean deleteCompleted = false;
        log.debug("Deleting VM with ExtId: " + extId);
        deleteServerById(vimInstance, extId);
        while (deleteCompleted == false) {
            log.debug("Waiting until VM with ExtId: " + extId + " is deleted...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                getServerById(vimInstance, extId);
            } catch (NullPointerException e) {
                deleteCompleted = true;
                log.info("Deleted VM with ExtId: " + extId);
            }
        }
    }

    @Override
    public List<NFVImage> listImages(VimInstance vimInstance) throws VimDriverException {
        log.debug("Listing images for VimInstance with name: " + vimInstance.getName());
        try {
            GlanceApi glanceApi = ContextBuilder.newBuilder("openstack-glance").endpoint(vimInstance.getAuthUrl()).credentials(vimInstance.getTenant() + ":" + vimInstance.getUsername(), vimInstance.getPassword()).modules(modules).overrides(overrides).buildApi(GlanceApi.class);
            ImageApi imageApi = glanceApi.getImageApi(getZone(vimInstance));
            ListImageOptions listImageOptions = new ListImageOptions();
            listImageOptions.limit(1000);
            List<NFVImage> images = new ArrayList<NFVImage>();
            for (ImageDetails jcloudsImage : imageApi.listInDetail(listImageOptions).toList()) {
                NFVImage image = new NFVImage();
{'fraction_non_alphanumeric': 0.11691310902267697, 'fraction_numerical': 0.007120633800558104, 'mean_word_length': 3.4255500354861605, 'pattern_counts': {'":': 18, '<': 33, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5329390', 'n_tokens_mistral': 9181, 'n_tokens_neox': 8985, 'n_words': 1685}