BUG-338 Allow incomming BGP connections.
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / client / 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.client;
10
11 import java.util.List;
12 import org.opendaylight.protocol.bgp.parser.AsNumberUtil;
13 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
14 import org.opendaylight.protocol.bgp.parser.BGPError;
15 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
16 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
17 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionValidator;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Validates Bgp sessions established from current device to remote.
26  */
27 public class BGPClientSessionValidator implements BGPSessionValidator {
28
29     private static final Logger LOG = LoggerFactory.getLogger(BGPClientSessionValidator.class);
30
31     private final AsNumber remoteAs;
32     private final BGPPeerRegistry peerRegistry;
33
34     public BGPClientSessionValidator(final AsNumber remoteAs, final BGPPeerRegistry peerRegistry) {
35         this.remoteAs = remoteAs;
36         this.peerRegistry = peerRegistry;
37     }
38
39     /**
40      * Validates with exception:
41      * <ul>
42      * <li>correct remote AS attribute</li>
43      * <li>non empty BgpParameters collection</li>
44      * </ul>
45      *
46      * Validates with log message:
47      * <ul>
48      * <li>local BgpParameters are superset of remote BgpParameters</li>
49      * </ul>
50      */
51     @Override
52     public void validate(final Open openObj, final BGPSessionPreferences localPref) throws BGPDocumentedException {
53         final AsNumber as = AsNumberUtil.advertizedAsNumber(openObj);
54         if (!this.remoteAs.equals(as)) {
55             LOG.warn("Unexpected remote AS number. Expecting {}, got {}", this.remoteAs, as);
56             throw new BGPDocumentedException("Peer AS number mismatch", BGPError.BAD_PEER_AS);
57         }
58
59         final List<BgpParameters> prefs = openObj.getBgpParameters();
60         if (prefs != null && !prefs.isEmpty()) {
61             if (!prefs.containsAll(localPref.getParams())) {
62                 LOG.info("BGP Open message session parameters differ, session still accepted.");
63             }
64         } else {
65             throw new BGPDocumentedException("Open message unacceptable. Check the configuration of BGP speaker.", BGPError.UNSPECIFIC_OPEN_ERROR);
66         }
67     }
68 }