diff --git a/gctrl/README.md b/gctrl/README.md new file mode 100644 index 0000000..9b6634f --- /dev/null +++ b/gctrl/README.md @@ -0,0 +1,2 @@ +# General Controller +![Software architecture of the project](src/misc/5sdbd_gc.draft.svg) diff --git a/gctrl/pom.xml b/gctrl/pom.xml new file mode 100644 index 0000000..b0efab4 --- /dev/null +++ b/gctrl/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + + fr.laas.sara.sdci + fr.laas.sara.sdci.gctrl + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 9 + 9 + + + + + + + com.h2database + h2 + 1.4.200 + + + de.vandermeer + asciitable + 0.3.2 + + + com.github.signaflo + timeseries + 0.4 + + + org.slf4j + slf4j-api + 1.7.5 + + + org.slf4j + slf4j-log4j12 + 1.7.5 + + + \ No newline at end of file diff --git a/gctrl/src/main/java/Analyze.java b/gctrl/src/main/java/Analyze.java new file mode 100644 index 0000000..62bb9d7 --- /dev/null +++ b/gctrl/src/main/java/Analyze.java @@ -0,0 +1,78 @@ +import java.util.List; + +// + +//* @author couedrao on 25/11/2019. + +//* @project gctrl + +// + +// +//* 1)Perform complex data analysis and reasoning on the symptoms provided by the monitor function. +//* 2)Influenced by stored knowledge data. +//* 3)If changes are required, a change request is logically passed to the plan function. +//* +@SuppressWarnings({"SameParameterValue", "SynchronizeOnNonFinalField"}) +class Analyze { + public String gw_current_RFC = ""; + private static int i; + + void start() { + Main.logger(this.getClass().getSimpleName(), "Start Analyzing"); + + while (Main.run) { + + String current_symptom = get_symptom(); + //Main.logger(this.getClass().getSimpleName(), "Received Symptom : " + current_symptom); + + update_rfc(rfc_generator(current_symptom)); + } + } + + //Symptom Receiver + private String get_symptom() { + synchronized (Main.monitor.gw_current_SYMP) { + try { + Main.monitor.gw_current_SYMP.wait(); + } catch (InterruptedException ex) { + ex.printStackTrace(); + } + } + return Main.monitor.gw_current_SYMP; + } + + //Rule-based RFC Generator + private String rfc_generator(String symptom) { + List symptoms = Main.shared_knowledge.get_symptoms(); + List rfcs = Main.shared_knowledge.get_rfc(); + + if (symptom.contentEquals(symptoms.get(0)) || symptom.contentEquals(symptoms.get(2))) { + Main.logger(this.getClass().getSimpleName(), "RFC --> To plan : " + rfcs.get(0)); + i = 0; + return rfcs.get(0); + } else if (symptom.contentEquals(symptoms.get(1))) { + i++; + if (i < 3) { + Main.logger(this.getClass().getSimpleName(), "RFC --> To plan : " + rfcs.get(1)); + return rfcs.get(1); + } else { + Main.logger(this.getClass().getSimpleName(), "RFC --> To plan : " + "YourPlansDoNotWork"); + return "YourPlansDoNotWork"; + } + } else + return null; + + } + + + private void update_rfc(String rfc) { + + synchronized (gw_current_RFC) { + gw_current_RFC.notify(); + gw_current_RFC = rfc; + + } + } + +} diff --git a/gctrl/src/main/java/Execute.java b/gctrl/src/main/java/Execute.java new file mode 100644 index 0000000..8c9c52d --- /dev/null +++ b/gctrl/src/main/java/Execute.java @@ -0,0 +1,102 @@ +import java.util.List; + +// + +//* @author couedrao on 25/11/2019. + +//* @project gctrl + +// + +// +//* Changes the behavior of the managed resource using effectors Changes the behavior of the managed resource using effectors, based on the actions recommended by the plan function. +//* +@SuppressWarnings({"SameParameterValue", "SynchronizeOnNonFinalField"}) +class Execute { + private static List workflow_lists; + private static final MANOAPI manoapi = new MANOAPI(); + private static final SDNCtrlAPI sdnctlrapi = new SDNCtrlAPI(); + + void start() throws InterruptedException { + Main.logger(this.getClass().getSimpleName(), "Start Execution"); + workflow_lists = Main.shared_knowledge.get_worklow_lists(); + + while (Main.run) { + String current_plan = get_plan(); + + // Main.logger(this.getClass().getSimpleName(), "Received Plan : " + current_plan); + String[] workflow = workflow_generator(current_plan); + for (int i = 0; i < workflow.length; i++) { + Main.logger(this.getClass().getSimpleName(), "workflow [" + i + "] : " + workflow[i]); + + } + + for (String w : workflow) { + Main.logger(this.getClass().getSimpleName(), "UC : " + w); + switch (w) { + case "UC1": + Main.logger(this.getClass().getSimpleName(), "Nothing to do"); + break; + case "UC2": + Main.logger(this.getClass().getSimpleName(), "Deploying GW"); + String newdestip = manoapi.deploy_gw(Main.shared_knowledge.getGwinfo()); + Main.shared_knowledge.setNewdestip(newdestip); + Main.shared_knowledge.setOldgwip(newdestip); + break; + case "UC3": + Main.logger(this.getClass().getSimpleName(), "Redirecting Traffic"); + String status = sdnctlrapi.redirect_traffic(Main.shared_knowledge.getOlddestip(), Main.shared_knowledge.getNewdestip()); + Main.logger(this.getClass().getSimpleName(), status); + break; + case "UC4": + Main.logger(this.getClass().getSimpleName(), "Deploying LB+GWs"); + List newgwsip = manoapi.deploy_multi_gws_and_lb(Main.shared_knowledge.getGwsinfo()); + Main.shared_knowledge.setLbip(newgwsip.get(0)); + Main.shared_knowledge.setNewgwsip(newgwsip.subList(1, newgwsip.size())); + break; + case "UC5": + Main.logger(this.getClass().getSimpleName(), "Inserting a loadbalancer"); + status = sdnctlrapi.insert_a_loadbalancer(Main.shared_knowledge.getOldgwip(), Main.shared_knowledge.getLbip(), Main.shared_knowledge.getNewgwsip()); + Main.logger(this.getClass().getSimpleName(), status); + break; + case "UC6": + Main.logger(this.getClass().getSimpleName(), "Removing less important traffic"); + status = sdnctlrapi.remove_less_important_traffic(Main.shared_knowledge.getImportantsrcip()); + Main.logger(this.getClass().getSimpleName(), status); + break; + default: + } + Thread.sleep(2000); + continue; + + + } + + } + } + + //Plan Receiver + private String get_plan() { + synchronized (Main.plan.gw_PLAN) { + try { + Main.plan.gw_PLAN.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + return Main.plan.gw_PLAN; + } + + //Rule-based Workflow Generator + private String[] workflow_generator(String plan) { + List plans = Main.shared_knowledge.get_plans(); + if (plan.contentEquals(plans.get(0))) { + return workflow_lists.get(0).split("/"); + } else if (plan.contentEquals(plans.get(1))) { + return workflow_lists.get(1).split("/"); + } else if (plan.contentEquals(plans.get(2))) { + return workflow_lists.get(2).split("/"); + } else + return null; + } +} diff --git a/gctrl/src/main/java/Knowledge.java b/gctrl/src/main/java/Knowledge.java new file mode 100644 index 0000000..1145379 --- /dev/null +++ b/gctrl/src/main/java/Knowledge.java @@ -0,0 +1,404 @@ +import org.h2.tools.DeleteDbFiles; + +import java.sql.*; +import java.util.*; + +// + +//* @author couedrao on 25/11/2019. + +//* @project gctrl + +// + +// + +//* 1)Standard data shared among the monitor analyze plan and Standard data shared among the monitor, analyze, plan and execute functions + +//* 2)The shared knowledge includes data such as topology information, historical logs, metrics, symptoms and policies + +//* 3)Created by the monitor part while execute part might update the knowledge + +//* + +class Knowledge { + + private static final String DB_DRIVER = "org.h2.Driver"; + private static final String DB_CONNECTION = "jdbc:h2:~/test"; + private static final String DB_USER = ""; + private static final String DB_PASSWORD = ""; + + static final int moving_wind = 10; + static final int horizon = 3; + static final String gw = "GW_I"; + static final double gw_lat_threshold = 20; + + /*TODO : edit symptom, rfc, workflow_lists, plan*/ + private static final List symptom = Arrays.asList("N/A", "NOK", "OK"); + private static final List rfc = Arrays.asList("DoNotDoAnything", "DecreaseLatencyIn" + gw); + private static final List workflow_lists = Arrays.asList("UC1", "UC2/UC3", "UC4/UC5/UC6"); + private static final List plan = Arrays.asList("A", "B", "C"); + private final Map gwinfo = new HashMap<>(); + private final List> gwsinfo = new ArrayList<>(); + private final String olddestip = "192.168.0.2"; + private String newdestip; + private String oldgwip; + private String lbip; + private List newgwsip; + private final String importantsrcip = "192.168.0.1"; + + void start() throws Exception { + // delete the H2 database named 'test' in the user home directory + DeleteDbFiles.execute("~", "test", true); + Main.logger(this.getClass().getSimpleName(), "old database 'test' deleted"); + //Initialization of the Knowledge + store_symptoms(); + store_rfcs(); + store_plans(); + store_execution_workflow(); + //TODO : update gwinfo + gwinfo.put("name", "gw"); + gwinfo.put("image", "alpine:latest"); + gwinfo.put("net", "new_network"); + + gwsinfo.add(0, gwinfo); + gwsinfo.add(1, gwinfo); + gwsinfo.add(2, gwinfo); + + Main.logger(this.getClass().getSimpleName(), "Knowledge Starting"); + + } + + void insert_in_tab(Timestamp timestamp, double lat) { + try (Connection conn = getDBConnection()) { + PreparedStatement insert; + String InsertQuery = "INSERT INTO " + Knowledge.gw + "_LAT" + " (id, latency) values" + "(?,?)"; + conn.setAutoCommit(false); + insert = conn.prepareStatement(InsertQuery); + insert.setTimestamp(1, timestamp); + insert.setDouble(2, lat); + insert.executeUpdate(); + insert.close(); + conn.commit(); + } catch (SQLException e) { + System.out.println("Exception Message " + e.getLocalizedMessage()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + List get_symptoms() { + String gw_symp = gw + "_SYMP"; + + Connection conn = getDBConnection(); + String SelectQuery = "select * from " + gw_symp; + PreparedStatement select; + List r = null; + try { + select = conn.prepareStatement(SelectQuery); + ResultSet rs = select.executeQuery(); + r = new ArrayList<>(); + while (rs.next()) { + r.add(rs.getString("symptom")); + } + } catch (SQLException e) { + System.out.println("Exception Message " + e.getLocalizedMessage()); + } catch (Exception e) { + e.printStackTrace(); + } + return r; + + } + + List get_rfc() { + String gw_rfc = gw + "_RFC"; + + Connection conn = getDBConnection(); + String SelectQuery = "select * from " + gw_rfc; + PreparedStatement select; + List r = null; + try { + select = conn.prepareStatement(SelectQuery); + ResultSet rs = select.executeQuery(); + r = new ArrayList<>(); + while (rs.next()) { + r.add(rs.getString("rfc")); + } + } catch (SQLException e) { + System.out.println("Exception Message " + e.getLocalizedMessage()); + } catch (Exception e) { + e.printStackTrace(); + } + + return r; + + } + + List get_plans() { + String gw_plan = gw + "_PLAN"; + + Connection conn = getDBConnection(); + String SelectQuery = "select * from " + gw_plan; + PreparedStatement select; + List r = null; + try { + select = conn.prepareStatement(SelectQuery); + ResultSet rs = select.executeQuery(); + r = new ArrayList<>(); + while (rs.next()) { + r.add(rs.getString("plan")); + } + } catch (SQLException e) { + System.out.println("Exception Message " + e.getLocalizedMessage()); + } catch (Exception e) { + e.printStackTrace(); + } + + return r; + + } + + List get_worklow_lists() { + String gw_execw = gw + "_EXECW"; + + Connection conn = getDBConnection(); + String SelectQuery = "select * from " + gw_execw; + PreparedStatement select; + List r = null; + try { + select = conn.prepareStatement(SelectQuery); + ResultSet rs = select.executeQuery(); + r = new ArrayList<>(); + while (rs.next()) { + r.add(rs.getString("workflow")); + } + } catch (SQLException e) { + System.out.println("Exception Message " + e.getLocalizedMessage()); + } catch (Exception e) { + e.printStackTrace(); + } + + return r; + + } + + ResultSet select_from_tab() { + //Main.logger("Select the last " + n + " latencies"); + Connection conn = getDBConnection(); + String SelectQuery = "select TOP " + moving_wind + " * from " + Knowledge.gw + "_LAT" + " ORDER BY id DESC"; + //PreparedStatement select; + ResultSet rs = null; + try { + Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); + // select = conn.prepareStatement(SelectQuery); + rs = stmt.executeQuery(SelectQuery); + } catch (SQLException e) { + System.out.println("Exception Message " + e.getLocalizedMessage()); + } catch (Exception e) { + e.printStackTrace(); + } + return rs; + + + } + + void create_lat_tab() { + try (Connection conn = getDBConnection()) { + Statement create; + conn.setAutoCommit(false); + create = conn.createStatement(); + create.execute("CREATE TABLE " + Knowledge.gw + "_LAT" + " (id timestamp primary key, latency double )"); + create.close(); + conn.commit(); + } catch (SQLException e) { + System.out.println("Exception Message " + e.getLocalizedMessage()); + } catch (Exception e) { + e.printStackTrace(); + } finally { + Main.logger(this.getClass().getSimpleName(), "... Database Created"); + + } + } + + private void store_plans() throws SQLException { + String gw_plan = gw + "_PLAN"; + Connection conn = getDBConnection(); + Statement create; + conn.setAutoCommit(false); + create = conn.createStatement(); + create.execute("CREATE TABLE " + gw_plan + " (id int primary key, plan varchar(20) )"); + create.close(); + + for (int i = 0; i < plan.size(); i++) { + conn = getDBConnection(); + PreparedStatement insert; + try { + insert = conn.prepareStatement("INSERT INTO " + gw_plan + " (id, plan) values" + "(?,?)"); + insert.setInt(1, i + 1); + insert.setString(2, plan.get(i)); + insert.executeUpdate(); + insert.close(); + conn.commit(); + } catch (SQLException e) { + System.out.println("Exception Message " + e.getLocalizedMessage()); + } catch (Exception e) { + e.printStackTrace(); + } finally { + conn.close(); + } + } + } + + private void store_rfcs() throws SQLException { + String gw_rfc = gw + "_RFC"; + Connection conn = getDBConnection(); + Statement create; + conn.setAutoCommit(false); + create = conn.createStatement(); + create.execute("CREATE TABLE " + gw_rfc + " (id int primary key, rfc varchar(40) )"); + create.close(); + + for (int i = 0; i < rfc.size(); i++) { + conn = getDBConnection(); + PreparedStatement insert; + try { + insert = conn.prepareStatement("INSERT INTO " + gw_rfc + " (id, rfc) values" + "(?,?)"); + insert.setInt(1, i + 1); + insert.setString(2, rfc.get(i)); + insert.executeUpdate(); + insert.close(); + conn.commit(); + } catch (SQLException e) { + System.out.println("Exception Message " + e.getLocalizedMessage()); + } catch (Exception e) { + e.printStackTrace(); + } finally { + conn.close(); + } + } + } + + private void store_execution_workflow() throws SQLException { + String gw_execw = gw + "_EXECW"; + Connection conn = getDBConnection(); + Statement create; + conn.setAutoCommit(false); + create = conn.createStatement(); + create.execute("CREATE TABLE " + gw_execw + " (id int primary key, workflow varchar(50) )"); + create.close(); + + for (int i = 0; i < workflow_lists.size(); i++) { + conn = getDBConnection(); + PreparedStatement insert; + try { + insert = conn.prepareStatement("INSERT INTO " + gw_execw + " (id, workflow) values" + "(?,?)"); + insert.setInt(1, i + 1); + insert.setString(2, workflow_lists.get(i)); + insert.executeUpdate(); + insert.close(); + conn.commit(); + } catch (SQLException e) { + System.out.println("Exception Message " + e.getLocalizedMessage()); + } catch (Exception e) { + e.printStackTrace(); + } finally { + conn.close(); + } + } + } + + private void store_symptoms() throws SQLException { + String gw_symp = gw + "_SYMP"; + Connection conn = getDBConnection(); + Statement create; + conn.setAutoCommit(false); + create = conn.createStatement(); + create.execute("CREATE TABLE " + gw_symp + " (id int primary key, symptom varchar(5) )"); + create.close(); + + for (int i = 0; i < symptom.size(); i++) { + conn = getDBConnection(); + PreparedStatement insert; + + try { + insert = conn.prepareStatement("INSERT INTO " + gw_symp + " (id, symptom) values" + "(?,?)"); + insert.setInt(1, i + 1); + insert.setString(2, symptom.get(i)); + insert.executeUpdate(); + insert.close(); + conn.commit(); + } catch (SQLException e) { + System.out.println("Exception Message " + e.getLocalizedMessage()); + } catch (Exception e) { + e.printStackTrace(); + } finally { + conn.close(); + } + } + } + + private Connection getDBConnection() { + // Main.logger("Connecting the database ..."); + try { + Class.forName(DB_DRIVER); + } catch (ClassNotFoundException e) { + System.out.println(e.getMessage()); + } + try { + return DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); + } catch (SQLException e) { + System.out.println(e.getMessage()); + return null; + } + + } + + public Map getGwinfo() { + return gwinfo; + } + + public List> getGwsinfo() { + return gwsinfo; + } + + public String getOlddestip() { + return olddestip; + } + + public String getNewdestip() { + return newdestip; + } + + public void setNewdestip(String newdestip) { + this.newdestip = newdestip; + } + + public String getOldgwip() { + return oldgwip; + } + + public void setOldgwip(String oldgwip) { + this.oldgwip = oldgwip; + } + + public String getLbip() { + return lbip; + } + + public void setLbip(String lbip) { + this.lbip = lbip; + } + + public List getNewgwsip() { + return newgwsip; + } + + public void setNewgwsip(List newgwsip) { + this.newgwsip = newgwsip; + } + + public String getImportantsrcip() { + return importantsrcip; + } + +} \ No newline at end of file diff --git a/gctrl/src/main/java/MANOAPI.java b/gctrl/src/main/java/MANOAPI.java new file mode 100644 index 0000000..c41999f --- /dev/null +++ b/gctrl/src/main/java/MANOAPI.java @@ -0,0 +1,36 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Random; + +/** + * @author couedrao on 27/11/2019. + * @project gctrl + */ +class MANOAPI { + + String deploy_gw(Map vnfinfos) { + String ip = "192.168.0." + (new Random().nextInt(253) + 1); + Main.logger(this.getClass().getSimpleName(), "Deploying VNF ..."); + + //printing + for (Entry e : vnfinfos.entrySet()) { + Main.logger(this.getClass().getSimpleName(), "\t" + e.getKey() + " : " + e.getValue()); + } + //TODO + + return ip; + } + + List deploy_multi_gws_and_lb(List> vnfsinfos) { + List ips = new ArrayList<>(); + //TODO + + for (Map vnfsinfo : vnfsinfos) { + ips.add(deploy_gw(vnfsinfo)); + } + + return ips; + } +} diff --git a/gctrl/src/main/java/Main.java b/gctrl/src/main/java/Main.java new file mode 100644 index 0000000..2eb6eb9 --- /dev/null +++ b/gctrl/src/main/java/Main.java @@ -0,0 +1,92 @@ +import org.apache.log4j.Level; +import org.apache.log4j.Logger; + +// + +//* @author couedrao on 25/11/2019. + +//* @project gctrl + +// + +class Main { + static boolean run = true; + static final Monitor monitor = new Monitor(); + static final Analyze analyze = new Analyze(); + static final Plan plan = new Plan(); + private static final Execute execute = new Execute(); + static final Knowledge shared_knowledge = new Knowledge(); + private static final boolean log = true; + + public static void main(String[] args) throws Exception { + Logger.getRootLogger().setLevel(Level.ERROR); + + + shared_knowledge.start(); + Thread.sleep(3000); + + Thread thread_m = new Thread(() -> { + try { + monitor.start(); + } catch (Exception e) { + e.printStackTrace(); + } + }); + + + Thread thread_a = new Thread(() -> { + try { + analyze.start(); + } catch (Exception e) { + e.printStackTrace(); + } + }); + + Thread thread_p = new Thread(() -> { + try { + plan.start(); + } catch (Exception e) { + e.printStackTrace(); + } + }); + + Thread thread_e = new Thread(() -> { + try { + execute.start(); + } catch (Exception e) { + e.printStackTrace(); + } + }); + + thread_m.start(); + thread_a.start(); + thread_p.start(); + thread_e.start(); + + } + + static void logger(String from, String msg) { + if (log) { + switch (from) { + case "Knowledge": + System.out.println("\u001B[1;31m" + "\t[" + from + "] : \t\t" + msg + "\u001B[0m"); + break; + case "Monitor": + System.out.println("\u001B[1;32m" + "\t[" + from + "] : \t\t" + msg + "\u001B[0m"); + break; + case "Analyze": + System.out.println("\u001B[1;34m" + "\t[" + from + "] : \t\t" + msg + "\u001B[0m"); + break; + case "Plan": + System.out.println("\u001B[1;35m" + "\t[" + from + "] : \t\t\t" + msg + "\u001B[0m"); + break; + case "Execute": + System.out.println("\u001B[1;36m" + "\t[" + from + "] : \t\t" + msg + "\u001B[0m"); + break; + default: + System.out.println("\t[" + from + "] : \t\t" + msg); + } + + } + } +} \ No newline at end of file diff --git a/gctrl/src/main/java/Monitor.java b/gctrl/src/main/java/Monitor.java new file mode 100644 index 0000000..e5fb4d5 --- /dev/null +++ b/gctrl/src/main/java/Monitor.java @@ -0,0 +1,159 @@ +import com.github.signaflo.math.operations.DoubleFunctions; +import com.github.signaflo.timeseries.TimeSeries; +import com.github.signaflo.timeseries.forecast.Forecast; +import com.github.signaflo.timeseries.model.arima.Arima; +import com.github.signaflo.timeseries.model.arima.ArimaOrder; +import de.vandermeer.asciitable.AsciiTable; +import de.vandermeer.asciitable.CWC_LongestWord; +import de.vandermeer.asciithemes.a7.A7_Grids; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; +// + +//* @author couedrao on 25/11/2019. + +//* @project gctrl + +// + +// + + +//* 1)Collects the details from the managed resources e g topology Collects the details from the managed resources e.g. topology information, metrics (e.g. offered capacity and throughput), configuration property settings and so on. + + +//* 2)The monitor function aggregates,correlates and filters these details until it determines a symptom that needs to be analyzed. + + +//* + + +@SuppressWarnings({"SynchronizeOnNonFinalField"}) +class Monitor { + private static List symptom; + private static final int period = 2000; + private static double i = 0; + public String gw_current_SYMP = "N/A"; + + void start() { + Main.logger(this.getClass().getSimpleName(), "Start monitoring of " + Knowledge.gw); + symptom = Main.shared_knowledge.get_symptoms(); + Main.shared_knowledge.create_lat_tab(); + data_collector(); //in bg + symptom_generator(); + } + + //Symptom Generator (can be modified) + private void symptom_generator() { + while (Main.run) + try { + Thread.sleep(period * 5); + ResultSet rs = Main.shared_knowledge.select_from_tab(); + //print_nice_rs(rs); + double[] prediction = predict_next_lat(rs); + boolean isOk = true; + for (int j = 0; j < Knowledge.horizon; j++) { + if (prediction[j] > Knowledge.gw_lat_threshold) { + Main.logger(this.getClass().getSimpleName(), "Symptom --> To Analyse : " + symptom.get(1)); + update_symptom(symptom.get(1)); + isOk = false; + break; + } else if (prediction[j] < .0) { + Main.logger(this.getClass().getSimpleName(), " Symptom --> To Analyse : " + symptom.get(0)); + update_symptom(symptom.get(0)); + isOk = false; + break; + } + } + if (isOk) { + Main.logger(this.getClass().getSimpleName(), "Symptom --> To Analyse : " + symptom.get(2)); + update_symptom(symptom.get(2)); + } + } catch (SQLException | InterruptedException e) { + e.printStackTrace(); + } + } + + //Data Collector TODO : modify + private void data_collector() { + new Thread(() -> { + Main.logger(this.getClass().getSimpleName(), "Filling db with latencies"); + while (Main.run) + try { + //TODO: Remove this + Thread.sleep(period); + Main.shared_knowledge.insert_in_tab(new java.sql.Timestamp(new java.util.Date().getTime()), get_fake_data()); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + + ).start(); + } + + private int get_data() { + //Call Sensors + /*TODO*/ + return 0; + } + + private double get_fake_data() { + //return new Random().nextInt(); + return i += 2.5; + } + + //ARIMA-based Forecasting + private double[] predict_next_lat(ResultSet rs) throws SQLException { + rs.first(); + double[] history = new double[Knowledge.moving_wind]; + double[] p = new double[Knowledge.horizon]; + int j = Knowledge.moving_wind - 1; + while (rs.next()) { + history[j] = Double.parseDouble(rs.getString("latency")); + j--; + } + TimeSeries timeSeries = TimeSeries.from(DoubleFunctions.arrayFrom(history)); + ArimaOrder modelOrder = ArimaOrder.order(0, 1, 1, 0, 1, 1); + //ArimaOrder modelOrder = ArimaOrder.order(0, 0, 0, 1, 1, 1); + Arima model = Arima.model(timeSeries, modelOrder); + Forecast forecast = model.forecast(Knowledge.moving_wind); + System.out.print("Point Estimates : "); + for (int k = 0; k < Knowledge.horizon; k++) { + p[k] = forecast.pointEstimates().at(k); + System.out.print(p[k] + "; "); + } + System.out.println(); + return p; + } + + private void print_nice_rs(ResultSet rs) throws SQLException { + rs.first(); + AsciiTable at = new AsciiTable(); + at.addRule(); + at.addRow("Timestamp", "Latency_in_" + Knowledge.gw); + at.addRule(); + while (rs.next()) { + at.addRow(rs.getTimestamp("id").getTime(), rs.getString("latency")); + at.addRule(); + } + at.getContext().setGrid(A7_Grids.minusBarPlusEquals()); + at.getRenderer().setCWC(new CWC_LongestWord()); + System.out.println(this.getClass().getSimpleName() + " : "); + System.out.println(at.render()); + + } + + private void update_symptom(String symptom) { + + synchronized (gw_current_SYMP) { + gw_current_SYMP.notify(); + gw_current_SYMP = symptom; + + } + } + + +} \ No newline at end of file diff --git a/gctrl/src/main/java/Plan.java b/gctrl/src/main/java/Plan.java new file mode 100644 index 0000000..7655407 --- /dev/null +++ b/gctrl/src/main/java/Plan.java @@ -0,0 +1,87 @@ +import java.util.List; + +// + +// @author couedrao on 25/11/2019. + +//* @project gctrl + +// + +// + +//* 1)Structures the actions needed to achieve goals and objectives Structures the actions needed to achieve goals and objectives. + +//* 2)The plan function creates or selects a procedure to enact a desired alteration in the managed resource. + +//* 3)The plan function can take on many forms, ranging from a single command to a complex workflow. + +//* + +@SuppressWarnings({"SynchronizeOnNonFinalField"}) +class Plan { + private static int i; + public String gw_PLAN = ""; + + void start() { + Main.logger(this.getClass().getSimpleName(), "Start Planning"); + + while (Main.run) { + String current_rfc = get_rfc(); + //Main.logger(this.getClass().getSimpleName(), "Received RFC : " + current_rfc); + update_plan(plan_generator(current_rfc)); + + } + } + + //RFC Receiver + private String get_rfc() { + synchronized (Main.analyze.gw_current_RFC) { + try { + Main.analyze.gw_current_RFC.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + return Main.analyze.gw_current_RFC; + } + + + //Rule-based Plan Generator + private String plan_generator(String rfc) { + List rfcs = Main.shared_knowledge.get_rfc(); + List plans = Main.shared_knowledge.get_plans(); + + if ("YourPlansDoNotWork".contentEquals(rfc)) { + // Thread.sleep(2000); + Main.run = false; + Main.logger(this.getClass().getSimpleName(), "All the Plans were executed without success. \n \t\t The loop will stop!"); + // Terminate JVM + System.exit(0); + } else if (rfc.contentEquals(rfcs.get(0))) { + Main.logger(this.getClass().getSimpleName(), "Plan --> To Execute : " + plans.get(0)); + i = 0; + return plans.get(0); + } else if (rfc.contentEquals(rfcs.get(1))) { + if (i == 0) { + Main.logger(this.getClass().getSimpleName(), "Plan --> To Execute : " + plans.get(1)); + i++; + return plans.get(1); + } else if (i == 1) { + Main.logger(this.getClass().getSimpleName(), "Plan --> To Execute : " + plans.get(2)); + i++; + return plans.get(2); + } + } + return null; + } + + + private void update_plan(String plan) { + synchronized (gw_PLAN) { + gw_PLAN.notify(); + gw_PLAN = plan; + } + } +} + diff --git a/gctrl/src/main/java/SDNCtrlAPI.java b/gctrl/src/main/java/SDNCtrlAPI.java new file mode 100644 index 0000000..c7cfbb2 --- /dev/null +++ b/gctrl/src/main/java/SDNCtrlAPI.java @@ -0,0 +1,34 @@ +import java.util.List; + +/** + * @author couedrao on 27/11/2019. + * @project gctrl + */ +class SDNCtrlAPI { + + String redirect_traffic(String olddestip, String newdestip) { + String status = "OK"; + Main.logger(this.getClass().getSimpleName(), "olddestip = " + olddestip + "; newdestip = " + newdestip); + //TODO + + return status; + } + + String insert_a_loadbalancer(String oldgwip, String lbip, List newgwsip) { + String status = "OK"; + Main.logger(this.getClass().getSimpleName(), "oldgwip = " + oldgwip + "; lbip = " + lbip + "; newgwsip = " + newgwsip); + //TODO + + return status; + } + + String remove_less_important_traffic(String importantsrcip) { + String status = "OK"; + Main.logger(this.getClass().getSimpleName(), "importantsrcip = " + importantsrcip); + //TODO + + return status; + } + + +} diff --git a/gctrl/src/misc/5sdbd_gc.draft.svg b/gctrl/src/misc/5sdbd_gc.draft.svg new file mode 100644 index 0000000..2e3127d --- /dev/null +++ b/gctrl/src/misc/5sdbd_gc.draft.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/gctrl/src/test/java/Tester.java b/gctrl/src/test/java/Tester.java new file mode 100644 index 0000000..10d35cd --- /dev/null +++ b/gctrl/src/test/java/Tester.java @@ -0,0 +1,35 @@ +import java.util.List; +import java.util.Scanner; + +// +//* @author couedrao on 25/11/2019. +//* @project gctrl +// +class Tester { + + private static final Knowledge k = new Knowledge(); + + public static void main(String[] args) throws Exception { + k.start(); + List workflow_lists = k.get_worklow_lists(); + while (Main.run) { + logger("You are in test mode! The following actions can be performed : "); + for (int i = 0; i < workflow_lists.size(); i++) { + logger("[" + i + "] :" + workflow_lists.get(i)); + } + logger("Select any number in [0-" + (workflow_lists.size() - 1) + "] to continue"); + int input = new Scanner(System.in).nextInt(); + if (input < workflow_lists.size() ) + logger("Execution of Action : [" + workflow_lists.get(input) + "]"); + else logger("(-_-)"); + + //Call Effectors + /*TODO : */ + } + } + + private static void logger(String msg) { + if (true) + System.out.println(msg); + } +} diff --git a/gctrl/target/classes/Analyze.class b/gctrl/target/classes/Analyze.class new file mode 100644 index 0000000..2078271 Binary files /dev/null and b/gctrl/target/classes/Analyze.class differ diff --git a/gctrl/target/classes/Execute.class b/gctrl/target/classes/Execute.class new file mode 100644 index 0000000..7440f2f Binary files /dev/null and b/gctrl/target/classes/Execute.class differ diff --git a/gctrl/target/classes/Knowledge.class b/gctrl/target/classes/Knowledge.class new file mode 100644 index 0000000..cfa5553 Binary files /dev/null and b/gctrl/target/classes/Knowledge.class differ diff --git a/gctrl/target/classes/Main.class b/gctrl/target/classes/Main.class new file mode 100644 index 0000000..e5ccb12 Binary files /dev/null and b/gctrl/target/classes/Main.class differ diff --git a/gctrl/target/classes/Monitor.class b/gctrl/target/classes/Monitor.class new file mode 100644 index 0000000..a333a25 Binary files /dev/null and b/gctrl/target/classes/Monitor.class differ diff --git a/gctrl/target/classes/Plan.class b/gctrl/target/classes/Plan.class new file mode 100644 index 0000000..e32fcc0 Binary files /dev/null and b/gctrl/target/classes/Plan.class differ diff --git a/gctrl/target/test-classes/Tester.class b/gctrl/target/test-classes/Tester.class new file mode 100644 index 0000000..1dbca41 Binary files /dev/null and b/gctrl/target/test-classes/Tester.class differ