bdi-network-resilience

First plan

8/27/2014 7:19:16 PM

Details

diff --git a/network-resilience/src/br/ufrgs/inf/bdinetr/BDINetRApp.java b/network-resilience/src/br/ufrgs/inf/bdinetr/BDINetRApp.java
index ca08a1b..6d153c6 100644
--- a/network-resilience/src/br/ufrgs/inf/bdinetr/BDINetRApp.java
+++ b/network-resilience/src/br/ufrgs/inf/bdinetr/BDINetRApp.java
@@ -40,10 +40,12 @@ import org.apache.commons.logging.LogFactory;
 import org.apache.log4j.PropertyConfigurator;
 
 import bdi4jade.core.AbstractBDIAgent;
+import bdi4jade.core.Capability;
 import bdi4jade.examples.BDI4JADEExamplesPanel;
 import br.ufrgs.inf.bdinetr.capability.LinkMonitorCapability;
 import br.ufrgs.inf.bdinetr.capability.RateLimiterCapability;
 import br.ufrgs.inf.bdinetr.domain.Device;
+import br.ufrgs.inf.bdinetr.domain.IpAddress;
 import br.ufrgs.inf.bdinetr.domain.Link;
 import br.ufrgs.inf.bdinetr.domain.Network;
 
@@ -58,7 +60,8 @@ public class BDINetRApp {
 			Random random = new Random(System.currentTimeMillis());
 			log.info("Updating link usage");
 			for (Link link : NETWORK.getLinks()) {
-				link.setUsedBandwidth(random.nextDouble() * link.getBandwidth());
+				link.setUsedBandwidth(random.nextDouble()
+						* link.getActualBandwidth());
 			}
 			log.info("Restarting agents");
 			for (AbstractBDIAgent agent : AGENTS.values()) {
@@ -67,7 +70,7 @@ public class BDINetRApp {
 		}
 	}
 
-	private static final Map<String, AbstractBDIAgent> AGENTS;
+	private static final Map<IpAddress, AbstractBDIAgent> AGENTS;
 
 	private static final Network NETWORK;
 
@@ -76,35 +79,38 @@ public class BDINetRApp {
 				.getResource("log4j.properties"));
 
 		NETWORK = new Network();
-		Device firewall1 = new Device("Firewall 1");
+		Device firewall1 = new Device(new IpAddress("Firewall 1"));
 		NETWORK.addDevice(firewall1);
-		Device firewall2 = new Device("Firewall 2");
+		/*Device firewall2 = new Device(new IpAddress("Firewall 2"));
 		NETWORK.addDevice(firewall2);
-		Device firewall3 = new Device("Firewall 3");
-		NETWORK.addDevice(firewall3);
-		Device rateLimiter1 = new Device("Rate Limiter 1");
+		Device firewall3 = new Device(new IpAddress("Firewall 3"));
+		NETWORK.addDevice(firewall3);*/
+		Device rateLimiter1 = new Device(new IpAddress("Rate Limiter 1"));
 		NETWORK.addDevice(rateLimiter1);
-		Device rateLimiter2 = new Device("Rate Limiter 2");
-		NETWORK.addDevice(rateLimiter2);
+		/*Device rateLimiter2 = new Device(new IpAddress("Rate Limiter 2"));
+		NETWORK.addDevice(rateLimiter2);*/
 
 		NETWORK.addLink(new Link("F1_RL1", 10.0, firewall1, rateLimiter1));
-		NETWORK.addLink(new Link("F2_RL2", 8.0, firewall2, rateLimiter2));
+		/*NETWORK.addLink(new Link("F2_RL2", 8.0, firewall2, rateLimiter2));
 		NETWORK.addLink(new Link("F3_RL1", 7.0, firewall3, rateLimiter1));
 		NETWORK.addLink(new Link("F1_RL2", 7.0, firewall1, rateLimiter2));
 		NETWORK.addLink(new Link("F2_RL1", 8.0, firewall2, rateLimiter1));
-		NETWORK.addLink(new Link("F3_RL2", 10.0, firewall3, rateLimiter2));
+		NETWORK.addLink(new Link("F3_RL2", 10.0, firewall3, rateLimiter2));*/
 
 		AGENTS = new HashMap<>();
-		AGENTS.put(firewall1.getId(), new BDINetRAgent(firewall1,
-				new LinkMonitorCapability()));
-		AGENTS.put(firewall2.getId(), new BDINetRAgent(firewall2,
-				new LinkMonitorCapability()));
-		AGENTS.put(firewall3.getId(), new BDINetRAgent(firewall3,
-				new LinkMonitorCapability()));
-		AGENTS.put(rateLimiter1.getId(), new BDINetRAgent(rateLimiter1,
-				new RateLimiterCapability()));
-		AGENTS.put(rateLimiter2.getId(), new BDINetRAgent(rateLimiter2,
+		AGENTS.put(firewall1.getIp(), new BDINetRAgent(firewall1,
+				new Capability[] { new LinkMonitorCapability(),
+						new RateLimiterCapability() }));
+		/*AGENTS.put(firewall2.getIp(), new BDINetRAgent(firewall2,
+				new Capability[] { new LinkMonitorCapability(),
+						new RateLimiterCapability() }));
+		AGENTS.put(firewall3.getIp(), new BDINetRAgent(firewall3,
+				new Capability[] { new LinkMonitorCapability(),
+						new RateLimiterCapability() }));*/
+		AGENTS.put(rateLimiter1.getIp(), new BDINetRAgent(rateLimiter1,
 				new RateLimiterCapability()));
+		/*AGENTS.put(rateLimiter2.getIp(), new BDINetRAgent(rateLimiter2,
+				new RateLimiterCapability()));*/
 
 	}
 
@@ -134,10 +140,11 @@ public class BDINetRApp {
 		PlatformController controller = runtime
 				.createMainContainer(bootProfile);
 
-		for (String agentName : AGENTS.keySet()) {
+		for (IpAddress agentName : AGENTS.keySet()) {
 			try {
 				AgentController ac = ((AgentContainer) controller)
-						.acceptNewAgent(agentName, AGENTS.get(agentName));
+						.acceptNewAgent(agentName.toString(),
+								AGENTS.get(agentName));
 				ac.start();
 			} catch (Exception e) {
 				log.error(e);
diff --git a/network-resilience/src/br/ufrgs/inf/bdinetr/capability/LinkMonitorCapability.java b/network-resilience/src/br/ufrgs/inf/bdinetr/capability/LinkMonitorCapability.java
index 7370a56..1f68f0d 100644
--- a/network-resilience/src/br/ufrgs/inf/bdinetr/capability/LinkMonitorCapability.java
+++ b/network-resilience/src/br/ufrgs/inf/bdinetr/capability/LinkMonitorCapability.java
@@ -57,45 +57,27 @@ public class LinkMonitorCapability extends Capability {
 			for (Belief<?, ?> belief : overUsageBeliefs) {
 				PropositionalBelief<OverUsage> overUsage = (PropositionalBelief<OverUsage>) belief;
 				if (overUsage.getValue()) {
-					getMyAgent().addGoal(
-							LinkMonitorCapability.this,
-							new PropositionalBeliefValueGoal<AttackPrevented>(
-									new AttackPrevented(overUsage.getName()
-											.getLink()), Boolean.TRUE));
-					log.debug("goal(attackPrevented("
-							+ overUsage.getName().getLink() + "))");
-					getMyAgent().addGoal(
-							LinkMonitorCapability.this,
-							new BeliefGoal<RegularUsage>(new RegularUsage(
-									overUsage.getName().getLink())));
-					log.debug("goal(?regularUsage("
-							+ overUsage.getName().getLink() + "))");
-				}
-			}
-
-			Set<Belief<?, ?>> fullyOperationalBeliefs = getBeliefBase()
-					.getBeliefsByType(FullyOperational.class);
-			for (Belief<?, ?> belief : fullyOperationalBeliefs) {
-				PropositionalBelief<FullyOperational> fullyOperational = (PropositionalBelief<FullyOperational>) belief;
-				if (!fullyOperational.getValue()) {
-					PropositionalBelief<RegularUsage> regularUsage = (PropositionalBelief<RegularUsage>) getBeliefBase()
+					PropositionalBelief<AttackPrevented> attackPrevented = (PropositionalBelief<AttackPrevented>) getBeliefBase()
 							.getBelief(
-									new RegularUsage(fullyOperational.getName()
+									new AttackPrevented(overUsage.getName()
 											.getLink()));
-					if (regularUsage != null && regularUsage.getValue()) {
+					if (attackPrevented == null || !attackPrevented.getValue()) {
 						getMyAgent()
 								.addGoal(
 										LinkMonitorCapability.this,
-										new PropositionalBeliefValueGoal<FullyOperational>(
-												new FullyOperational(
-														fullyOperational
-																.getName()
-																.getLink()),
+										new PropositionalBeliefValueGoal<AttackPrevented>(
+												new AttackPrevented(overUsage
+														.getName().getLink()),
 												Boolean.TRUE));
-						log.debug("goal(fullyOperational("
-								+ fullyOperational.getName().getLink() + "))");
+						log.debug("goal(attackPrevented("
+								+ overUsage.getName().getLink() + "))");
+						getMyAgent().addGoal(
+								LinkMonitorCapability.this,
+								new BeliefGoal<RegularUsage>(new RegularUsage(
+										overUsage.getName().getLink())));
+						log.debug("goal(?regularUsage("
+								+ overUsage.getName().getLink() + "))");
 					}
-
 				}
 			}
 		}
@@ -110,17 +92,23 @@ public class LinkMonitorCapability extends Capability {
 						.getUsedBandwidthPercentage();
 				linkUsage.setValue(percentageUsed);
 				if (percentageUsed > overUsageThreshold.getValue()) {
-					getBeliefBase()
-							.addOrUpdateBelief(
-									new TransientPropositionalBelief<OverUsage>(
-											new OverUsage(linkUsage.getName()
-													.getLink()), Boolean.TRUE));
-					log.debug("belief(overUsage("
-							+ linkUsage.getName().getLink() + "))");
-					getBeliefBase().removeBelief(
-							new RegularUsage(linkUsage.getName().getLink()));
-					log.debug("belief(~regularUsage("
-							+ linkUsage.getName().getLink() + "))");
+					PropositionalBelief<OverUsage> overUsage = (PropositionalBelief<OverUsage>) getBeliefBase()
+							.getBelief(
+									new OverUsage(linkUsage.getName().getLink()));
+					if (overUsage == null || !overUsage.getValue()) {
+						getBeliefBase().addOrUpdateBelief(
+								new TransientPropositionalBelief<OverUsage>(
+										new OverUsage(linkUsage.getName()
+												.getLink()), Boolean.TRUE));
+						log.debug("belief(overUsage("
+								+ linkUsage.getName().getLink() + "))");
+						getBeliefBase()
+								.removeBelief(
+										new RegularUsage(linkUsage.getName()
+												.getLink()));
+						log.debug("belief(~regularUsage("
+								+ linkUsage.getName().getLink() + "))");
+					}
 				} else {
 					getBeliefBase()
 							.addOrUpdateBelief(
diff --git a/network-resilience/src/br/ufrgs/inf/bdinetr/capability/RateLimiterCapability.java b/network-resilience/src/br/ufrgs/inf/bdinetr/capability/RateLimiterCapability.java
index d1e5822..396e218 100644
--- a/network-resilience/src/br/ufrgs/inf/bdinetr/capability/RateLimiterCapability.java
+++ b/network-resilience/src/br/ufrgs/inf/bdinetr/capability/RateLimiterCapability.java
@@ -21,17 +21,102 @@
 //----------------------------------------------------------------------------
 package br.ufrgs.inf.bdinetr.capability;
 
+import java.util.Set;
+
+import bdi4jade.annotation.Parameter;
+import bdi4jade.annotation.Parameter.Direction;
+import bdi4jade.belief.Belief;
+import bdi4jade.belief.PropositionalBelief;
+import bdi4jade.belief.TransientPropositionalBelief;
 import bdi4jade.core.Capability;
+import bdi4jade.core.GoalUpdateSet;
+import bdi4jade.goal.GoalTemplateFactory;
+import bdi4jade.goal.PropositionalBeliefValueGoal;
+import bdi4jade.plan.DefaultPlan;
+import bdi4jade.plan.Plan;
+import bdi4jade.plan.planbody.BeliefGoalPlanBody;
+import bdi4jade.reasoning.AbstractReasoningStrategy;
+import bdi4jade.reasoning.OptionGenerationFunction;
+import br.ufrgs.inf.bdinetr.domain.Link;
+import br.ufrgs.inf.bdinetr.domain.LinkProposition.AttackPrevented;
+import br.ufrgs.inf.bdinetr.domain.LinkProposition.FullyOperational;
+import br.ufrgs.inf.bdinetr.domain.LinkProposition.RegularUsage;
 
 /**
  * @author Ingrid Nunes
  */
 public class RateLimiterCapability extends Capability {
 
+	private class ReasoningStrategy extends AbstractReasoningStrategy implements
+			OptionGenerationFunction {
+		@Override
+		public void generateGoals(GoalUpdateSet goalUpdateSet) {
+			Set<Belief<?, ?>> fullyOperationalBeliefs = getBeliefBase()
+					.getBeliefsByType(FullyOperational.class);
+			for (Belief<?, ?> belief : fullyOperationalBeliefs) {
+				PropositionalBelief<FullyOperational> fullyOperational = (PropositionalBelief<FullyOperational>) belief;
+				if (!fullyOperational.getValue()) {
+					PropositionalBelief<RegularUsage> regularUsage = (PropositionalBelief<RegularUsage>) getBeliefBase()
+							.getBelief(
+									new RegularUsage(fullyOperational.getName()
+											.getLink()));
+					if (regularUsage != null && regularUsage.getValue()) {
+						getMyAgent()
+								.addGoal(
+										RateLimiterCapability.this,
+										new PropositionalBeliefValueGoal<FullyOperational>(
+												new FullyOperational(
+														fullyOperational
+																.getName()
+																.getLink()),
+												Boolean.TRUE));
+						log.debug("goal(fullyOperational("
+								+ fullyOperational.getName().getLink() + "))");
+					}
+
+				}
+			}
+		}
+	}
+
+	public static final double LINK_LIMIT_RATE = 0.5;
+
 	private static final long serialVersionUID = -1705728861020677126L;
-	
-	public static final String LINK_USAGE = "linkUsage";
-	public static final String LINK_OVER_USAGE = "overUsage";
-	public static final String LINK_REGULAR_USAGE = "regularUsage";
+
+	public static class LimitLinkRatePlan extends BeliefGoalPlanBody {
+		private static final long serialVersionUID = -3493377510830902961L;
+
+		private Link link;
+
+		@Override
+		public void execute() {
+			link.setLimitedBandwidth(LINK_LIMIT_RATE * link.getBandwidth());
+			getBeliefBase().addOrUpdateBelief(
+					new TransientPropositionalBelief<FullyOperational>(
+							new FullyOperational(link), Boolean.FALSE));
+			getCapability()
+					.getWholeCapability()
+					.getBeliefBase()
+					.addOrUpdateBelief(
+							new TransientPropositionalBelief<AttackPrevented>(
+									new AttackPrevented(link), Boolean.TRUE));
+			log.info(getGoal());
+		}
+
+		@Parameter(direction = Direction.IN)
+		public void setBeliefName(AttackPrevented attackPrevented) {
+			this.link = attackPrevented.getLink();
+		}
+	}
+
+	@bdi4jade.annotation.Plan
+	private Plan limitLinkRate = new DefaultPlan(
+			GoalTemplateFactory.beliefValueGoal(AttackPrevented.class,
+					Boolean.TRUE), LimitLinkRatePlan.class);
+
+	public RateLimiterCapability() {
+		ReasoningStrategy strategy = new ReasoningStrategy();
+		setOptionGenerationFunction(strategy);
+	}
 
 }
diff --git a/network-resilience/src/br/ufrgs/inf/bdinetr/domain/Device.java b/network-resilience/src/br/ufrgs/inf/bdinetr/domain/Device.java
index 5e18984..e9f3c1a 100644
--- a/network-resilience/src/br/ufrgs/inf/bdinetr/domain/Device.java
+++ b/network-resilience/src/br/ufrgs/inf/bdinetr/domain/Device.java
@@ -30,10 +30,10 @@ import java.util.Set;
 public class Device {
 
 	private final Set<Link> connectedLinks;
-	private final String id;
+	private final IpAddress ip;
 
-	public Device(final String id) {
-		this.id = id;
+	public Device(final IpAddress id) {
+		this.ip = id;
 		this.connectedLinks = new HashSet<>();
 	}
 
@@ -49,7 +49,7 @@ public class Device {
 	public boolean equals(Object obj) {
 		if (obj instanceof Device) {
 			Device d = (Device) obj;
-			return this.id.equals(d.id);
+			return this.ip.equals(d.ip);
 		}
 		return false;
 	}
@@ -58,18 +58,18 @@ public class Device {
 		return connectedLinks;
 	}
 
-	public String getId() {
-		return id;
+	public IpAddress getIp() {
+		return ip;
 	}
 
 	@Override
 	public int hashCode() {
-		return id == null ? 0 : id.hashCode();
+		return ip == null ? 0 : ip.hashCode();
 	}
 
 	@Override
 	public String toString() {
-		return id;
+		return ip.toString();
 	}
 
 }
diff --git a/network-resilience/src/br/ufrgs/inf/bdinetr/domain/Flow.java b/network-resilience/src/br/ufrgs/inf/bdinetr/domain/Flow.java
index 0853dbf..ed7dcd0 100644
--- a/network-resilience/src/br/ufrgs/inf/bdinetr/domain/Flow.java
+++ b/network-resilience/src/br/ufrgs/inf/bdinetr/domain/Flow.java
@@ -26,10 +26,32 @@ package br.ufrgs.inf.bdinetr.domain;
  */
 public class Flow {
 
-	private final String id;
+	private static long id_counter = 0;
 
-	public Flow(String id) {
+	private IpAddress dstIp;
+	private int dstPort;
+	private final Long id;
+	private String protocol;
+	private IpAddress srcIp;
+	private int srcPort;
+
+	public Flow(IpAddress srcIp, int srcPort, IpAddress dstIp, int dstPort,
+			String protocol) {
+		this(++id_counter);
+	}
+
+	public Flow(Long id) {
+		this.id = id;
+	}
+
+	public Flow(Long id, IpAddress srcIp, int srcPort, IpAddress dstIp,
+			int dstPort, String protocol) {
 		this.id = id;
+		this.srcIp = srcIp;
+		this.srcPort = srcPort;
+		this.dstIp = dstIp;
+		this.dstPort = dstPort;
+		this.protocol = protocol;
 	}
 
 	@Override
@@ -41,14 +63,58 @@ public class Flow {
 		return false;
 	}
 
+	public IpAddress getDstIp() {
+		return dstIp;
+	}
+
+	public int getDstPort() {
+		return dstPort;
+	}
+
+	public Long getId() {
+		return id;
+	}
+
+	public String getProtocol() {
+		return protocol;
+	}
+
+	public IpAddress getSrcIp() {
+		return srcIp;
+	}
+
+	public int getSrcPort() {
+		return srcPort;
+	}
+
 	@Override
 	public int hashCode() {
 		return id == null ? 0 : id.hashCode();
 	}
 
+	public void setDstIp(IpAddress dstIp) {
+		this.dstIp = dstIp;
+	}
+
+	public void setDstPort(int dstPort) {
+		this.dstPort = dstPort;
+	}
+
+	public void setProtocol(String protocol) {
+		this.protocol = protocol;
+	}
+
+	public void setSrcIp(IpAddress srcIp) {
+		this.srcIp = srcIp;
+	}
+
+	public void setSrcPort(int srcPort) {
+		this.srcPort = srcPort;
+	}
+
 	@Override
 	public String toString() {
-		return id;
+		return id.toString();
 	}
 
 }
diff --git a/network-resilience/src/br/ufrgs/inf/bdinetr/domain/Link.java b/network-resilience/src/br/ufrgs/inf/bdinetr/domain/Link.java
index b1807f6..53fcef6 100644
--- a/network-resilience/src/br/ufrgs/inf/bdinetr/domain/Link.java
+++ b/network-resilience/src/br/ufrgs/inf/bdinetr/domain/Link.java
@@ -28,6 +28,7 @@ public class Link {
 
 	private Double bandwidth;
 	private final String id;
+	private Double limitedBandwidth;
 	private Device source;
 	private Device target;
 	private Double usedBandwidth;
@@ -39,6 +40,7 @@ public class Link {
 	public Link(String id, Double bandwidth, Device source, Device target) {
 		this(id);
 		this.bandwidth = bandwidth;
+		this.limitedBandwidth = null;
 		this.usedBandwidth = 0.0;
 		setSource(source);
 		setTarget(target);
@@ -53,10 +55,18 @@ public class Link {
 		return false;
 	}
 
+	public Double getActualBandwidth() {
+		return limitedBandwidth == null ? bandwidth : limitedBandwidth;
+	}
+
 	public Double getBandwidth() {
 		return bandwidth;
 	}
 
+	public Double getLimitedBandwidth() {
+		return limitedBandwidth;
+	}
+
 	public Device getSource() {
 		return source;
 	}
@@ -82,6 +92,12 @@ public class Link {
 		this.bandwidth = bandwidth;
 	}
 
+	public void setLimitedBandwidth(Double limitedBandwidth) {
+		if (limitedBandwidth > bandwidth)
+			return;
+		this.limitedBandwidth = limitedBandwidth;
+	}
+
 	public void setSource(Device source) {
 		if (this.source != null) {
 			this.source.disconnectLink(this);