Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [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.onosproject.codec.impl; |
| 17 | |
| 18 | import org.onlab.packet.Ethernet; |
| 19 | import org.onosproject.codec.CodecContext; |
| 20 | import org.onosproject.codec.JsonCodec; |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
| 24 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | |
| 28 | /** |
| 29 | * Ethernet codec. |
| 30 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 31 | public final class EthernetCodec extends JsonCodec<Ethernet> { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 32 | |
| 33 | protected static final Logger log = LoggerFactory.getLogger(CriterionCodec.class); |
| 34 | |
| 35 | @Override |
| 36 | public ObjectNode encode(Ethernet ethernet, CodecContext context) { |
| 37 | checkNotNull(ethernet, "Ethernet cannot be null"); |
| 38 | |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 39 | final ObjectNode result = context.mapper().createObjectNode() |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 40 | .put("vlanId", ethernet.getVlanID()) |
| 41 | .put("etherType", ethernet.getEtherType()) |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 42 | .put("priorityCode", ethernet.getPriorityCode()) |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 43 | .put("pad", ethernet.isPad()); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 44 | |
| 45 | if (ethernet.getDestinationMAC() != null) { |
| 46 | result.put("destMac", |
| 47 | ethernet.getDestinationMAC().toString()); |
| 48 | } |
| 49 | |
| 50 | if (ethernet.getSourceMAC() != null) { |
| 51 | result.put("srcMac", |
| 52 | ethernet.getSourceMAC().toString()); |
| 53 | } |
| 54 | |
| 55 | return result; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | } |