53a6b75735e09968d238e50b3e1e00629f692d5f
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPClientSessionValidator.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.protocol.bgp.rib.impl;
10
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;
29
30 /**
31  * Validates Bgp sessions established from current device to remote.
32  */
33 public class BGPClientSessionValidator implements BGPSessionValidator {
34
35     private static final Logger LOG = LoggerFactory.getLogger(BGPClientSessionValidator.class);
36
37     private final AsNumber remoteAs;
38
39     public BGPClientSessionValidator(final AsNumber remoteAs) {
40         this.remoteAs = remoteAs;
41     }
42
43     /**
44      * Validates with exception:
45      * <ul>
46      * <li>correct remote AS attribute</li>
47      * <li>non empty BgpParameters collection</li>
48      * </ul>
49      *
50      * Validates with log message:
51      * <ul>
52      * <li>local BgpParameters are superset of remote BgpParameters</li>
53      * </ul>
54      */
55     @Override
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);
61         }
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);
66         }
67
68         final List<BgpParameters> prefs = openObj.getBgpParameters();
69         if (prefs != null) {
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()));
73             }
74             if (!prefs.containsAll(localPref.getParams())) {
75                 LOG.info("BGP Open message session parameters differ, session still accepted.");
76             }
77         } else {
78             throw new BGPDocumentedException("Open message unacceptable. Check the configuration of BGP speaker.", BGPError.UNSPECIFIC_OPEN_ERROR);
79         }
80     }
81
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());
87                 }
88             }
89         }
90         return Optional.absent();
91     }
92
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();
98     }
99 }