blob: 88665623d6c53f01f853fd842eb136409c0e83cf [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.NiciraSetNshContextHeader;
22import org.onosproject.net.NshContextHeader;
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 NiciraSetNshContextHeader class.
30 */
31public class NiciraSetNshContextHeaderCodec extends JsonCodec<NiciraSetNshContextHeader> {
32
33 private static final String NSH_CONTEXT_HEADER = "nshch";
34 private static final String TYPE = "type";
35
36 private static final String MISSING_MEMBER_MESSAGE = " member is required in NiciraSetNshContextHeader";
37
38 @Override
39 public ObjectNode encode(NiciraSetNshContextHeader niciraSetNshContextHeader, CodecContext context) {
40 checkNotNull(niciraSetNshContextHeader, "Nicira Set Nsh Context Header cannot be null");
41 ObjectNode root = context.mapper().createObjectNode()
42 .put(NSH_CONTEXT_HEADER, niciraSetNshContextHeader.nshCh().nshContextHeader())
43 .put(TYPE, niciraSetNshContextHeader.type().type());
44 return root;
45 }
46
47 @Override
48 public NiciraSetNshContextHeader decode(ObjectNode json, CodecContext context) {
49 if (json == null || !json.isObject()) {
50 return null;
51 }
52
53 // parse nsh context header
54 int contextHeaderInt = nullIsIllegal(json.get(NSH_CONTEXT_HEADER),
55 NSH_CONTEXT_HEADER + MISSING_MEMBER_MESSAGE).asInt();
56
57 NshContextHeader contextHeader = NshContextHeader.of(contextHeaderInt);
58
59 // parse type
60 int extensionTypeInt = nullIsIllegal(json.get(TYPE),
61 TYPE + MISSING_MEMBER_MESSAGE).asInt();
62
63 ExtensionTreatmentType type = new ExtensionTreatmentType(extensionTypeInt);
64
Jian Li08926a92016-05-05 15:35:40 -070065 return new NiciraSetNshContextHeader(contextHeader, type);
Jian Lidab72562016-04-12 14:10:32 -070066 }
67}