/* * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.bgpmanager; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.HashMap; import java.util.Map; import java.util.concurrent.Future; import org.apache.karaf.shell.commands.*; import org.apache.karaf.shell.console.OsgiCommandSupport; import org.opendaylight.bgpmanager.thrift.gen.af_afi; import org.opendaylight.bgpmanager.thrift.gen.af_safi; import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.*; import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.*; import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.*; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.*; import com.google.common.base.Optional; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; @Command(scope = "odl", name = "configure-bgp", description = "") public class ConfigureBgpCli extends OsgiCommandSupport { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureBgpCli.class); private static BgpManager bgpManager; public static void setBgpManager(BgpManager mgr) { bgpManager = mgr; } @Option(name = "-op", aliases = {"--operation","--op"}, description = "[start-bgp-server, stop-bgp-server, add-neighbor, delete-neighbor, graceful-restart, enable-log ]", required = false, multiValued = false) String op; //exec configure-bgp add-neighbor --ip --as-num --address-family --use-source-ip --ebgp-multihops --next-hop @Option(name = "--as-num", description = "as number of the bgp neighbor", required = false, multiValued = false) String asNumber = null; @Option(name = "--ip", description = "ip of the bgp neighbor", required = false, multiValued = false) String ip = null; @Option(name = "--address-family", description = "address family of the bgp neighbor SAFI_IPV4_LABELED_UNICAST|SAFI_MPLS_VPN", required = false, multiValued = false) String addressFamily = null; @Option(name = "--use-source-ip", description = "source ip to be used for neighborship connection establishment", required = false, multiValued = false) String sourceIp = null; @Option(name = "--ebgp-multihops", description = "ebgp multihops of the bgp neighbor", required = false, multiValued = false) String ebgpMultihops = null; @Option(name = "--router-id", description = "router id of the bgp instance", required = false, multiValued = false) String routerId = null; @Option(name = "--stalepath-time", description = "the time delay after bgp restart stalepaths are cleaned", required = false, multiValued = false) String stalePathTime = null; @Option(name = "--log-file-path", description = "bgp log file path", required = false, multiValued = false) String logFile = null; @Option(name = "--log-level", description = "log level emergencies,alerts,critical,errors,warnings,notifications,informational,debugging", required = false, multiValued = false) String logLevel = null; enum LogLevels { emergencies,alerts,critical,errors,warnings,notifications,informational,debugging; } @Override protected Object doExecute() throws Exception { try { if (op == null) { System.out.println("Please provide valid operation"); usage(); System.out.println("exec configure-bgp -op [start-bgp-server | stop-bgp-server | add-neighbor | delete-neighbor| graceful-restart| enable-log ]"); } switch(op) { case "start-bgp-server": startBgp(); break; case "stop-bgp-server": stopBgp(); break; case "add-neighbor": addNeighbor(); break; case "delete-neighbor": deleteNeighbor(); break; case "graceful-restart": configureGR(); break; case "enable-log" : enableBgpLogLevel(); break; default : System.out.println("invalid operation"); usage(); System.out.println("exec configure-bgp -op [start-bgp-server | stop-bgp-server | add-neighbor | delete-neighbor| graceful-restart| enable-log ]"); } } catch (Exception e) { log.error("failed to handle the command",e); } return null; } public boolean validateStalepathTime() { try { int time = Integer.parseInt(stalePathTime); if (time < 30 || time > 3600) { System.out.println("invalid stale path time valid range [30-3600]"); printGracefulRestartHelp(); return false; } } catch (Exception e) { System.out.println("invalid stale path time"); printGracefulRestartHelp(); return false; } return true; } private void configureGR() throws Exception { boolean validStalepathTime = validateStalepathTime(); if (!validStalepathTime) { return; } bgpManager.configureGR(Integer.parseInt(stalePathTime)); } private void deleteNeighbor() throws Exception { if (ip == null || !validateIp(ip)) { System.out.println("invalid neighbor ip"); printDeleteNeighborHelp(); return; } long asNo = getAsNumber(ip); if (asNo < 0) { System.out.println("neighbor does not exist"); printDeleteNeighborHelp(); return; } bgpManager.deleteNeighbor(ip); } public long getAsNumber(String nbrIp) { Bgp conf = bgpManager.getConfig(); if (conf == null) { return -1; } List nbrs = conf.getNeighbors(); if (nbrs == null) { return -1; } for (Neighbors nbr : nbrs) { if (nbrIp.equals(nbr.getAddress().getValue())) { return nbr.getRemoteAs().longValue(); } } return -1; } private void stopBgp() throws Exception { Bgp conf = bgpManager.getConfig(); if (conf == null) { return; } List nbrs = conf.getNeighbors(); if (nbrs != null && nbrs.size() > 0) { System.err.println("error: all BGP congiguration must be deleted before stopping the router instance"); return; } bgpManager.stopBgp(); } void usage() { System.out.println("usage:"); } void printStartBgpHelp() { usage(); System.out.println("exec configure-bgp -op start-bgp-server --as-num --router-id [--stalepath-time