Add DiscreteResourceCodec for PortNumber
Change-Id: Iedda1a0ba46b3d044f127334a8abdb419dabcd9f
(cherry picked from commit cbd103dfc8057432bb5bef63cd9a3adafe9c06ad)
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/Codecs.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/Codecs.java
index 41769e2..c16d944 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/Codecs.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/Codecs.java
@@ -17,6 +17,7 @@
import org.onlab.packet.MplsLabel;
import org.onlab.packet.VlanId;
+import org.onosproject.net.PortNumber;
import org.onosproject.net.resource.DiscreteResource;
import org.onosproject.net.resource.DiscreteResourceCodec;
@@ -46,6 +47,7 @@
}
private void init() {
+ codecs.put(PortNumber.class, new PortNumberCodec());
codecs.put(VlanId.class, new VlanIdCodec());
codecs.put(MplsLabel.class, new MplsLabelCodec());
}
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentResourceStore.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentResourceStore.java
index e581a5b..de1c3b7 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentResourceStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentResourceStore.java
@@ -78,6 +78,7 @@
.register(EmptyDiscreteResources.class)
.register(new EncodedResourcesSerializer(), EncodedDiscreteResources.class)
.register(ContinuousResourceAllocation.class)
+ .register(PortNumberCodec.class)
.register(VlanIdCodec.class)
.register(MplsLabelCodec.class)
.build());
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/PortNumberCodec.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/PortNumberCodec.java
new file mode 100644
index 0000000..d91b207
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/PortNumberCodec.java
@@ -0,0 +1,50 @@
+/*
+ * 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 org.onosproject.net.PortNumber;
+import org.onosproject.net.resource.DiscreteResourceCodec;
+
+class PortNumberCodec implements DiscreteResourceCodec<PortNumber> {
+ @Override
+ public int encode(PortNumber resource) {
+ return (int) resource.toLong();
+ }
+
+ @Override
+ public PortNumber decode(int value) {
+ return PortNumber.portNumber(value);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ return PortNumber.class.hashCode();
+ }
+}