blob: 796a1b1837b33df2fe7dff948c3cf4687dac169b [file] [log] [blame]
Jian Licc01e452020-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
Daniel Park17fe7982022-04-04 17:48:01 +090018import com.fasterxml.jackson.databind.JsonNode;
Jian Licc01e452020-12-21 01:44:05 +090019import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
22import org.onosproject.kubevirtnode.api.DefaultKubevirtPhyInterface;
23import org.onosproject.kubevirtnode.api.KubevirtPhyInterface;
Daniel Park17fe7982022-04-04 17:48:01 +090024import org.onosproject.net.DeviceId;
Jian Licc01e452020-12-21 01:44:05 +090025
26import static com.google.common.base.Preconditions.checkNotNull;
27import static org.onlab.util.Tools.nullIsIllegal;
28
29/**
30 * Kubevirt physical interface codec used for serializing and de-serializing JSON string.
31 */
32public final class KubevirtPhyInterfaceCodec extends JsonCodec<KubevirtPhyInterface> {
33
34 private static final String NETWORK = "network";
35 private static final String INTERFACE = "intf";
Daniel Park17fe7982022-04-04 17:48:01 +090036 private static final String PHYS_BRIDGE_ID = "physBridgeId";
Jian Licc01e452020-12-21 01:44:05 +090037
38 private static final String MISSING_MESSAGE = " is required in KubevirtPhyInterface";
39
40 @Override
41 public ObjectNode encode(KubevirtPhyInterface phyIntf, CodecContext context) {
42 checkNotNull(phyIntf, "Kubevirt physical interface cannot be null");
43
Daniel Park17fe7982022-04-04 17:48:01 +090044 ObjectNode result = context.mapper().createObjectNode()
Jian Licc01e452020-12-21 01:44:05 +090045 .put(NETWORK, phyIntf.network())
46 .put(INTERFACE, phyIntf.intf());
Daniel Park17fe7982022-04-04 17:48:01 +090047
48 if (phyIntf.physBridge() != null) {
49 result.put(PHYS_BRIDGE_ID, phyIntf.physBridge().toString());
50 }
51
52 return result;
Jian Licc01e452020-12-21 01:44:05 +090053 }
54
55 @Override
56 public KubevirtPhyInterface decode(ObjectNode json, CodecContext context) {
57 if (json == null || !json.isObject()) {
58 return null;
59 }
60
61 String network = nullIsIllegal(json.get(NETWORK).asText(),
62 NETWORK + MISSING_MESSAGE);
63 String intf = nullIsIllegal(json.get(INTERFACE).asText(),
64 INTERFACE + MISSING_MESSAGE);
65
Daniel Park17fe7982022-04-04 17:48:01 +090066 KubevirtPhyInterface.Builder builder = DefaultKubevirtPhyInterface.builder()
Jian Licc01e452020-12-21 01:44:05 +090067 .network(network)
Daniel Park17fe7982022-04-04 17:48:01 +090068 .intf(intf);
69
70 JsonNode physBridgeJson = json.get(PHYS_BRIDGE_ID);
71 if (physBridgeJson != null) {
72 builder.physBridge(DeviceId.deviceId(physBridgeJson.asText()));
73 }
74
75 return builder.build();
Jian Licc01e452020-12-21 01:44:05 +090076 }
77}