Skip to content

Commit

Permalink
Merge branch 'master' of github.com:openbaton/plugin-vimdriver-amazon
Browse files Browse the repository at this point in the history
  • Loading branch information
gc4rella committed May 3, 2018
2 parents b792231 + 1c552fa commit 434aff9
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 66 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ $ java -jar path-to-plugin.jar amazon [rabbitmq-ip] [rabbitmq-port] [n-of-consum
* **rabbitmq-ip** is the ip of the host where the rabbitmq server is installed and running
* **rabbitmq-port** is the port on which the rabbitmq accepts the messages(it is usually 5672 by default)
* **number-of-consumers** specifies the number of actors that will accept the requests
* **user** rabbitmq username (default: admin)
* **password** rabbitmq password (default: openbaton)

## How to use the Amazon EC2 driver

Expand All @@ -39,14 +37,20 @@ $ java -jar path-to-plugin.jar amazon [rabbitmq-ip] [rabbitmq-port] [n-of-consum
* Get your access key and access secret key from your aws account. It will be username and password in your VIM information.
* Locate the region you want to work in and use the name to of the region without the letter in the end, for example us-east-2
* After you have all this data either create the form as a json data or fill out the form in the dashboard to upload your vim information to NFVO.
* Due to the high number of images in AWS system the plugin filters the image by ids and names, these can be provided inside the plugin properties:
* Due to the high number of images in AWS it is not feasible to list all of the in dashboard of the openbaton, you can use any images found on AWS, but only those that in properties would be listed in NFVO.
* All the instances currently get the public ip assigned to them in order to provide the internet functionality. For the instances that have more than 1 interface, elastic ip will be used, remember,
that by default the user has generally only 5 elastic ips available.

```properties
type = amazon
external-properties-file = /etc/openbaton/plugin/amazon/driver.properties
image-key-word = ami-10547475,ami-8a7859ef,ami-f990b69c,ami-43391926
launchTimeout = 128
```
image-key-word property provides an ability to list images that you want, it is recommended to paste the ids of the image which you can get from AWS itself
image-key-word property provides an ability to list images that you want from the openbaton, it is recommended to paste the ids of the image which you can get from AWS itself, so that you can
see the images from the openbaton dashboard, however, you can use any image listed on AWS, the check on whether the image is present will be done during launch attempt by the plugin itself

launchTimeout property define how long the plugin should wait for the instance to get in status "running" before assuming that the instance was not launched for some reason

A step-by-step tutorial on how to make those changes is available [here for the dashboard](docs/how-to-ec2-dashboard.md) and [here for the CLI](docs/how-to-ec2-cli.md)

Expand Down
136 changes: 74 additions & 62 deletions src/main/java/org/openbaton/drivers/amazon/AmazonDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ private String changeHostname(String userdata, String hostname) {
String newData;
Pattern pattern = Pattern.compile(Pattern.quote("echo hostname=$hn"));
Matcher matcher = pattern.matcher(trimmedUserdata);
String start = "";
String end = "";
String start;
String end;
if (matcher.find()) {
start = trimmedUserdata.substring(0, matcher.start());
end = trimmedUserdata.substring(matcher.end());
Expand Down Expand Up @@ -265,9 +265,18 @@ public BaseVimInstance refresh(BaseVimInstance vimInstance) throws VimDriverExce
}
amazon.removeAllFlavours();
amazon.addAllFlavours(newFlavors);
return (BaseVimInstance) amazon;
return amazon;
}

/**
* Waits for instance to go into status "running"
*
* @param name instance name
* @param vimInstance amazon vim instance
* @return server in status "running"
* @throws VimDriverException
* @throws InterruptedException if timeout time is exceeded
*/
private Server waitForInstance(String name, BaseVimInstance vimInstance)
throws VimDriverException, InterruptedException {
int timeOut = Integer.parseInt(properties.getProperty("launchTimeout"));
Expand All @@ -291,6 +300,12 @@ private Server waitForInstance(String name, BaseVimInstance vimInstance)
"Launch Timeout reached, seems that the instance never went into running status");
}

/**
* Associates elastic ips to servers interfaces
*
* @param client amazon client
* @param server server with iterfaces
*/
private void setupInstanceNetwork(AmazonEC2 client, Server server) {
List<Address> freeAddresses = getUnallocatedAddresses(client);
List<NetworkInterface> instanceInterfaces =
Expand All @@ -312,6 +327,12 @@ private void setupInstanceNetwork(AmazonEC2 client, Server server) {
}
}

/**
* Allocated a number of elastic ips
*
* @param client amazon client
* @param number number of addresses needed
*/
private void allocateElasticIps(AmazonEC2 client, int number) {
log.info("Allocating " + number + " elastic ips");
AllocateAddressRequest req = new AllocateAddressRequest();
Expand All @@ -320,6 +341,12 @@ private void allocateElasticIps(AmazonEC2 client, int number) {
}
}

/**
* Returns unallocated elastic ip addresses
*
* @param client amazon
* @return list of elastic ips
*/
private List<Address> getUnallocatedAddresses(AmazonEC2 client) {
DescribeAddressesRequest addReq = new DescribeAddressesRequest();
DescribeAddressesResult re = client.describeAddresses(addReq);
Expand All @@ -343,7 +370,7 @@ private List<NetworkInterface> listInterfaceByAttachment(AmazonEC2 client, Strin
Filter filter = new Filter();
filter.setName("attachment.instance-id");
filter.setValues(Collections.singletonList(instanceId));
Filter filter1 = new Filter();
//Filter filter1 = new Filter();
// filter1.setName("attachment.device-index");
// filter1.setValues(Collections.singletonList("0"));
//
Expand Down Expand Up @@ -393,17 +420,17 @@ private InternetGateway createAndAttachInternetGateway(AmazonEC2 client, String
private String imageExistsOnAWS(String nameId, AmazonEC2 client) throws AmazonClientException {
Filter filter = new Filter();
filter.setName("name");
filter.setValues(Arrays.asList(nameId));
filter.setValues(Collections.singletonList(nameId));
DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest();
describeImagesRequest.setFilters(Arrays.asList(filter));
describeImagesRequest.setFilters(Collections.singletonList(filter));
DescribeImagesResult describeImagesResult = client.describeImages(describeImagesRequest);
if (describeImagesResult.getImages().size() > 0) {
return describeImagesResult.getImages().get(0).getImageId();
}
filter.setName("image-id");
filter.setValues(Arrays.asList(nameId));
filter.setValues(Collections.singletonList(nameId));
describeImagesRequest = new DescribeImagesRequest();
describeImagesRequest.setFilters(Arrays.asList(filter));
describeImagesRequest.setFilters(Collections.singletonList(filter));
describeImagesResult = client.describeImages(describeImagesRequest);
if (describeImagesResult.getImages().size() > 0) {
return describeImagesResult.getImages().get(0).getImageId();
Expand Down Expand Up @@ -450,7 +477,7 @@ private InstanceNetworkInterfaceSpecification createInterface(
* @param net subnet inside AWS represented through NFVO network
* @param deviceIndex device index for the interface is required by AWS
* @param secIDs security group ID
* @return
* @return interface specification
*/
private InstanceNetworkInterfaceSpecification createInterfaceWithPublicIp(
BaseNetwork net, int deviceIndex, Set<String> secIDs) {
Expand All @@ -461,7 +488,6 @@ private InstanceNetworkInterfaceSpecification createInterfaceWithPublicIp(
.withGroups(secIDs)
.withDeviceIndex(deviceIndex)
.withAssociatePublicIpAddress(true);
log.info("Device index" + deviceIndex);
return interSpec;

/*String floatingIp = "";
Expand All @@ -485,17 +511,14 @@ private List<AWSNetwork> searchForRelevantSubnets(
ArrayList<VNFDConnectionPoint> cpList = new ArrayList<>(cps);
Collections.sort(
cpList,
new Comparator<VNFDConnectionPoint>() {
@Override
public int compare(VNFDConnectionPoint t, VNFDConnectionPoint t1) {
if (t.getInterfaceId() > t1.getInterfaceId()) {
return -1;
}
if (t.getInterfaceId() == t1.getInterfaceId()) {
return 0;
} else {
return 1;
}
(t, t1) -> {
if (t.getInterfaceId() > t1.getInterfaceId()) {
return -1;
}
if (t.getInterfaceId().equals(t1.getInterfaceId())) {
return 0;
} else {
return 1;
}
});
List<AWSNetwork> relevantSubnets = new ArrayList<>();
Expand Down Expand Up @@ -526,14 +549,13 @@ private Set<String> getSecurityIdFromName(
AmazonVimInstance vimInstance = (AmazonVimInstance) vimInstanceBase;
String vpcId = getVpcsMap(vimInstance).get(vimInstance.getVpcName());
if (vpcId == null) {
throw new VimDriverException(
"No such VPC " + ((AmazonVimInstance) vimInstance).getVpcName() + " exists");
throw new VimDriverException("No such VPC " + (vimInstance).getVpcName() + " exists");
}
Filter filter = new Filter();
filter.setName("vpc-id");
filter.setValues(Arrays.asList(vpcId));
filter.setValues(Collections.singletonList(vpcId));
DescribeSecurityGroupsRequest req = new DescribeSecurityGroupsRequest();
req.setFilters(Arrays.asList(filter));
req.setFilters(Collections.singletonList(filter));
DescribeSecurityGroupsResult res = client.describeSecurityGroups(req);
Set<String> groupIds = new HashSet<>();
for (String name : groupNames) {
Expand All @@ -547,7 +569,7 @@ private Set<String> getSecurityIdFromName(
groupIds.add(id);
} else {
throw new VimDriverException(
"No group " + name + "exists on VPC " + ((AmazonVimInstance) vimInstance).getVpcName());
"No group " + name + "exists on VPC " + (vimInstance).getVpcName());
}
}

Expand All @@ -568,7 +590,7 @@ public java.util.List<BaseNfvImage> listImages(BaseVimInstance vimInstance)
filter.setName("name");
filter.setValues(Arrays.asList(keyWords));
DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest();
describeImagesRequest.setFilters(Arrays.asList(filter));
describeImagesRequest.setFilters(Collections.singletonList(filter));
DescribeImagesResult describeImagesResult = client.describeImages(describeImagesRequest);
List<BaseNfvImage> images = new ArrayList<>();
for (Image image : describeImagesResult.getImages()) {
Expand All @@ -577,16 +599,15 @@ public java.util.List<BaseNfvImage> listImages(BaseVimInstance vimInstance)
filter.setName("image-id");
filter.setValues(Arrays.asList(keyWords));
describeImagesRequest = new DescribeImagesRequest();
describeImagesRequest.setFilters(Arrays.asList(filter));
describeImagesRequest.setFilters(Collections.singletonList(filter));
describeImagesResult = client.describeImages(describeImagesRequest);
for (Image image : describeImagesResult.getImages()) {
images.add(Utils.getImage(image));
}

return images;
} catch (AmazonClientException e) {
VimDriverException vimDriverException = new VimDriverException(e.getMessage());
throw vimDriverException;
throw new VimDriverException(e.getMessage());
}
}

Expand All @@ -603,12 +624,12 @@ public java.util.List<Server> listServer(BaseVimInstance vimInstanceBase)
}
Filter filter = new Filter();
filter.setName("vpc-id");
filter.setValues(Arrays.asList(vpcId));
filter.setValues(Collections.singletonList(vpcId));
boolean done = false;
List<BaseNetwork> nets = listNetworks(vimInstance);
while (!done) {
DescribeInstancesRequest request = new DescribeInstancesRequest();
request.setFilters(Arrays.asList(filter));
request.setFilters(Collections.singletonList(filter));
DescribeInstancesResult response = client.describeInstances(request);
for (Reservation reservation : response.getReservations()) {
for (Instance instance : reservation.getInstances()) {
Expand All @@ -622,8 +643,7 @@ public java.util.List<Server> listServer(BaseVimInstance vimInstanceBase)
}
return servers;
} catch (AmazonClientException e) {
VimDriverException vimDriverException = new VimDriverException(e.getMessage());
throw vimDriverException;
throw new VimDriverException(e.getMessage());
}
}

Expand All @@ -645,13 +665,13 @@ public List<BaseNetwork> listNetworks(BaseVimInstance vimInstanceBase) throws Vi
}
Filter filter = new Filter();
filter.setName("vpc-id");
filter.setValues(Arrays.asList(vpcId));
filter.setValues(Collections.singletonList(vpcId));
DescribeSubnetsRequest describeSubnetsRequest = new DescribeSubnetsRequest();
describeSubnetsRequest.setFilters(Arrays.asList(filter));
describeSubnetsRequest.setFilters(Collections.singletonList(filter));
DescribeSubnetsResult subnetsResult = client.describeSubnets(describeSubnetsRequest);
List<com.amazonaws.services.ec2.model.Subnet> subnets = subnetsResult.getSubnets();
for (Subnet subnet : subnets) {
nfvoNetworks.add((BaseNetwork) Utils.getNetworkFromSubnet(subnet));
nfvoNetworks.add(Utils.getNetworkFromSubnet(subnet));
}
return nfvoNetworks;
}
Expand Down Expand Up @@ -683,11 +703,8 @@ public Server launchInstanceAndWait(
java.util.Set<Key> keys)
throws VimDriverException {

Server server =
launchInstance(
vimInstance, hostname, image, flavorExtId, keyPair, networks, securityGroups, userData);

return server;
return launchInstance(
vimInstance, hostname, image, flavorExtId, keyPair, networks, securityGroups, userData);
}

@Override
Expand Down Expand Up @@ -720,10 +737,9 @@ public void deleteServerByIdAndWait(BaseVimInstance vimInstance, String id)
try {
AmazonEC2 client = createClient((AmazonVimInstance) vimInstance);
TerminateInstancesRequest req = new TerminateInstancesRequest().withInstanceIds(id);
TerminateInstancesResult res = client.terminateInstances(req);
client.terminateInstances(req);
} catch (AmazonClientException e) {
VimDriverException vimDriverException = new VimDriverException(e.getMessage());
throw vimDriverException;
throw new VimDriverException(e.getMessage());
}
}

Expand Down Expand Up @@ -778,17 +794,16 @@ public BaseNetwork createNetwork(BaseVimInstance vimInstanceBase, BaseNetwork ne

return returnNetwork;
} catch (AmazonClientException e) {
VimDriverException vimDriverException = new VimDriverException(e.getMessage());
throw vimDriverException;
throw new VimDriverException(e.getMessage());
}
}

/**
* Get the table of VPCs name to id by name
*
* @param vimInstance
* @return
* @throws VimDriverException
* @param vimInstance viminstance descriptions
* @return map with vpc ids
* @throws VimDriverException if error is occured
*/
private HashMap<String, String> getVpcsMap(AmazonVimInstance vimInstance)
throws VimDriverException {
Expand All @@ -809,8 +824,7 @@ private HashMap<String, String> getVpcsMap(AmazonVimInstance vimInstance)
}
return vpcNameId;
} catch (AmazonClientException e) {
VimDriverException vimDriverException = new VimDriverException(e.getMessage());
throw vimDriverException;
throw new VimDriverException(e.getMessage());
}
}

Expand All @@ -830,9 +844,9 @@ public BaseNfvImage addImage(
AWSImage newImage;
Filter filter = new Filter();
filter.setName("image-id");
filter.setValues(Arrays.asList(image.getName()));
filter.setValues(Collections.singletonList(image.getName()));
DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest();
describeImagesRequest.setFilters(Arrays.asList(filter));
describeImagesRequest.setFilters(Collections.singletonList(filter));
DescribeImagesResult describeImagesResult = client.describeImages(describeImagesRequest);
if (describeImagesResult.getImages().size() > 1) {
throw new VimDriverException("There are several images with this name");
Expand All @@ -842,9 +856,9 @@ public BaseNfvImage addImage(
return newImage;
}
filter.setName("name");
filter.setValues(Arrays.asList(image.getName()));
filter.setValues(Collections.singletonList(image.getName()));
describeImagesRequest = new DescribeImagesRequest();
describeImagesRequest.setFilters(Arrays.asList(filter));
describeImagesRequest.setFilters(Collections.singletonList(filter));
describeImagesResult = client.describeImages(describeImagesRequest);
if (describeImagesResult.getImages().size() > 1) {
throw new VimDriverException("There are several images with this id");
Expand Down Expand Up @@ -943,11 +957,10 @@ public boolean deleteNetwork(BaseVimInstance vimInstanceBase, String extId)
AmazonVimInstance vimInstance = (AmazonVimInstance) vimInstanceBase;
AmazonEC2 client = createClient(vimInstance);
DeleteSubnetRequest req = new DeleteSubnetRequest().withSubnetId(extId);
DeleteSubnetResult res = client.deleteSubnet(req);
client.deleteSubnet(req);
return true;
} catch (AmazonClientException e) {
VimDriverException vimDriverException = new VimDriverException(e.getMessage());
throw vimDriverException;
throw new VimDriverException(e.getMessage());
}
}

Expand All @@ -959,16 +972,15 @@ public BaseNetwork getNetworkById(BaseVimInstance vimInstanceBase, String id)
try {
Filter filter = new Filter();
filter.setName("subnet-id");
filter.setValues(Arrays.asList(id));
filter.setValues(Collections.singletonList(id));
DescribeSubnetsRequest req = new DescribeSubnetsRequest().withFilters(filter);
DescribeSubnetsResult res = client.describeSubnets(req);
if (res.getSubnets().size() < 1) {
throw new VimDriverException("Network with id " + id + " does not exist");
}
return Utils.getNetworkFromSubnet(res.getSubnets().get(0));
} catch (AmazonClientException e) {
VimDriverException vimDriverException = new VimDriverException(e.getMessage());
throw vimDriverException;
throw new VimDriverException(e.getMessage());
}
}

Expand Down

0 comments on commit 434aff9

Please sign in to comment.