Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.k8snetworking.codec; |
| 17 | |
Jian Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onosproject.codec.CodecContext; |
| 23 | import org.onosproject.codec.JsonCodec; |
| 24 | import org.onosproject.k8snetworking.api.DefaultK8sPort; |
| 25 | import org.onosproject.k8snetworking.api.K8sPort; |
| 26 | import org.onosproject.k8snetworking.api.K8sPort.State; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.PortNumber; |
| 29 | import org.slf4j.Logger; |
| 30 | |
| 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | import static org.onlab.util.Tools.nullIsIllegal; |
| 33 | import static org.slf4j.LoggerFactory.getLogger; |
| 34 | |
| 35 | /** |
| 36 | * Kubernetes port codec used for serializing and de-serializing JSON string. |
| 37 | */ |
| 38 | public 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 Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 56 | ObjectNode result = context.mapper().createObjectNode() |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 57 | .put(NETWORK_ID, port.networkId()) |
| 58 | .put(PORT_ID, port.portId()) |
| 59 | .put(MAC_ADDRESS, port.macAddress().toString()) |
Jian Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 60 | .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 Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 75 | } |
| 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 Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 91 | |
Jian Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 92 | K8sPort.Builder builder = DefaultK8sPort.builder() |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 93 | .networkId(networkId) |
| 94 | .portId(portId) |
| 95 | .macAddress(MacAddress.valueOf(macAddress)) |
Jian Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 96 | .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())); |
| 111 | } |
| 112 | |
| 113 | return builder.build(); |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 114 | } |
| 115 | } |