Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | * not use this file except in compliance with the License. You may obtain |
| 6 | * 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, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations |
| 14 | * under the License. |
| 15 | **/ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 16 | |
| 17 | package net.floodlightcontroller.util; |
| 18 | |
| 19 | import java.io.IOException; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 20 | import java.sql.Timestamp; |
| 21 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 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 | |
| 27 | |
| 28 | /** |
| 29 | * @author subrata |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 30 | */ |
| 31 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 32 | public class EventHistoryBaseInfoJSONSerializer extends |
| 33 | JsonSerializer<EventHistoryBaseInfo> { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 34 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 35 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 36 | /** |
| 37 | * Performs the serialization of a EventHistory.BaseInfo object |
| 38 | */ |
| 39 | @Override |
| 40 | public void serialize(EventHistoryBaseInfo base_info, JsonGenerator jGen, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 41 | SerializerProvider serializer) |
| 42 | throws IOException, JsonProcessingException { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 43 | jGen.writeStartObject(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 44 | jGen.writeNumberField("Idx", base_info.getIdx()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 45 | Timestamp ts = new Timestamp(base_info.getTime_ms()); |
| 46 | String tsStr = ts.toString(); |
| 47 | while (tsStr.length() < 23) { |
| 48 | tsStr = tsStr.concat("0"); |
| 49 | } |
| 50 | jGen.writeStringField("Time", tsStr); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 51 | jGen.writeStringField("State", base_info.getState().name()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 52 | String acStr = base_info.getAction().name().toLowerCase(); |
| 53 | // Capitalize the first letter |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 54 | acStr = acStr.substring(0, 1).toUpperCase().concat(acStr.substring(1)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 55 | jGen.writeStringField("Action", acStr); |
| 56 | jGen.writeEndObject(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Tells SimpleModule that we are the serializer for OFMatch |
| 61 | */ |
| 62 | @Override |
| 63 | public Class<EventHistoryBaseInfo> handledType() { |
| 64 | return EventHistoryBaseInfo.class; |
| 65 | } |
| 66 | } |