From eb7fbbd08bb003a47d9788a9f6476695ab46b271 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Tue, 31 Dec 2019 17:31:38 +0100 Subject: Add message to set a single attribute --- .../agent/messages/SetAttributeMessage.java | 37 ++++++++++++++ .../cloudatlas/agent/messages/StanikMessage.java | 1 + .../edu/mimuw/cloudatlas/agent/modules/Stanik.java | 25 ++++++++++ .../mimuw/cloudatlas/agent/modules/StanikTest.java | 58 +++++++++++++++++++++- 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/SetAttributeMessage.java (limited to 'src') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/SetAttributeMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/SetAttributeMessage.java new file mode 100644 index 0000000..4888484 --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/SetAttributeMessage.java @@ -0,0 +1,37 @@ +package pl.edu.mimuw.cloudatlas.agent.messages; + +import pl.edu.mimuw.cloudatlas.agent.modules.ModuleType; +import pl.edu.mimuw.cloudatlas.model.Attribute; +import pl.edu.mimuw.cloudatlas.model.Value; +import pl.edu.mimuw.cloudatlas.model.ValueTime; + +public class SetAttributeMessage extends StanikMessage { + private String pathName; + private Attribute attribute; + private Value value; + private ValueTime updateTimestamp; + + public SetAttributeMessage(String messageId, long timestamp, String pathName, Attribute attribute, Value value, ValueTime updateTimestamp) { + super(messageId, timestamp, Type.SET_ATTRIBUTE); + this.pathName = pathName; + this.attribute = attribute; + this.value = value; + this.updateTimestamp = updateTimestamp; + } + + public String getPathName() { + return pathName; + } + + public Attribute getAttribute() { + return attribute; + } + + public Value getValue() { + return value; + } + + public ValueTime getUpdateTimestamp() { + return updateTimestamp; + } +} diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StanikMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StanikMessage.java index d86de4a..844f31c 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StanikMessage.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StanikMessage.java @@ -7,6 +7,7 @@ public abstract class StanikMessage extends AgentMessage { public enum Type { GET_STATE, REMOVE_ZMI, + SET_ATTRIBUTE, UPDATE_ATTRIBUTES, UPDATE_QUERIES } 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 b030901..3e5b790 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 @@ -6,6 +6,7 @@ import java.util.Map.Entry; import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; import pl.edu.mimuw.cloudatlas.agent.messages.GetStateMessage; import pl.edu.mimuw.cloudatlas.agent.messages.RemoveZMIMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.SetAttributeMessage; import pl.edu.mimuw.cloudatlas.agent.messages.StateMessage; import pl.edu.mimuw.cloudatlas.agent.messages.StanikMessage; import pl.edu.mimuw.cloudatlas.agent.messages.UpdateAttributesMessage; @@ -49,6 +50,9 @@ public class Stanik extends Module { case REMOVE_ZMI: handleRemoveZMI((RemoveZMIMessage) message); break; + case SET_ATTRIBUTE: + handleSetAttribte((SetAttributeMessage) message); + break; case UPDATE_ATTRIBUTES: handleUpdateAttributes((UpdateAttributesMessage) message); break; @@ -85,6 +89,27 @@ public class Stanik extends Module { } } + /* + * Always adds the new attribute. + * The zone must already exist. + * The zone's timestamp will be the maximum of its current timestamp or the + * timestamp provided with the new value. + */ + public void handleSetAttribte(SetAttributeMessage message) { + try { + ZMI zmi = hierarchy.findDescendant(new PathName(message.getPathName())); + ValueTime updateTimestamp = message.getUpdateTimestamp(); + ValueTime currentTimestamp = (ValueTime) zmi.getAttributes().getOrNull("timestamp"); + if (ValueUtils.valueLower(currentTimestamp, updateTimestamp)) { + zmi.getAttributes().addOrChange("timestamp", updateTimestamp); + } + + zmi.getAttributes().addOrChange(message.getAttribute(), message.getValue()); + } catch (ZMI.NoSuchZoneException e) { + System.out.println("DEBUG: trying to set attribute in zone that doesn't exist"); + } + } + public void handleUpdateAttributes(UpdateAttributesMessage message) { try { validateUpdateAttributesMessage(message); diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java index 495a012..2e1ccea 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java @@ -7,9 +7,10 @@ import java.util.Map.Entry; import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; import pl.edu.mimuw.cloudatlas.agent.messages.GetStateMessage; -import pl.edu.mimuw.cloudatlas.agent.messages.StateMessage; import pl.edu.mimuw.cloudatlas.agent.messages.RemoveZMIMessage; import pl.edu.mimuw.cloudatlas.agent.messages.ResponseMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.SetAttributeMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.StateMessage; import pl.edu.mimuw.cloudatlas.agent.messages.UpdateAttributesMessage; import pl.edu.mimuw.cloudatlas.agent.messages.UpdateQueriesMessage; import pl.edu.mimuw.cloudatlas.agent.MockExecutor; @@ -236,4 +237,59 @@ public class StanikTest { stanik.getHierarchy().findDescendant("/new"); } + + @Test + public void setOldAttribute() throws Exception { + AttributesMap attributes = new AttributesMap(); + attributes.add("foo", new ValueInt(1337l)); + attributes.add("name", new ValueString("new")); + attributes.add("timestamp", new ValueTime(42l)); + UpdateAttributesMessage message = new UpdateAttributesMessage("test_msg", 0, "/new", attributes); + stanik.handleTyped(message); + + SetAttributeMessage setMessage = new SetAttributeMessage("test_msg2", 0, "/new", new Attribute("foo"), new ValueInt(43l), new ValueTime(40l)); + stanik.handleTyped(setMessage); + + AttributesMap actualAttributes = stanik.getHierarchy().findDescendant("/new").getAttributes(); + assertEquals(3, TestUtil.iterableSize(actualAttributes)); + assertEquals(new ValueInt(43l), actualAttributes.getOrNull("foo")); + assertEquals(new ValueTime(42l), actualAttributes.getOrNull("timestamp")); + } + + @Test + public void setOldAttribute2() throws Exception { + AttributesMap attributes = new AttributesMap(); + attributes.add("foo", new ValueInt(1337l)); + attributes.add("name", new ValueString("new")); + attributes.add("timestamp", new ValueTime(42l)); + UpdateAttributesMessage message = new UpdateAttributesMessage("test_msg", 0, "/new", attributes); + stanik.handleTyped(message); + + SetAttributeMessage setMessage = new SetAttributeMessage("test_msg2", 0, "/new", new Attribute("foo"), new ValueInt(43l), new ValueTime(43l)); + stanik.handleTyped(setMessage); + + AttributesMap actualAttributes = stanik.getHierarchy().findDescendant("/new").getAttributes(); + assertEquals(3, TestUtil.iterableSize(actualAttributes)); + assertEquals(new ValueInt(43l), actualAttributes.getOrNull("foo")); + assertEquals(new ValueTime(43l), actualAttributes.getOrNull("timestamp")); + } + + @Test + public void setNewAttribute() throws Exception { + AttributesMap attributes = new AttributesMap(); + attributes.add("foo", new ValueInt(1337l)); + attributes.add("name", new ValueString("new")); + attributes.add("timestamp", new ValueTime(42l)); + UpdateAttributesMessage message = new UpdateAttributesMessage("test_msg", 0, "/new", attributes); + stanik.handleTyped(message); + + SetAttributeMessage setMessage = new SetAttributeMessage("test_msg2", 0, "/new", new Attribute("bar"), new ValueInt(43l), new ValueTime(43l)); + stanik.handleTyped(setMessage); + + AttributesMap actualAttributes = stanik.getHierarchy().findDescendant("/new").getAttributes(); + assertEquals(4, TestUtil.iterableSize(actualAttributes)); + assertEquals(new ValueInt(1337l), actualAttributes.getOrNull("foo")); + assertEquals(new ValueInt(43l), actualAttributes.getOrNull("bar")); + assertEquals(new ValueTime(43l), actualAttributes.getOrNull("timestamp")); + } } -- cgit v1.2.3