Jian Li | b54d14b | 2017-03-28 21:34:34 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jian Li | b54d14b | 2017-03-28 21:34:34 +0900 | [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 | */ |
Jian Li | 2e818b0 | 2017-04-12 19:28:30 +0900 | [diff] [blame] | 16 | package org.onosproject.mapping.codec; |
Jian Li | b54d14b | 2017-03-28 21:34:34 +0900 | [diff] [blame] | 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.mapping.instructions.MappingInstruction; |
| 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
| 27 | /** |
| 28 | * Mapping instruction codec. |
| 29 | */ |
| 30 | public final class MappingInstructionCodec extends JsonCodec<MappingInstruction> { |
| 31 | |
Jian Li | 8e07271 | 2017-04-14 21:52:59 +0900 | [diff] [blame] | 32 | protected static final Logger log = LoggerFactory.getLogger(MappingInstructionCodec.class); |
Jian Li | b54d14b | 2017-03-28 21:34:34 +0900 | [diff] [blame] | 33 | |
| 34 | protected static final String TYPE = "type"; |
| 35 | protected static final String SUBTYPE = "subtype"; |
| 36 | protected static final String UNICAST_WEIGHT = "unicastWeight"; |
| 37 | protected static final String UNICAST_PRIORITY = "unicastPriority"; |
| 38 | protected static final String MULTICAST_WEIGHT = "multicastWeight"; |
| 39 | protected static final String MULTICAST_PRIORITY = "multicastPriority"; |
| 40 | |
| 41 | protected static final String MISSING_MEMBER_MESSAGE = |
| 42 | " member is required in Instruction"; |
| 43 | protected static final String ERROR_MESSAGE = |
| 44 | " not specified in Instruction"; |
| 45 | |
| 46 | @Override |
| 47 | public ObjectNode encode(MappingInstruction instruction, CodecContext context) { |
| 48 | checkNotNull(instruction, "Mapping instruction cannot be null"); |
| 49 | |
| 50 | return new EncodeMappingInstructionCodecHelper(instruction, context).encode(); |
| 51 | } |
Jian Li | 5de36e4 | 2017-03-29 19:40:28 +0900 | [diff] [blame] | 52 | |
| 53 | @Override |
| 54 | public MappingInstruction decode(ObjectNode json, CodecContext context) { |
| 55 | if (json == null || !json.isObject()) { |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | return new DecodeMappingInstructionCodecHelper(json, context).decode(); |
| 60 | } |
Jian Li | b54d14b | 2017-03-28 21:34:34 +0900 | [diff] [blame] | 61 | } |