Harshada Chaundkar | 77fa8ab | 2019-01-14 14:47:56 -0500 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.fpm.web; |
| 17 | |
| 18 | import org.onlab.util.Tools; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.routing.fpm.FpmPeerInfo; |
| 24 | |
William Davies | d368750 | 2019-06-19 20:23:17 +0000 | [diff] [blame^] | 25 | |
Harshada Chaundkar | 77fa8ab | 2019-01-14 14:47:56 -0500 | [diff] [blame] | 26 | /** |
| 27 | * Codec of FpmPeerInfo class. |
| 28 | */ |
| 29 | public 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 Davies | d368750 | 2019-06-19 20:23:17 +0000 | [diff] [blame^] | 37 | private static final String ACCEPT_ROUTES = "acceptRoutes"; |
Harshada Chaundkar | 77fa8ab | 2019-01-14 14:47:56 -0500 | [diff] [blame] | 38 | |
| 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 Davies | d368750 | 2019-06-19 20:23:17 +0000 | [diff] [blame^] | 52 | fpmNode.put(ACCEPT_ROUTES, connection.isAcceptRoutes()); |
Harshada Chaundkar | 77fa8ab | 2019-01-14 14:47:56 -0500 | [diff] [blame] | 53 | connectionArray.add(fpmNode); |
| 54 | }); |
| 55 | |
| 56 | fpmConnectionArray.put("connection", connectionArray); |
| 57 | return fpmConnectionArray; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | |
| 62 | } |