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