blob: 65ed92bc2d22434f0092a631bc4d3eb7448d25f0 [file] [log] [blame]
Jian Li08926a92016-05-05 15:35:40 -07001/*
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 */
16package org.onosproject.driver.extensions.codec;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onlab.packet.VlanId;
20import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
22import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25import static org.onlab.util.Tools.nullIsIllegal;
26
27/**
28 * JSON Codec for Ofdpa match vlan vid class.
29 */
30public class OfdpaMatchVlanVidCodec extends JsonCodec<OfdpaMatchVlanVid> {
31
32 private static final String VLAN_ID = "vlanId";
Jian Li08926a92016-05-05 15:35:40 -070033 private static final String MISSING_MEMBER_MESSAGE = " member is required in OfdpaMatchVlanVid";
Charles Chancad338a2016-09-16 18:03:11 -070034 private static final String MISSING_VLAN_ID_MESSAGE = "Vlan ID cannot be null";
Jian Li08926a92016-05-05 15:35:40 -070035
36 @Override
37 public ObjectNode encode(OfdpaMatchVlanVid vlanId, CodecContext context) {
Charles Chancad338a2016-09-16 18:03:11 -070038 checkNotNull(vlanId, MISSING_VLAN_ID_MESSAGE);
39 return context.mapper().createObjectNode()
Jian Li08926a92016-05-05 15:35:40 -070040 .put(VLAN_ID, vlanId.vlanId().id());
Jian Li08926a92016-05-05 15:35:40 -070041 }
42
43 @Override
44 public OfdpaMatchVlanVid decode(ObjectNode json, CodecContext context) {
45 if (json == null || !json.isObject()) {
46 return null;
47 }
48
49 // parse ofdpa match vlan vid
50 short vlanVid = (short) nullIsIllegal(json.get(VLAN_ID),
51 VLAN_ID + MISSING_MEMBER_MESSAGE).asInt();
52 return new OfdpaMatchVlanVid(VlanId.vlanId(vlanVid));
53 }
54}