blob: 9ca74cb56bf5c072dd3c9ff7dada638c411d1f7d [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
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.MacAddress;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.k8snetworking.api.DefaultK8sPort;
24import org.onosproject.k8snetworking.api.K8sPort;
25import org.onosproject.k8snetworking.api.K8sPort.State;
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
34/**
35 * Kubernetes port codec used for serializing and de-serializing JSON string.
36 */
37public final class K8sPortCodec extends JsonCodec<K8sPort> {
38
39 private final Logger log = getLogger(getClass());
40
41 private static final String NETWORK_ID = "networkId";
42 private static final String PORT_ID = "portId";
43 private static final String MAC_ADDRESS = "macAddress";
44 private static final String IP_ADDRESS = "ipAddress";
45 private static final String DEVICE_ID = "deviceId";
46 private static final String PORT_NUMBER = "portNumber";
47 private static final String STATE = "state";
48
49 private static final String MISSING_MESSAGE = " is required in K8sPort";
50
51 @Override
52 public ObjectNode encode(K8sPort port, CodecContext context) {
53 checkNotNull(port, "Kubernetes port cannot be null");
54
55 return context.mapper().createObjectNode()
56 .put(NETWORK_ID, port.networkId())
57 .put(PORT_ID, port.portId())
58 .put(MAC_ADDRESS, port.macAddress().toString())
59 .put(IP_ADDRESS, port.ipAddress().toString())
60 .put(DEVICE_ID, port.deviceId().toString())
61 .put(PORT_NUMBER, port.portNumber().toString())
62 .put(STATE, port.state().name());
63 }
64
65 @Override
66 public K8sPort decode(ObjectNode json, CodecContext context) {
67 if (json == null || !json.isObject()) {
68 return null;
69 }
70
71 String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(),
72 NETWORK_ID + MISSING_MESSAGE);
73 String portId = nullIsIllegal(json.get(PORT_ID).asText(),
74 PORT_ID + MISSING_MESSAGE);
75 String macAddress = nullIsIllegal(json.get(MAC_ADDRESS).asText(),
76 MAC_ADDRESS + MISSING_MESSAGE);
77 String ipAddress = nullIsIllegal(json.get(IP_ADDRESS).asText(),
78 IP_ADDRESS + MISSING_MESSAGE);
79 String deviceId = nullIsIllegal(json.get(DEVICE_ID).asText(),
80 DEVICE_ID + MISSING_MESSAGE);
81 String portNumber = nullIsIllegal(json.get(PORT_NUMBER).asText(),
82 PORT_NUMBER + MISSING_MESSAGE);
83 String state = nullIsIllegal(json.get(STATE).asText(),
84 STATE + MISSING_MESSAGE);
85
86 return DefaultK8sPort.builder()
87 .networkId(networkId)
88 .portId(portId)
89 .macAddress(MacAddress.valueOf(macAddress))
90 .ipAddress(IpAddress.valueOf(ipAddress))
91 .deviceId(DeviceId.deviceId(deviceId))
92 .portNumber(PortNumber.portNumber(portNumber))
93 .state(State.valueOf(state))
94 .build();
95 }
96}