Skip to content

Commit

Permalink
#20 - merge traffic generation work from csr
Browse files Browse the repository at this point in the history
  • Loading branch information
obriensystems committed Jun 12, 2022
1 parent 4b5a61c commit b4abd02
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 32 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ ARG USERVICE_HOME=/opt/app/
# Build up the deployment folder structure
RUN mkdir -p $USERVICE_HOME

EXPOSE 8080
ADD magellan-nbi/target/magellan-nbi-*.jar $USERVICE_HOME/ROOT.jar
#ADD startService.sh $USERVICE_HOME/bin/

EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/opt/app/ROOT.jar"]

#CMD ["/opt/app/bin/startService.sh"]
#CMD ["/opt/app/bin/startService.sh"]
4 changes: 4 additions & 0 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@ images:
["northamerica-northeast1-docker.pkg.dev/$PROJECT_ID/traffic-generation/traffic-generation:latest"]


#ERROR: failed to find one or more images after execution of build steps: ["gcr.io/innovapost-351413/magellan-nbi:latest"]
#options:
# logging: CLOUD_LOGGING_ONLY
# [END cloudbuild_maven]


Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ public String getPacket(
@RequestParam String dnsTo,
@RequestParam String from,
@RequestParam String to,
@RequestParam String delay) {
return forwardingService.forward(dnsFrom, dnsTo, from, to, delay).toString();
@RequestParam String delay,
@RequestParam String region) {
return forwardingService.forward(dnsFrom, dnsTo, from, to, delay, region).toString();
}

@GetMapping("/reset")
Expand All @@ -55,21 +56,23 @@ public String getReset() {
return forwardingService.reset();
}


