Sho SHIMIZU
Committed by Sho SHIMIZU

Add Codecs class holding DiscreteResourceCodecs

This is for ONOS-4281

Change-Id: I156f23aa7027381cdd4453631654d7eaa33858dd
1 +/*
2 + * Copyright 2016-present 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.store.resource.impl;
17 +
18 +import org.onlab.packet.MplsLabel;
19 +import org.onlab.packet.VlanId;
20 +import org.onosproject.net.resource.DiscreteResource;
21 +import org.onosproject.net.resource.DiscreteResourceCodec;
22 +import org.onosproject.net.resource.MplsCodec;
23 +import org.onosproject.net.resource.VlanCodec;
24 +
25 +import java.util.HashMap;
26 +import java.util.Map;
27 +
28 +/**
29 + * A set of {@link DiscreteResourceCodec DiscreteResourceCodecs}.
30 + */
31 +final class Codecs {
32 + private static final Codecs INSTANCE = new Codecs();
33 +
34 + private final Map<Class<?>, DiscreteResourceCodec<?>> codecs;
35 +
36 + /**
37 + * Returns an instance of this class.
38 + *
39 + * @return instance
40 + */
41 + static Codecs getInstance() {
42 + return INSTANCE;
43 + }
44 +
45 + private Codecs() {
46 + this.codecs = new HashMap<>();
47 + init();
48 + }
49 +
50 + private void init() {
51 + codecs.put(VlanId.class, new VlanCodec());
52 + codecs.put(MplsLabel.class, new MplsCodec());
53 + }
54 +
55 + /**
56 + * Returns if there is a codec available for the specified resource.
57 + *
58 + * @param resource resource to be encoded
59 + * @return true if this class can encode the resource, otherwise false.
60 + */
61 + boolean isEncodable(DiscreteResource resource) {
62 + return resource.valueAs(Object.class)
63 + .map(Object::getClass)
64 + .map(codecs::containsKey)
65 + .orElse(Boolean.FALSE);
66 + }
67 +
68 + /**
69 + * Returns the codec for the specified class.
70 + *
71 + * @param cls class of an instance to be encoded
72 + * @return the codec for the class
73 + */
74 + DiscreteResourceCodec<?> getCodec(Class<?> cls) {
75 + return codecs.get(cls);
76 + }
77 +}
1 +/*
2 + * Copyright 2016-present 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.store.resource.impl;
17 +
18 +import org.junit.Before;
19 +import org.junit.Test;
20 +import org.onlab.packet.MplsLabel;
21 +import org.onlab.packet.VlanId;
22 +import org.onosproject.net.ChannelSpacing;
23 +import org.onosproject.net.DeviceId;
24 +import org.onosproject.net.OchSignal;
25 +import org.onosproject.net.PortNumber;
26 +import org.onosproject.net.resource.DiscreteResource;
27 +import org.onosproject.net.resource.Resource;
28 +import org.onosproject.net.resource.Resources;
29 +
30 +import static org.hamcrest.Matchers.is;
31 +import static org.junit.Assert.assertThat;
32 +
33 +/**
34 + * Unit tests for Codecs.
35 + */
36 +public class CodecsTest {
37 +
38 + private static final DeviceId DID = DeviceId.deviceId("a");
39 + private static final PortNumber PN = PortNumber.portNumber(1);
40 + private static final VlanId VLAN = VlanId.vlanId((short) 1);
41 + private static final MplsLabel MPLS = MplsLabel.mplsLabel(1);
42 + private static final OchSignal OCH = OchSignal.newDwdmSlot(ChannelSpacing.CHL_50GHZ, 1);
43 +
44 + private Codecs sut;
45 +
46 + @Before
47 + public void setUp() {
48 + sut = Codecs.getInstance();
49 + }
50 +
51 + /**
52 + * Checks that it's possible to encode a VLAN ID.
53 + */
54 + @Test
55 + public void isVlanEncodable() {
56 + DiscreteResource resource = Resources.discrete(DID, PN, VLAN).resource();
57 +
58 + assertThat(sut.isEncodable(resource), is(true));
59 + }
60 +
61 + /**
62 + * Checks that it's possible to encode a MPLS label.
63 + */
64 + @Test
65 + public void isMplsEncodable() {
66 + DiscreteResource resource = Resources.discrete(DID, PN, MPLS).resource();
67 +
68 + assertThat(sut.isEncodable(resource), is(true));
69 + }
70 +
71 + /**
72 + * Checks that it's not possible to encode the root resource.
73 + */
74 + @Test
75 + public void isRootNonEncodable() {
76 + DiscreteResource resource = Resource.ROOT;
77 +
78 + assertThat(sut.isEncodable(resource), is(false));
79 + }
80 +
81 + /**
82 + * Checks that it's not possible to encode an Och signal.
83 + */
84 + @Test
85 + public void isOchNonEncodable() {
86 + DiscreteResource resource = Resources.discrete(DID, PN, OCH).resource();
87 +
88 + assertThat(sut.isEncodable(resource), is(false));
89 + }
90 +}