Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [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 | */ |
Jonathan Hart | 317f476 | 2015-11-09 16:05:36 -0800 | [diff] [blame] | 16 | package org.onosproject.bgpio.protocol.linkstate; |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 17 | |
| 18 | import org.jboss.netty.buffer.ChannelBuffer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 19 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 20 | import org.onosproject.bgpio.protocol.BgpNodeLSNlri; |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 21 | import org.onosproject.bgpio.protocol.NlriType; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 22 | import org.onosproject.bgpio.types.BgpErrorType; |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 23 | import org.onosproject.bgpio.types.RouteDistinguisher; |
| 24 | import org.onosproject.bgpio.util.Constants; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import com.google.common.base.MoreObjects; |
| 29 | |
| 30 | /** |
| 31 | * Implementation of Node LS NLRI. |
| 32 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 33 | public class BgpNodeLSNlriVer4 implements BgpNodeLSNlri { |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | *REFERENCE : draft-ietf-idr-ls-distribution-11 |
| 37 | 0 1 2 3 |
| 38 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 39 | +-+-+-+-+-+-+-+-+ |
| 40 | | Protocol-ID | |
| 41 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 42 | | Identifier | |
| 43 | | (64 bits) | |
| 44 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 45 | // Local Node Descriptors (variable) // |
| 46 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 47 | |
| 48 | Figure : The Node NLRI format |
| 49 | */ |
| 50 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 51 | protected static final Logger log = LoggerFactory.getLogger(BgpNodeLSNlriVer4.class); |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 52 | |
| 53 | public static final int NODE_NLRITYPE = 1; |
| 54 | public static final int IDENTIFIER_LENGTH = 16; |
| 55 | private long identifier; |
| 56 | private byte protocolId; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 57 | private BgpNodeLSIdentifier localNodeDescriptors; |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 58 | private RouteDistinguisher routeDistinguisher; |
| 59 | private boolean isVpn; |
| 60 | |
| 61 | /** |
| 62 | * Enum to provide PROTOCOLTYPE. |
| 63 | */ |
Sho SHIMIZU | 98fc1e7 | 2015-11-23 10:25:22 -0800 | [diff] [blame] | 64 | public enum ProtocolType { |
| 65 | ISIS_LEVEL_ONE(1), ISIS_LEVEL_TWO(2), OSPF_V2(3), DIRECT(4), STATIC_CONFIGURATION(5), OSPF_V3(6); |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 66 | int value; |
| 67 | |
| 68 | /** |
| 69 | * Assign val with the value as the protocol type. |
| 70 | * |
| 71 | * @param val protocol type |
| 72 | */ |
Sho SHIMIZU | 98fc1e7 | 2015-11-23 10:25:22 -0800 | [diff] [blame] | 73 | ProtocolType(int val) { |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 74 | value = val; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns value of protocol type. |
| 79 | * |
| 80 | * @return protocol type |
| 81 | */ |
| 82 | public byte getType() { |
| 83 | return (byte) value; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Reset fields. |
| 89 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 90 | public BgpNodeLSNlriVer4() { |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 91 | this.identifier = 0; |
| 92 | this.protocolId = 0; |
| 93 | this.localNodeDescriptors = null; |
| 94 | this.routeDistinguisher = null; |
| 95 | this.isVpn = false; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Constructors to initialize its parameters. |
| 100 | * |
| 101 | * @param identifier of LinkState Nlri |
| 102 | * @param protocolId of LinkState Nlri |
| 103 | * @param localNodeDescriptors local node descriptors |
| 104 | * @param isVpn true if VPN info is present |
| 105 | * @param routeDistinguisher unique for each VPN |
| 106 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 107 | public BgpNodeLSNlriVer4(long identifier, byte protocolId, BgpNodeLSIdentifier localNodeDescriptors, boolean isVpn, |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 108 | RouteDistinguisher routeDistinguisher) { |
| 109 | this.identifier = identifier; |
| 110 | this.protocolId = protocolId; |
| 111 | this.localNodeDescriptors = localNodeDescriptors; |
| 112 | this.routeDistinguisher = routeDistinguisher; |
| 113 | this.isVpn = isVpn; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Reads from channelBuffer and parses Node LS Nlri. |
| 118 | * |
| 119 | * @param cb ChannelBuffer |
| 120 | * @param afi Address Family Identifier |
| 121 | * @param safi Subsequent Address Family Identifier |
| 122 | * @return object of this class |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 123 | * @throws BgpParseException while parsing node descriptors |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 124 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 125 | public static BgpNodeLSNlriVer4 read(ChannelBuffer cb, short afi, byte safi) throws BgpParseException { |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 126 | boolean isVpn = false; |
| 127 | RouteDistinguisher routeDistinguisher = null; |
| 128 | if ((afi == Constants.AFI_VALUE) && (safi == Constants.VPN_SAFI_VALUE)) { |
| 129 | routeDistinguisher = new RouteDistinguisher(); |
| 130 | routeDistinguisher = RouteDistinguisher.read(cb); |
| 131 | isVpn = true; |
| 132 | } else { |
| 133 | isVpn = false; |
| 134 | } |
| 135 | byte protocolId = cb.readByte(); |
| 136 | long identifier = cb.readLong(); |
| 137 | |
| 138 | // Parse Local Node Descriptors |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 139 | BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(); |
| 140 | localNodeDescriptors = BgpNodeLSIdentifier.parseLocalNodeDescriptors(cb, protocolId); |
| 141 | return new BgpNodeLSNlriVer4(identifier, protocolId, localNodeDescriptors, isVpn, routeDistinguisher); |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public NlriType getNlriType() { |
| 146 | return NlriType.NODE; |
| 147 | } |
| 148 | |
| 149 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 150 | public BgpNodeLSIdentifier getLocalNodeDescriptors() { |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 151 | return this.localNodeDescriptors; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Returns whether VPN is present or not. |
| 156 | * |
| 157 | * @return whether VPN is present or not |
| 158 | */ |
| 159 | public boolean isVpnPresent() { |
| 160 | return this.isVpn; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public RouteDistinguisher getRouteDistinguisher() { |
| 165 | return this.routeDistinguisher; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public long getIdentifier() { |
| 170 | return this.identifier; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Set the node LS identifier. |
| 175 | * |
| 176 | * @param localNodeDescriptors node LS identifier to set |
| 177 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 178 | public void setNodeLSIdentifier(BgpNodeLSIdentifier localNodeDescriptors) { |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 179 | this.localNodeDescriptors = localNodeDescriptors; |
| 180 | } |
| 181 | |
| 182 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 183 | public ProtocolType getProtocolId() throws BgpParseException { |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 184 | switch (protocolId) { |
| 185 | case Constants.ISIS_LEVELONE: |
Sho SHIMIZU | 98fc1e7 | 2015-11-23 10:25:22 -0800 | [diff] [blame] | 186 | return ProtocolType.ISIS_LEVEL_ONE; |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 187 | case Constants.ISIS_LEVELTWO: |
Sho SHIMIZU | 98fc1e7 | 2015-11-23 10:25:22 -0800 | [diff] [blame] | 188 | return ProtocolType.ISIS_LEVEL_TWO; |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 189 | case Constants.OSPFV2: |
Sho SHIMIZU | 98fc1e7 | 2015-11-23 10:25:22 -0800 | [diff] [blame] | 190 | return ProtocolType.OSPF_V2; |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 191 | case Constants.DIRECT: |
Sho SHIMIZU | 98fc1e7 | 2015-11-23 10:25:22 -0800 | [diff] [blame] | 192 | return ProtocolType.DIRECT; |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 193 | case Constants.STATIC_CONFIGURATION: |
Sho SHIMIZU | 98fc1e7 | 2015-11-23 10:25:22 -0800 | [diff] [blame] | 194 | return ProtocolType.STATIC_CONFIGURATION; |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 195 | case Constants.OSPFV3: |
Sho SHIMIZU | 98fc1e7 | 2015-11-23 10:25:22 -0800 | [diff] [blame] | 196 | return ProtocolType.OSPF_V3; |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 197 | default: |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 198 | throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null); |
Priyanka B | 0bee1f8 | 2015-10-13 15:39:49 +0530 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public String toString() { |
| 204 | return MoreObjects.toStringHelper(getClass()) |
| 205 | .omitNullValues() |
| 206 | .add("protocolId", protocolId) |
| 207 | .add("identifier", identifier) |
| 208 | .add("RouteDistinguisher ", routeDistinguisher) |
| 209 | .add("localNodeDescriptors", localNodeDescriptors) |
| 210 | .toString(); |
| 211 | } |
Jonathan Hart | 317f476 | 2015-11-09 16:05:36 -0800 | [diff] [blame] | 212 | } |