blob: 9782194c860962f18ccebb9a7bd19088a9d038a4 [file] [log] [blame]
Dhruv Dhody43f3ce62016-02-16 22:44:21 +05301/*
2* Copyright 2016 Open Networking Laboratory
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*/
16package org.onosproject.ospf.protocol.ospfpacket;
17
18import org.jboss.netty.buffer.ChannelBuffer;
19import org.jboss.netty.buffer.ChannelBuffers;
20import org.onosproject.ospf.protocol.util.OspfParameters;
21import org.onosproject.ospf.protocol.util.OspfUtil;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25/**
26 * A message writer which writes an OspfMessage to ChannelBuffer.
27 */
28public class OspfMessageWriter {
29 private static final Logger log = LoggerFactory.getLogger(OspfMessageWriter.class);
30
31 /**
32 * Writes OSPF message to ChannelBuffer.
33 *
34 * @param ospfMessage OSPF message
35 * @param interfaceState interface state
36 * @param interfaceType interface type
37 * @return channelBuffer channel buffer instance
38 * @throws Exception might throws exception while parsing message
39 */
40 public ChannelBuffer writeToBuffer(OspfMessage ospfMessage, int interfaceState,
41 int interfaceType) throws Exception {
42
43 ChannelBuffer buf = null;
44 switch (ospfMessage.ospfMessageType().value()) {
45 case OspfParameters.HELLO:
46 case OspfParameters.LSACK:
47 buf = writeMessageToBuffer(ospfMessage, interfaceState);
48 break;
49 case OspfParameters.DD:
50 case OspfParameters.LSREQUEST:
51 case OspfParameters.LSUPDATE:
52 buf = writeMessageToBuffer(ospfMessage, interfaceState);
53 break;
54 default:
55 log.debug("Message Writer[Encoder] - Unknown Message to encode..!!!");
56 break;
57 }
58
59 return buf;
60 }
61
62 /**
63 * Writes an OSPF Message to channel buffer.
64 *
65 * @param ospfMessage OSPF Message instance
66 * @param interfaceState interface state
67 * @return channelBuffer instance
68 */
69 private ChannelBuffer writeMessageToBuffer(OspfMessage ospfMessage, int interfaceState) throws Exception {
70 ChannelBuffer channelBuffer = null;
71 byte[] ospfMessageAsByte = ospfMessage.asBytes();
72 //Add the length and checksum in byte array at length position 2 & 3 and Checksum position
73 ospfMessageAsByte = OspfUtil.addLengthAndCheckSum(ospfMessageAsByte, OspfUtil.OSPFPACKET_LENGTH_POS1,
74 OspfUtil.OSPFPACKET_LENGTH_POS2,
75 OspfUtil.OSPFPACKET_CHECKSUM_POS1,
76 OspfUtil.OSPFPACKET_CHECKSUM_POS2);
77 //Add Interface State Info and destination IP as metadata
78 if (interfaceState == OspfParameters.DR || interfaceState == OspfParameters.BDR) {
79 ospfMessageAsByte = OspfUtil.addMetadata(ospfMessageAsByte, OspfUtil.JOIN_ALL_DROUTERS,
80 ospfMessage.destinationIp());
81 } else {
82 ospfMessageAsByte = OspfUtil.addMetadata(ospfMessageAsByte, OspfUtil.ONLY_ALL_SPF_ROUTERS,
83 ospfMessage.destinationIp());
84 }
85
86 channelBuffer = ChannelBuffers.buffer(ospfMessageAsByte.length);
87 channelBuffer.writeBytes(ospfMessageAsByte);
88
89 return channelBuffer;
90 }
91}