Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 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.ospf.protocol.ospfpacket; |
| 17 | |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 18 | import org.onosproject.ospf.controller.OspfMessage; |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 19 | import org.onosproject.ospf.protocol.util.OspfParameters; |
| 20 | import org.onosproject.ospf.protocol.util.OspfUtil; |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
| 24 | /** |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 25 | * A message writer which writes an OSPF message to byte array. |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 26 | */ |
| 27 | public class OspfMessageWriter { |
| 28 | private static final Logger log = LoggerFactory.getLogger(OspfMessageWriter.class); |
| 29 | |
| 30 | /** |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 31 | * Writes OSPF message to byte array. |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 32 | * |
| 33 | * @param ospfMessage OSPF message |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 34 | * @param interfaceIndex interface index |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 35 | * @param interfaceState interface state |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 36 | * @return message as byte array |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 37 | */ |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 38 | public byte[] getMessage(OspfMessage ospfMessage, int interfaceIndex, int interfaceState) { |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 39 | |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 40 | byte[] buf = null; |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 41 | switch (ospfMessage.ospfMessageType().value()) { |
| 42 | case OspfParameters.HELLO: |
| 43 | case OspfParameters.LSACK: |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 44 | case OspfParameters.DD: |
| 45 | case OspfParameters.LSREQUEST: |
| 46 | case OspfParameters.LSUPDATE: |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 47 | buf = writeMessageToBytes(ospfMessage, interfaceIndex, interfaceState); |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 48 | break; |
| 49 | default: |
| 50 | log.debug("Message Writer[Encoder] - Unknown Message to encode..!!!"); |
| 51 | break; |
| 52 | } |
| 53 | |
| 54 | return buf; |
| 55 | } |
| 56 | |
| 57 | /** |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 58 | * Writes an OSPF Message to byte array. |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 59 | * |
| 60 | * @param ospfMessage OSPF Message instance |
| 61 | * @param interfaceState interface state |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 62 | * @return message as byte array |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 63 | */ |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 64 | private byte[] writeMessageToBytes(OspfMessage ospfMessage, int interfaceIndex, int interfaceState) { |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 65 | byte[] ospfMessageAsByte = ospfMessage.asBytes(); |
| 66 | //Add the length and checksum in byte array at length position 2 & 3 and Checksum position |
| 67 | ospfMessageAsByte = OspfUtil.addLengthAndCheckSum(ospfMessageAsByte, OspfUtil.OSPFPACKET_LENGTH_POS1, |
| 68 | OspfUtil.OSPFPACKET_LENGTH_POS2, |
| 69 | OspfUtil.OSPFPACKET_CHECKSUM_POS1, |
| 70 | OspfUtil.OSPFPACKET_CHECKSUM_POS2); |
| 71 | //Add Interface State Info and destination IP as metadata |
| 72 | if (interfaceState == OspfParameters.DR || interfaceState == OspfParameters.BDR) { |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 73 | ospfMessageAsByte = OspfUtil.addMetadata(interfaceIndex, ospfMessageAsByte, OspfUtil.JOIN_ALL_DROUTERS, |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 74 | ospfMessage.destinationIp()); |
| 75 | } else { |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 76 | ospfMessageAsByte = OspfUtil.addMetadata(interfaceIndex, ospfMessageAsByte, OspfUtil.ONLY_ALL_SPF_ROUTERS, |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 77 | ospfMessage.destinationIp()); |
| 78 | } |
| 79 | |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 80 | return ospfMessageAsByte; |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 81 | } |
| 82 | } |