blob: 26166ff0b4b678437efdf80b0b7d64bd64bbc07f [file] [log] [blame]
Dhruv Dhody43f3ce62016-02-16 22:44:21 +05301/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002* Copyright 2016-present Open Networking Laboratory
Dhruv Dhody43f3ce62016-02-16 22:44:21 +05303*
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.ospf.protocol.ospfpacket;
17
sunishvkf7c56552016-07-18 16:02:39 +053018import org.onosproject.ospf.controller.OspfMessage;
Dhruv Dhody43f3ce62016-02-16 22:44:21 +053019import org.onosproject.ospf.protocol.util.OspfParameters;
20import org.onosproject.ospf.protocol.util.OspfUtil;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24/**
sunishvkf7c56552016-07-18 16:02:39 +053025 * A message writer which writes an OSPF message to byte array.
Dhruv Dhody43f3ce62016-02-16 22:44:21 +053026 */
27public class OspfMessageWriter {
28 private static final Logger log = LoggerFactory.getLogger(OspfMessageWriter.class);
29
30 /**
sunishvkf7c56552016-07-18 16:02:39 +053031 * Writes OSPF message to byte array.
Dhruv Dhody43f3ce62016-02-16 22:44:21 +053032 *
33 * @param ospfMessage OSPF message
sunishvkf7c56552016-07-18 16:02:39 +053034 * @param interfaceIndex interface index
Dhruv Dhody43f3ce62016-02-16 22:44:21 +053035 * @param interfaceState interface state
sunishvkf7c56552016-07-18 16:02:39 +053036 * @return message as byte array
Dhruv Dhody43f3ce62016-02-16 22:44:21 +053037 */
sunishvkf7c56552016-07-18 16:02:39 +053038 public byte[] getMessage(OspfMessage ospfMessage, int interfaceIndex, int interfaceState) {
Dhruv Dhody43f3ce62016-02-16 22:44:21 +053039
sunishvkf7c56552016-07-18 16:02:39 +053040 byte[] buf = null;
Dhruv Dhody43f3ce62016-02-16 22:44:21 +053041 switch (ospfMessage.ospfMessageType().value()) {
42 case OspfParameters.HELLO:
43 case OspfParameters.LSACK:
Dhruv Dhody43f3ce62016-02-16 22:44:21 +053044 case OspfParameters.DD:
45 case OspfParameters.LSREQUEST:
46 case OspfParameters.LSUPDATE:
sunishvkf7c56552016-07-18 16:02:39 +053047 buf = writeMessageToBytes(ospfMessage, interfaceIndex, interfaceState);
Dhruv Dhody43f3ce62016-02-16 22:44:21 +053048 break;
49 default:
50 log.debug("Message Writer[Encoder] - Unknown Message to encode..!!!");
51 break;
52 }
53
54 return buf;
55 }
56
57 /**
sunishvkf7c56552016-07-18 16:02:39 +053058 * Writes an OSPF Message to byte array.
Dhruv Dhody43f3ce62016-02-16 22:44:21 +053059 *
60 * @param ospfMessage OSPF Message instance
61 * @param interfaceState interface state
sunishvkf7c56552016-07-18 16:02:39 +053062 * @return message as byte array
Dhruv Dhody43f3ce62016-02-16 22:44:21 +053063 */
sunishvkf7c56552016-07-18 16:02:39 +053064 private byte[] writeMessageToBytes(OspfMessage ospfMessage, int interfaceIndex, int interfaceState) {
Dhruv Dhody43f3ce62016-02-16 22:44:21 +053065 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) {
sunishvkf7c56552016-07-18 16:02:39 +053073 ospfMessageAsByte = OspfUtil.addMetadata(interfaceIndex, ospfMessageAsByte, OspfUtil.JOIN_ALL_DROUTERS,
Dhruv Dhody43f3ce62016-02-16 22:44:21 +053074 ospfMessage.destinationIp());
75 } else {
sunishvkf7c56552016-07-18 16:02:39 +053076 ospfMessageAsByte = OspfUtil.addMetadata(interfaceIndex, ospfMessageAsByte, OspfUtil.ONLY_ALL_SPF_ROUTERS,
Dhruv Dhody43f3ce62016-02-16 22:44:21 +053077 ospfMessage.destinationIp());
78 }
79
sunishvkf7c56552016-07-18 16:02:39 +053080 return ospfMessageAsByte;
Dhruv Dhody43f3ce62016-02-16 22:44:21 +053081 }
82}