blob: fda14dbda21821bfe1b9a1de3d38db45053a9f16 [file] [log] [blame]
Harshada Chaundkarb78db192019-02-19 16:18:16 -05001/*
2 * Copyright 2015-present Open Networking Foundation
3 *
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.fpm.web;
17
18import org.onlab.util.Tools;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.routing.fpm.FpmPeerInfo;
24
25/**
26 * Codec of FpmPeerInfo class.
27 */
28public final class FpmCodec extends JsonCodec<FpmPeerInfo> {
29
30 // JSON field names
31 private static final String PEER_ADDRESS = "peerAddress";
32 private static final String PEER_PORT = "peerPort";
33 private static final String CONNECTED_TO = "connectedTo";
34 private static final String CONNECTION_TIME = "connectionTime";
35 private static final String LOCAL_ROUTES = "localRoutes";
36
37
38 @Override
39 public ObjectNode encode(FpmPeerInfo fpmPeerInfo, CodecContext context) {
40 final ObjectNode fpmConnectionArray = context.mapper().createObjectNode();
41
42 ArrayNode connectionArray = context.mapper().createArrayNode();
43 fpmPeerInfo.connections().forEach(connection -> {
44 ObjectNode fpmNode = context.mapper().createObjectNode();
45 fpmNode.put(PEER_ADDRESS, connection.peer().address().toString());
46 fpmNode.put(PEER_PORT, connection.peer().port());
47 fpmNode.put(CONNECTED_TO, connection.connectedTo().toString());
48 fpmNode.put(CONNECTION_TIME, Tools.timeAgo(connection.connectTime()));
49 fpmNode.put(LOCAL_ROUTES, fpmPeerInfo.routes());
50 connectionArray.add(fpmNode);
51 });
52
53 fpmConnectionArray.put("connection", connectionArray);
54 return fpmConnectionArray;
55 }
56
57
58
59}