Add JSON to CLI commands
- Drivers
- Groups
Change-Id: Ib47dc75d9db839329e6cf8fc4439150848f604f5
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/DriverCodecTest.java b/core/common/src/test/java/org/onosproject/codec/impl/DriverCodecTest.java
new file mode 100644
index 0000000..a1c9517
--- /dev/null
+++ b/core/common/src/test/java/org/onosproject/codec/impl/DriverCodecTest.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.codec.impl;
+
+
+import java.util.Map;
+
+import org.junit.Test;
+import org.onosproject.net.driver.Behaviour;
+import org.onosproject.net.driver.DefaultDriver;
+import org.onosproject.net.driver.Driver;
+import org.onosproject.net.driver.TestBehaviour;
+import org.onosproject.net.driver.TestBehaviourImpl;
+import org.onosproject.net.driver.TestBehaviourTwo;
+import org.onosproject.net.driver.TestBehaviourTwoImpl;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableMap;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.onosproject.codec.impl.DriverJsonMatcher.matchesDriver;
+
+/**
+ * Unit tests for the driver codec.
+ */
+public class DriverCodecTest {
+
+ @Test
+ public void codecTest() {
+ Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours =
+ ImmutableMap.of(TestBehaviour.class,
+ TestBehaviourImpl.class,
+ TestBehaviourTwo.class,
+ TestBehaviourTwoImpl.class);
+ Map<String, String> properties =
+ ImmutableMap.of("key1", "value1", "key2", "value2");
+
+ DefaultDriver parent = new DefaultDriver("parent", null, "Acme",
+ "HW1.2.3", "SW1.2.3",
+ behaviours,
+ properties);
+ DefaultDriver child = new DefaultDriver("child", parent, "Acme",
+ "HW1.2.3.1", "SW1.2.3.1",
+ behaviours,
+ properties);
+
+ MockCodecContext context = new MockCodecContext();
+ ObjectNode driverJson = context.codec(Driver.class).encode(child, context);
+
+ assertThat(driverJson, matchesDriver(child));
+ }
+}
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/DriverJsonMatcher.java b/core/common/src/test/java/org/onosproject/codec/impl/DriverJsonMatcher.java
new file mode 100644
index 0000000..6f0070e
--- /dev/null
+++ b/core/common/src/test/java/org/onosproject/codec/impl/DriverJsonMatcher.java
@@ -0,0 +1,118 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.codec.impl;
+
+import java.util.Map;
+
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.net.driver.Driver;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+/**
+ * Hamcrest matcher for drivers.
+ */
+public final class DriverJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+ private final Driver driver;
+
+ private DriverJsonMatcher(Driver driver) {
+ this.driver = driver;
+ }
+
+ @Override
+ public boolean matchesSafely(JsonNode jsonDriver, Description description) {
+ // check id
+ String jsonDriverName = jsonDriver.get("name").asText();
+ String driverName = driver.name();
+ if (!jsonDriverName.equals(driverName)) {
+ description.appendText("name was " + jsonDriverName);
+ return false;
+ }
+
+
+ // check parent
+ String jsonParent = jsonDriver.get("parent").asText();
+ String parent = driver.parent().name();
+ if (!jsonParent.equals(parent)) {
+ description.appendText("parent was " + jsonParent);
+ return false;
+ }
+
+ // check manufacturer
+ String jsonManufacturer = jsonDriver.get("manufacturer").asText();
+ String manufacturer = driver.manufacturer();
+ if (!jsonManufacturer.equals(manufacturer)) {
+ description.appendText("manufacturer was " + jsonManufacturer);
+ return false;
+ }
+
+ // check HW version
+ String jsonHWVersion = jsonDriver.get("hwVersion").asText();
+ String hwVersion = driver.hwVersion();
+ if (!jsonHWVersion.equals(hwVersion)) {
+ description.appendText("HW version was " + jsonHWVersion);
+ return false;
+ }
+
+ // check SW version
+ String jsonSWVersion = jsonDriver.get("swVersion").asText();
+ String swVersion = driver.swVersion();
+ if (!jsonSWVersion.equals(swVersion)) {
+ description.appendText("SW version was " + jsonSWVersion);
+ return false;
+ }
+
+ // Check properties
+ JsonNode jsonProperties = jsonDriver.get("properties");
+ if (driver.properties().size() != jsonProperties.size()) {
+ description.appendText("properties map size was was " + jsonProperties.size());
+ return false;
+ }
+ for (Map.Entry<String, String> entry : driver.properties().entrySet()) {
+ boolean propertyFound = false;
+ for (int propertyIndex = 0; propertyIndex < jsonProperties.size(); propertyIndex++) {
+ String jsonName = jsonProperties.get(propertyIndex).get("name").asText();
+ String jsonValue = jsonProperties.get(propertyIndex).get("value").asText();
+ if (!jsonName.equals(entry.getKey()) ||
+ !jsonValue.equals(entry.getValue())) {
+ propertyFound = true;
+ break;
+ }
+ }
+ if (!propertyFound) {
+ description.appendText("property not found " + entry.getKey());
+ return false;
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(driver.toString());
+ }
+
+ /**
+ * Factory to allocate a driver matcher.
+ *
+ * @param driver driver object we are looking for
+ * @return matcher
+ */
+ public static DriverJsonMatcher matchesDriver(Driver driver) {
+ return new DriverJsonMatcher(driver);
+ }
+}
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/GroupBucketJsonMatcher.java b/core/common/src/test/java/org/onosproject/codec/impl/GroupBucketJsonMatcher.java
new file mode 100644
index 0000000..b305621
--- /dev/null
+++ b/core/common/src/test/java/org/onosproject/codec/impl/GroupBucketJsonMatcher.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.codec.impl;
+
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.net.group.GroupBucket;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+/**
+ * Hamcrest matcher for instructions.
+ */
+public final class GroupBucketJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+ private final GroupBucket bucket;
+
+ private GroupBucketJsonMatcher(GroupBucket bucket) {
+ this.bucket = bucket;
+ }
+
+ /**
+ * Matches the contents of a group bucket.
+ *
+ * @param bucketJson JSON representation of bucket to match
+ * @param description Description object used for recording errors
+ * @return true if contents match, false otherwise
+ */
+ @Override
+ public boolean matchesSafely(JsonNode bucketJson, Description description) {
+
+ // check type
+ final String jsonType = bucketJson.get("type").textValue();
+ if (!bucket.type().name().equals(jsonType)) {
+ description.appendText("type was " + jsonType);
+ return false;
+ }
+
+ final long jsonWeight = bucketJson.get("weight").longValue();
+ if (bucket.weight() != jsonWeight) {
+ description.appendText("weight was " + jsonWeight);
+ return false;
+ }
+
+ final long packetsJson = bucketJson.get("packets").asLong();
+ if (bucket.packets() != packetsJson) {
+ description.appendText("packets was " + packetsJson);
+ return false;
+ }
+
+ final long bytesJson = bucketJson.get("bytes").asLong();
+ if (bucket.bytes() != bytesJson) {
+ description.appendText("bytes was " + packetsJson);
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(bucket.toString());
+ }
+
+ /**
+ * Factory to allocate an bucket matcher.
+ *
+ * @param bucket bucket object we are looking for
+ * @return matcher
+ */
+ public static GroupBucketJsonMatcher matchesGroupBucket(GroupBucket bucket) {
+ return new GroupBucketJsonMatcher(bucket);
+ }
+}
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/GroupCodecTest.java b/core/common/src/test/java/org/onosproject/codec/impl/GroupCodecTest.java
new file mode 100644
index 0000000..409f8eb
--- /dev/null
+++ b/core/common/src/test/java/org/onosproject/codec/impl/GroupCodecTest.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.codec.impl;
+
+import org.junit.Test;
+import org.onosproject.core.DefaultGroupId;
+import org.onosproject.net.NetTestTools;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.group.DefaultGroup;
+import org.onosproject.net.group.DefaultGroupBucket;
+import org.onosproject.net.group.GroupBucket;
+import org.onosproject.net.group.GroupBuckets;
+import org.onosproject.net.group.GroupDescription;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableList;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.onosproject.codec.impl.GroupJsonMatcher.matchesGroup;
+
+/**
+ * Group codec unit tests.
+ */
+
+public class GroupCodecTest {
+
+ @Test
+ public void codecTest() {
+ GroupBucket bucket1 = DefaultGroupBucket
+ .createSelectGroupBucket(DefaultTrafficTreatment.emptyTreatment());
+ GroupBucket bucket2 = DefaultGroupBucket
+ .createIndirectGroupBucket(DefaultTrafficTreatment.emptyTreatment());
+ GroupBuckets buckets = new GroupBuckets(ImmutableList.of(bucket1, bucket2));
+
+
+ DefaultGroup group = new DefaultGroup(
+ new DefaultGroupId(1),
+ NetTestTools.did("d1"),
+ GroupDescription.Type.INDIRECT,
+ buckets);
+
+ MockCodecContext context = new MockCodecContext();
+ GroupCodec codec = new GroupCodec();
+ ObjectNode groupJson = codec.encode(group, context);
+
+ assertThat(groupJson, matchesGroup(group));
+ }
+}
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/GroupJsonMatcher.java b/core/common/src/test/java/org/onosproject/codec/impl/GroupJsonMatcher.java
new file mode 100644
index 0000000..0e62c30
--- /dev/null
+++ b/core/common/src/test/java/org/onosproject/codec/impl/GroupJsonMatcher.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.codec.impl;
+
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.net.group.Group;
+import org.onosproject.net.group.GroupBucket;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+/**
+ * Hamcrest matcher for groups.
+ */
+
+public final class GroupJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+ private final Group group;
+
+ private GroupJsonMatcher(Group group) {
+ this.group = group;
+ }
+
+ @Override
+ public boolean matchesSafely(JsonNode jsonGroup, Description description) {
+ // check id
+ String jsonGroupId = jsonGroup.get("id").asText();
+ String groupId = group.id().toString();
+ if (!jsonGroupId.equals(groupId)) {
+ description.appendText("group id was " + jsonGroupId);
+ return false;
+ }
+
+ // check state
+ String jsonState = jsonGroup.get("state").asText();
+ String state = group.state().toString();
+ if (!jsonState.equals(state)) {
+ description.appendText("state was " + jsonState);
+ return false;
+ }
+
+ // check life
+ long jsonLife = jsonGroup.get("life").asLong();
+ long life = group.life();
+ if (life != jsonLife) {
+ description.appendText("life was " + jsonLife);
+ return false;
+ }
+
+ // check bytes
+ long jsonBytes = jsonGroup.get("bytes").asLong();
+ long bytes = group.bytes();
+ if (bytes != jsonBytes) {
+ description.appendText("bytes was " + jsonBytes);
+ return false;
+ }
+
+ // check packets
+ long jsonPackets = jsonGroup.get("packets").asLong();
+ long packets = group.packets();
+ if (packets != jsonPackets) {
+ description.appendText("packets was " + jsonPackets);
+ return false;
+ }
+
+ // check size of bucket array
+ JsonNode jsonBuckets = jsonGroup.get("buckets");
+ if (jsonBuckets.size() != group.buckets().buckets().size()) {
+ description.appendText("buckets size was " + jsonBuckets.size());
+ return false;
+ }
+
+ // Check buckets
+ for (GroupBucket bucket : group.buckets().buckets()) {
+ boolean bucketFound = false;
+ for (int bucketIndex = 0; bucketIndex < jsonBuckets.size(); bucketIndex++) {
+ GroupBucketJsonMatcher bucketMatcher =
+ GroupBucketJsonMatcher.matchesGroupBucket(bucket);
+ if (bucketMatcher.matches(jsonBuckets.get(bucketIndex))) {
+ bucketFound = true;
+ break;
+ }
+ }
+ if (!bucketFound) {
+ description.appendText("bucket not found " + bucket.toString());
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(group.toString());
+ }
+
+ /**
+ * Factory to allocate a group matcher.
+ *
+ * @param group group object we are looking for
+ * @return matcher
+ */
+ public static GroupJsonMatcher matchesGroup(Group group) {
+ return new GroupJsonMatcher(group);
+ }
+}