Andreas Pantelopoulos | fdcfe53 | 2018-04-02 10:59:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Foundation |
| 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.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
| 22 | import org.onosproject.driver.extensions.Ofdpa3CopyField; |
| 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 copy field class. |
| 29 | */ |
| 30 | public class Ofdpa3CopyFieldCodec extends JsonCodec<Ofdpa3CopyField> { |
| 31 | |
| 32 | private static final String N_BITS = "nBits"; |
| 33 | private static final String SRC = "src"; |
| 34 | private static final String DST = "dst"; |
| 35 | private static final String SRC_OFFSET = "srcOffset"; |
| 36 | private static final String DST_OFFSET = "dstOffset"; |
| 37 | |
| 38 | private static final String MISSING_MEMBER_MESSAGE = " member is required in Ofdpa3CopyField"; |
| 39 | private static final String MISSING_COPY_FIELD_MESSAGE = "CopyField can not be null"; |
| 40 | |
| 41 | @Override |
| 42 | public ObjectNode encode(Ofdpa3CopyField copyField, CodecContext context) { |
| 43 | checkNotNull(copyField, MISSING_COPY_FIELD_MESSAGE); |
| 44 | return context.mapper().createObjectNode() |
| 45 | .put(N_BITS, copyField.getnBits()) |
| 46 | .put(SRC_OFFSET, copyField.getSrcOffset()) |
| 47 | .put(DST_OFFSET, copyField.getDstOffset()) |
| 48 | .put(SRC, copyField.getSrc()) |
| 49 | .put(DST, copyField.getDst()); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public Ofdpa3CopyField decode(ObjectNode json, CodecContext context) { |
| 54 | if (json == null || !json.isObject()) { |
| 55 | return null; |
| 56 | } |
| 57 | |
| 58 | // parse members of copy field action |
| 59 | int nBits = (int) nullIsIllegal(json.get(N_BITS), |
| 60 | N_BITS + MISSING_MEMBER_MESSAGE).asInt(); |
| 61 | int src = (int) nullIsIllegal(json.get(SRC), |
| 62 | SRC + MISSING_MEMBER_MESSAGE).asInt(); |
| 63 | int dst = (int) nullIsIllegal(json.get(DST), |
| 64 | DST + MISSING_MEMBER_MESSAGE).asInt(); |
| 65 | int srcOffset = (int) nullIsIllegal(json.get(SRC_OFFSET), |
| 66 | SRC_OFFSET + MISSING_MEMBER_MESSAGE).asInt(); |
| 67 | int dstOffset = (int) nullIsIllegal(json.get(DST_OFFSET), |
| 68 | DST_OFFSET + MISSING_MEMBER_MESSAGE).asInt(); |
| 69 | return new Ofdpa3CopyField(nBits, src, dst, srcOffset, dstOffset); |
| 70 | } |
| 71 | } |