blob: c72aa2ccf664cb3cc72d9f6054770468d3f39edb [file] [log] [blame]
Andreas Pantelopoulosfdcfe532018-04-02 10:59:23 -07001/*
2 * Copyright 2017-present Open Networking Foundation
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.OfdpaMatchAllowVlanTranslation;
22
23import static com.google.common.base.Preconditions.checkNotNull;
24import static org.onlab.util.Tools.nullIsIllegal;
25
26/**
27 * JSON Codec for Ofdpa match allow vlan translation class.
28 */
29public class OfdpaMatchAllowVlanTranslationCodec extends JsonCodec<OfdpaMatchAllowVlanTranslation> {
30
31 private static final String ALLOW_VLAN_TRANSLATION = "allowVlanTranslation";
32 private static final String MISSING_MEMBER_MESSAGE = " member is required in OfdpaMatchAllowVlanTranslation";
33 private static final String MISSING_ALLOW_VLAN_TRANSLATION_MESSAGE = "Allow Vlan Translation cannot be null";
34
35 @Override
36 public ObjectNode encode(OfdpaMatchAllowVlanTranslation allowVlanTranslation, CodecContext context) {
37 checkNotNull(allowVlanTranslation, MISSING_ALLOW_VLAN_TRANSLATION_MESSAGE);
38 return context.mapper().createObjectNode().put(ALLOW_VLAN_TRANSLATION,
39 allowVlanTranslation.allowVlanTranslation());
40 }
41
42 @Override
43 public OfdpaMatchAllowVlanTranslation decode(ObjectNode json, CodecContext context) {
44 if (json == null || !json.isObject()) {
45 return null;
46 }
47
48 // parse ofdpa match allow vlan translation
49 short allowVlanTranslation = (short) nullIsIllegal(json.get(ALLOW_VLAN_TRANSLATION),
50 ALLOW_VLAN_TRANSLATION + MISSING_MEMBER_MESSAGE).asLong();
51 return new OfdpaMatchAllowVlanTranslation(allowVlanTranslation);
52 }
53}