blob: dbbf9c8b82ea79e309fb8ec04cb6667180f40b7a [file] [log] [blame]
Jian Lid5e8ea82021-01-18 00:19:31 +09001/*
2 * Copyright 2021-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.kubevirtnetworking.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onlab.packet.IpAddress;
21import org.onlab.packet.MacAddress;
22import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.kubevirtnetworking.api.DefaultKubevirtPort;
25import org.onosproject.kubevirtnetworking.api.KubevirtPort;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.PortNumber;
28import org.slf4j.Logger;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31import static org.onlab.util.Tools.nullIsIllegal;
32import static org.slf4j.LoggerFactory.getLogger;
33
34public final class KubevirtPortCodec extends JsonCodec<KubevirtPort> {
35
36 private final Logger log = getLogger(getClass());
37
38 private static final String NETWORK_ID = "networkId";
39 private static final String MAC_ADDRESS = "macAddress";
40 private static final String IP_ADDRESS = "ipAddress";
41 private static final String DEVICE_ID = "deviceId";
42 private static final String PORT_NUMBER = "portNumber";
43
44 private static final String MISSING_MESSAGE = " is required in KubevirtPort";
45
46 @Override
47 public ObjectNode encode(KubevirtPort port, CodecContext context) {
48 checkNotNull(port, "Kubevirt port cannot be null");
49
50 ObjectNode result = context.mapper().createObjectNode()
51 .put(NETWORK_ID, port.networkId())
52 .put(MAC_ADDRESS, port.macAddress().toString());
53
54 if (port.ipAddress() != null) {
55 result.put(IP_ADDRESS, port.ipAddress().toString());
56 }
57
58 if (port.deviceId() != null) {
59 result.put(DEVICE_ID, port.deviceId().toString());
60 }
61
62 if (port.portNumber() != null) {
63 result.put(PORT_NUMBER, port.portNumber().toString());
64 }
65
66 return result;
67 }
68
69 @Override
70 public KubevirtPort decode(ObjectNode json, CodecContext context) {
71 if (json == null || !json.isObject()) {
72 return null;
73 }
74
75 String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(),
76 NETWORK_ID + MISSING_MESSAGE);
77
78 String macAddress = nullIsIllegal(json.get(MAC_ADDRESS).asText(),
79 MAC_ADDRESS + MISSING_MESSAGE);
80
81 KubevirtPort.Builder builder = DefaultKubevirtPort.builder()
82 .networkId(networkId)
83 .macAddress(MacAddress.valueOf(macAddress));
84
85 JsonNode ipAddressJson = json.get(IP_ADDRESS);
86 if (ipAddressJson != null) {
87 final IpAddress ipAddress = IpAddress.valueOf(ipAddressJson.asText());
88 builder.ipAddress(ipAddress);
89 }
90
91 JsonNode deviceIdJson = json.get(DEVICE_ID);
92 if (deviceIdJson != null) {
93 final DeviceId deviceId = DeviceId.deviceId(deviceIdJson.asText());
94 builder.deviceId(deviceId);
95 }
96
97 JsonNode portNumberJson = json.get(PORT_NUMBER);
98 if (portNumberJson != null) {
99 final PortNumber portNumber = PortNumber.portNumber(portNumberJson.asText());
100 builder.portNumber(portNumber);
101 }
102
103 log.trace("Port is {}", builder.build().toString());
104
105 return builder.build();
106 }
107}