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