Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 1 | /* |
Joey Armstrong | 53fcac2 | 2023-01-11 13:25:01 -0500 | [diff] [blame^] | 2 | * Copyright 2017-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Tunahan Sezen | 03e5527 | 2020-04-18 09:18:53 +0000 | [diff] [blame] | 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.opencord.maclearner.app.rest; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onlab.packet.VlanId; |
| 23 | import org.onosproject.codec.CodecContext; |
| 24 | import org.onosproject.codec.JsonCodec; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.opencord.maclearner.api.DefaultMacLearner; |
| 28 | import org.opencord.maclearner.api.MacLearner; |
| 29 | import org.opencord.maclearner.api.MacLearnerKey; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | |
| 35 | /** |
| 36 | * MAC Learner JSON codec. |
| 37 | */ |
| 38 | public final class MacLearnerCodec extends JsonCodec<MacLearner> { |
| 39 | |
| 40 | private static final Logger log = LoggerFactory.getLogger(MacLearnerCodec.class); |
| 41 | |
| 42 | @Override |
| 43 | public ObjectNode encode(MacLearner macLearner, CodecContext context) { |
| 44 | checkNotNull(macLearner, "macLearner cannot be null"); |
| 45 | |
| 46 | ObjectMapper mapper = context.mapper(); |
| 47 | ObjectNode ofAgentNode = mapper.createObjectNode(); |
| 48 | ofAgentNode |
| 49 | .put(DefaultMacLearner.DEVICE_ID_FN, macLearner.deviceId().toString()) |
| 50 | .put(DefaultMacLearner.PORT_NUMBER_FN, macLearner.portNumber().toString()) |
| 51 | .put(DefaultMacLearner.VLAN_ID_FN, macLearner.vlanId().toString()) |
| 52 | .put(DefaultMacLearner.MAC_ADDRESS_FN, macLearner.macAddress().toString()); |
| 53 | return ofAgentNode; |
| 54 | } |
| 55 | |
| 56 | public ObjectNode encodePort(MacLearnerKey ignoredPort, CodecContext context) { |
| 57 | checkNotNull(ignoredPort, "ignoredPort cannot be null"); |
| 58 | |
| 59 | ObjectMapper mapper = context.mapper(); |
| 60 | ObjectNode ofAgentNode = mapper.createObjectNode(); |
| 61 | ofAgentNode |
| 62 | .put(DefaultMacLearner.DEVICE_ID_FN, ignoredPort.getDeviceId().toString()) |
| 63 | .put(DefaultMacLearner.PORT_NUMBER_FN, ignoredPort.getPortNumber().toString()); |
| 64 | return ofAgentNode; |
| 65 | } |
| 66 | |
| 67 | public ObjectNode encodeMac(MacAddress macAddress, CodecContext context) { |
| 68 | checkNotNull(macAddress, "macAddress cannot be null"); |
| 69 | |
| 70 | ObjectMapper mapper = context.mapper(); |
| 71 | ObjectNode ofAgentNode = mapper.createObjectNode(); |
| 72 | ofAgentNode |
| 73 | .put(DefaultMacLearner.MAC_ADDRESS_FN, macAddress.toString()); |
| 74 | return ofAgentNode; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public MacLearner decode(ObjectNode json, CodecContext context) { |
| 79 | JsonNode deviceId = json.get(DefaultMacLearner.DEVICE_ID_FN); |
| 80 | checkNotNull(deviceId); |
| 81 | JsonNode portNumber = json.get(DefaultMacLearner.PORT_NUMBER_FN); |
| 82 | checkNotNull(portNumber); |
| 83 | JsonNode vlanId = json.get(DefaultMacLearner.VLAN_ID_FN); |
| 84 | checkNotNull(vlanId); |
| 85 | JsonNode macAddress = json.get(DefaultMacLearner.MAC_ADDRESS_FN); |
| 86 | checkNotNull(macAddress); |
| 87 | |
| 88 | return new DefaultMacLearner(DeviceId.deviceId(deviceId.asText()), |
| 89 | PortNumber.portNumber(portNumber.asLong()), |
| 90 | VlanId.vlanId(vlanId.shortValue()), |
| 91 | MacAddress.valueOf(macAddress.asText())); |
| 92 | } |
| 93 | |
| 94 | } |