Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 16 | package org.onlab.packet.pim; |
| 17 | |
| 18 | import org.onlab.packet.BasePacket; |
| 19 | import org.onlab.packet.Deserializer; |
| 20 | import org.onlab.packet.IPacket; |
| 21 | import org.onlab.packet.IpAddress; |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 22 | import java.nio.ByteBuffer; |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 23 | import java.util.HashMap; |
| 24 | import java.util.Map; |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 25 | |
| 26 | import static org.onlab.packet.PacketUtils.checkInput; |
| 27 | |
| 28 | public class PIMHello extends BasePacket { |
| 29 | |
| 30 | private IpAddress nbrIpAddress; |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 31 | private boolean priorityPresent = false; |
| 32 | |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 33 | private Map<Short, PIMHelloOption> options = new HashMap<>(); |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 34 | |
| 35 | /** |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 36 | * Create a PIM Hello packet with the most common hello options and default |
| 37 | * values. The values of any options can be easily changed by modifying the value of |
| 38 | * the option with the desired change. |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 39 | */ |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 40 | public void createDefaultOptions() { |
| 41 | options.put(PIMHelloOption.OPT_HOLDTIME, new PIMHelloOption(PIMHelloOption.OPT_HOLDTIME)); |
| 42 | options.put(PIMHelloOption.OPT_PRIORITY, new PIMHelloOption(PIMHelloOption.OPT_PRIORITY)); |
| 43 | options.put(PIMHelloOption.OPT_GENID, new PIMHelloOption(PIMHelloOption.OPT_GENID)); |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /** |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 47 | * Add a PIM Hello option to this hello message. Note |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 48 | * |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 49 | * @param opt the PIM Hello option we are adding |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 50 | */ |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 51 | public void addOption(PIMHelloOption opt) { |
| 52 | this.options.put(opt.getOptType(), opt); |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 55 | public Map<Short, PIMHelloOption> getOptions() { |
| 56 | return this.options; |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Sets all payloads parent packet if applicable, then serializes this |
| 61 | * packet and all payloads. |
| 62 | * |
| 63 | * @return a byte[] containing this packet and payloads |
| 64 | */ |
| 65 | @Override |
| 66 | public byte[] serialize() { |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 67 | int totalLen = 0; |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 68 | |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 69 | |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 70 | // Since we are likely to only have 3-4 options, go head and walk the |
| 71 | // hashmap twice, once to calculate the space needed to allocate a |
| 72 | // buffer, the second time serialize the options into the buffer. This |
| 73 | // saves us from allocating an over sized buffer the re-allocating and |
| 74 | // copying. |
| 75 | for (Short optType : options.keySet()) { |
| 76 | PIMHelloOption opt = options.get(optType); |
| 77 | totalLen += PIMHelloOption.MINIMUM_OPTION_LEN_BYTES + opt.getOptLength(); |
| 78 | } |
| 79 | |
| 80 | byte[] data = new byte[totalLen]; |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 81 | ByteBuffer bb = ByteBuffer.wrap(data); |
| 82 | |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 83 | // Now serialize the data. |
| 84 | for (Short optType : options.keySet()) { |
| 85 | PIMHelloOption opt = options.get(optType); |
| 86 | bb.put(opt.serialize()); |
| 87 | } |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 88 | return data; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * XXX: This is deprecated, DO NOT USE, use the deserializer() function instead. |
| 93 | */ |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 94 | public IPacket deserialize(final byte[] data, final int offset, |
| 95 | final int length) { |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 96 | // TODO: throw an expection? |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 97 | return null; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Deserialize this hello message. |
| 102 | * |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 103 | * @return a deserialized hello message |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 104 | */ |
| 105 | public static Deserializer<PIMHello> deserializer() { |
| 106 | return (data, offset, length) -> { |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 107 | checkInput(data, offset, length, PIMHelloOption.MINIMUM_OPTION_LEN_BYTES); |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 108 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 109 | |
| 110 | PIMHello hello = new PIMHello(); |
| 111 | while (bb.hasRemaining()) { |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 112 | PIMHelloOption opt = PIMHelloOption.deserialize(bb); |
| 113 | hello.addOption(opt); |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 114 | } |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 115 | return hello; |
| 116 | }; |
| 117 | } |
| 118 | } |