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.rib.impl.spi.BGPSessionPreferences;
20 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionValidator;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.OptionalCapabilities;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.CParameters;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.CParametersBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapability;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Validates Bgp sessions established from current device to remote.
34 public class BGPClientSessionValidator implements BGPSessionValidator {
36 private static final Logger LOG = LoggerFactory.getLogger(BGPClientSessionValidator.class);
38 private final AsNumber remoteAs;
40 public BGPClientSessionValidator(final AsNumber remoteAs) {
41 this.remoteAs = remoteAs;
45 * Validates with exception:
47 * <li>correct remote AS attribute</li>
48 * <li>non empty BgpParameters collection</li>
51 * Validates with log message:
53 * <li>local BgpParameters are superset of remote BgpParameters</li>
57 public void validate(final Open openObj, final BGPSessionPreferences localPref) throws BGPDocumentedException {
58 final AsNumber as = AsNumberUtil.advertizedAsNumber(openObj);
59 if (!this.remoteAs.equals(as)) {
60 LOG.warn("Unexpected remote AS number. Expecting {}, got {}", this.remoteAs, as);
61 throw new BGPDocumentedException("Peer AS number mismatch", BGPError.BAD_PEER_AS);
63 //https://tools.ietf.org/html/rfc6286#section-2.2
64 if (openObj.getBgpIdentifier() != null && openObj.getBgpIdentifier().equals(localPref.getBgpId())) {
65 LOG.warn("Remote and local BGP Identifiers are the same: {}", openObj.getBgpIdentifier());
66 throw new BGPDocumentedException("Remote and local BGP Identifiers are the same.", BGPError.BAD_BGP_ID);
69 final List<BgpParameters> prefs = openObj.getBgpParameters();
71 if(getAs4BytesCapability(localPref.getParams()).isPresent() && !getAs4BytesCapability(prefs).isPresent()) {
72 throw new BGPDocumentedException("The peer must advertise AS4Bytes capability.", BGPError.UNSUPPORTED_CAPABILITY,
73 serializeAs4BytesCapability(getAs4BytesCapability(localPref.getParams()).get()));
75 if (!prefs.containsAll(localPref.getParams())) {
76 LOG.info("BGP Open message session parameters differ, session still accepted.");
79 throw new BGPDocumentedException("Open message unacceptable. Check the configuration of BGP speaker.", BGPError.UNSPECIFIC_OPEN_ERROR);
83 private static Optional<As4BytesCapability> getAs4BytesCapability(final List<BgpParameters> prefs) {
84 for(final BgpParameters param : prefs) {
85 for (final OptionalCapabilities capa : param.getOptionalCapabilities()) {
86 final CParameters cParam = capa.getCParameters();
87 if(cParam.getAs4BytesCapability() !=null) {
88 return Optional.of(cParam.getAs4BytesCapability());
92 return Optional.absent();
95 private static byte[] serializeAs4BytesCapability(final As4BytesCapability as4Capability) {
96 final ByteBuf buffer = Unpooled.buffer(1 /*CODE*/ + 1 /*LENGTH*/ + Integer.SIZE / Byte.SIZE /*4 byte value*/);
97 final As4CapabilityHandler serializer = new As4CapabilityHandler();
98 serializer.serializeCapability(new CParametersBuilder().setAs4BytesCapability(as4Capability).build(), buffer);
99 return buffer.array();