Fix bug causing ClassCastException
Change-Id: Ic8787f05d05871b4473ff437b98e0b307ee1cacd
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedDiscreteResources.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedDiscreteResources.java
index a4818e2..59c39d8 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedDiscreteResources.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedDiscreteResources.java
@@ -74,7 +74,10 @@
@SuppressWarnings("unchecked")
boolean contains(DiscreteResource resource) {
- return rangeSet.contains(codec.encode(resource));
+ return resource.valueAs(Object.class)
+ .map(x -> codec.encode(x))
+ .map(rangeSet::contains)
+ .orElse(false);
}
boolean isEmpty() {
diff --git a/core/store/dist/src/test/java/org/onosproject/store/resource/impl/EncodedDiscreteResourcesTest.java b/core/store/dist/src/test/java/org/onosproject/store/resource/impl/EncodedDiscreteResourcesTest.java
new file mode 100644
index 0000000..e7483cd
--- /dev/null
+++ b/core/store/dist/src/test/java/org/onosproject/store/resource/impl/EncodedDiscreteResourcesTest.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2016-present 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.store.resource.impl;
+
+import com.google.common.collect.ImmutableSet;
+import org.junit.Test;
+import org.onlab.packet.VlanId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.resource.DiscreteResource;
+import org.onosproject.net.resource.Resources;
+
+import java.util.Set;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.*;
+
+public class EncodedDiscreteResourcesTest {
+ private static final DeviceId DID = DeviceId.deviceId("device1");
+ private static final PortNumber PN = PortNumber.portNumber(1);
+ private static final VlanId VID1 = VlanId.vlanId((short) 1);
+ private static final VlanId VID2 = VlanId.vlanId((short) 2);
+ private static final VlanId VID3 = VlanId.vlanId((short) 3);
+
+ @Test
+ public void testContains() {
+ DiscreteResource res1 = Resources.discrete(DID, PN, VID1).resource();
+ DiscreteResource res2 = Resources.discrete(DID, PN, VID2).resource();
+ DiscreteResource res3 = Resources.discrete(DID, PN, VID3).resource();
+
+ Set<DiscreteResource> resources = ImmutableSet.of(res1, res2);
+
+ EncodedDiscreteResources sut = EncodedDiscreteResources.of(resources, new VlanIdCodec());
+
+ assertThat(sut.contains(res1), is(true));
+ assertThat(sut.contains(res3), is(false));
+ }
+
+}