amit.ghosh | 6104cf5 | 2021-01-07 16:53:28 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-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.opencord.olttopology.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onlab.packet.LLDPTLV; |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.opencord.olttopology.OltNeighborInfo; |
| 24 | |
| 25 | import java.util.Date; |
| 26 | import java.util.List; |
| 27 | |
| 28 | /* |
| 29 | Codec for JSON encoding of OLT topology information. |
| 30 | */ |
| 31 | public class OltTopologyInformationCodec extends JsonCodec<OltNeighborInfo> { |
| 32 | /** |
| 33 | * Encoder for the information in Json format. |
| 34 | * |
| 35 | * @param en : The topology information to be encoded |
| 36 | * @param context : The context to which the Json data needs to be added |
| 37 | * @return Json Object Node |
| 38 | */ |
| 39 | @Override |
| 40 | public ObjectNode encode(OltNeighborInfo en, CodecContext context) { |
| 41 | final ObjectNode result = context.mapper().createObjectNode() |
| 42 | .put("oltName", (en.oltName() == null) ? "" : en.oltName()) |
| 43 | .put("oltPort", (en.oltPort().annotations().value("portName").isEmpty()) ? "" : |
| 44 | en.oltPort().annotations().value("portName")) |
| 45 | .put("oltSerialNo", (en.oltSerialNo() == null) ? "" : en.oltSerialNo()) |
| 46 | .put("neighborName", (en.neighborName() == null) ? "" : en.neighborName()) |
| 47 | .put("neighborPort", (en.neighborPort() == null) ? "" : en.neighborPort()) |
| 48 | .put("neighborManagementAddress", (en.mgmtAddr() == null) ? "" : en.mgmtAddr()); |
| 49 | |
| 50 | if (en.getOtherOptionalTlvs() != null) { |
| 51 | ArrayNode optionalTlvNodes = result.putArray("optionalTlvs"); |
| 52 | |
| 53 | List<LLDPTLV> optionalTlvsList = en.getOtherOptionalTlvs(); |
| 54 | for (LLDPTLV tlv : optionalTlvsList) { |
| 55 | ObjectNode optionalTlvNode = context.mapper().createObjectNode(); |
| 56 | optionalTlvNode.put("type", tlv.getType()); |
| 57 | optionalTlvNode.put("value", "0x" + byteArrayInHex(tlv.getValue())); |
| 58 | optionalTlvNodes.add(optionalTlvNode); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | Date currentTime = new Date(); |
| 63 | long lastUpdatedValue = currentTime.getTime() - en.getLastUpdated().getTime(); |
| 64 | long lastUpdatedSecondsValue = lastUpdatedValue / 1000; |
| 65 | result.put("last_updated", Long.toString(lastUpdatedSecondsValue)); |
| 66 | return result; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Utility function to convert byte array to Hex String. |
| 71 | * |
| 72 | * @param bytes : The byte arrary to be converted |
| 73 | * @return Hex string representation of the byte array |
| 74 | */ |
| 75 | private String byteArrayInHex(byte[] bytes) { |
| 76 | String s = ""; |
| 77 | for (byte b : bytes) { |
| 78 | s += String.format("%02x", b); |
| 79 | } |
| 80 | return s; |
| 81 | } |
| 82 | } |