// curl -X GET "http://127.0.0.1:8080/nbi/forward/traffic?dns=127.0.0.1&to=8080&delay=1000&iterations=20"
// curl -X GET "http://127.0.0.1:8080/nbi/forward/traffic?dns=127.0.0.1&to=8080&delay=1000&iterations=20"
//curl -X GET "http://127.0.0.1:8080/nbi/forward/reset"
@GetMapping("/traffic")
@ResponseBody
public String getTraffic(
@RequestParam String useCaseNumber,
@RequestParam String client,
@RequestParam String chaosPercentage,
//@RequestParam String dnsFrom,
@RequestParam String dns,
//@RequestParam String from,
@RequestParam String to,
@RequestParam String delay,
@RequestParam String iterations) {
return forwardingService.traffic(dns, to, delay, iterations).toString();
@RequestParam String iterations,
@RequestParam String region) {
return forwardingService.traffic(useCaseNumber, client, chaosPercentage, dns, to, delay, iterations, region).toString();
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

public interface ForwardingService {

String forward(String dnsFrom, String dnsTo, String from, String to, String delay);
String forward(String dnsFrom, String dnsTo, String from, String to, String delay, String region);

String traffic(String dns, String to, String delay, String iterationsString);
String traffic(String useCaseNumber, String client, String chaosPercentage, String dns, String to, String delay, String iterationsString, String region);

String reset();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@

import java.io.IOException;
import java.net.URI;
import java.math.BigDecimal;
//import java.net.http.Builder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodyHandlers;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Logger;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import java.util.Random;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

Expand All @@ -18,21 +29,30 @@
* 20210926
* Stats
* - 2 3610 MacMini on 1g - 2 vCores sat, 65/sec
* curl -X GET "http://127.0.0.1:8080/nbi/forward/traffic?chaosPercentage=0.1&client=1&delay=1000&dns=34.111.243.135&iterations=1&to=80&useCaseNumber=2"
* curl -X GET "http://127.0.0.1:8080/nbi/forward/traffic?chaosPercentage=0.1&client=1&delay=1000&dns=innova-pop.testgcp.ca&iterations=1&to=80&useCaseNumber=2"
*
*
* @author michaelobrien
*
*/
@Service
public class ForwardingServiceImpl implements ForwardingService {

// TODO: turn these into a map keyed on thread
private String useCaseNumber;
private String dnsFrom;
private String dnsTo;
private String delay;
private String portTo;
private String portFrom;
private double chaosPercentage;
private String client;
private AtomicLong counter = new AtomicLong(1);
private HttpClient httpClient = HttpClient.newBuilder().build();
//private static boolean stopForwarding = false;
private AtomicLong iterations = new AtomicLong(100);
private String region; // L1 L2


static Logger logger = Logger.getLogger(ForwardingServiceImpl.class.getName());
Expand All @@ -41,16 +61,40 @@ public class ForwardingServiceImpl implements ForwardingService {
private AtomicBoolean running = new AtomicBoolean(false);
private AtomicBoolean stopped = new AtomicBoolean(true);

private static final String PROTOCOL = "http";
//private static final String PROTOCOL = "http";
private static final String PROTOCOL = "https";

// private static final String URL_RETURN = "http://host.docker.internal:8888";
// private static final String URL_RETURN = "http://host.docker.internal:";//8080/nbi/forward/packet";
private static final String URL_POSTFIX_REFLECTOR = "/nbi/forward/packet";
private static final String URL_POSTFIX_TRAFFIC = "/nbi/forward/traffic";
//private static final String URL_POSTFIX_TRAFFIC = "/sbi/createEvent";///nbi/forward/traffic";
private static final String URL_POSTFIX_TRAFFIC = "/popRequest";///nbi/forward/traffic";
private static final String URL_POSTFIX_API = "/nbi/api";
private Runnable runnable = () -> { sendMessage(); };
private Runnable runnableTraffic = () -> { sendMessage(false); };

// random string to avoid http caching
private Random randomGenerator = new Random();
// for simulated db failures
private Random randomGeneratorDB = new Random();

//https://stackoverflow.com/questions/52988677/allow-insecure-https-connection-for-java-jdk-11-httpclient
private static TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};


// debug
//curl -X GET "http://127.0.0.1:8080/nbi/forward/traffic?delay=100&dns=traffic-generation-backend-vyua7q27tq-nn.a.run.app&iterations=10&to=80" -H "accept: */*"
@Override
public String reset() {
String ret = "";
Expand All @@ -67,12 +111,15 @@ public String reset() {
return ret;
}


// add random configurable (null parameter passing)
@Override
public String forward(String dnsFrom, String dnsTo, String from, String to, String delay) {
public String forward(String dnsFrom, String dnsTo, String from, String to, String delay, String region) {
try {
Thread.sleep(Long.parseLong(delay));
} catch (Exception e) {};

this.region = region;
portFrom = from;
portTo = to;
this.delay = delay;
Expand All @@ -88,33 +135,41 @@ public String forward(String dnsFrom, String dnsTo, String from, String to, Stri
return Long.toString(counter.addAndGet(1));
}


private void sendMessage() {
sendMessage(true);
}

private void sendMessage(boolean isReflector) {
String url = PROTOCOL + "://";
private void sendMessage(boolean isReflector) {
String url = "";//PROTOCOL + "://";
if(isReflector) {
url = url + dnsTo +
":" + portTo + URL_POSTFIX_REFLECTOR +
"?dnsFrom=" + dnsTo +
"&dnsTo=" + dnsFrom +
"&from=" + portTo +
"&to=" + portFrom +
"&delay=" + delay;
"&delay=" + delay +

"&nocache=" + Integer.toString(randomGenerator.nextInt());
} else {
url = url + dnsTo +
":" + portTo + URL_POSTFIX_API +
"?dns=" + dnsTo +
"&to=" + portTo +
"&delay=" + delay +
"&iterations=" + iterations.get();
}
":" + portTo +
URL_POSTFIX_TRAFFIC + //;// + //URL_POSTFIX_API +
"?usecasename=uc" + useCaseNumber +
"&client=" + client;


//"?dns=" + dnsTo +
//"&to=" + portTo +
//"&delay=" + delay +
//"&iterations=" + iterations.get();
}
sendMessage(url, isReflector);
}

private void sendMessage(String url, boolean isReflector) {
//Builder HttpRequest.newBuilder();
if(isReflector) {
if(!stopped.get()) {
logger.info("Request reflection: " + url);
Expand All @@ -137,26 +192,96 @@ private void sendMessage(String url, boolean isReflector) {

}
} else {

if(!stopped.get()) {
// traffic generation
long iters = iterations.get();
//long iters = iterations.get();
//IntStream.range(0, iters).forEach()

//logger.info("traffic generation: " + fullUrl);
String fullUrl;
for(long i=0; i<iterations.get(); i++) {
try {
Thread.sleep(Long.parseLong(delay));
} catch (Exception e) {};

logger.info("traffic generation: " + i + " of " + iterations.get() + " : " + url);
if(this.isBroken(chaosPercentage)) {
fullUrl = url +
"&timestamp=" + System.currentTimeMillis();// +

} else {
fullUrl = url +
"&requestnumber=" + i +
"&timestamp=" + System.currentTimeMillis() +
"&loadGeneratorRegionName=" + region;
//"&timestampns=" + System.nanoTime() +
//"&timestampms=" + System.currentTimeMillis();// +
//"&nocache=" + Integer.toString(randomGenerator.nextInt());
}

logger.info("Request: " + i + " of " + iterations.get() + " : at: " + System.currentTimeMillis() + " url: " + fullUrl);

// Integer.toString(randomGenerator.nextInt()
// System.nanoTime()
// For POST id is autogenerated
//String json = "{\"id\": 0,\"timestamp\": \"" +
// System.nanoTime() + "\",\"state\": \"" +
// Integer.toString(randomGenerator.nextInt()) + "\"}";
//logger.info("json: " + json);

SSLContext sslContext = null;

try {
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new SecureRandom());
} catch (Exception e) {

}

HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
//.uri(URI.create("http://34.110.234.112/popRequest?usecasename=uc3&requestnumber=1&timestamp=8698717501110"))//url))
.uri(URI.create(fullUrl))
// POST
// fix 2022-05-30 19:46:31.967 INFO 7693 --- [ Thread-138] g.p.m.service.ForwardingServiceImpl : Response: {"timestamp":"2022-05-30T19:46:31.948+0000","status":415,"error":"Unsupported Media Type","message":"Content type '' not supported","path":"/sbi/createEvent"}
//.header("Content-Type", "application/json")
//.POST(BodyPublishers.ofString(json))

// GET
.GET()
//.sslContext(sslContext)
.build();
try {
long startTime = System.currentTimeMillis();
HttpClient httpClient2 = HttpClient.newBuilder().sslContext(sslContext).build();
HttpResponse<String> response =
httpClient.send(request, BodyHandlers.ofString());
httpClient2.send(request, BodyHandlers.ofString());
String body = response.body();
logger.info("Response: " + body);
long duration = System.currentTimeMillis() - startTime;
logger.info("Request/Response duration: " + duration);
boolean requestCommitted = false; // stub for now
if(body.equalsIgnoreCase("ok")) {
requestCommitted = true;
}

// attempt simulated timeout
if(this.isBroken(chaosPercentage)) {
body = "timeout";
logger.info("simulated timeout");
logger.info("timeout " + fullUrl + " " + 100000);
}

// Response (ok/dbaas/timeout) Request url and Request/Response duration
//logger.info(" Response " + body + " Request url " + fullUrl + " and Request/Response duration " + duration);
logger.info("DEMO LOG response: " + body + " url: " + fullUrl + " duration: " + duration);
if(requestCommitted) {
logger.info("request committed " + fullUrl + " " + duration);
} else {
logger.info("request not committed " + duration);
}
} catch (IOException ioe) {
logger.info("request not committed ");
logger.info("backend server exception: " + ioe.getMessage());
ioe.printStackTrace();
} catch (InterruptedException ie) {
ie.printStackTrace();
Expand All @@ -167,14 +292,34 @@ private void sendMessage(String url, boolean isReflector) {
}
}

private boolean isBroken(double percentage) {
BigDecimal subset = new BigDecimal(100000);
subset = subset.multiply(new BigDecimal(percentage));
//logger.info("sub: " + subset);
BigDecimal actual = new BigDecimal(randomGeneratorDB.nextInt(100000));
//logger.info("act: " + actual);

if(actual.intValue() < subset.intValue()) {
//if(randomGeneratorDB.nextInt(1) > 0) {
logger.info("force simulated DBaaS or timeout failure");
return true;
} else {
return false;
}
}


@Override
public String traffic(String dns, String to, String delay, String iterationsString) {
public String traffic(String useCaseNumber, String client, String chaosPercentage, String dns, String to, String delay, String iterationsString, String region) {
try {
Thread.sleep(Long.parseLong(delay));
} catch (Exception e) {};

this.region = region;
portTo = to;
this.useCaseNumber = useCaseNumber;
this.client = client;
this.chaosPercentage = Double.parseDouble(chaosPercentage);
this.delay = delay;
this.dnsTo = dns;
this.iterations = new AtomicLong(Long.parseLong(iterationsString));
Expand All @@ -190,4 +335,12 @@ public String traffic(String dns, String to, String delay, String iterationsStri
return Long.toString(counter.addAndGet(1));
}

// swagger
//https://traffic-generation-source-vyua7q27tq-nn.a.run.app/nbi/swagger-ui.html#/forwarding-controller/getTrafficUsingGET
// curl -X GET "https://traffic-generation-source-vyua7q27tq-nn.a.run.app/nbi/forward/traffic?chaosPercentage=.01&client=0&delay=1&dns=34.110.234.112&iterations=1000&to=80&useCaseNumber=2" -H "accept: */*"

// curl -X GET "http://127.0.0.1:8080/nbi/forward/traffic?useCaseNumber=3&client=1&chaosPercentage=0.01&delay=0&dns=34.110.234.112&iterations=2000&to=80" -H "accept: */*"



}
Loading

0 comments on commit b4abd02

Please sign in to comment.