blob: e6ca4a99da4e97b72d6c49f7ea137eacf29d7327 [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.NiciraMatchNshSi;
22import org.onosproject.net.NshServiceIndex;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25import static org.onlab.util.Tools.nullIsIllegal;
26
27/**
28 * JSON Codec for NiciraMatchNshSi class.
29 */
30public final class NiciraMatchNshSiCodec extends JsonCodec<NiciraMatchNshSi> {
31
32 private static final String NSH_SERVICE_INDEX = "nsi";
33
34 private static final String MISSING_MEMBER_MESSAGE = " member is required in NiciraMatchNshSi";
35
36 @Override
37 public ObjectNode encode(NiciraMatchNshSi niciraMatchNshSi, CodecContext context) {
38 checkNotNull(niciraMatchNshSi, "Nicira Match Nsh Si cannot be null");
39 ObjectNode root = context.mapper().createObjectNode()
40 .put(NSH_SERVICE_INDEX, niciraMatchNshSi.nshSi().serviceIndex());
41 return root;
42 }
43
44 @Override
45 public NiciraMatchNshSi decode(ObjectNode json, CodecContext context) {
46 if (json == null || !json.isObject()) {
47 return null;
48 }
49
50 // parse nsh service index
51 short nshSiShort = (short) nullIsIllegal(json.get(NSH_SERVICE_INDEX),
52 NSH_SERVICE_INDEX + MISSING_MEMBER_MESSAGE).asInt();
53 NshServiceIndex nshSi = NshServiceIndex.of(nshSiShort);
54
55 NiciraMatchNshSi niciraMatchNshSi = new NiciraMatchNshSi(nshSi);
56
57 return niciraMatchNshSi;
58 }
59}