blob: b32bd1494de771eae15db25373c15c6bf9b3e77c [file] [log] [blame]
Pier Ventre9cf536b2016-10-21 13:30:18 -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 */
16
17package org.onosproject.driver.extensions.codec;
18
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
22import org.onosproject.driver.extensions.Ofdpa3MatchMplsL2Port;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25import static org.onlab.util.Tools.nullIsIllegal;
26
27/**
28 * JSON Codec for Ofdpa match mpls l2 port class.
29 */
30public class Ofdpa3MatchMplsL2PortCodec extends JsonCodec<Ofdpa3MatchMplsL2Port> {
31
32 private static final String MPLS_L2_PORT = "mplsL2Port";
33 private static final String MISSING_MEMBER_MESSAGE = " member is required in Ofdpa3MatchMplsL2Port";
34 private static final String MISSING_MPLS_L2_PORT_MESSAGE = "MPLS L2 Port cannot be null";
35
36 @Override
37 public ObjectNode encode(Ofdpa3MatchMplsL2Port matchMplsL2Port, CodecContext context) {
38 checkNotNull(matchMplsL2Port, MISSING_MPLS_L2_PORT_MESSAGE);
39 return context.mapper().createObjectNode()
40 .put(MPLS_L2_PORT, matchMplsL2Port.mplsL2Port());
41 }
42
43 @Override
44 public Ofdpa3MatchMplsL2Port decode(ObjectNode json, CodecContext context) {
45 if (json == null || !json.isObject()) {
46 return null;
47 }
48
49 // parse ofdpa match mpls l2 port
50 int mplsL2Port = (int) nullIsIllegal(json.get(MPLS_L2_PORT),
51 MPLS_L2_PORT + MISSING_MEMBER_MESSAGE).asInt();
52 return new Ofdpa3MatchMplsL2Port(mplsL2Port);
53 }
54}