Commit bb6dd32e authored by zh9314's avatar zh9314

Add a planner2provisioner converter.

This is used for coverting the current output of planner to be the input for previous version of provisioner.
parent 00a13b34
{
"creationDate": 1488459287833,
"parameters": [
{
"url": null,
"attributes": null,
"value": [
{
"name": "2d13d708e3a9441ab8336ce874e08dd1",
"size": "Small",
"docker": "mogswitch/InputDistributor"
},
{
"name": "8fcc1788d9ee462c826572c79fdb2a6a",
"size": "Small",
"docker": "mogswitch/InputDistributor"
},
{
"name": "5e0add703c8a43938a39301f572e46c0",
"size": "Small",
"docker": "mogswitch/InputDistributor"
}
],
"encoding": "UTF-8"
}
]
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nl.uva.sne.drip</groupId>
<artifactId>drip-planner2provisioner</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--mvn clean compile assembly:single-->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass></mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import plannerOut.Parameter;
import plannerOut.PlannerOutput;
import plannerOut.Value;
import provisionerIn.Eth;
import provisionerIn.SubTopology;
import provisionerIn.SubTopologyInfo;
import provisionerIn.Subnet;
import provisionerIn.TopTopology;
import provisionerIn.VM;
public class P2PConverter {
public static SimplePlanContainer transfer(String plannerOutputJson, String userName, String OStype, String clusterType) throws JsonParseException, JsonMappingException, IOException{
Parameter plannerOutput = getInfoFromPlanner(plannerOutputJson);
TopTopology topTopology = new TopTopology();
SubTopology subTopology = new SubTopology();
SubTopologyInfo sti = new SubTopologyInfo();
sti.cloudProvider = "EC2";
sti.topology = UUID.randomUUID().toString();
subTopology.publicKeyPath = null;
subTopology.userName = userName;
Subnet s = new Subnet();
s.name = "s1";
s.subnet = "192.168.10.0";
s.netmask = "255.255.255.0";
subTopology.subnets = new ArrayList<Subnet>();
subTopology.subnets.add(s);
subTopology.components = new ArrayList<VM>();
boolean firstVM = true;
for(int vi = 0 ; vi < plannerOutput.value.size() ; vi++){
Value curValue = plannerOutput.value.get(vi);
VM curVM = new VM();
curVM.name = curValue.name;
curVM.type = "Switch.nodes.Compute";
curVM.OStype = OStype;
curVM.domain = "ec2.us-east-1.amazonaws.com";
curVM.clusterType = clusterType;
curVM.dockers = curValue.docker;
curVM.public_address = curValue.name;
if(curValue.size.trim().toLowerCase().equals("small"))
curVM.nodeType = "t2.small";
else if(curValue.size.trim().toLowerCase().equals("medium"))
curVM.nodeType = "t2.medium";
else if(curValue.size.trim().toLowerCase().equals("large"))
curVM.nodeType = "t2.large";
else{
throw new IllegalArgumentException("Invalid value for field 'size' in input JSON String");
}
Eth eth = new Eth();
eth.name = "p1";
eth.subnet_name = "s1";
int hostNum = 10+vi;
String priAddress = "192.168.10."+hostNum;
eth.address = priAddress;
curVM.ethernet_port = new ArrayList<Eth>();
curVM.ethernet_port.add(eth);
if(firstVM){
curVM.role = "master";
firstVM = false;
}else
curVM.role = "slave";
subTopology.components.add(curVM);
}
sti.subTopology = subTopology;
topTopology.topologies = new ArrayList<SubTopologyInfo>();
topTopology.topologies.add(sti);
SimplePlanContainer spc = generateInfo(topTopology);
return spc;
}
private static Parameter getInfoFromPlanner(String json) throws JsonParseException, JsonMappingException, IOException{
ObjectMapper mapper = new ObjectMapper();
PlannerOutput po = mapper.readValue(json, PlannerOutput.class);
System.out.println("");
return po.parameters.get(0);
}
private static SimplePlanContainer generateInfo(TopTopology topTopology) throws JsonProcessingException{
SimplePlanContainer spc = new SimplePlanContainer();
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
String yamlString = mapper.writeValueAsString(topTopology);
spc.topLevelContents = yamlString.substring(4);
Map<String, String> output = new HashMap<String, String>();
for(int i = 0 ; i<topTopology.topologies.size() ; i++){
String key = topTopology.topologies.get(i).topology;
String value = mapper.writeValueAsString(topTopology.topologies.get(i).subTopology);
output.put(key, value.substring(4));
}
spc.lowerLevelContents = output;
return spc;
}
}
import java.util.Map;
public class SimplePlanContainer {
public String topLevelContents;
public Map<String, String> lowerLevelContents;
}
package plannerOut;
import java.util.ArrayList;
public class Parameter {
public String url;
public String attributes;
public ArrayList<Value> value;
public String encoding;
}
package plannerOut;
import java.util.ArrayList;
public class PlannerOutput {
public String creationDate;
public ArrayList<Parameter> parameters;
}
package plannerOut;
public class Value {
public String name;
public String size;
public String docker;
}
package provisionerIn;
public class Eth {
public String name;
public String subnet_name;
public String address;
}
package provisionerIn;
import java.util.ArrayList;
public class SubTopology {
public String publicKeyPath;
public String userName;
//Indicate a subnet that several can be put in.
public ArrayList<Subnet> subnets;
public ArrayList<VM> components;
}
package provisionerIn;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class SubTopologyInfo {
//The name of the topology. It is also the file name of the low level description.
public String topology;
//Currently, only support "EC2" and "ExoGENI"/"GENI". This is not case sensitive.
public String cloudProvider;
//Point to the detailed description of the sub-topology.
@JsonIgnore
public SubTopology subTopology;
}
package provisionerIn;
public class Subnet {
//The name of the subnet. Two subnets cannot have same name in one subnet.
public String name;
public String subnet;
public String netmask;
}
package provisionerIn;
import java.util.ArrayList;
public class TopTopology {
public ArrayList<SubTopologyInfo> topologies;
}
package provisionerIn;
import java.util.ArrayList;
public class VM {
public String name;
public String type;
public String nodeType;
public String OStype;
//Currently, the SIDE subsystem uses this field for GUI.
public String script;
public String domain;
public String installation;
public String clusterType;
//The role of this node in docker cluster.
//The possible value can only be "null", "slave" and "master".
//This is not case sensitive.
public String role;
//The name of the docker in repository, which can be "null".
public String dockers;
//Do not need to be the same with the node name any more.
//The initial value should be "null", which means the public is not determined.
//When the status of the sub-topology is stopped, deleted or failed, this field should also be "null".
public String public_address;
public ArrayList<Eth> ethernet_port;
}
import java.io.File;
import java.io.IOException;
import java.util.Map;
import org.apache.commons.io.FileUtils;
public class testConverter {
public static void main(String[] args) {
File jsonFile = new File("input.json");
String json = "";
try {
json = FileUtils.readFileToString(jsonFile, "UTF-8");
} catch (IOException e1) {
e1.printStackTrace();
}
try {
SimplePlanContainer spc = P2PConverter.transfer(json, "zh9314", "Ubuntu 16.04", "kubernetes");
System.out.println("--topLevel:\n"+spc.topLevelContents);
System.out.println("--lowLevel:");
for (Map.Entry<String, String> entry : spc.lowerLevelContents.entrySet()){
System.out.println(entry.getKey()+":\n"+entry.getValue());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
...@@ -2,16 +2,57 @@ ...@@ -2,16 +2,57 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>nl.uva.sne.drip</groupId> <groupId>nl.uva.sne.drip</groupId>
<artifactId>drip</artifactId> <artifactId>drip-planner2provisioner</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<packaging>pom</packaging> <packaging>jar</packaging>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties> </properties>
<modules>
<module>drip-api</module> <dependencies>
<module>drip-commons</module>
<module>drip-simple_planner</module> <dependency>
<module>drip-provisioner</module> <groupId>com.fasterxml.jackson.dataformat</groupId>
</modules> <artifactId>jackson-dataformat-yaml</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--mvn clean compile assembly:single-->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass></mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment