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);
63 final List<BgpParameters> prefs = openObj.getBgpParameters();
64 if (prefs != null && !prefs.isEmpty()) {
65 if(getAs4BytesCapability(localPref.getParams()).isPresent() && !getAs4BytesCapability(prefs).isPresent()) {
66 throw new BGPDocumentedException("The peer must advertise AS4Bytes capability.", BGPError.UNSUPPORTED_CAPABILITY,
67 serializeAs4BytesCapability(getAs4BytesCapability(localPref.getParams()).get()));
69 if (!prefs.containsAll(localPref.getParams())) {
70 LOG.info("BGP Open message session parameters differ, session still accepted.");
73 throw new BGPDocumentedException("Open message unacceptable. Check the configuration of BGP speaker.", BGPError.UNSPECIFIC_OPEN_ERROR);
77 private static Optional<As4BytesCase> getAs4BytesCapability(final List<BgpParameters> prefs) {
78 for(final BgpParameters param : prefs) {
79 for (final OptionalCapabilities capa : param.getOptionalCapabilities()) {
80 if(capa.getCParameters() instanceof As4BytesCase) {
81 return Optional.of((As4BytesCase) capa.getCParameters());
85 return Optional.absent();
88 private static byte[] serializeAs4BytesCapability(final As4BytesCase as4Capability) {
89 final ByteBuf buffer = Unpooled.buffer(1 /*CODE*/ + 1 /*LENGTH*/ + Integer.SIZE / Byte.SIZE /*4 byte value*/);
90 final CapabilitySerializer serializer = new As4CapabilityHandler();
91 serializer.serializeCapability(as4Capability, buffer);
92 return buffer.array();