blob: fa127e48dbc56685c4d5fd634351105505049854 [file] [log] [blame]
Harshada Chaundkar77fa8ab2019-01-14 14:47:56 -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
William Daviesd3687502019-06-19 20:23:17 +000025
Harshada Chaundkar77fa8ab2019-01-14 14:47:56 -050026/**
27 * Codec of FpmPeerInfo class.
28 */
29public final class FpmCodec extends JsonCodec<FpmPeerInfo> {
30
31 // JSON field names
32 private static final String PEER_ADDRESS = "peerAddress";
33 private static final String PEER_PORT = "peerPort";
34 private static final String CONNECTED_TO = "connectedTo";
35 private static final String CONNECTION_TIME = "connectionTime";
36 private static final String LOCAL_ROUTES = "localRoutes";
William Daviesd3687502019-06-19 20:23:17 +000037 private static final String ACCEPT_ROUTES = "acceptRoutes";
Harshada Chaundkar77fa8ab2019-01-14 14:47:56 -050038
39
40 @Override
41 public ObjectNode encode(FpmPeerInfo fpmPeerInfo, CodecContext context) {
42 final ObjectNode fpmConnectionArray = context.mapper().createObjectNode();
43
44 ArrayNode connectionArray = context.mapper().createArrayNode();
45 fpmPeerInfo.connections().forEach(connection -> {
46 ObjectNode fpmNode = context.mapper().createObjectNode();
47 fpmNode.put(PEER_ADDRESS, connection.peer().address().toString());
48 fpmNode.put(PEER_PORT, connection.peer().port());
49 fpmNode.put(CONNECTED_TO, connection.connectedTo().toString());
50 fpmNode.put(CONNECTION_TIME, Tools.timeAgo(connection.connectTime()));
51 fpmNode.put(LOCAL_ROUTES, fpmPeerInfo.routes());
William Daviesd3687502019-06-19 20:23:17 +000052 fpmNode.put(ACCEPT_ROUTES, connection.isAcceptRoutes());
Harshada Chaundkar77fa8ab2019-01-14 14:47:56 -050053 connectionArray.add(fpmNode);
54 });
55
56 fpmConnectionArray.put("connection", connectionArray);
57 return fpmConnectionArray;
58 }
59
60
61
62}