Pier Ventre | 6f63005 | 2016-10-18 09:58:41 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Pier Ventre | 6f63005 | 2016-10-18 09:58:41 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.driver.extensions.codec; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onlab.packet.VlanId; |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.driver.extensions.Ofdpa3MatchOvid; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | import static org.onlab.util.Tools.nullIsIllegal; |
| 27 | |
| 28 | /** |
| 29 | * JSON Codec for Ofdpa match ovid class. |
| 30 | */ |
| 31 | public class Ofdpa3MatchOvidCodec extends JsonCodec<Ofdpa3MatchOvid> { |
| 32 | |
| 33 | private static final String OVID = "oVid"; |
| 34 | private static final String MISSING_MEMBER_MESSAGE = " member is required in Ofdpa3MatchOvid"; |
| 35 | private static final String MISSING_OVID_MESSAGE = "OVID cannot be null"; |
| 36 | |
| 37 | @Override |
| 38 | public ObjectNode encode(Ofdpa3MatchOvid matchOvid, CodecContext context) { |
| 39 | checkNotNull(matchOvid, MISSING_OVID_MESSAGE); |
| 40 | return context.mapper().createObjectNode() |
| 41 | .put(OVID, matchOvid.vlanId().id()); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public Ofdpa3MatchOvid decode(ObjectNode json, CodecContext context) { |
| 46 | if (json == null || !json.isObject()) { |
| 47 | return null; |
| 48 | } |
| 49 | |
| 50 | // parse ofdpa match ovid |
| 51 | short vlanVid = (short) nullIsIllegal(json.get(OVID), |
| 52 | OVID + MISSING_MEMBER_MESSAGE).asInt(); |
| 53 | return new Ofdpa3MatchOvid(VlanId.vlanId(vlanVid)); |
| 54 | } |
| 55 | } |