blob: f3938e69057ee881b5b94e42ae4b170d27d4f097 [file] [log] [blame]
Jian Lidab72562016-04-12 14:10:32 -07001/*
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 */
16package org.onosproject.driver.extensions.codec;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onosproject.codec.CodecContext;
20import org.onosproject.codec.JsonCodec;
21import org.onosproject.driver.extensions.DefaultMoveExtensionTreatment;
22import org.onosproject.driver.extensions.MoveExtensionTreatment;
23import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
24
25import static com.google.common.base.Preconditions.checkNotNull;
26import static org.onlab.util.Tools.nullIsIllegal;
27
28/**
29 * JSON Codec for MoveExtensionTreatment class.
30 */
31public final class MoveExtensionTreatmentCodec extends JsonCodec<MoveExtensionTreatment> {
32
33 private static final String SRC_OFS = "srcOfs";
34 private static final String DST_OFS = "dstOfs";
35 private static final String N_BITS = "nBits";
36 private static final String SRC = "src";
37 private static final String DST = "dst";
38 private static final String TYPE = "type";
39
40 private static final String MISSING_MEMBER_MESSAGE = " member is required in MoveExtensionTreatment";
41
42 @Override
43 public ObjectNode encode(MoveExtensionTreatment moveExtensionTreatment, CodecContext context) {
44 checkNotNull(moveExtensionTreatment, "Move Extension Treatment cannot be null");
45 ObjectNode root = context.mapper().createObjectNode()
46 .put(SRC_OFS, moveExtensionTreatment.srcOffset())
47 .put(DST_OFS, moveExtensionTreatment.dstOffset())
48 .put(N_BITS, moveExtensionTreatment.nBits())
49 .put(SRC, moveExtensionTreatment.src())
50 .put(DST, moveExtensionTreatment.dst());
51 return root;
52 }
53
54 @Override
55 public MoveExtensionTreatment decode(ObjectNode json, CodecContext context) {
56 if (json == null || !json.isObject()) {
57 return null;
58 }
59
60 // parse extension treatment type
61 ExtensionTreatmentType type = new ExtensionTreatmentType(nullIsIllegal(json.get(TYPE),
62 TYPE + MISSING_MEMBER_MESSAGE).asInt());
63
64 // parse src off set
65 int srcOfs = nullIsIllegal(json.get(SRC_OFS), SRC_OFS + MISSING_MEMBER_MESSAGE).asInt();
66
67 // parse dst off set
68 int dstOfs = nullIsIllegal(json.get(DST_OFS), DST_OFS + MISSING_MEMBER_MESSAGE).asInt();
69
70 // parse n bits
71 int nBits = nullIsIllegal(json.get(N_BITS), N_BITS + MISSING_MEMBER_MESSAGE).asInt();
72
73 // parse src
74 int src = nullIsIllegal(json.get(SRC), SRC + MISSING_MEMBER_MESSAGE).asInt();
75
76 // parse dst
77 int dst = nullIsIllegal(json.get(DST), DST + MISSING_MEMBER_MESSAGE).asInt();
78
Jian Li08926a92016-05-05 15:35:40 -070079 return new DefaultMoveExtensionTreatment(srcOfs, dstOfs, nBits, src, dst, type);
Jian Lidab72562016-04-12 14:10:32 -070080 }
81}