Add Codecs class holding DiscreteResourceCodecs

This is for ONOS-4281

Change-Id: I156f23aa7027381cdd4453631654d7eaa33858dd
(cherry picked from commit 32c07af24e7119f71cca87196cde76eacacd0f32)
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
new file mode 100644
index 0000000..6b15e51
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/Codecs.java
@@ -0,0 +1,77 @@
+/*
+ * 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.onlab.packet.MplsLabel;
+import org.onlab.packet.VlanId;
+import org.onosproject.net.resource.DiscreteResource;
+import org.onosproject.net.resource.DiscreteResourceCodec;
+import org.onosproject.net.resource.MplsCodec;
+import org.onosproject.net.resource.VlanCodec;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A set of {@link DiscreteResourceCodec DiscreteResourceCodecs}.
+ */
+final class Codecs {
+    private static final Codecs INSTANCE = new Codecs();
+
+    private final Map<Class<?>, DiscreteResourceCodec<?>> codecs;
+
+    /**
+     * Returns an instance of this class.
+     *
+     * @return instance
+     */
+    static Codecs getInstance() {
+        return INSTANCE;
+    }
+
+    private Codecs() {
+        this.codecs = new HashMap<>();
+        init();
+    }
+
+    private void init() {
+        codecs.put(VlanId.class, new VlanCodec());
+        codecs.put(MplsLabel.class, new MplsCodec());
+    }
+
+    /**
+     * Returns if there is a codec available for the specified resource.
+     *
+     * @param resource resource to be encoded
+     * @return true if this class can encode the resource, otherwise false.
+     */
+    boolean isEncodable(DiscreteResource resource) {
+        return resource.valueAs(Object.class)
+                .map(Object::getClass)
+                .map(codecs::containsKey)
+                .orElse(Boolean.FALSE);
+    }
+
+    /**
+     * Returns the codec for the specified class.
+     *
+     * @param cls class of an instance to be encoded
+     * @return the codec for the class
+     */
+    DiscreteResourceCodec<?> getCodec(Class<?> cls) {
+        return codecs.get(cls);
+    }
+}
diff --git a/core/store/dist/src/test/java/org/onosproject/store/resource/impl/CodecsTest.java b/core/store/dist/src/test/java/org/onosproject/store/resource/impl/CodecsTest.java
new file mode 100644
index 0000000..b48f2ad
--- /dev/null
+++ b/core/store/dist/src/test/java/org/onosproject/store/resource/impl/CodecsTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.MplsLabel;
+import org.onlab.packet.VlanId;
+import org.onosproject.net.ChannelSpacing;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.OchSignal;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.resource.DiscreteResource;
+import org.onosproject.net.resource.Resource;
+import org.onosproject.net.resource.Resources;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Unit tests for Codecs.
+ */
+public class CodecsTest {
+
+    private static final DeviceId DID = DeviceId.deviceId("a");
+    private static final PortNumber PN = PortNumber.portNumber(1);
+    private static final VlanId VLAN = VlanId.vlanId((short) 1);
+    private static final MplsLabel MPLS = MplsLabel.mplsLabel(1);
+    private static final OchSignal OCH = OchSignal.newDwdmSlot(ChannelSpacing.CHL_50GHZ, 1);
+
+    private Codecs sut;
+
+    @Before
+    public void setUp() {
+        sut = Codecs.getInstance();
+    }
+
+    /**
+     * Checks that it's possible to encode a VLAN ID.
+     */
+    @Test
+    public void isVlanEncodable() {
+        DiscreteResource resource = Resources.discrete(DID, PN, VLAN).resource();
+
+        assertThat(sut.isEncodable(resource), is(true));
+    }
+
+    /**
+     * Checks that it's possible to encode a MPLS label.
+     */
+    @Test
+    public void isMplsEncodable() {
+        DiscreteResource resource = Resources.discrete(DID, PN, MPLS).resource();
+
+        assertThat(sut.isEncodable(resource), is(true));
+    }
+
+    /**
+     * Checks that it's not possible to encode the root resource.
+     */
+    @Test
+    public void isRootNonEncodable() {
+        DiscreteResource resource = Resource.ROOT;
+
+        assertThat(sut.isEncodable(resource), is(false));
+    }
+
+    /**
+     * Checks that it's not possible to encode an Och signal.
+     */
+    @Test
+    public void isOchNonEncodable() {
+        DiscreteResource resource = Resources.discrete(DID, PN, OCH).resource();
+
+        assertThat(sut.isEncodable(resource), is(false));
+    }
+}