Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [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.onosproject.bgpio.protocol.linkstate; |
| 17 | |
| 18 | import java.util.List; |
| 19 | |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 22 | import org.onosproject.bgpio.protocol.BgpLinkLsNlri; |
| 23 | import org.onosproject.bgpio.protocol.NlriType; |
| 24 | import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4.ProtocolType; |
| 25 | import org.onosproject.bgpio.types.BgpErrorType; |
| 26 | import org.onosproject.bgpio.types.BgpValueType; |
| 27 | import org.onosproject.bgpio.types.RouteDistinguisher; |
| 28 | import org.onosproject.bgpio.util.Constants; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import com.google.common.base.MoreObjects; |
| 33 | |
| 34 | /** |
| 35 | * Implementation of Link LS NLRI. |
| 36 | */ |
| 37 | public class BgpLinkLsNlriVer4 implements BgpLinkLsNlri { |
| 38 | |
| 39 | /* |
| 40 | * REFERENCE : draft-ietf-idr-ls-distribution-11 |
| 41 | 0 1 2 3 |
| 42 | 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 |
| 43 | +-+-+-+-+-+-+-+-+ |
| 44 | | Protocol-ID | |
| 45 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 46 | | Identifier | |
| 47 | | (64 bits) | |
| 48 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 49 | // Local Node Descriptors (variable) // |
| 50 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 51 | // Remote Node Descriptors (variable) // |
| 52 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 53 | // Link Descriptors (variable) // |
| 54 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 55 | |
| 56 | Figure : The Link NLRI format |
| 57 | */ |
| 58 | private static final Logger log = LoggerFactory.getLogger(BgpLinkLsNlriVer4.class); |
| 59 | public static final int LINK_NLRITYPE = 2; |
| 60 | |
| 61 | private BgpLinkLSIdentifier linkLSIdentifier; |
| 62 | private byte protocolId; |
| 63 | private long identifier; |
| 64 | private RouteDistinguisher routeDistinguisher; |
| 65 | private boolean isVpn; |
| 66 | |
| 67 | /** |
| 68 | * Initialize fields. |
| 69 | */ |
| 70 | public BgpLinkLsNlriVer4() { |
| 71 | this.protocolId = 0; |
| 72 | this.identifier = 0; |
| 73 | this.linkLSIdentifier = null; |
| 74 | this.routeDistinguisher = null; |
| 75 | this.isVpn = false; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Constructor to initialize parameters for BGP LinkLSNlri. |
| 80 | * |
| 81 | * @param protocolId protocol Id |
| 82 | * @param identifier field in BGP LinkLSNlri |
| 83 | * @param linkLSIdentifier link LS identifier |
| 84 | * @param routeDistinguisher route distinguisher from message |
| 85 | * @param isVpn vpn info availability in message |
| 86 | */ |
| 87 | public BgpLinkLsNlriVer4(byte protocolId, long identifier, BgpLinkLSIdentifier linkLSIdentifier, |
| 88 | RouteDistinguisher routeDistinguisher, boolean isVpn) { |
| 89 | this.protocolId = protocolId; |
| 90 | this.identifier = identifier; |
| 91 | this.linkLSIdentifier = linkLSIdentifier; |
| 92 | this.routeDistinguisher = routeDistinguisher; |
| 93 | this.isVpn = isVpn; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Reads from channelBuffer and parses Link LS Nlri. |
| 98 | * |
| 99 | * @param cb ChannelBuffer |
| 100 | * @param afi Address Family Identifier |
| 101 | * @param safi Subsequent Address Family Identifier |
| 102 | * @return object of this class |
| 103 | * @throws BgpParseException while parsing Link LS NLRI |
| 104 | */ |
| 105 | public static BgpLinkLsNlriVer4 read(ChannelBuffer cb, short afi, byte safi) throws BgpParseException { |
| 106 | boolean isVpn = false; |
| 107 | RouteDistinguisher routeDistinguisher = null; |
| 108 | if ((afi == Constants.AFI_VALUE) && (safi == Constants.VPN_SAFI_VALUE)) { |
| 109 | routeDistinguisher = new RouteDistinguisher(); |
| 110 | routeDistinguisher = RouteDistinguisher.read(cb); |
| 111 | isVpn = true; |
| 112 | } else { |
| 113 | isVpn = false; |
| 114 | } |
| 115 | byte protocolId = cb.readByte(); |
| 116 | long identifier = cb.readLong(); |
| 117 | |
| 118 | BgpLinkLSIdentifier linkLSIdentifier = new BgpLinkLSIdentifier(); |
| 119 | linkLSIdentifier = BgpLinkLSIdentifier.parseLinkIdendifier(cb, protocolId); |
| 120 | return new BgpLinkLsNlriVer4(protocolId, identifier, linkLSIdentifier, routeDistinguisher, isVpn); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public NlriType getNlriType() { |
| 125 | return NlriType.LINK; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public long getIdentifier() { |
| 130 | return this.identifier; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Set the link LS identifier. |
| 135 | * |
| 136 | * @param linkLSIdentifier link LS identifier to set |
| 137 | */ |
| 138 | public void setLinkLSIdentifier(BgpLinkLSIdentifier linkLSIdentifier) { |
| 139 | this.linkLSIdentifier = linkLSIdentifier; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public ProtocolType getProtocolId() throws BgpParseException { |
| 144 | switch (protocolId) { |
| 145 | case Constants.ISIS_LEVELONE: |
| 146 | return ProtocolType.ISIS_LEVEL_ONE; |
| 147 | case Constants.ISIS_LEVELTWO: |
| 148 | return ProtocolType.ISIS_LEVEL_TWO; |
| 149 | case Constants.OSPFV2: |
| 150 | return ProtocolType.OSPF_V2; |
| 151 | case Constants.DIRECT: |
| 152 | return ProtocolType.DIRECT; |
| 153 | case Constants.STATIC_CONFIGURATION: |
| 154 | return ProtocolType.STATIC_CONFIGURATION; |
| 155 | case Constants.OSPFV3: |
| 156 | return ProtocolType.OSPF_V3; |
| 157 | default: |
| 158 | throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public NodeDescriptors localNodeDescriptors() { |
| 164 | return this.linkLSIdentifier.localNodeDescriptors(); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public NodeDescriptors remoteNodeDescriptors() { |
| 169 | return this.linkLSIdentifier.remoteNodeDescriptors(); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Returns whether VPN is present or not. |
| 174 | * |
| 175 | * @return whether VPN is present or not |
| 176 | */ |
| 177 | public boolean isVpnPresent() { |
| 178 | return this.isVpn; |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public RouteDistinguisher getRouteDistinguisher() { |
| 183 | return this.routeDistinguisher; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Returns link identifier. |
| 188 | * |
| 189 | * @return link identifier |
| 190 | */ |
| 191 | public BgpLinkLSIdentifier getLinkIdentifier() { |
| 192 | return this.linkLSIdentifier; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public List<BgpValueType> linkDescriptors() { |
| 197 | return this.linkLSIdentifier.linkDescriptors(); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public String toString() { |
| 202 | return MoreObjects.toStringHelper(getClass()) |
| 203 | .omitNullValues() |
| 204 | .add("protocolId", protocolId) |
| 205 | .add("identifier", identifier) |
| 206 | .add("RouteDistinguisher ", routeDistinguisher) |
| 207 | .add("linkLSIdentifier", linkLSIdentifier) |
| 208 | .toString(); |
| 209 | } |
| 210 | } |