2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.protocol.bgp.rib.impl;
11 import com.google.common.base.Optional;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.List;
15 import org.opendaylight.protocol.bgp.parser.AsNumberUtil;
16 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
17 import org.opendaylight.protocol.bgp.parser.BGPError;
18 import org.opendaylight.protocol.bgp.parser.impl.message.open.As4CapabilityHandler;
19 import org.opendaylight.protocol.bgp.parser.spi.CapabilitySerializer;
20 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
21 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionValidator;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.OptionalCapabilities;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.As4BytesCase;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Validates Bgp sessions established from current device to remote.
33 public class BGPClientSessionValidator implements BGPSessionValidator {
35 private static final Logger LOG = LoggerFactory.getLogger(BGPClientSessionValidator.class);
37 private final AsNumber remoteAs;
39 public BGPClientSessionValidator(final AsNumber remoteAs) {
40 this.remoteAs = remoteAs;
44 * Validates with exception:
46 * <li>correct remote AS attribute</li>
47 * <li>non empty BgpParameters collection</li>
50 * Validates with log message:
52 * <li>local BgpParameters are superset of remote BgpParameters</li>
56 public void validate(final Open openObj, final BGPSessionPreferences localPref) throws BGPDocumentedException {
57 final AsNumber as = AsNumberUtil.advertizedAsNumber(openObj);
58 if (!this.remoteAs.equals(as)) {
59 LOG.warn("Unexpected remote AS number. Expecting {}, got {}", this.remoteAs, as);
60 throw new BGPDocumentedException("Peer AS number mismatch", BGPError.BAD_PEER_AS);
62 //https://tools.ietf.org/html/rfc6286#section-2.2
63 if (openObj.getBgpIdentifier() != null && openObj.getBgpIdentifier().equals(localPref.getBgpId())) {
64 LOG.warn("Remote and local BGP Identifiers are the same: {}", openObj.getBgpIdentifier());
65 throw new BGPDocumentedException("Remote and local BGP Identifiers are the same.", BGPError.BAD_BGP_ID);
68 final List<BgpParameters> prefs = openObj.getBgpParameters();
70 if(getAs4BytesCapability(localPref.getParams()).isPresent() && !getAs4BytesCapability(prefs).isPresent()) {
71 throw new BGPDocumentedException("The peer must advertise AS4Bytes capability.", BGPError.UNSUPPORTED_CAPABILITY,
72 serializeAs4BytesCapability(getAs4BytesCapability(localPref.getParams()).get()));
74 if (!prefs.containsAll(localPref.getParams())) {
75 LOG.info("BGP Open message session parameters differ, session still accepted.");
78 throw new BGPDocumentedException("Open message unacceptable. Check the configuration of BGP speaker.", BGPError.UNSPECIFIC_OPEN_ERROR);
82 private static Optional<As4BytesCase> getAs4BytesCapability(final List<BgpParameters> prefs) {
83 for(final BgpParameters param : prefs) {
84 for (final OptionalCapabilities capa : param.getOptionalCapabilities()) {
85 if(capa.getCParameters() instanceof As4BytesCase) {
86 return Optional.of((As4BytesCase) capa.getCParameters());
90 return Optional.absent();
93 private static byte[] serializeAs4BytesCapability(final As4BytesCase as4Capability) {
94 final ByteBuf buffer = Unpooled.buffer(1 /*CODE*/ + 1 /*LENGTH*/ + Integer.SIZE / Byte.SIZE /*4 byte value*/);
95 final CapabilitySerializer serializer = new As4CapabilityHandler();
96 serializer.serializeCapability(as4Capability, buffer);
97 return buffer.array();