Skip to content
Snippets Groups Projects
Commit 1fe0a4cd authored by erbel's avatar erbel
Browse files

Removing deprecated files

parent 51cc3620
No related branches found
No related tags found
No related merge requests found
package de.ugoe.cs.rwm.mocci.connector;
import java.util.Random;
import org.eclipse.cmf.occi.core.AttributeState;
import org.eclipse.cmf.occi.core.impl.OCCIFactoryImpl;
import monitoring.Monitorableproperty;
public class CPUSimulation implements Runnable{
private Monitorableproperty monProp;
private OCCIFactoryImpl factory = new OCCIFactoryImpl();
private static Random random;
public CPUSimulation(Monitorableproperty monProp) {
this.monProp = monProp;
}
@Override
public void run() {
while(true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(monProp.getId());
AttributeState monPropAttr = factory.createAttributeState();
monPropAttr.setName("monitoring.result");
String value = CpuValue.getRandomValue().toString();
monPropAttr.setValue(value);
System.out.println("MonProp: " + monProp.getTitle() + ", set to: " + value);
monProp.getAttributes().add(monPropAttr);
}
}
}
package de.ugoe.cs.rwm.mocci.connector;
import java.util.Random;
public enum CpuValue {
Critical, High, Medium, Low, None;
public static CpuValue getRandomValue() {
Random random = new Random();
return values()[random.nextInt(values().length)];
}
}
\ No newline at end of file
/de/
de.ugoe.cs.rwm.mocci.model/new Extension diagram.jpg

1.1 MiB

