Jian Li | dab7256 | 2016-04-12 14:10:32 -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.onosproject.codec.CodecContext; |
| 20 | import org.onosproject.codec.JsonCodec; |
| 21 | import org.onosproject.driver.extensions.NiciraResubmit; |
| 22 | import org.onosproject.net.PortNumber; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | import static org.onlab.util.Tools.nullIsIllegal; |
| 26 | |
| 27 | /** |
| 28 | * JSON Codec for NiciraResubmit class. |
| 29 | */ |
| 30 | public final class NiciraResubmitCodec extends JsonCodec<NiciraResubmit> { |
| 31 | |
| 32 | private static final String RESUBMIT_PORT = "inPort"; |
| 33 | private static final String RESUBMIT_TABLE = "table"; |
| 34 | |
| 35 | private static final String MISSING_MEMBER_MESSAGE = " member is required in NiciraResubmit"; |
| 36 | |
| 37 | @Override |
| 38 | public ObjectNode encode(NiciraResubmit niciraResubmit, CodecContext context) { |
| 39 | checkNotNull(niciraResubmit, "Nicira Resubmit cannot be null"); |
| 40 | ObjectNode root = context.mapper().createObjectNode() |
| 41 | .put(RESUBMIT_PORT, niciraResubmit.inPort().toLong()) |
| 42 | .put(RESUBMIT_TABLE, niciraResubmit.table()); |
| 43 | return root; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public NiciraResubmit decode(ObjectNode json, CodecContext context) { |
| 48 | if (json == null || !json.isObject()) { |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | // parse in port number |
| 53 | long portNumberLong = nullIsIllegal(json.get(RESUBMIT_PORT), RESUBMIT_PORT + MISSING_MEMBER_MESSAGE).asLong(); |
| 54 | PortNumber portNumber = PortNumber.portNumber(portNumberLong); |
| 55 | |
Jian Li | 08926a9 | 2016-05-05 15:35:40 -0700 | [diff] [blame] | 56 | return new NiciraResubmit(portNumber); |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 57 | } |
| 58 | } |