m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2019-12-27 21:08:44 +0100
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2019-12-27 21:08:44 +0100
commit5f7e37d7b26832b3b512f9dda310cb9bc92c93fb (patch)
tree0886802b0b472f466697b4fc05c72e3d1d87b2c2 /src/main/java/pl/edu/mimuw/cloudatlas/agent/modules
parent614e4e0d4fd07967f928fac122cc36b66a513944 (diff)
Create new zones with UpdateAttributes message
Diffstat (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/modules')
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java
index b8db08a..a457a94 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java
@@ -1,9 +1,17 @@
package pl.edu.mimuw.cloudatlas.agent.modules;
+import java.util.Map.Entry;
+
import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.GetHierarchyMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.HierarchyMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.StanikMessage;
+import pl.edu.mimuw.cloudatlas.agent.messages.UpdateAttributesMessage;
+import pl.edu.mimuw.cloudatlas.model.AttributesMap;
+import pl.edu.mimuw.cloudatlas.model.Attribute;
+import pl.edu.mimuw.cloudatlas.model.PathName;
+import pl.edu.mimuw.cloudatlas.model.Value;
+import pl.edu.mimuw.cloudatlas.model.ValueString;
import pl.edu.mimuw.cloudatlas.model.ZMI;
public class Stanik extends Module {
@@ -19,6 +27,9 @@ public class Stanik extends Module {
case GET_HIERARCHY:
handleGetHierarchy((GetHierarchyMessage) message);
break;
+ case UPDATE_ATTRIBUTES:
+ handleUpdateAttributes((UpdateAttributesMessage) message);
+ break;
default:
throw new InvalidMessageType("This type of message cannot be handled by Stanik");
}
@@ -28,4 +39,41 @@ public class Stanik extends Module {
HierarchyMessage response = new HierarchyMessage("", message.getRequestingModule(), 0, message.getRequestId(), hierarchy.clone());
sendMessage(response);
}
+
+ public void handleUpdateAttributes(UpdateAttributesMessage message) {
+ try {
+ addMissingZones(new PathName(message.getPathName()));
+ ZMI zone = hierarchy.findDescendant(message.getPathName());
+ for (Entry<Attribute, Value> entry : zone.getAttributes()) {
+ Attribute attribute = entry.getKey();
+ Value newValue = message.getAttributes().getOrNull(attribute);
+ if (newValue == null) {
+ zone.getAttributes().remove(attribute);
+ }
+ }
+ for (Entry<Attribute, Value> entry : message.getAttributes()) {
+ zone.getAttributes().addOrChange(entry.getKey(), entry.getValue());
+ }
+ } catch (ZMI.NoSuchZoneException e) {
+ System.out.println("ERROR: zone should exist after being added");
+ }
+ }
+
+ private void addMissingZones(PathName path) {
+ try {
+ if (!hierarchy.descendantExists(path)) {
+ addMissingZones(path.levelUp());
+ ZMI parent = hierarchy.findDescendant(path.levelUp());
+ ZMI newSon = new ZMI(parent);
+ newSon.getAttributes().add("name", new ValueString(path.getSingletonName()));
+ parent.addSon(newSon);
+ }
+ } catch (ZMI.NoSuchZoneException e) {
+ System.out.println("ERROR: zone should exist after being added");
+ }
+ }
+
+ public ZMI getHierarchy() {
+ return hierarchy;
+ }
}