blob: 3ba0164055f1575fb3408e0b2055fa66b9011f69 [file] [log] [blame]
Jian Li171582e2020-12-21 01:44:05 +09001/*
2 * Copyright 2020-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.kubevirtnode.codec;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onosproject.codec.CodecContext;
20import org.onosproject.codec.JsonCodec;
21import org.onosproject.kubevirtnode.api.DefaultKubevirtPhyInterface;
22import org.onosproject.kubevirtnode.api.KubevirtPhyInterface;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25import static org.onlab.util.Tools.nullIsIllegal;
26
27/**
28 * Kubevirt physical interface codec used for serializing and de-serializing JSON string.
29 */
30public final class KubevirtPhyInterfaceCodec extends JsonCodec<KubevirtPhyInterface> {
31
32 private static final String NETWORK = "network";
33 private static final String INTERFACE = "intf";
34
35 private static final String MISSING_MESSAGE = " is required in KubevirtPhyInterface";
36
37 @Override
38 public ObjectNode encode(KubevirtPhyInterface phyIntf, CodecContext context) {
39 checkNotNull(phyIntf, "Kubevirt physical interface cannot be null");
40
41 return context.mapper().createObjectNode()
42 .put(NETWORK, phyIntf.network())
43 .put(INTERFACE, phyIntf.intf());
44 }
45
46 @Override
47 public KubevirtPhyInterface decode(ObjectNode json, CodecContext context) {
48 if (json == null || !json.isObject()) {
49 return null;
50 }
51
52 String network = nullIsIllegal(json.get(NETWORK).asText(),
53 NETWORK + MISSING_MESSAGE);
54 String intf = nullIsIllegal(json.get(INTERFACE).asText(),
55 INTERFACE + MISSING_MESSAGE);
56
57 return DefaultKubevirtPhyInterface.builder()
58 .network(network)
59 .intf(intf)
60 .build();
61 }
62}