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