package de.ugoe.cs.rwm.mocci;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.io.IOUtils;
import org.eclipse.cmf.occi.core.Entity;
import de.ugoe.cs.rwm.docci.connector.Connector;
/**
* Contains multiple methods required by every Executor
*
* @author erbel
*
*/
public abstract class AbsExecutor {
protected Connector connector;
protected Integer maxTries;
private static final String ENCODINGCHARSET = "UTF-8";
/**
* Establish a HttpURLConnection to the given address using the given REST
* method, output, contentType and authToken.
*
* @param address
* Address of the REST interface.
* @param method
* CRUD operation to be used.
* @param output
* Boolean to define doOutput.
* @param contentType
* Type of content.
* @param authToken
* Authentication token to be used.
* @return HttpURLConnection for further adjustment
*/
@Deprecated
protected HttpURLConnection establishConnection(String address, String method, Boolean output, String contentType,
String authToken) {
try {
URL url = new URL(address);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(output);
if (method != null) {
conn.setRequestMethod(method);
}
if (contentType != null) {
conn.setRequestProperty("Content-Type", contentType);
}
if (authToken != null) {
conn.setRequestProperty("X-Auth-token", authToken);
}
return conn;
} catch (MalformedURLException e) {
System.out.println("Malformed URL: " + address);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* Establish a HttpURLConnection to the given address using the given REST
* method, output, contentType and authToken.
*
* @param address
* Address of the REST interface.
* @param method
* CRUD operation to be used.
* @param output
* Boolean to define doOutput.
* @param contentType
* Type of content.
* @return HttpURLConnection for further adjustment
*/
protected HttpURLConnection establishConnection(String address, String method, Boolean output, String contentType) {
try {
URL url = new URL(address);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(output);
if (contentType != null) {
conn.setRequestProperty("Content-Type", contentType);
}
if (method != null) {
conn.setRequestMethod(method);
}
return conn;
} catch (MalformedURLException e) {
System.out.println("Malformed URL: " + address);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* Checks whether the connection is successful. Otherwise the HTTPResponse code
* is logged.
*
* @param conn
* HttpURLConnection to be checked.
* @return Boolean whether connection is successful.
*/
protected boolean connectionSuccessful(HttpURLConnection conn) {
try {
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED
&& conn.getResponseCode() != HttpURLConnection.HTTP_OK
&& conn.getResponseCode() != HttpURLConnection.HTTP_NO_CONTENT) {
System.out.println("Failed: " + conn.getURL() + " HTTP error code: " + conn.getResponseCode());
// log.error("Failed: "+ conn.getURL() + " HTTP error code: "+
// conn.getResponseCode() + " " +conn.getRequestProperties());
conn.disconnect();
return false;
}
} catch (IOException e) {
conn.disconnect();
e.printStackTrace();
}
return true;
}
/**
* Writes the String input as input into the HttpUrlConnection conn.
*
* @param conn
* HttpURLConnection to be used.
* @param input
* Input for the Connection conn.
*/
protected void writeInput(HttpURLConnection conn, String input) {
try {
OutputStream os;
os = conn.getOutputStream();
os.write(input.getBytes(ENCODINGCHARSET));
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Returns the output from the connection as concatenated String. On fault a
* null is returned.
*
* @param conn
* HttpURLConnection to be read.
* @return Output of th HTTP Connection.
*/
protected String getOutput(HttpURLConnection conn) {
int responseCode = getResponseCode(conn);
if (200 <= responseCode && responseCode <= 299) {
return getInputStream(conn);
} else {
return getErrorStream(conn);
}
}
private String getErrorStream(HttpURLConnection conn) {
InputStreamReader isr = null;
StringWriter writer = new StringWriter();
try {
if (conn.getErrorStream() != null) {
isr = new InputStreamReader(conn.getErrorStream(), ENCODINGCHARSET);
IOUtils.copy(isr, writer);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return writer.toString();
/*
* StringBuffer sb = new StringBuffer(); if (is != null) { InputStreamReader isr
* = null; BufferedReader br = null; try { isr = new InputStreamReader(is); br =
* new BufferedReader(isr); sb = new StringBuffer(); String inputLine = "";
* while ((inputLine = br.readLine()) != null) { sb.append(inputLine); }
* LOG.info("ERRORSTREAM: " + sb); return sb.toString(); } catch (IOException
* e1) { LOG.info("Could not retrieve error stream"); return ""; } finally { if
* (br != null) { try { br.close(); } catch (IOException e) {
* LOG.info("Could not close BufferedReader while retrieving errorstream");
* e.printStackTrace(); } }
*
* if (isr != null) { try { isr.close(); } catch (IOException e1) {
* LOG.info("Could not close InputStreamReader while retrieving errorstream"); }
* } } } return null;
*/
}
private String getInputStream(HttpURLConnection conn) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), ENCODINGCHARSET))) {
StringBuffer sb = new StringBuffer();
String inputLine = "";
while ((inputLine = br.readLine()) != null) {
sb.append(inputLine);
}
return sb.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private int getResponseCode(HttpURLConnection conn) {
int code;
try {
code = conn.getResponseCode();
} catch (IOException e) {
code = 0;
e.printStackTrace();
}
return code;
}
/**
* Adapts address, so that the address contains the path of OCCI element to be
* created. E.g. Adds compute/storage or network to the path.
*
* @param entity
* to be provisioned.
* @return adapted address.
*/
protected String getEntityKindURI(Entity entity) {
// String address = Deployer.adress;
String address = this.connector.getAddress();
if (entity.getKind().getTerm().equals("networkinterface")) {
address += "/networklink/";
} else {
address += "/" + entity.getKind().getTerm() + "/";
}
return address;
}
/**
* Logs the respones of a request stored in the passed String output.
*
* @param output
* Output of the REST response.
*/
protected void logResponseOfRequest(String output) {
System.out.println("Rest response: " + output);
}
/**
* Returns Connector of the Executor object.
*
* @return connector Current Connector of the Executor object.
*/
public Connector getConn() {
return connector;
}
/**
* Sets Connector of the Executor object.
*
* @param conn
* Desired Connector of the Executor object.
*/
public void setConn(Connector conn) {
this.connector = conn;
}
}
package de.ugoe.cs.rwm.mocci;
/**Making javadoc happy.
* @author erbel
*
*/
public class ExampleClass {
/**Making javadoc happy.
* @param args Making javadoc happy.
*/
public static void main(String[] args) {
System.out.println("Hello world");
}
}
package de.ugoe.cs.rwm.mocci;
import java.net.HttpURLConnection;
import org.eclipse.cmf.occi.core.Action;
import org.eclipse.cmf.occi.core.AttributeState;
import org.eclipse.cmf.occi.core.Entity;
import org.eclipse.cmf.occi.core.Link;
import org.eclipse.cmf.occi.core.Mixin;
import org.eclipse.cmf.occi.core.MixinBase;
import org.eclipse.emf.ecore.EObject;
import de.ugoe.cs.rwm.docci.connector.Connector;
/**
* Handles execution of OCCI Model Elements.
*
* @author erbel
*
*/
public class MartExecutor extends AbsExecutor {
/**
* Creates an Executor to the OCCI API of the specified connection. Sets
* maxTries to 3.
*
* @param conn
*/
public MartExecutor(Connector conn) {
this.connector = conn;
this.maxTries = 3;
}
/**
* Creates Executor to the OCCI API of the specified connection. maxTries is
* hereby the maximum amount of retries for a request. Should be at least 2 to
* handle connection issues.
*
* @param conn
* @param maxTries
*/
public MartExecutor(Connector conn, int maxTries) {
this.connector = conn;
this.maxTries = maxTries;
}
public String executeOperation(String operation, EObject element, EObject action) {
Boolean success = false;
String output = "";
int count = 0;
while (success == false && count < maxTries) {
if (operation.equals("POST") && action == null) {
output = executePostOperation(element);
} else if (operation.equals("PUT")) {
output = executePutOperation(element);
} else if (operation.equals("GET")) {
output = executeGetOperation(element);
} else if (operation.equals("DELETE")) {
output = executeDeleteOperation(element);
} else if (operation.equals("POST") && action != null) {
output = executeActionOperation(element, action);
}
// TODO: Output kommt eigentlich immer;
if (output != null) {
success = true;
}
if (success == false) {
count++;
try {
System.out.println(operation + " Failed: " + ((Entity) element).getTitle() + " Rerequest in 5s!");
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return output;
}
/**
* Performs a POST(Action) request on the EObject element with the Action stored
* in EObject eAction.
*
* @param element
* @param eAction
* @return
*/
private String executeActionOperation(EObject element, EObject eAction) {
Entity entity = (Entity) element;
Action action = (Action) eAction;
String adaptedAddress = "http://" + connector.getAddress() + ":" + connector.getPort() + getLocation(entity);
adaptedAddress += "?action=";
adaptedAddress += action.getTerm();
HttpURLConnection conn = establishConnection(adaptedAddress, "POST", true, "text/occi");
conn.setRequestProperty("Category", generateCategoryHeader(action));
conn.setRequestProperty("Accept", "application/json");
System.out.println("POST " + conn.getURL() + " -H 'Content-Type: " + conn.getRequestProperty("Content-Type") + "' "
+ " -H 'Category: " + conn.getRequestProperty("Category") + "' ");
if (connectionSuccessful(conn)) {
String output = getOutput(conn);
return output;
} else {
conn.disconnect();
return null;
}
}
private String getLocation(Entity ent) {
if (ent.getLocation() != null && ent.getLocation().isEmpty() == false) {
return ent.getLocation();
} else {
StringBuilder location = new StringBuilder();
location.append("/");
location.append(ent.getKind().getTerm());
location.append("/");
location.append(ent.getId());
location.append("/");
return location.toString();
}
}
/**
* Returns generated Category header for the passed action.
*
* @param action
* @return
*/
private String generateCategoryHeader(Action action) {
String category = action.getTerm() + "; " + "scheme=\"" + action.getScheme() + "\"; " + "class=\"action\"";
return category;
}
/**
* Returns output of GET request for the passed Entity EObject extracted.
*
* @param extracted
* @return Output of the GET request.
*/
public String executeGetOperation(EObject extracted) {
Entity entity = (Entity) extracted;
String adaptedAddress = "http://" + connector.getAddress() + ":" + connector.getPort() + getLocation(entity);
HttpURLConnection conn = establishConnection(adaptedAddress, null, false, null);
conn.setRequestProperty("Accept", "application/json");
System.out.println("GET" + " " + adaptedAddress);
return getOutput(conn);
}
public String executeGetOperation(String query) {
String adaptedAddress = "http://" + connector.getAddress() + ":" + connector.getPort() + query;
HttpURLConnection conn = establishConnection(adaptedAddress, null, false, null);
conn.setRequestProperty("Accept", "application/json");
System.out.println("GET" + " " + adaptedAddress);
return getOutput(conn);
}
/**
* Issues a cloud session token required for authorization.
*
* @param user
* @param password
* @param project
* @param address
* @return cloud session token
*/
@Deprecated
public String createToken(String user, String password, String project, String address) {
return "";
}
/**
* Performs POST request to provision the Resource described by the EObject
* element.
*
* @param element
* @return
*/
private String executePostOperation(EObject element) {
if (element instanceof Entity) {
Entity entity = (Entity) element;
return postEntity(entity);
} else if (element instanceof Mixin) {
Mixin mixin = (Mixin) element;
if (mixin.getScheme().equals("http://schemas.modmacao.org/usermixins#")) {
return postUserMixin(mixin);
}
}
throw new IllegalArgumentException("Can not post element: " + element);
}
private String postUserMixin(Mixin mixin) {
String adaptedAddress = "http://" + connector.getAddress() + ":" + connector.getPort() + "/-/";
HttpURLConnection conn = establishConnection(adaptedAddress, "POST", true, "text/occi");
conn.setRequestProperty("Category", generateUserMixinHeader(mixin));
conn.setRequestProperty("Accept", "application/json");
System.out.println("POST" + " " + conn.getURL() + " Category: " + conn.getRequestProperty("Category"));
if (connectionSuccessful(conn)) {
String output = getOutput(conn);
conn.disconnect();
return output;
} else {
conn.disconnect();
return null; // getOutput(conn);
}
}
private String generateUserMixinHeader(Mixin mixin) {
String category = "";
category = mixin.getTerm() + "; " + "scheme=\"" + mixin.getScheme() + "\"; " + "class=\"mixin\"; "
+ "location=\"/usermixins/\"";
return category;
}
private String postEntity(Entity entity) {
String adaptedAddress = "http://" + connector.getAddress() + ":" + connector.getPort() + getLocation(entity);
HttpURLConnection conn = establishConnection(adaptedAddress, "POST", true, "text/occi");
conn.setRequestProperty("Category", generateCategoryHeader(entity));
conn.setRequestProperty("X-OCCI-Attribute", generateAttributeHeader(entity));
conn.setRequestProperty("Accept", "application/json");
System.out.println("POST " + conn.getURL() + " -H 'Content-Type: " + conn.getRequestProperty("Content-Type") + "' "
+ " -H 'Category: " + conn.getRequestProperty("Category") + "' " + " -H 'X-OCCI-Attribute:"
+ conn.getRequestProperty("X-OCCI-Attribute") + "' ");
if (connectionSuccessful(conn)) {
String output = getOutput(conn);
conn.disconnect();
return output;
} else {
conn.disconnect();
return null; // getOutput(conn);
}
}
/**
* Performs DELETE request to deprovision Resource described by the EObject
* element.
*
* @param element
* @return
*/
private String executeDeleteOperation(EObject element) {
Entity entity = (Entity) element;
String adaptedAddress = "http://" + connector.getAddress() + ":" + connector.getPort() + getLocation(entity);
HttpURLConnection conn = establishConnection(adaptedAddress, "DELETE", false, null);
conn.setRequestProperty("Accept", "application/json");
System.out.println("DELETE" + " " + conn.getURL());
if (connectionSuccessful(conn)) {
String output = getOutput(conn);
conn.disconnect();
return output;
} else {
conn.disconnect();
return null;
}
}
/**
* Generates X-OCCI-Attribute header for the HttpURLConnection.
*
* @param entity
* containing the Attributes.
* @return String containing the X-OCCI-Attribute Header.
*/
private String generateAttributeHeader(Entity entity) {
StringBuffer attributes = new StringBuffer();
if (entity instanceof Link) {
Link link = (Link) entity;
attributes.append("occi.core.source=\"" + getLocation(link.getSource()) + "\",");
attributes.append("occi.core.target=\"" + getLocation(link.getTarget()) + "\",");
}
for (AttributeState state : entity.getAttributes()) {
if ((state.getName().equals("occi.core.target") || state.getName().equals("occi.core.source")) == false) {
attributes.append(state.getName() + "=\"" + state.getValue() + "\", ");
}
}
for (MixinBase base : entity.getParts()) {
for (AttributeState baseState : base.getAttributes()) {
attributes.append(baseState.getName() + "=\"" + baseState.getValue() + "\", ");
}
}
System.out.println(attributes);
return attributes.substring(0, attributes.lastIndexOf(","));
}
/**
* Generates Category header for the HttpURLConnection.
*
* @param entity
* containing the Attributes.
* @return String containing the Category Header.
*/
private String generateCategoryHeader(Entity entity) {
StringBuffer category = new StringBuffer();
category.append(entity.getKind().getTerm() + "; " + "scheme=\"" + entity.getKind().getScheme() + "\"; "
+ "class=\"kind\"");
for (Mixin mixin : entity.getMixins()) {
category.append(
", " + mixin.getTerm() + "; " + "scheme=\"" + mixin.getScheme() + "\"; " + "class=\"mixin\"");
}
return category.toString();
}
/**
* Executes PUT requested based on the Resource described in the EObject
* element.
*
* @param element
* Element to PUT
* @return Connection Output.
*/
public String executePutOperation(EObject element) {
Entity entity = (Entity) element;
String adaptedAddress = "http://" + connector.getAddress() + ":" + connector.getPort() + getLocation(entity);
HttpURLConnection conn = establishConnection(adaptedAddress, "PUT", true, "text/occi");
conn.setRequestProperty("Category", generateCategoryHeader(entity));
conn.setRequestProperty("X-OCCI-Attribute", generateAttributeHeader(entity));
conn.setRequestProperty("Accept", "application/json");
System.out.println("PUT " + conn.getURL() + " -H 'Content-Type: " + conn.getRequestProperty("Content-Type") + "' "
+ " -H 'Category: " + conn.getRequestProperty("Category") + "' " + " -H 'X-OCCI-Attribute:"
+ conn.getRequestProperty("X-OCCI-Attribute") + "' ");
if (connectionSuccessful(conn)) {
String output = getOutput(conn);
conn.disconnect();
/*
* try { LOG.info("Waiting for configuration: 10000ms."); Thread.sleep(10000); }
* catch (InterruptedException e) { // TODO Auto-generated catch block
* e.printStackTrace(); }
*/
return output;
} else {
conn.disconnect();
System.out.println("Request failed: " + getOutput(conn));
return null;// getOutput(conn);
}
}
/**
* Returns registered usermixins.
*
* @return String containing the REST response when asking for usermixins.
*/
public String getUsermixins() {
String adaptedAddress = "http://" + connector.getAddress() + ":" + connector.getPort() + "/-/";
HttpURLConnection conn = establishConnection(adaptedAddress, null, false, null);
conn.setRequestProperty("Accept", "application/json");
System.out.println("GET" + " " + adaptedAddress);
String output = getOutput(conn);
System.out.println(output);
return output;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment