blob: 57a5e48734bd45d10d86ab84888ba38f8da4546c [file] [log] [blame]
Jian Lib54d14b2017-03-28 21:34:34 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Lib54d14b2017-03-28 21:34:34 +09003 *
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 Li2e818b02017-04-12 19:28:30 +090016package org.onosproject.mapping.codec;
Jian Lib54d14b2017-03-28 21:34:34 +090017
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onosproject.codec.CodecContext;
20import org.onosproject.codec.JsonCodec;
21import org.onosproject.mapping.instructions.MappingInstruction;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import static com.google.common.base.Preconditions.checkNotNull;
26
27/**
28 * Mapping instruction codec.
29 */
30public final class MappingInstructionCodec extends JsonCodec<MappingInstruction> {
31
Jian Li8e072712017-04-14 21:52:59 +090032 protected static final Logger log = LoggerFactory.getLogger(MappingInstructionCodec.class);
Jian Lib54d14b2017-03-28 21:34:34 +090033
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 Li5de36e42017-03-29 19:40:28 +090052
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 Lib54d14b2017-03-28 21:34:34 +090061}