blob: 281855ea2ae7a659d04d8326252a03fffadac75a [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
Jian Li9557e902021-06-08 10:12:52 +090039 private static final String VM_NAME = "vmName";
Jian Lica20b712021-01-18 00:19:31 +090040 private static final String NETWORK_ID = "networkId";
41 private static final String MAC_ADDRESS = "macAddress";
42 private static final String IP_ADDRESS = "ipAddress";
43 private static final String DEVICE_ID = "deviceId";
44 private static final String PORT_NUMBER = "portNumber";
Jian Li8f944d42021-03-23 00:43:29 +090045 private static final String SECURITY_GROUPS = "securityGroups";
Jian Lica20b712021-01-18 00:19:31 +090046
47 private static final String MISSING_MESSAGE = " is required in KubevirtPort";
48
49 @Override
50 public ObjectNode encode(KubevirtPort port, CodecContext context) {
51 checkNotNull(port, "Kubevirt port cannot be null");
52
53 ObjectNode result = context.mapper().createObjectNode()
Jian Li9557e902021-06-08 10:12:52 +090054 .put(VM_NAME, port.vmName())
Jian Lica20b712021-01-18 00:19:31 +090055 .put(NETWORK_ID, port.networkId())
56 .put(MAC_ADDRESS, port.macAddress().toString());
57
58 if (port.ipAddress() != null) {
59 result.put(IP_ADDRESS, port.ipAddress().toString());
60 }
61
62 if (port.deviceId() != null) {
63 result.put(DEVICE_ID, port.deviceId().toString());
64 }
65
66 if (port.portNumber() != null) {
67 result.put(PORT_NUMBER, port.portNumber().toString());
68 }
69
Jian Li8f944d42021-03-23 00:43:29 +090070 if (port.securityGroups() != null) {
71 ArrayNode sgIds = context.mapper().createArrayNode();
72 for (String sgId : port.securityGroups()) {
73 sgIds.add(sgId);
74 }
75 result.set(SECURITY_GROUPS, sgIds);
76 }
77
Jian Lica20b712021-01-18 00:19:31 +090078 return result;
79 }
80
81 @Override
82 public KubevirtPort decode(ObjectNode json, CodecContext context) {
83 if (json == null || !json.isObject()) {
84 return null;
85 }
86
Jian Li9557e902021-06-08 10:12:52 +090087 String vmName = nullIsIllegal(json.get(VM_NAME).asText(),
88 VM_NAME + MISSING_MESSAGE);
89
Jian Lica20b712021-01-18 00:19:31 +090090 String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(),
91 NETWORK_ID + MISSING_MESSAGE);
92
93 String macAddress = nullIsIllegal(json.get(MAC_ADDRESS).asText(),
94 MAC_ADDRESS + MISSING_MESSAGE);
95
96 KubevirtPort.Builder builder = DefaultKubevirtPort.builder()
Jian Li9557e902021-06-08 10:12:52 +090097 .vmName(vmName)
Jian Lica20b712021-01-18 00:19:31 +090098 .networkId(networkId)
99 .macAddress(MacAddress.valueOf(macAddress));
100
101 JsonNode ipAddressJson = json.get(IP_ADDRESS);
102 if (ipAddressJson != null) {
103 final IpAddress ipAddress = IpAddress.valueOf(ipAddressJson.asText());
104 builder.ipAddress(ipAddress);
105 }
106
107 JsonNode deviceIdJson = json.get(DEVICE_ID);
108 if (deviceIdJson != null) {
109 final DeviceId deviceId = DeviceId.deviceId(deviceIdJson.asText());
110 builder.deviceId(deviceId);
111 }
112
113 JsonNode portNumberJson = json.get(PORT_NUMBER);
114 if (portNumberJson != null) {
115 final PortNumber portNumber = PortNumber.portNumber(portNumberJson.asText());
116 builder.portNumber(portNumber);
117 }
118
119 log.trace("Port is {}", builder.build().toString());
120
121 return builder.build();
122 }
123}