Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
| 17 | |
| 18 | package org.openflow.protocol.serializers; |
| 19 | |
| 20 | import java.io.IOException; |
| 21 | |
| 22 | import org.codehaus.jackson.JsonGenerator; |
| 23 | import org.codehaus.jackson.JsonProcessingException; |
| 24 | import org.codehaus.jackson.map.JsonSerializer; |
| 25 | import org.codehaus.jackson.map.SerializerProvider; |
| 26 | import org.openflow.protocol.OFMatch; |
| 27 | import org.openflow.util.HexString; |
| 28 | |
| 29 | public class OFMatchJSONSerializer extends JsonSerializer<OFMatch> { |
| 30 | |
| 31 | /** |
| 32 | * Converts an IP in a 32 bit integer to a dotted-decimal string |
| 33 | * @param i The IP address in a 32 bit integer |
| 34 | * @return An IP address string in dotted-decimal |
| 35 | */ |
| 36 | private String intToIp(int i) { |
| 37 | return ((i >> 24 ) & 0xFF) + "." + |
| 38 | ((i >> 16 ) & 0xFF) + "." + |
| 39 | ((i >> 8 ) & 0xFF) + "." + |
| 40 | ( i & 0xFF); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Performs the serialization of a OFMatch object |
| 45 | */ |
| 46 | @Override |
| 47 | public void serialize(OFMatch match, JsonGenerator jGen, |
| 48 | SerializerProvider serializer) |
| 49 | throws IOException, JsonProcessingException { |
| 50 | jGen.writeStartObject(); |
| 51 | jGen.writeStringField("dataLayerDestination", |
| 52 | HexString.toHexString(match.getDataLayerDestination())); |
| 53 | jGen.writeStringField("dataLayerSource", |
| 54 | HexString.toHexString(match.getDataLayerSource())); |
| 55 | String dataType = Integer.toHexString(match.getDataLayerType()); |
| 56 | while (dataType.length() < 4) { |
| 57 | dataType = "0".concat(dataType); |
| 58 | } |
| 59 | jGen.writeStringField("dataLayerType", "0x" + dataType); |
| 60 | jGen.writeNumberField("dataLayerVirtualLan", |
| 61 | match.getDataLayerVirtualLan()); |
| 62 | jGen.writeNumberField("dataLayerVirtualLanPriorityCodePoint", |
| 63 | match.getDataLayerVirtualLanPriorityCodePoint()); |
| 64 | jGen.writeNumberField("inputPort", match.getInputPort()); |
| 65 | jGen.writeStringField("networkDestination", |
| 66 | intToIp(match.getNetworkDestination())); |
| 67 | jGen.writeNumberField("networkDestinationMaskLen", |
| 68 | match.getNetworkDestinationMaskLen()); |
| 69 | jGen.writeNumberField("networkProtocol", match.getNetworkProtocol()); |
| 70 | jGen.writeStringField("networkSource", |
| 71 | intToIp(match.getNetworkSource())); |
| 72 | jGen.writeNumberField("networkSourceMaskLen", |
| 73 | match.getNetworkSourceMaskLen()); |
| 74 | jGen.writeNumberField("networkTypeOfService", |
| 75 | match.getNetworkTypeOfService()); |
| 76 | jGen.writeNumberField("transportDestination", |
| 77 | match.getTransportDestination()); |
| 78 | jGen.writeNumberField("transportSource", |
| 79 | match.getTransportSource()); |
| 80 | jGen.writeNumberField("wildcards", match.getWildcards()); |
| 81 | jGen.writeEndObject(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Tells SimpleModule that we are the serializer for OFMatch |
| 86 | */ |
| 87 | @Override |
| 88 | public Class<OFMatch> handledType() { |
| 89 | return OFMatch.class; |
| 90 | } |
| 91 | } |