blob: 0fe4b1722a13807ad67f981f17ddde3cc1f2630e [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * 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 Krishnaswamy345ee992012-12-13 20:29:48 -080016
17package net.floodlightcontroller.util;
18
19import java.io.IOException;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080020import java.sql.Timestamp;
21
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080022import org.codehaus.jackson.JsonGenerator;
23import org.codehaus.jackson.JsonProcessingException;
24import org.codehaus.jackson.map.JsonSerializer;
25import org.codehaus.jackson.map.SerializerProvider;
26
27
28/**
29 * @author subrata
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080030 */
31
Ray Milkey269ffb92014-04-03 14:43:30 -070032public class EventHistoryBaseInfoJSONSerializer extends
33 JsonSerializer<EventHistoryBaseInfo> {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080034
Ray Milkey269ffb92014-04-03 14:43:30 -070035
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080036 /**
37 * Performs the serialization of a EventHistory.BaseInfo object
38 */
39 @Override
40 public void serialize(EventHistoryBaseInfo base_info, JsonGenerator jGen,
Ray Milkey269ffb92014-04-03 14:43:30 -070041 SerializerProvider serializer)
42 throws IOException, JsonProcessingException {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080043 jGen.writeStartObject();
Ray Milkey269ffb92014-04-03 14:43:30 -070044 jGen.writeNumberField("Idx", base_info.getIdx());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080045 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 Milkey269ffb92014-04-03 14:43:30 -070051 jGen.writeStringField("State", base_info.getState().name());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080052 String acStr = base_info.getAction().name().toLowerCase();
53 // Capitalize the first letter
Ray Milkey269ffb92014-04-03 14:43:30 -070054 acStr = acStr.substring(0, 1).toUpperCase().concat(acStr.substring(1));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080055 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}