blob: debc4193615700dd6050736695446c4ef784ffda [file] [log] [blame]
Jian Li9ee9c8b2019-01-24 11:48:12 +09001/*
2 * Copyright 2019-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.k8snetworking.codec;
17
Jian Lia80b1582019-01-25 12:47:42 +090018import com.fasterxml.jackson.databind.JsonNode;
Jian Li9ee9c8b2019-01-24 11:48:12 +090019import 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.k8snetworking.api.DefaultK8sPort;
25import org.onosproject.k8snetworking.api.K8sPort;
26import org.onosproject.k8snetworking.api.K8sPort.State;
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
35/**
36 * Kubernetes port codec used for serializing and de-serializing JSON string.
37 */
38public final class K8sPortCodec extends JsonCodec<K8sPort> {
39
40 private final Logger log = getLogger(getClass());
41
42 private static final String NETWORK_ID = "networkId";
43 private static final String PORT_ID = "portId";
44 private static final String MAC_ADDRESS = "macAddress";
45 private static final String IP_ADDRESS = "ipAddress";
46 private static final String DEVICE_ID = "deviceId";
47 private static final String PORT_NUMBER = "portNumber";
48 private static final String STATE = "state";
49
50 private static final String MISSING_MESSAGE = " is required in K8sPort";
51
52 @Override
53 public ObjectNode encode(K8sPort port, CodecContext context) {
54 checkNotNull(port, "Kubernetes port cannot be null");
55
Jian Lia80b1582019-01-25 12:47:42 +090056 ObjectNode result = context.mapper().createObjectNode()
Jian Li9ee9c8b2019-01-24 11:48:12 +090057 .put(NETWORK_ID, port.networkId())
58 .put(PORT_ID, port.portId())
59 .put(MAC_ADDRESS, port.macAddress().toString())
Jian Lia80b1582019-01-25 12:47:42 +090060 .put(IP_ADDRESS, port.ipAddress().toString());
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
70 if (port.state() != null) {
71 result.put(STATE, port.state().name());
72 }
73
74 return result;
Jian Li9ee9c8b2019-01-24 11:48:12 +090075 }
76
77 @Override
78 public K8sPort decode(ObjectNode json, CodecContext context) {
79 if (json == null || !json.isObject()) {
80 return null;
81 }
82
83 String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(),
84 NETWORK_ID + MISSING_MESSAGE);
85 String portId = nullIsIllegal(json.get(PORT_ID).asText(),
86 PORT_ID + MISSING_MESSAGE);
87 String macAddress = nullIsIllegal(json.get(MAC_ADDRESS).asText(),
88 MAC_ADDRESS + MISSING_MESSAGE);
89 String ipAddress = nullIsIllegal(json.get(IP_ADDRESS).asText(),
90 IP_ADDRESS + MISSING_MESSAGE);
Jian Li9ee9c8b2019-01-24 11:48:12 +090091
Jian Lia80b1582019-01-25 12:47:42 +090092 K8sPort.Builder builder = DefaultK8sPort.builder()
Jian Li9ee9c8b2019-01-24 11:48:12 +090093 .networkId(networkId)
94 .portId(portId)
95 .macAddress(MacAddress.valueOf(macAddress))
Jian Lia80b1582019-01-25 12:47:42 +090096 .ipAddress(IpAddress.valueOf(ipAddress));
97
98 JsonNode deviceIdJson = json.get(DEVICE_ID);
99 if (deviceIdJson != null) {
100 builder.deviceId(DeviceId.deviceId(deviceIdJson.asText()));
101 }
102
103 JsonNode portNumberJson = json.get(PORT_NUMBER);
104 if (portNumberJson != null) {
105 builder.portNumber(PortNumber.portNumber(portNumberJson.asText()));
106 }
107
108 JsonNode stateJson = json.get(STATE);
109 if (stateJson != null) {
110 builder.state(State.valueOf(stateJson.asText()));
Jian Li4aa17642019-01-30 00:01:11 +0900111 } else {
112 builder.state(State.INACTIVE);
Jian Lia80b1582019-01-25 12:47:42 +0900113 }
114
115 return builder.build();
Jian Li9ee9c8b2019-01-24 11:48:12 +0900116 }
117}