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 | |
| 25 | /** |
| 26 | * Codec of FpmPeerInfo class. |
| 27 | */ |
| 28 | public 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 | } |