Committed by
Gerrit Code Review
Add JSON to CLI commands
- Drivers - Groups Change-Id: Ib47dc75d9db839329e6cf8fc4439150848f604f5
Showing
11 changed files
with
728 additions
and
10 deletions
| ... | @@ -15,12 +15,16 @@ | ... | @@ -15,12 +15,16 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.cli.net; | 16 | package org.onosproject.cli.net; |
| 17 | 17 | ||
| 18 | +import java.util.Set; | ||
| 19 | + | ||
| 18 | import org.apache.karaf.shell.commands.Argument; | 20 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; | 21 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onosproject.cli.AbstractShellCommand; | 22 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.net.driver.Driver; | 23 | import org.onosproject.net.driver.Driver; |
| 22 | import org.onosproject.net.driver.DriverAdminService; | 24 | import org.onosproject.net.driver.DriverAdminService; |
| 23 | 25 | ||
| 26 | +import com.fasterxml.jackson.databind.node.ArrayNode; | ||
| 27 | + | ||
| 24 | /** | 28 | /** |
| 25 | * Lists device drivers. | 29 | * Lists device drivers. |
| 26 | */ | 30 | */ |
| ... | @@ -43,17 +47,35 @@ public class DriversListCommand extends AbstractShellCommand { | ... | @@ -43,17 +47,35 @@ public class DriversListCommand extends AbstractShellCommand { |
| 43 | if (driverName != null) { | 47 | if (driverName != null) { |
| 44 | printDriver(service.getDriver(driverName)); | 48 | printDriver(service.getDriver(driverName)); |
| 45 | } else { | 49 | } else { |
| 46 | - service.getDrivers().forEach(this::printDriver); | 50 | + if (outputJson()) { |
| 51 | + json(service.getDrivers()); | ||
| 52 | + } else { | ||
| 53 | + service.getDrivers().forEach(this::printDriver); | ||
| 54 | + } | ||
| 47 | } | 55 | } |
| 48 | } | 56 | } |
| 49 | 57 | ||
| 58 | + private void json(Driver driver) { | ||
| 59 | + print("%s", jsonForEntity(driver, Driver.class)); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + private void json(Set<Driver> drivers) { | ||
| 63 | + ArrayNode result = mapper().createArrayNode(); | ||
| 64 | + drivers.forEach(driver -> result.add(jsonForEntity(driver, Driver.class))); | ||
| 65 | + print("%s", result.toString()); | ||
| 66 | + } | ||
| 67 | + | ||
| 50 | private void printDriver(Driver driver) { | 68 | private void printDriver(Driver driver) { |
| 51 | - Driver parent = driver.parent(); | 69 | + if (outputJson()) { |
| 52 | - print(FMT, driver.name(), parent != null ? parent.name() : "none", | 70 | + json(driver); |
| 53 | - driver.manufacturer(), driver.hwVersion(), driver.swVersion()); | 71 | + } else { |
| 54 | - driver.behaviours().forEach(b -> print(FMT_B, b.getCanonicalName(), | 72 | + Driver parent = driver.parent(); |
| 55 | - driver.implementation(b).getCanonicalName())); | 73 | + print(FMT, driver.name(), parent != null ? parent.name() : "none", |
| 56 | - driver.properties().forEach((k, v) -> print(FMT_P, k, v)); | 74 | + driver.manufacturer(), driver.hwVersion(), driver.swVersion()); |
| 75 | + driver.behaviours().forEach(b -> print(FMT_B, b.getCanonicalName(), | ||
| 76 | + driver.implementation(b).getCanonicalName())); | ||
| 77 | + driver.properties().forEach((k, v) -> print(FMT_P, k, v)); | ||
| 78 | + } | ||
| 57 | } | 79 | } |
| 58 | 80 | ||
| 59 | } | 81 | } | ... | ... |
| ... | @@ -15,10 +15,9 @@ | ... | @@ -15,10 +15,9 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.cli.net; | 16 | package org.onosproject.cli.net; |
| 17 | 17 | ||
| 18 | -import static com.google.common.collect.Lists.newArrayList; | ||
| 19 | - | ||
| 20 | import java.util.Collections; | 18 | import java.util.Collections; |
| 21 | import java.util.List; | 19 | import java.util.List; |
| 20 | +import java.util.Map; | ||
| 22 | import java.util.SortedMap; | 21 | import java.util.SortedMap; |
| 23 | import java.util.TreeMap; | 22 | import java.util.TreeMap; |
| 24 | 23 | ||
| ... | @@ -34,6 +33,11 @@ import org.onosproject.net.group.Group.GroupState; | ... | @@ -34,6 +33,11 @@ import org.onosproject.net.group.Group.GroupState; |
| 34 | import org.onosproject.net.group.GroupBucket; | 33 | import org.onosproject.net.group.GroupBucket; |
| 35 | import org.onosproject.net.group.GroupService; | 34 | import org.onosproject.net.group.GroupService; |
| 36 | 35 | ||
| 36 | +import com.fasterxml.jackson.databind.JsonNode; | ||
| 37 | +import com.fasterxml.jackson.databind.node.ArrayNode; | ||
| 38 | + | ||
| 39 | +import static com.google.common.collect.Lists.newArrayList; | ||
| 40 | + | ||
| 37 | /** | 41 | /** |
| 38 | * Lists all groups in the system. | 42 | * Lists all groups in the system. |
| 39 | */ | 43 | */ |
| ... | @@ -54,6 +58,16 @@ public class GroupsListCommand extends AbstractShellCommand { | ... | @@ -54,6 +58,16 @@ public class GroupsListCommand extends AbstractShellCommand { |
| 54 | required = false, multiValued = false) | 58 | required = false, multiValued = false) |
| 55 | String state; | 59 | String state; |
| 56 | 60 | ||
| 61 | + private JsonNode json(Map<Device, List<Group>> sortedGroups) { | ||
| 62 | + ArrayNode result = mapper().createArrayNode(); | ||
| 63 | + | ||
| 64 | + sortedGroups.forEach((device, groups) -> | ||
| 65 | + groups.forEach(group -> | ||
| 66 | + result.add(jsonForEntity(group, Group.class)))); | ||
| 67 | + | ||
| 68 | + return result; | ||
| 69 | + } | ||
| 70 | + | ||
| 57 | @Override | 71 | @Override |
| 58 | protected void execute() { | 72 | protected void execute() { |
| 59 | DeviceService deviceService = get(DeviceService.class); | 73 | DeviceService deviceService = get(DeviceService.class); |
| ... | @@ -61,7 +75,11 @@ public class GroupsListCommand extends AbstractShellCommand { | ... | @@ -61,7 +75,11 @@ public class GroupsListCommand extends AbstractShellCommand { |
| 61 | SortedMap<Device, List<Group>> sortedGroups = | 75 | SortedMap<Device, List<Group>> sortedGroups = |
| 62 | getSortedGroups(deviceService, groupService); | 76 | getSortedGroups(deviceService, groupService); |
| 63 | 77 | ||
| 64 | - sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups)); | 78 | + if (outputJson()) { |
| 79 | + print("%s", json(sortedGroups)); | ||
| 80 | + } else { | ||
| 81 | + sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups)); | ||
| 82 | + } | ||
| 65 | } | 83 | } |
| 66 | 84 | ||
| 67 | /** | 85 | /** | ... | ... |
| ... | @@ -33,11 +33,14 @@ import org.onosproject.net.HostLocation; | ... | @@ -33,11 +33,14 @@ import org.onosproject.net.HostLocation; |
| 33 | import org.onosproject.net.Link; | 33 | import org.onosproject.net.Link; |
| 34 | import org.onosproject.net.Path; | 34 | import org.onosproject.net.Path; |
| 35 | import org.onosproject.net.Port; | 35 | import org.onosproject.net.Port; |
| 36 | +import org.onosproject.net.driver.Driver; | ||
| 36 | import org.onosproject.net.flow.FlowEntry; | 37 | import org.onosproject.net.flow.FlowEntry; |
| 37 | import org.onosproject.net.flow.TrafficSelector; | 38 | import org.onosproject.net.flow.TrafficSelector; |
| 38 | import org.onosproject.net.flow.TrafficTreatment; | 39 | import org.onosproject.net.flow.TrafficTreatment; |
| 39 | import org.onosproject.net.flow.criteria.Criterion; | 40 | import org.onosproject.net.flow.criteria.Criterion; |
| 40 | import org.onosproject.net.flow.instructions.Instruction; | 41 | import org.onosproject.net.flow.instructions.Instruction; |
| 42 | +import org.onosproject.net.group.Group; | ||
| 43 | +import org.onosproject.net.group.GroupBucket; | ||
| 41 | import org.onosproject.net.intent.ConnectivityIntent; | 44 | import org.onosproject.net.intent.ConnectivityIntent; |
| 42 | import org.onosproject.net.intent.Constraint; | 45 | import org.onosproject.net.intent.Constraint; |
| 43 | import org.onosproject.net.intent.HostToHostIntent; | 46 | import org.onosproject.net.intent.HostToHostIntent; |
| ... | @@ -89,6 +92,9 @@ public class CodecManager implements CodecService { | ... | @@ -89,6 +92,9 @@ public class CodecManager implements CodecService { |
| 89 | registerCodec(Topology.class, new TopologyCodec()); | 92 | registerCodec(Topology.class, new TopologyCodec()); |
| 90 | registerCodec(TopologyCluster.class, new TopologyClusterCodec()); | 93 | registerCodec(TopologyCluster.class, new TopologyClusterCodec()); |
| 91 | registerCodec(Path.class, new PathCodec()); | 94 | registerCodec(Path.class, new PathCodec()); |
| 95 | + registerCodec(Group.class, new GroupCodec()); | ||
| 96 | + registerCodec(Driver.class, new DriverCodec()); | ||
| 97 | + registerCodec(GroupBucket.class, new GroupBucketCodec()); | ||
| 92 | log.info("Started"); | 98 | log.info("Started"); |
| 93 | } | 99 | } |
| 94 | 100 | ... | ... |
| 1 | +/* | ||
| 2 | + * Copyright 2015 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.codec.impl; | ||
| 17 | + | ||
| 18 | +import org.onosproject.codec.CodecContext; | ||
| 19 | +import org.onosproject.codec.JsonCodec; | ||
| 20 | +import org.onosproject.net.driver.Driver; | ||
| 21 | + | ||
| 22 | +import com.fasterxml.jackson.databind.node.ArrayNode; | ||
| 23 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 24 | + | ||
| 25 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 26 | + | ||
| 27 | +/** | ||
| 28 | + * JSON codec for the Driver class. | ||
| 29 | + */ | ||
| 30 | +public final class DriverCodec extends JsonCodec<Driver> { | ||
| 31 | + private static final String PARENT = "parent"; | ||
| 32 | + private static final String NAME = "name"; | ||
| 33 | + private static final String MANUFACTURER = "manufacturer"; | ||
| 34 | + private static final String HW_VERSION = "hwVersion"; | ||
| 35 | + private static final String SW_VERSION = "swVersion"; | ||
| 36 | + private static final String BEHAVIOURS = "behaviours"; | ||
| 37 | + private static final String BEHAVIORS_NAME = "name"; | ||
| 38 | + private static final String BEHAVIORS_IMPLEMENTATION_NAME = "implementationName"; | ||
| 39 | + private static final String PROPERTIES = "properties"; | ||
| 40 | + | ||
| 41 | + @Override | ||
| 42 | + public ObjectNode encode(Driver driver, CodecContext context) { | ||
| 43 | + checkNotNull(driver, "Driver cannot be null"); | ||
| 44 | + | ||
| 45 | + ObjectNode result = context.mapper().createObjectNode() | ||
| 46 | + .put(NAME, driver.name()) | ||
| 47 | + .put(MANUFACTURER, driver.manufacturer()) | ||
| 48 | + .put(HW_VERSION, driver.hwVersion()) | ||
| 49 | + .put(SW_VERSION, driver.swVersion()); | ||
| 50 | + | ||
| 51 | + if (driver.parent() != null) { | ||
| 52 | + result.put(PARENT, driver.parent().name()); | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + ArrayNode behaviours = context.mapper().createArrayNode(); | ||
| 56 | + driver.behaviours().forEach(behaviour -> { | ||
| 57 | + ObjectNode entry = context.mapper().createObjectNode() | ||
| 58 | + .put(BEHAVIORS_NAME, behaviour.getCanonicalName()) | ||
| 59 | + .put(BEHAVIORS_IMPLEMENTATION_NAME, | ||
| 60 | + driver.implementation(behaviour).getCanonicalName()); | ||
| 61 | + | ||
| 62 | + behaviours.add(entry); | ||
| 63 | + }); | ||
| 64 | + result.set(BEHAVIOURS, behaviours); | ||
| 65 | + | ||
| 66 | + ArrayNode properties = context.mapper().createArrayNode(); | ||
| 67 | + driver.properties().forEach((name, value) -> { | ||
| 68 | + ObjectNode entry = context.mapper().createObjectNode() | ||
| 69 | + .put("name", name) | ||
| 70 | + .put("value", value); | ||
| 71 | + | ||
| 72 | + properties.add(entry); | ||
| 73 | + }); | ||
| 74 | + result.set(PROPERTIES, properties); | ||
| 75 | + | ||
| 76 | + return result; | ||
| 77 | + } | ||
| 78 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2015 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.codec.impl; | ||
| 17 | + | ||
| 18 | +import org.onosproject.codec.CodecContext; | ||
| 19 | +import org.onosproject.codec.JsonCodec; | ||
| 20 | +import org.onosproject.net.flow.TrafficTreatment; | ||
| 21 | +import org.onosproject.net.group.GroupBucket; | ||
| 22 | + | ||
| 23 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 24 | + | ||
| 25 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 26 | + | ||
| 27 | +/** | ||
| 28 | + * Group bucket JSON codec. | ||
| 29 | + */ | ||
| 30 | +public class GroupBucketCodec extends JsonCodec<GroupBucket> { | ||
| 31 | + | ||
| 32 | + private static final String TYPE = "type"; | ||
| 33 | + private static final String TREATMENT = "treatment"; | ||
| 34 | + private static final String WEIGHT = "weight"; | ||
| 35 | + private static final String WATCH_PORT = "watchPort"; | ||
| 36 | + private static final String WATCH_GROUP = "watchGroup"; | ||
| 37 | + private static final String PACKETS = "packets"; | ||
| 38 | + private static final String BYTES = "bytes"; | ||
| 39 | + | ||
| 40 | + @Override | ||
| 41 | + public ObjectNode encode(GroupBucket bucket, CodecContext context) { | ||
| 42 | + checkNotNull(bucket, "Driver cannot be null"); | ||
| 43 | + | ||
| 44 | + ObjectNode result = context.mapper().createObjectNode() | ||
| 45 | + .put(TYPE, bucket.type().toString()) | ||
| 46 | + .put(WEIGHT, bucket.weight()) | ||
| 47 | + .put(PACKETS, bucket.packets()) | ||
| 48 | + .put(BYTES, bucket.bytes()); | ||
| 49 | + | ||
| 50 | + if (bucket.watchPort() != null) { | ||
| 51 | + result.put(WATCH_PORT, bucket.watchPort().toString()); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + if (bucket.watchGroup() != null) { | ||
| 55 | + result.put(WATCH_GROUP, bucket.watchGroup().toString()); | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + if (bucket.treatment() != null) { | ||
| 59 | + result.set(TREATMENT, context.codec(TrafficTreatment.class).encode(bucket.treatment(), context)); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + return result; | ||
| 63 | + } | ||
| 64 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2015 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.codec.impl; | ||
| 17 | + | ||
| 18 | +import org.onosproject.codec.CodecContext; | ||
| 19 | +import org.onosproject.codec.JsonCodec; | ||
| 20 | +import org.onosproject.net.group.Group; | ||
| 21 | +import org.onosproject.net.group.GroupBucket; | ||
| 22 | + | ||
| 23 | +import com.fasterxml.jackson.databind.node.ArrayNode; | ||
| 24 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 25 | + | ||
| 26 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 27 | + | ||
| 28 | +/** | ||
| 29 | + * Group JSON codec. | ||
| 30 | + */ | ||
| 31 | +public final class GroupCodec extends JsonCodec<Group> { | ||
| 32 | + // JSON field names | ||
| 33 | + private static final String ID = "id"; | ||
| 34 | + private static final String STATE = "state"; | ||
| 35 | + private static final String LIFE = "life"; | ||
| 36 | + private static final String PACKETS = "packets"; | ||
| 37 | + private static final String BYTES = "bytes"; | ||
| 38 | + private static final String REFERENCE_COUNT = "referenceCount"; | ||
| 39 | + private static final String TYPE = "type"; | ||
| 40 | + private static final String DEVICE_ID = "deviceId"; | ||
| 41 | + private static final String APP_ID = "appId"; | ||
| 42 | + private static final String APP_COOKIE = "appCookie"; | ||
| 43 | + private static final String GIVEN_GROUP_ID = "givenGroupId"; | ||
| 44 | + private static final String BUCKETS = "buckets"; | ||
| 45 | + | ||
| 46 | + @Override | ||
| 47 | + public ObjectNode encode(Group group, CodecContext context) { | ||
| 48 | + checkNotNull(group, "Group cannot be null"); | ||
| 49 | + ObjectNode result = context.mapper().createObjectNode() | ||
| 50 | + .put(ID, group.id().toString()) | ||
| 51 | + .put(STATE, group.state().toString()) | ||
| 52 | + .put(LIFE, group.life()) | ||
| 53 | + .put(PACKETS, group.packets()) | ||
| 54 | + .put(BYTES, group.bytes()) | ||
| 55 | + .put(REFERENCE_COUNT, group.referenceCount()) | ||
| 56 | + .put(TYPE, group.type().toString()) | ||
| 57 | + .put(DEVICE_ID, group.deviceId().toString()); | ||
| 58 | + | ||
| 59 | + if (group.appId() != null) { | ||
| 60 | + result.put(APP_ID, group.appId().toString()); | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + if (group.appCookie() != null) { | ||
| 64 | + result.put(APP_COOKIE, group.appCookie().toString()); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + if (group.givenGroupId() != null) { | ||
| 68 | + result.put(GIVEN_GROUP_ID, group.givenGroupId()); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + ArrayNode buckets = context.mapper().createArrayNode(); | ||
| 72 | + group.buckets().buckets().forEach(bucket -> { | ||
| 73 | + ObjectNode bucketJson = context.codec(GroupBucket.class).encode(bucket, context); | ||
| 74 | + buckets.add(bucketJson); | ||
| 75 | + }); | ||
| 76 | + result.set(BUCKETS, buckets); | ||
| 77 | + return result; | ||
| 78 | + } | ||
| 79 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2015 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.codec.impl; | ||
| 17 | + | ||
| 18 | + | ||
| 19 | +import java.util.Map; | ||
| 20 | + | ||
| 21 | +import org.junit.Test; | ||
| 22 | +import org.onosproject.net.driver.Behaviour; | ||
| 23 | +import org.onosproject.net.driver.DefaultDriver; | ||
| 24 | +import org.onosproject.net.driver.Driver; | ||
| 25 | +import org.onosproject.net.driver.TestBehaviour; | ||
| 26 | +import org.onosproject.net.driver.TestBehaviourImpl; | ||
| 27 | +import org.onosproject.net.driver.TestBehaviourTwo; | ||
| 28 | +import org.onosproject.net.driver.TestBehaviourTwoImpl; | ||
| 29 | + | ||
| 30 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 31 | +import com.google.common.collect.ImmutableMap; | ||
| 32 | + | ||
| 33 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 34 | +import static org.onosproject.codec.impl.DriverJsonMatcher.matchesDriver; | ||
| 35 | + | ||
| 36 | +/** | ||
| 37 | + * Unit tests for the driver codec. | ||
| 38 | + */ | ||
| 39 | +public class DriverCodecTest { | ||
| 40 | + | ||
| 41 | + @Test | ||
| 42 | + public void codecTest() { | ||
| 43 | + Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours = | ||
| 44 | + ImmutableMap.of(TestBehaviour.class, | ||
| 45 | + TestBehaviourImpl.class, | ||
| 46 | + TestBehaviourTwo.class, | ||
| 47 | + TestBehaviourTwoImpl.class); | ||
| 48 | + Map<String, String> properties = | ||
| 49 | + ImmutableMap.of("key1", "value1", "key2", "value2"); | ||
| 50 | + | ||
| 51 | + DefaultDriver parent = new DefaultDriver("parent", null, "Acme", | ||
| 52 | + "HW1.2.3", "SW1.2.3", | ||
| 53 | + behaviours, | ||
| 54 | + properties); | ||
| 55 | + DefaultDriver child = new DefaultDriver("child", parent, "Acme", | ||
| 56 | + "HW1.2.3.1", "SW1.2.3.1", | ||
| 57 | + behaviours, | ||
| 58 | + properties); | ||
| 59 | + | ||
| 60 | + MockCodecContext context = new MockCodecContext(); | ||
| 61 | + ObjectNode driverJson = context.codec(Driver.class).encode(child, context); | ||
| 62 | + | ||
| 63 | + assertThat(driverJson, matchesDriver(child)); | ||
| 64 | + } | ||
| 65 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2015 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.codec.impl; | ||
| 17 | + | ||
| 18 | +import java.util.Map; | ||
| 19 | + | ||
| 20 | +import org.hamcrest.Description; | ||
| 21 | +import org.hamcrest.TypeSafeDiagnosingMatcher; | ||
| 22 | +import org.onosproject.net.driver.Driver; | ||
| 23 | + | ||
| 24 | +import com.fasterxml.jackson.databind.JsonNode; | ||
| 25 | + | ||
| 26 | +/** | ||
| 27 | + * Hamcrest matcher for drivers. | ||
| 28 | + */ | ||
| 29 | +public final class DriverJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { | ||
| 30 | + private final Driver driver; | ||
| 31 | + | ||
| 32 | + private DriverJsonMatcher(Driver driver) { | ||
| 33 | + this.driver = driver; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + @Override | ||
| 37 | + public boolean matchesSafely(JsonNode jsonDriver, Description description) { | ||
| 38 | + // check id | ||
| 39 | + String jsonDriverName = jsonDriver.get("name").asText(); | ||
| 40 | + String driverName = driver.name(); | ||
| 41 | + if (!jsonDriverName.equals(driverName)) { | ||
| 42 | + description.appendText("name was " + jsonDriverName); | ||
| 43 | + return false; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + | ||
| 47 | + // check parent | ||
| 48 | + String jsonParent = jsonDriver.get("parent").asText(); | ||
| 49 | + String parent = driver.parent().name(); | ||
| 50 | + if (!jsonParent.equals(parent)) { | ||
| 51 | + description.appendText("parent was " + jsonParent); | ||
| 52 | + return false; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + // check manufacturer | ||
| 56 | + String jsonManufacturer = jsonDriver.get("manufacturer").asText(); | ||
| 57 | + String manufacturer = driver.manufacturer(); | ||
| 58 | + if (!jsonManufacturer.equals(manufacturer)) { | ||
| 59 | + description.appendText("manufacturer was " + jsonManufacturer); | ||
| 60 | + return false; | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + // check HW version | ||
| 64 | + String jsonHWVersion = jsonDriver.get("hwVersion").asText(); | ||
| 65 | + String hwVersion = driver.hwVersion(); | ||
| 66 | + if (!jsonHWVersion.equals(hwVersion)) { | ||
| 67 | + description.appendText("HW version was " + jsonHWVersion); | ||
| 68 | + return false; | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + // check SW version | ||
| 72 | + String jsonSWVersion = jsonDriver.get("swVersion").asText(); | ||
| 73 | + String swVersion = driver.swVersion(); | ||
| 74 | + if (!jsonSWVersion.equals(swVersion)) { | ||
| 75 | + description.appendText("SW version was " + jsonSWVersion); | ||
| 76 | + return false; | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + // Check properties | ||
| 80 | + JsonNode jsonProperties = jsonDriver.get("properties"); | ||
| 81 | + if (driver.properties().size() != jsonProperties.size()) { | ||
| 82 | + description.appendText("properties map size was was " + jsonProperties.size()); | ||
| 83 | + return false; | ||
| 84 | + } | ||
| 85 | + for (Map.Entry<String, String> entry : driver.properties().entrySet()) { | ||
| 86 | + boolean propertyFound = false; | ||
| 87 | + for (int propertyIndex = 0; propertyIndex < jsonProperties.size(); propertyIndex++) { | ||
| 88 | + String jsonName = jsonProperties.get(propertyIndex).get("name").asText(); | ||
| 89 | + String jsonValue = jsonProperties.get(propertyIndex).get("value").asText(); | ||
| 90 | + if (!jsonName.equals(entry.getKey()) || | ||
| 91 | + !jsonValue.equals(entry.getValue())) { | ||
| 92 | + propertyFound = true; | ||
| 93 | + break; | ||
| 94 | + } | ||
| 95 | + } | ||
| 96 | + if (!propertyFound) { | ||
| 97 | + description.appendText("property not found " + entry.getKey()); | ||
| 98 | + return false; | ||
| 99 | + } | ||
| 100 | + } | ||
| 101 | + return true; | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + @Override | ||
| 105 | + public void describeTo(Description description) { | ||
| 106 | + description.appendText(driver.toString()); | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + /** | ||
| 110 | + * Factory to allocate a driver matcher. | ||
| 111 | + * | ||
| 112 | + * @param driver driver object we are looking for | ||
| 113 | + * @return matcher | ||
| 114 | + */ | ||
| 115 | + public static DriverJsonMatcher matchesDriver(Driver driver) { | ||
| 116 | + return new DriverJsonMatcher(driver); | ||
| 117 | + } | ||
| 118 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2015 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.codec.impl; | ||
| 17 | + | ||
| 18 | +import org.hamcrest.Description; | ||
| 19 | +import org.hamcrest.TypeSafeDiagnosingMatcher; | ||
| 20 | +import org.onosproject.net.group.GroupBucket; | ||
| 21 | + | ||
| 22 | +import com.fasterxml.jackson.databind.JsonNode; | ||
| 23 | + | ||
| 24 | +/** | ||
| 25 | + * Hamcrest matcher for instructions. | ||
| 26 | + */ | ||
| 27 | +public final class GroupBucketJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { | ||
| 28 | + | ||
| 29 | + private final GroupBucket bucket; | ||
| 30 | + | ||
| 31 | + private GroupBucketJsonMatcher(GroupBucket bucket) { | ||
| 32 | + this.bucket = bucket; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + /** | ||
| 36 | + * Matches the contents of a group bucket. | ||
| 37 | + * | ||
| 38 | + * @param bucketJson JSON representation of bucket to match | ||
| 39 | + * @param description Description object used for recording errors | ||
| 40 | + * @return true if contents match, false otherwise | ||
| 41 | + */ | ||
| 42 | + @Override | ||
| 43 | + public boolean matchesSafely(JsonNode bucketJson, Description description) { | ||
| 44 | + | ||
| 45 | + // check type | ||
| 46 | + final String jsonType = bucketJson.get("type").textValue(); | ||
| 47 | + if (!bucket.type().name().equals(jsonType)) { | ||
| 48 | + description.appendText("type was " + jsonType); | ||
| 49 | + return false; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + final long jsonWeight = bucketJson.get("weight").longValue(); | ||
| 53 | + if (bucket.weight() != jsonWeight) { | ||
| 54 | + description.appendText("weight was " + jsonWeight); | ||
| 55 | + return false; | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + final long packetsJson = bucketJson.get("packets").asLong(); | ||
| 59 | + if (bucket.packets() != packetsJson) { | ||
| 60 | + description.appendText("packets was " + packetsJson); | ||
| 61 | + return false; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + final long bytesJson = bucketJson.get("bytes").asLong(); | ||
| 65 | + if (bucket.bytes() != bytesJson) { | ||
| 66 | + description.appendText("bytes was " + packetsJson); | ||
| 67 | + return false; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + return true; | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + @Override | ||
| 74 | + public void describeTo(Description description) { | ||
| 75 | + description.appendText(bucket.toString()); | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + /** | ||
| 79 | + * Factory to allocate an bucket matcher. | ||
| 80 | + * | ||
| 81 | + * @param bucket bucket object we are looking for | ||
| 82 | + * @return matcher | ||
| 83 | + */ | ||
| 84 | + public static GroupBucketJsonMatcher matchesGroupBucket(GroupBucket bucket) { | ||
| 85 | + return new GroupBucketJsonMatcher(bucket); | ||
| 86 | + } | ||
| 87 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2015 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.codec.impl; | ||
| 17 | + | ||
| 18 | +import org.junit.Test; | ||
| 19 | +import org.onosproject.core.DefaultGroupId; | ||
| 20 | +import org.onosproject.net.NetTestTools; | ||
| 21 | +import org.onosproject.net.flow.DefaultTrafficTreatment; | ||
| 22 | +import org.onosproject.net.group.DefaultGroup; | ||
| 23 | +import org.onosproject.net.group.DefaultGroupBucket; | ||
| 24 | +import org.onosproject.net.group.GroupBucket; | ||
| 25 | +import org.onosproject.net.group.GroupBuckets; | ||
| 26 | +import org.onosproject.net.group.GroupDescription; | ||
| 27 | + | ||
| 28 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 29 | +import com.google.common.collect.ImmutableList; | ||
| 30 | + | ||
| 31 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 32 | +import static org.onosproject.codec.impl.GroupJsonMatcher.matchesGroup; | ||
| 33 | + | ||
| 34 | +/** | ||
| 35 | + * Group codec unit tests. | ||
| 36 | + */ | ||
| 37 | + | ||
| 38 | +public class GroupCodecTest { | ||
| 39 | + | ||
| 40 | + @Test | ||
| 41 | + public void codecTest() { | ||
| 42 | + GroupBucket bucket1 = DefaultGroupBucket | ||
| 43 | + .createSelectGroupBucket(DefaultTrafficTreatment.emptyTreatment()); | ||
| 44 | + GroupBucket bucket2 = DefaultGroupBucket | ||
| 45 | + .createIndirectGroupBucket(DefaultTrafficTreatment.emptyTreatment()); | ||
| 46 | + GroupBuckets buckets = new GroupBuckets(ImmutableList.of(bucket1, bucket2)); | ||
| 47 | + | ||
| 48 | + | ||
| 49 | + DefaultGroup group = new DefaultGroup( | ||
| 50 | + new DefaultGroupId(1), | ||
| 51 | + NetTestTools.did("d1"), | ||
| 52 | + GroupDescription.Type.INDIRECT, | ||
| 53 | + buckets); | ||
| 54 | + | ||
| 55 | + MockCodecContext context = new MockCodecContext(); | ||
| 56 | + GroupCodec codec = new GroupCodec(); | ||
| 57 | + ObjectNode groupJson = codec.encode(group, context); | ||
| 58 | + | ||
| 59 | + assertThat(groupJson, matchesGroup(group)); | ||
| 60 | + } | ||
| 61 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2015 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.codec.impl; | ||
| 17 | + | ||
| 18 | +import org.hamcrest.Description; | ||
| 19 | +import org.hamcrest.TypeSafeDiagnosingMatcher; | ||
| 20 | +import org.onosproject.net.group.Group; | ||
| 21 | +import org.onosproject.net.group.GroupBucket; | ||
| 22 | + | ||
| 23 | +import com.fasterxml.jackson.databind.JsonNode; | ||
| 24 | + | ||
| 25 | +/** | ||
| 26 | + * Hamcrest matcher for groups. | ||
| 27 | + */ | ||
| 28 | + | ||
| 29 | +public final class GroupJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { | ||
| 30 | + | ||
| 31 | + private final Group group; | ||
| 32 | + | ||
| 33 | + private GroupJsonMatcher(Group group) { | ||
| 34 | + this.group = group; | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + @Override | ||
| 38 | + public boolean matchesSafely(JsonNode jsonGroup, Description description) { | ||
| 39 | + // check id | ||
| 40 | + String jsonGroupId = jsonGroup.get("id").asText(); | ||
| 41 | + String groupId = group.id().toString(); | ||
| 42 | + if (!jsonGroupId.equals(groupId)) { | ||
| 43 | + description.appendText("group id was " + jsonGroupId); | ||
| 44 | + return false; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + // check state | ||
| 48 | + String jsonState = jsonGroup.get("state").asText(); | ||
| 49 | + String state = group.state().toString(); | ||
| 50 | + if (!jsonState.equals(state)) { | ||
| 51 | + description.appendText("state was " + jsonState); | ||
| 52 | + return false; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + // check life | ||
| 56 | + long jsonLife = jsonGroup.get("life").asLong(); | ||
| 57 | + long life = group.life(); | ||
| 58 | + if (life != jsonLife) { | ||
| 59 | + description.appendText("life was " + jsonLife); | ||
| 60 | + return false; | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + // check bytes | ||
| 64 | + long jsonBytes = jsonGroup.get("bytes").asLong(); | ||
| 65 | + long bytes = group.bytes(); | ||
| 66 | + if (bytes != jsonBytes) { | ||
| 67 | + description.appendText("bytes was " + jsonBytes); | ||
| 68 | + return false; | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + // check packets | ||
| 72 | + long jsonPackets = jsonGroup.get("packets").asLong(); | ||
| 73 | + long packets = group.packets(); | ||
| 74 | + if (packets != jsonPackets) { | ||
| 75 | + description.appendText("packets was " + jsonPackets); | ||
| 76 | + return false; | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + // check size of bucket array | ||
| 80 | + JsonNode jsonBuckets = jsonGroup.get("buckets"); | ||
| 81 | + if (jsonBuckets.size() != group.buckets().buckets().size()) { | ||
| 82 | + description.appendText("buckets size was " + jsonBuckets.size()); | ||
| 83 | + return false; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + // Check buckets | ||
| 87 | + for (GroupBucket bucket : group.buckets().buckets()) { | ||
| 88 | + boolean bucketFound = false; | ||
| 89 | + for (int bucketIndex = 0; bucketIndex < jsonBuckets.size(); bucketIndex++) { | ||
| 90 | + GroupBucketJsonMatcher bucketMatcher = | ||
| 91 | + GroupBucketJsonMatcher.matchesGroupBucket(bucket); | ||
| 92 | + if (bucketMatcher.matches(jsonBuckets.get(bucketIndex))) { | ||
| 93 | + bucketFound = true; | ||
| 94 | + break; | ||
| 95 | + } | ||
| 96 | + } | ||
| 97 | + if (!bucketFound) { | ||
| 98 | + description.appendText("bucket not found " + bucket.toString()); | ||
| 99 | + return false; | ||
| 100 | + } | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + return true; | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + @Override | ||
| 107 | + public void describeTo(Description description) { | ||
| 108 | + description.appendText(group.toString()); | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + /** | ||
| 112 | + * Factory to allocate a group matcher. | ||
| 113 | + * | ||
| 114 | + * @param group group object we are looking for | ||
| 115 | + * @return matcher | ||
| 116 | + */ | ||
| 117 | + public static GroupJsonMatcher matchesGroup(Group group) { | ||
| 118 | + return new GroupJsonMatcher(group); | ||
| 119 | + } | ||
| 120 | +} |
-
Please register or login to post a comment