Ray Milkey | 7d7f0a0 | 2014-06-18 12:52:13 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent.runtime.web.serializers; |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 2 | |
| 3 | |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame^] | 4 | import java.io.IOException; |
| 5 | |
Pavlin Radoslavov | dc59c15 | 2014-06-17 17:04:41 -0700 | [diff] [blame] | 6 | import net.floodlightcontroller.util.MACAddress; |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.intent.ShortestPathIntent; |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame^] | 8 | |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 9 | import org.codehaus.jackson.JsonGenerator; |
| 10 | import org.codehaus.jackson.map.SerializerProvider; |
| 11 | import org.codehaus.jackson.map.ser.std.SerializerBase; |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame^] | 12 | import org.projectfloodlight.openflow.util.HexString; |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 13 | |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame^] | 14 | import com.google.common.net.InetAddresses; |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * JSON serializer for ShortestPathIntents. |
| 18 | */ |
| 19 | public class ShortestPathIntentSerializer extends SerializerBase<ShortestPathIntent> { |
| 20 | |
| 21 | /** |
| 22 | * Public constructor - just calls its super class constructor. |
| 23 | */ |
| 24 | public ShortestPathIntentSerializer() { |
| 25 | super(ShortestPathIntent.class); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Converts an integer into a string representing an IP address. |
| 30 | * |
| 31 | * @param ipAddress integer representation of the address |
| 32 | * @return string that represents the address |
| 33 | */ |
| 34 | private String toIPAddressString(final int ipAddress) { |
| 35 | return InetAddresses.fromInteger(ipAddress).getHostAddress(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Serializes a ShortestPathIntent object. |
| 40 | * |
| 41 | * @param intent Intent to serialize |
| 42 | * @param jsonGenerator generator to add the serialized object to |
| 43 | * @param serializerProvider not used |
| 44 | * @throws IOException if the JSON serialization fails |
| 45 | */ |
| 46 | @Override |
| 47 | public void serialize(final ShortestPathIntent intent, |
| 48 | final JsonGenerator jsonGenerator, |
| 49 | final SerializerProvider serializerProvider) |
| 50 | throws IOException { |
| 51 | jsonGenerator.writeStartObject(); |
| 52 | |
| 53 | jsonGenerator.writeStringField("id", intent.getId()); |
| 54 | jsonGenerator.writeStringField("state", intent.getState().toString()); |
| 55 | jsonGenerator.writeStringField("pathFrozen", |
| 56 | Boolean.toString(intent.isPathFrozen())); |
| 57 | |
| 58 | jsonGenerator.writeStringField("srcSwitchDpid", |
| 59 | HexString.toHexString(intent.getSrcSwitchDpid())); |
| 60 | jsonGenerator.writeStringField("srcPortNumber", |
| 61 | Long.toString(intent.getSrcPortNumber())); |
| 62 | jsonGenerator.writeStringField("srcMac", |
Pavlin Radoslavov | dc59c15 | 2014-06-17 17:04:41 -0700 | [diff] [blame] | 63 | MACAddress.valueOf(intent.getSrcMac()).toString()); |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 64 | jsonGenerator.writeStringField("srcIp", |
| 65 | toIPAddressString(intent.getSrcIp())); |
| 66 | |
| 67 | jsonGenerator.writeStringField("dstSwitchDpid", |
| 68 | HexString.toHexString(intent.getDstSwitchDpid())); |
| 69 | jsonGenerator.writeStringField("dstPortNumber", |
| 70 | Long.toString(intent.getDstPortNumber())); |
| 71 | jsonGenerator.writeStringField("dstMac", |
Pavlin Radoslavov | dc59c15 | 2014-06-17 17:04:41 -0700 | [diff] [blame] | 72 | MACAddress.valueOf(intent.getDstMac()).toString()); |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 73 | jsonGenerator.writeStringField("dstIp", |
| 74 | toIPAddressString(intent.getDstIp())); |
| 75 | |
| 76 | jsonGenerator.writeStringField("idleTimeout", |
| 77 | Integer.toString(intent.getIdleTimeout())); |
| 78 | jsonGenerator.writeStringField("hardTimeout", |
| 79 | Integer.toString(intent.getHardTimeout())); |
| 80 | jsonGenerator.writeStringField("firstSwitchIdleTimeout", |
| 81 | Integer.toString(intent.getFirstSwitchIdleTimeout())); |
| 82 | jsonGenerator.writeStringField("firstSwitchHardTimeout", |
| 83 | Integer.toString(intent.getFirstSwitchHardTimeout())); |
| 84 | |
| 85 | jsonGenerator.writeArrayFieldStart("logs"); |
| 86 | for (final String log : intent.getLogs()) { |
| 87 | jsonGenerator.writeObject(log); |
| 88 | } |
| 89 | jsonGenerator.writeEndArray(); |
| 90 | |
| 91 | jsonGenerator.writeEndObject(); |
| 92 | } |
| 93 | } |