blob: db351280c773650e4a3f8d7b89ed260167d58fa2 [file] [log] [blame]
Tunahan Sezen03e55272020-04-18 09:18:53 +00001/*
Joey Armstrong53fcac22023-01-11 13:25:01 -05002 * Copyright 2017-2023 Open Networking Foundation (ONF) and the ONF Contributors
Tunahan Sezen03e55272020-04-18 09:18:53 +00003 *
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.opencord.maclearner.app.rest;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onlab.packet.MacAddress;
22import org.onlab.packet.VlanId;
23import org.onosproject.codec.CodecContext;
24import org.onosproject.codec.JsonCodec;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.PortNumber;
27import org.opencord.maclearner.api.DefaultMacLearner;
28import org.opencord.maclearner.api.MacLearner;
29import org.opencord.maclearner.api.MacLearnerKey;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import static com.google.common.base.Preconditions.checkNotNull;
34
35/**
36 * MAC Learner JSON codec.
37 */
38public 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}