blob: 40c139f975862983252e31dfb71627c4cc1e5468 [file] [log] [blame]
Ray Milkeyc95bb9d2015-01-06 10:28:24 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ray Milkeyc95bb9d2015-01-06 10:28:24 -08003 *
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.codec.impl;
17
18import org.onlab.packet.Ethernet;
19import org.onosproject.codec.CodecContext;
20import org.onosproject.codec.JsonCodec;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import com.fasterxml.jackson.databind.node.ObjectNode;
25
26import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * Ethernet codec.
30 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080031public final class EthernetCodec extends JsonCodec<Ethernet> {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080032
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 Milkeyd03eda02015-01-09 14:58:48 -080039 final ObjectNode result = context.mapper().createObjectNode()
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080040 .put("vlanId", ethernet.getVlanID())
41 .put("etherType", ethernet.getEtherType())
Ray Milkeyd03eda02015-01-09 14:58:48 -080042 .put("priorityCode", ethernet.getPriorityCode())
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080043 .put("pad", ethernet.isPad());
Ray Milkeyd03eda02015-01-09 14:58:48 -080044
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 Milkeyc95bb9d2015-01-06 10:28:24 -080056 }
57
58}