LCOV - code coverage report
Current view: top level - vnsw/agent/controller - controller_init.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 296 621 47.7 %
Date: 2026-11-09 02:10:49 Functions: 25 50 50.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include <algorithm>
       7             : #include <base/string_util.h>
       8             : #include <base/logging.h>
       9             : #include <base/timer.h>
      10             : #include <base/contrail_ports.h>
      11             : #include <base/connection_info.h>
      12             : #include <base/address_util.h>
      13             : #include <net/tunnel_encap_type.h>
      14             : #include <sandesh/sandesh_trace.h>
      15             : #include <cmn/agent_cmn.h>
      16             : #include <cmn/xmpp_server_address_parser.h>
      17             : #include <xmpp/xmpp_init.h>
      18             : #include <pugixml/pugixml.hpp>
      19             : #include <oper/global_qos_config.h>
      20             : #include <oper/global_system_config.h>
      21             : #include <oper/vrf.h>
      22             : #include <oper/peer.h>
      23             : #include <oper/mirror_table.h>
      24             : #include <oper/multicast.h>
      25             : #include <controller/controller_types.h>
      26             : #include <controller/controller_init.h>
      27             : #include <controller/controller_timer.h>
      28             : #include <controller/controller_peer.h>
      29             : #include <controller/controller_ifmap.h>
      30             : #include <controller/controller_dns.h>
      31             : #include <controller/controller_export.h>
      32             : 
      33             : using namespace boost::asio;
      34             : 
      35             : SandeshTraceBufferPtr ControllerConnectionsTraceBuf(SandeshTraceBufferCreate(
      36             :     "ControllerConnections", 5000));
      37             : SandeshTraceBufferPtr ControllerInfoTraceBuf(SandeshTraceBufferCreate(
      38             :     "ControllerInfo", 5000));
      39             : SandeshTraceBufferPtr ControllerTxConfigTraceBuf1(SandeshTraceBufferCreate(
      40             :     "ControllerTxConfig_1", 5000));
      41             : SandeshTraceBufferPtr ControllerTxConfigTraceBuf2(SandeshTraceBufferCreate(
      42             :     "ControllerTxConfig_2", 5000));
      43             : SandeshTraceBufferPtr ControllerRouteWalkerTraceBuf(SandeshTraceBufferCreate(
      44             :     "ControllerRouteWalker", 5000));
      45             : SandeshTraceBufferPtr ControllerTraceBuf(SandeshTraceBufferCreate(
      46             :     "Controller", 5000));
      47             : SandeshTraceBufferPtr ControllerRxRouteMessageTraceBuf1(SandeshTraceBufferCreate(
      48             :     "ControllerRxRouteXmppMessage1", 5000));
      49             : SandeshTraceBufferPtr ControllerRxConfigMessageTraceBuf1(SandeshTraceBufferCreate(
      50             :     "ControllerRxConfigXmppMessage1", 5000));
      51             : SandeshTraceBufferPtr ControllerRxRouteMessageTraceBuf2(SandeshTraceBufferCreate(
      52             :     "ControllerRxRouteXmppMessage2", 5000));
      53             : SandeshTraceBufferPtr ControllerRxConfigMessageTraceBuf2(SandeshTraceBufferCreate(
      54             :     "ControllerRxConfigXmppMessage2", 5000));
      55             : SandeshTraceBufferPtr ControllerTxMessageTraceBuf1(SandeshTraceBufferCreate(
      56             :     "ControllerTxXmppMessage_1", 5000));
      57             : SandeshTraceBufferPtr ControllerTxMessageTraceBuf2(SandeshTraceBufferCreate(
      58             :     "ControllerTxXmppMessage_2", 5000));
      59             : 
      60           0 : ControllerReConfigData::ControllerReConfigData(std::string service_name,
      61           0 :                                                std::vector<string>server_list) :
      62           0 :     ControllerWorkQueueData(), service_name_(service_name),
      63           0 :     server_list_(server_list) {
      64           0 : }
      65             : 
      66          21 : ControllerDelPeerData::ControllerDelPeerData(AgentXmppChannel *channel) :
      67          21 :     ControllerWorkQueueData(), channel_(channel) {
      68          21 : }
      69             : 
      70           2 : VNController::VNController(Agent *agent)
      71           2 :     : agent_(agent), multicast_sequence_number_(0),
      72           2 :     work_queue_(agent->task_scheduler()->GetTaskId("Agent::ControllerXmpp"), 0,
      73             :                 boost::bind(&VNController::ControllerWorkQueueProcess, this,
      74             :                             _1)),
      75           6 :     fabric_multicast_label_range_(), xmpp_channel_down_cb_(),
      76           8 :     disconnect_(false) {
      77           2 :     work_queue_.set_name("Controller Queue");
      78           6 :     for (uint8_t count = 0; count < MAX_XMPP_SERVERS; count++) {
      79           4 :         timed_out_channels_[count].clear();
      80             :     }
      81           2 :     delpeer_walks_.clear();
      82           2 :     work_queue_.set_disable(true);
      83           2 : }
      84             : 
      85          14 : VNController::~VNController() {
      86           2 :     work_queue_.Shutdown();
      87          20 : }
      88             : 
      89           4 : void VNController::FillMcastLabelRange(uint32_t *start_idx,
      90             :                                        uint32_t *end_idx,
      91             :                                        uint8_t idx) const {
      92             :     // Multicast labels required by both control nodes
      93           4 :     uint32_t max_mc_labels = 2 * (agent_->vrouter_max_vrfs());
      94             :     // Multicast label count per control node
      95           4 :     uint32_t mc_label_count = 0;
      96           4 :     uint32_t vrouter_max_labels = agent_->vrouter_max_labels();
      97             : 
      98           4 :     if (max_mc_labels + MIN_UNICAST_LABEL_RANGE < vrouter_max_labels) {
      99           4 :         mc_label_count = agent_->vrouter_max_vrfs();
     100             :     } else {
     101           0 :         mc_label_count = (vrouter_max_labels - MIN_UNICAST_LABEL_RANGE)/2;
     102             :     }
     103             : 
     104           4 :     *start_idx = vrouter_max_labels - ((idx + 1) * mc_label_count);
     105           4 :     *end_idx = (vrouter_max_labels - ((idx) * mc_label_count) - 1);
     106           4 : }
     107             : 
     108           4 : void VNController::SetAgentMcastLabelRange(uint8_t idx) {
     109           4 :     uint32_t start = 0;
     110           4 :     uint32_t end = 0;
     111           4 :     std::stringstream str;
     112             : 
     113             :     //Logic for multicast label allocation
     114             :     //  1> Reserve minimum 4k label for unicast
     115             :     //  2> In the remaining label space
     116             :     //       * Try allocating labels equal to no. of VN
     117             :     //         for each control node
     118             :     //       * If label space is not huge enough
     119             :     //         split remaining unicast label for both control
     120             :     //         node
     121             :     //  Remaining label would be used for unicast mpls label
     122           4 :     if (agent_->vrouter_max_labels() <=  MIN_UNICAST_LABEL_RANGE) {
     123           0 :         str << 0 << "-" << 0;
     124           0 :         fabric_multicast_label_range_[idx].start = 0;
     125           0 :         fabric_multicast_label_range_[idx].end = 0;
     126           0 :         fabric_multicast_label_range_[idx].fabric_multicast_label_range_str =
     127           0 :             str.str();
     128           0 :         return;
     129             :     }
     130             : 
     131           4 :     FillMcastLabelRange(&start, &end, idx);
     132           4 :     str << start << "-" << end;
     133             : 
     134           4 :     agent_->mpls_table()->ReserveMulticastLabel(start, end, idx);
     135           4 :     fabric_multicast_label_range_[idx].start = start;
     136           4 :     fabric_multicast_label_range_[idx].end = end;
     137           4 :     fabric_multicast_label_range_[idx].fabric_multicast_label_range_str =
     138           4 :         str.str();
     139           4 : }
     140             : 
     141           4 : void VNController::SetDscpConfig(XmppChannelConfig *xmpp_cfg) const {
     142           4 :     GlobalQosConfig* qos = NULL;
     143           4 :     if (agent_->oper_db()) {
     144           4 :         qos = agent_->oper_db()->global_qos_config();
     145             :     }
     146           4 :     if (qos && qos->control_dscp() != GlobalQosConfig::kInvalidDscp) {
     147           4 :         xmpp_cfg->dscp_value = qos->control_dscp();
     148             :     } else {
     149           0 :         xmpp_cfg->dscp_value = 0;
     150             :     }
     151           4 : }
     152             : 
     153           2 : void VNController::XmppServerConnect() {
     154             : 
     155           2 :     uint8_t count = 0;
     156             : 
     157           2 :     if (agent_->is_vhost_interface_up() == false) {
     158             :         // Sleep 100 msec to avoid cpu hogging.
     159           0 :         usleep(100000);
     160             :         ControllerConnectRetryDataType data(
     161           0 :             new ControllerConnectRetryData(true, false));
     162             :         ControllerWorkQueueDataType base_data =
     163           0 :             boost::static_pointer_cast<ControllerWorkQueueData>(data);
     164           0 :         work_queue_.Enqueue(base_data);
     165           0 :         return;
     166           0 :     }
     167             : 
     168           6 :     while (count < MAX_XMPP_SERVERS) {
     169           4 :         SetAgentMcastLabelRange(count);
     170           4 :         if (!agent_->controller_ifmap_xmpp_server(count).empty()) {
     171             : 
     172           2 :             AgentXmppChannel *ch = agent_->controller_xmpp_channel(count);
     173           2 :             if (ch) {
     174             :                 // Channel is created, do not disturb
     175           0 :                 CONTROLLER_CONNECTIONS_TRACE(DiscoveryConnection,
     176             :                     "XMPP Server is already present, ignore reconfig response",
     177             :                     count, ch->GetXmppServer(), "");
     178           0 :                 count++;
     179           0 :                 continue;
     180             :             }
     181             : 
     182           2 :             boost::system::error_code ec;
     183           2 :             XmppChannelConfig *xmpp_cfg = new XmppChannelConfig(true);
     184           2 :             xmpp_cfg->ToAddr = XmppInit::kControlNodeJID;
     185           2 :             xmpp_cfg->FromAddr = agent_->agent_name();
     186           2 :             xmpp_cfg->NodeAddr = XmppInit::kPubSubNS;
     187             :             FastConvergenceParameters fc_params =
     188           2 :                 agent_->oper_db()->global_system_config()->fc_params();
     189           2 :             if (fc_params.enable) {
     190           0 :                 xmpp_cfg->xmpp_hold_time = fc_params.xmpp_hold_time;
     191             :             }
     192             :             /*
     193             :              * Since TcpServer asserts in bind failure,
     194             :              * dont need to assert in failure.
     195             :              * Also TorAgent can be run anywhere. so vhost0
     196             :              * ip binding should not be done for TorAgent.
     197             :              */
     198           2 :             Ip4Address local_endpoint;
     199           2 :             if (!agent_->tor_agent_enabled() && !agent_->isMockMode()) {
     200           2 :                 local_endpoint = agent_->compute_node_ip();
     201           2 :                 xmpp_cfg->local_endpoint.address(AddressFromString(
     202           4 :                     local_endpoint.to_string(), &ec));
     203             :             }
     204           2 :             if (agent_->test_mode()) {
     205           2 :                 local_endpoint = Ip4Address::from_string("127.0.0.1", ec);
     206           2 :                 xmpp_cfg->local_endpoint.address(AddressFromString(
     207           4 :                     local_endpoint.to_string(), &ec));
     208             :             }
     209           2 :             xmpp_cfg->endpoint.address(AddressFromString(
     210           2 :                 agent_->controller_ifmap_xmpp_server(count), &ec));
     211           2 :             assert(ec.value() == 0);
     212           2 :             xmpp_cfg->auth_enabled = agent_->xmpp_auth_enabled();
     213           2 :             if (xmpp_cfg->auth_enabled) {
     214           0 :                 xmpp_cfg->path_to_server_cert =  agent_->xmpp_server_cert();
     215           0 :                 xmpp_cfg->path_to_server_priv_key =  agent_->xmpp_server_key();
     216           0 :                 xmpp_cfg->path_to_ca_cert =  agent_->xmpp_ca_cert();
     217             :             }
     218           2 :             uint16_t port = agent_->controller_ifmap_xmpp_port(count);
     219           2 :             xmpp_cfg->endpoint.port(port);
     220           2 :             SetDscpConfig(xmpp_cfg);
     221           2 :             xmpp_cfg->xmlns = agent_->subcluster_name();
     222             : 
     223             :             // Create Xmpp Client
     224           2 :             XmppClient *client = new XmppClient(agent_->event_manager(), xmpp_cfg);
     225             : 
     226           2 :             XmppInit *xmpp = new XmppInit();
     227           2 :             xmpp->AddXmppChannelConfig(xmpp_cfg);
     228             :             // create bgp peer
     229             :             AgentXmppChannel *bgp_peer = new AgentXmppChannel(agent_,
     230           2 :                               agent_->controller_ifmap_xmpp_server(count),
     231           2 :                               fabric_multicast_label_range(count).
     232           2 :                                                fabric_multicast_label_range_str,
     233           2 :                               count);
     234           2 :             client->RegisterConnectionEvent(xmps::BGP,
     235             :                boost::bind(&AgentXmppChannel::XmppClientChannelEvent,
     236             :                            bgp_peer, _2));
     237           2 :             xmpp->InitClient(client);
     238             : 
     239             :             XmppChannel *channel = client->
     240           2 :                 FindChannel(XmppInit::kControlNodeJID);
     241           2 :             assert(channel);
     242           2 :             channel->RegisterRxMessageTraceCallback(
     243             :                              boost::bind(&VNController::RxXmppMessageTrace,
     244           2 :                                          this, bgp_peer->GetXmppServerIdx(),
     245             :                                          _1, _2, _3, _4, _5));
     246           2 :             channel->RegisterTxMessageTraceCallback(
     247             :                              boost::bind(&VNController::TxXmppMessageTrace,
     248           2 :                                          this, bgp_peer->GetXmppServerIdx(),
     249             :                                          _1, _2, _3, _4, _5));
     250           2 :             bgp_peer->RegisterXmppChannel(channel);
     251             : 
     252           2 :             bgp_peer->UpdateConnectionInfo(channel->GetPeerState());
     253             : 
     254             :             // create ifmap peer
     255             :             AgentIfMapXmppChannel *ifmap_peer =
     256           2 :                 new AgentIfMapXmppChannel(agent_, channel, count);
     257             : 
     258           2 :             agent_->set_controller_xmpp_channel(bgp_peer, count);
     259           2 :             agent_->set_ifmap_xmpp_channel(ifmap_peer, count);
     260           2 :             agent_->set_controller_ifmap_xmpp_client(client, count);
     261           2 :             agent_->set_controller_ifmap_xmpp_init(xmpp, count);
     262             :         }
     263           4 :         count++;
     264             :     }
     265             : }
     266             : 
     267           2 : void VNController::DnsXmppServerConnect() {
     268             : 
     269           2 :     if (agent_->GetDnsProto() == NULL) {
     270           0 :         return;
     271             :     }
     272             : 
     273           2 :     if (agent_->is_vhost_interface_up() == false) {
     274             :         // Sleep 100 msec to avoid cpu hogging.
     275           0 :         usleep(100000);
     276             :         ControllerConnectRetryDataType data(
     277           0 :             new ControllerConnectRetryData(false, true));
     278             :         ControllerWorkQueueDataType base_data =
     279           0 :             boost::static_pointer_cast<ControllerWorkQueueData>(data);
     280           0 :         work_queue_.Enqueue(base_data);
     281           0 :         return;
     282           0 :     }
     283             : 
     284           2 :     uint8_t count = 0;
     285           6 :     while (count < MAX_XMPP_SERVERS) {
     286           4 :         if (!agent_->dns_server(count).empty()) {
     287             : 
     288           2 :             AgentDnsXmppChannel *ch = agent_->dns_xmpp_channel(count);
     289           2 :             if (ch) {
     290             :                 // Channel is up and running, do not disturb
     291           0 :                 CONTROLLER_CONNECTIONS_TRACE(DiscoveryConnection,
     292             :                     "DNS Server is already present, ignore reconfig response",
     293             :                     count, ch->GetXmppServer(), "");
     294           0 :                 count++;
     295           0 :                 continue;
     296             :             }
     297             : 
     298             :             // XmppChannel Configuration
     299           2 :             boost::system::error_code ec;
     300           2 :             XmppChannelConfig *xmpp_cfg_dns = new XmppChannelConfig(true);
     301           2 :             xmpp_cfg_dns->ToAddr = XmppInit::kDnsNodeJID;
     302           2 :             xmpp_cfg_dns->FromAddr = agent_->agent_name() + "/dns";
     303           2 :             xmpp_cfg_dns->NodeAddr = "";
     304             :             /*
     305             :              * Since TcpServer asserts in bind failure,
     306             :              * dont need to assert in failure.
     307             :              * Also TorAgent can be run anywhere. so vhost0
     308             :              * ip binding should not be done for TorAgent.
     309             :              */
     310           2 :             Ip4Address local_endpoint;
     311           2 :             if (!agent_->tor_agent_enabled() && !agent_->isMockMode()) {
     312           2 :                 local_endpoint = agent_->compute_node_ip();
     313           2 :                 xmpp_cfg_dns->local_endpoint.address(AddressFromString(
     314           4 :                     local_endpoint.to_string(), &ec));
     315             :             }
     316           2 :             if (agent_->test_mode()) {
     317           2 :                 local_endpoint = Ip4Address::from_string("127.0.0.1", ec);
     318           2 :                 xmpp_cfg_dns->local_endpoint.address(AddressFromString(
     319           4 :                     local_endpoint.to_string(), &ec));
     320             :             }
     321           2 :             xmpp_cfg_dns->endpoint.address(AddressFromString(
     322           2 :                 agent_->dns_server(count), &ec));
     323           2 :             assert(ec.value() == 0);
     324           2 :             if (agent_->xmpp_dns_test_mode()) {
     325           0 :                 xmpp_cfg_dns->endpoint.port(agent_->dns_server_port(count));
     326             :             } else {
     327           2 :                 xmpp_cfg_dns->endpoint.port(ContrailPorts::DnsXmpp());
     328             :             }
     329           2 :             xmpp_cfg_dns->auth_enabled = agent_->dns_auth_enabled();
     330           2 :             if (xmpp_cfg_dns->auth_enabled) {
     331           0 :                 xmpp_cfg_dns->path_to_server_cert = agent_->xmpp_server_cert();
     332           0 :                 xmpp_cfg_dns->path_to_server_priv_key =  agent_->xmpp_server_key();
     333           0 :                 xmpp_cfg_dns->path_to_ca_cert =  agent_->xmpp_ca_cert();
     334             :             }
     335           2 :             SetDscpConfig(xmpp_cfg_dns);
     336             : 
     337             :             // Create Xmpp Client
     338           2 :             XmppClient *client_dns = new XmppClient(agent_->event_manager(),
     339           2 :                                                     xmpp_cfg_dns);
     340             : 
     341           2 :             XmppInit *xmpp_dns = new XmppInit();
     342             :             // create dns peer
     343             :             AgentDnsXmppChannel *dns_peer = new AgentDnsXmppChannel(agent_,
     344           2 :                                                 agent_->dns_server(count),
     345           2 :                                                 count);
     346           2 :             client_dns->RegisterConnectionEvent(xmps::DNS,
     347             :                 boost::bind(&AgentDnsXmppChannel::XmppClientChannelEvent,
     348             :                             dns_peer, _2));
     349             : 
     350           2 :             xmpp_dns->AddXmppChannelConfig(xmpp_cfg_dns);
     351           2 :             xmpp_dns->InitClient(client_dns);
     352             : 
     353           2 :             XmppChannel *channel_dns = client_dns->FindChannel(
     354             :                                                    XmppInit::kDnsNodeJID);
     355           2 :             assert(channel_dns);
     356           2 :             dns_peer->RegisterXmppChannel(channel_dns);
     357             : 
     358             : 
     359           2 :             dns_peer->UpdateConnectionInfo(channel_dns->GetPeerState());
     360           2 :             agent_->set_dns_xmpp_client(client_dns, count);
     361           2 :             agent_->set_dns_xmpp_channel(dns_peer, count);
     362           2 :             agent_->set_dns_xmpp_init(xmpp_dns, count);
     363             :         }
     364           4 :         count++;
     365             :     }
     366             : }
     367             : 
     368           2 : void VNController::Connect() {
     369             :     /* Connect to Control-Node Xmpp Server */
     370           2 :     controller_list_chksum_ = Agent::GetInstance()->GetControllerlistChksum();
     371           2 :     XmppServerConnect();
     372             : 
     373             :     /* Connect to DNS Xmpp Server */
     374           2 :     dns_list_chksum_ = Agent::GetInstance()->GetDnslistChksum();
     375           2 :     DnsXmppServerConnect();
     376             : 
     377             :     /* Inits */
     378           2 :     agent_->controller()->increment_multicast_sequence_number();
     379           2 :     agent_->set_cn_mcast_builder(NULL);
     380           2 :     agent_ifmap_vm_export_.reset(new AgentIfMapVmExport(agent_));
     381           2 :     agent_->oper_db()->global_system_config()->gres_parameters().
     382           2 :         Register(boost::bind(&VNController::GracefulRestartConfigListener,
     383             :                              this));
     384           2 : }
     385             : 
     386             : // Disconnect on agent shutdown.
     387           2 : void VNController::XmppServerDisConnect() {
     388             :     XmppClient *cl;
     389           2 :     uint8_t count = 0;
     390           2 :     disconnect_ = true;
     391           6 :     while (count < MAX_XMPP_SERVERS) {
     392           4 :         if ((cl = agent_->controller_ifmap_xmpp_client(count)) != NULL) {
     393           0 :             cl->Shutdown();
     394             :         }
     395             :         // Delpeer walk for channel slot = count
     396           4 :         StartDelPeerWalk(agent_->controller_xmpp_channel_ref(count));
     397             :         // Delpeer walk done for timedout channels which used to represent this
     398             :         // slot.
     399           4 :         FlushTimedOutChannels(count);
     400           4 :         count ++;
     401             :     }
     402           2 : }
     403             : 
     404             : // Test-only code to verify that xmpp timeout has been updated correctly.
     405           0 : bool VNController::VerifyXmppServerTimeout(uint32_t to) {
     406             :     XmppClient *cl;
     407           0 :     uint8_t count = 0;
     408           0 :     while (count < MAX_XMPP_SERVERS) {
     409           0 :         if ((cl = agent_->controller_ifmap_xmpp_client(count)) != NULL) {
     410           0 :             if (to != cl->XmppTimeOut(XmppInit::kControlNodeJID))
     411           0 :                 return false;
     412             :         }
     413           0 :         count++;
     414             :     }
     415           0 :     return true;
     416             : }
     417             : 
     418             : // Update agent-controller xmpp connection witth new timeout value.
     419           0 : void VNController::XmppServerUpdate(uint8_t xmpp_hold_time) {
     420             :     XmppClient *cl;
     421           0 :     uint8_t count = 0;
     422           0 :     while (count < MAX_XMPP_SERVERS) {
     423           0 :         if ((cl = agent_->controller_ifmap_xmpp_client(count)) != NULL) {
     424           0 :             cl->UpdateTimeOut(xmpp_hold_time, XmppInit::kControlNodeJID);
     425             :         }
     426           0 :         count ++;
     427             :     }
     428           0 : }
     429             : 
     430           2 : void VNController::DnsXmppServerDisConnect() {
     431             :     XmppClient *cl;
     432           2 :     uint8_t count = 0;
     433           6 :     while (count < MAX_XMPP_SERVERS) {
     434           4 :         if ((cl = agent_->dns_xmpp_client(count)) != NULL) {
     435           0 :             cl->Shutdown();
     436             :         }
     437           4 :         count ++;
     438             :     }
     439           2 : }
     440             : 
     441          25 : void VNController::StartDelPeerWalk(AgentXmppChannelPtr ptr) {
     442          25 :     if (!ptr.get())
     443           4 :         return;
     444             : 
     445          21 :     ptr.get()->bgp_peer_id()->DelPeerRoutes(
     446             :         boost::bind(&VNController::DelPeerWalkDone, this, ptr.get()),
     447             :         ptr.get()->sequence_number());
     448          21 :     delpeer_walks_.push_back(ptr);
     449             : }
     450             : 
     451             : //During delete of xmpp channel, check if BGP peer is deleted.
     452             : //If not agent never got a channel down state and is being removed
     453             : //as it is not part of discovery list.
     454             : //Artificially inject NOT_READY in agent xmpp channel.
     455          21 : void VNController::DeleteAgentXmppChannel(uint8_t idx) {
     456          21 :     AgentXmppChannel *channel = agent_->controller_xmpp_channel(idx);
     457          21 :     if (!channel)
     458           0 :         return;
     459             : 
     460          21 :     BgpPeer *bgp_peer = channel->bgp_peer_id();
     461          21 :     if (bgp_peer != NULL) {
     462             :         //Defer delete of channel till delete walk of bgp peer is over.
     463             :         //Till walk is over, unregister as table listener is not done and there
     464             :         //may be notifications(which will be ignored though, but valid pointer is
     465             :         //needed). BgpPeer destructor will handle deletion of channel.
     466          21 :         AgentXmppChannel::HandleAgentXmppClientChannelEvent(channel,
     467             :                                                             xmps::NOT_READY);
     468          21 :         channel->Unregister();
     469             :     }
     470             :     //Every delete of channel should delete flow of bgp-as-a-service,
     471             :     //which is using this CN.
     472          21 :     if (xmpp_channel_down_cb_.empty() == false) {
     473          21 :         xmpp_channel_down_cb_(idx);
     474             :     }
     475             : }
     476             : 
     477             : //Trigger shutdown and cleanup of routes for the client
     478           2 : void VNController::DisConnect() {
     479           2 :     XmppServerDisConnect();
     480           2 :     DnsXmppServerDisConnect();
     481           2 : }
     482             : 
     483           4 : void VNController::Cleanup() {
     484           4 :     uint8_t count = 0;
     485             :     XmppClient *cl;
     486          12 :     while (count < MAX_XMPP_SERVERS) {
     487           8 :         if ((cl = agent_->controller_ifmap_xmpp_client(count)) != NULL) {
     488           2 :             DisConnectControllerIfmapServer(count);
     489             :         }
     490           8 :         if ((cl = agent_->dns_xmpp_client(count)) != NULL) {
     491           2 :             DisConnectDnsServer(count);
     492             :         }
     493           8 :         count++;
     494             :     }
     495             : 
     496           4 :     agent_->controller()->increment_multicast_sequence_number();
     497           4 :     agent_->set_cn_mcast_builder(NULL);
     498           4 :     agent_ifmap_vm_export_.reset();
     499           4 : }
     500             : 
     501             : 
     502           0 : AgentXmppChannel *VNController::FindAgentXmppChannel(
     503             :                                 const std::string &server_ip) {
     504             : 
     505           0 :     uint8_t count = 0;
     506           0 :     while (count < MAX_XMPP_SERVERS) {
     507           0 :         AgentXmppChannel *ch = agent_->controller_xmpp_channel(count);
     508           0 :         if (ch && (ch->GetXmppServer().compare(server_ip) == 0)) {
     509           0 :             return ch;
     510             :         }
     511           0 :         count++;
     512             :     }
     513             : 
     514           0 :     return NULL;
     515             : }
     516             : 
     517           4 : const string VNController::MakeConnectionPrefix(bool is_dns) const {
     518           4 :     string name_prefix;
     519           4 :     if (is_dns) {
     520           2 :         name_prefix = agent_->xmpp_dns_server_prefix();
     521             :     } else {
     522           2 :         name_prefix = agent_->xmpp_control_node_prefix();
     523             :     }
     524           4 :     return name_prefix;
     525           0 : }
     526             : 
     527           4 : void VNController::DeleteConnectionInfo(const std::string &addr, bool is_dns)
     528             :                                         const {
     529           4 :     boost::asio::ip::tcp::endpoint ep;
     530           4 :     boost::system::error_code ec;
     531           4 :     ep.address(AddressFromString(addr, &ec));
     532           4 :     const string name =  ep.address().to_string();
     533           4 :     const string &name_prefix = MakeConnectionPrefix(is_dns);
     534           8 :     agent_->connection_state()->Delete(process::ConnectionType::XMPP,
     535           8 :                                            name_prefix + name);
     536           4 : }
     537             : 
     538          21 : void VNController::DelPeerWalkDone(AgentXmppChannel* ptr) {
     539          21 :     ControllerDelPeerDataType data(new ControllerDelPeerData(ptr));
     540             :     ControllerWorkQueueDataType base_data =
     541          21 :         boost::static_pointer_cast<ControllerWorkQueueData>(data);
     542          21 :     work_queue_.Enqueue(base_data);
     543          21 : }
     544             : 
     545          21 : void VNController::DelPeerWalkDoneProcess(AgentXmppChannel *channel) {
     546          21 :     DynamicPeer::ProcessDelete(channel->bgp_peer_id());
     547          21 :     for (AgentXmppChannelListIter it = delpeer_walks_.begin();
     548          21 :          it != delpeer_walks_.end(); it++) {
     549          21 :         if ((*it).get() == channel) {
     550          21 :             delpeer_walks_.erase(it);
     551          21 :             break;
     552             :         }
     553             :     }
     554             :     //delete channel;
     555          21 :     if (disconnect_ & delpeer_walks_.empty())
     556           2 :         Cleanup();
     557          21 : }
     558             : 
     559          63 : void VNController::FlushTimedOutChannels(uint8_t idx) {
     560          63 :     for (AgentXmppChannelListIter it = timed_out_channels_[idx].begin();
     561          84 :          it != timed_out_channels_[idx].end(); it++) {
     562          21 :         StartDelPeerWalk((*it));
     563             :     }
     564          63 :     timed_out_channels_[idx].clear();
     565          63 : }
     566             : 
     567          21 : void VNController::DisConnectControllerIfmapServer(uint8_t idx) {
     568             : 
     569             :     // Managed Delete of XmppClient object, which deletes the
     570             :     // dependent XmppClientConnection object and
     571             :     // scoped XmppChannel object
     572          21 :     XmppClient *xc = agent_->controller_ifmap_xmpp_client(idx);
     573             :     //In case of UT xc can be NULL
     574          21 :     if (!agent_->test_mode() || xc) {
     575           2 :         xc->UnRegisterConnectionEvent(xmps::BGP);
     576           2 :         xc->Shutdown(); // ManagedDelete
     577             :     }
     578          21 :     agent_->set_controller_ifmap_xmpp_client(NULL, idx);
     579             : 
     580             :     //cleanup AgentXmppChannel
     581          21 :     DeleteAgentXmppChannel(idx);
     582             :     //Trigger removal from service inuse list for discovery
     583             :     //cleanup AgentIfmapXmppChannel
     584          21 :     timed_out_channels_[idx].push_back(agent_->controller_xmpp_channel_ref(idx));
     585          21 :     agent_->controller_xmpp_channel(idx)->UpdateConnectionInfo(xmps::TIMEDOUT);
     586          21 :     agent_->reset_controller_xmpp_channel(idx);
     587             : 
     588          21 :     delete agent_->ifmap_xmpp_channel(idx);
     589          21 :     agent_->set_ifmap_xmpp_channel(NULL, idx);
     590             : 
     591          21 :     if (!agent_->test_mode() || xc) {
     592           2 :         agent_->controller_ifmap_xmpp_init(idx)->Reset();
     593           2 :         delete agent_->controller_ifmap_xmpp_init(idx);
     594           2 :         agent_->set_controller_ifmap_xmpp_init(NULL, idx);
     595           2 :         DeleteConnectionInfo(agent_->controller_ifmap_xmpp_server(idx), false);
     596             :     }
     597             : 
     598          21 :     agent_->reset_controller_ifmap_xmpp_server(idx);
     599          21 : }
     600             : 
     601           0 : bool VNController::AgentReConfigXmppServerConnectedExists(
     602             :                                  const std::string &server_ip,
     603             :                                  std::vector<std::string> resp) {
     604             : 
     605           0 :     std::vector<std::string>::iterator iter;
     606           0 :     int8_t count = -1;
     607           0 :     int8_t min_iter = std::min(static_cast<int>(resp.size()), MAX_XMPP_SERVERS);
     608           0 :     for (iter = resp.begin(); ++count < min_iter; iter++) {
     609           0 :         std::vector<string> servers;
     610           0 :         boost::split(servers, *iter, boost::is_any_of(":"));
     611           0 :         if (servers[0].compare(server_ip) == 0) {
     612           0 :             return true;
     613             :         }
     614           0 :     }
     615           0 :     return false;
     616             : }
     617             : 
     618             : 
     619           0 : void VNController::ReConnectXmppServer() {
     620             : 
     621             :     std::vector<string> controller_list =
     622           0 :         Agent::GetInstance()->GetControllerlist();
     623             : 
     624             :     ControllerReConfigDataType data(new ControllerReConfigData(
     625             :         g_vns_constants.XMPP_SERVER_DISCOVERY_SERVICE_NAME,
     626           0 :         controller_list));
     627             :     ControllerWorkQueueDataType base_data =
     628           0 :         boost::static_pointer_cast<ControllerWorkQueueData>(data);
     629           0 :     work_queue_.Enqueue(base_data);
     630           0 : }
     631             : 
     632           0 : void VNController::ReConnectDnsServer() {
     633             : 
     634             :     std::vector<string> dns_list =
     635           0 :         Agent::GetInstance()->GetDnslist();
     636             : 
     637             :     ControllerReConfigDataType data(new ControllerReConfigData(
     638             :         g_vns_constants.DNS_SERVER_DISCOVERY_SERVICE_NAME,
     639           0 :         dns_list));
     640             :     ControllerWorkQueueDataType base_data =
     641           0 :         boost::static_pointer_cast<ControllerWorkQueueData>(data);
     642           0 :     work_queue_.Enqueue(base_data);
     643           0 : }
     644             : 
     645           0 : void VNController::ReConnect() {
     646             : 
     647           0 :     if (controller_list_chksum_ !=
     648           0 :         Agent::GetInstance()->GetControllerlistChksum()) {
     649             : 
     650           0 :         controller_list_chksum_ =
     651           0 :             Agent::GetInstance()->GetControllerlistChksum();
     652             : 
     653           0 :         ReConnectXmppServer();
     654             :     }
     655             : 
     656           0 :     if (dns_list_chksum_ !=
     657           0 :         Agent::GetInstance()->GetDnslistChksum()) {
     658             : 
     659           0 :         dns_list_chksum_ =
     660           0 :             Agent::GetInstance()->GetDnslistChksum();
     661             : 
     662           0 :         ReConnectDnsServer();
     663             :     }
     664           0 : }
     665             : 
     666           0 : bool VNController::ApplyControllerReConfigInternal(std::vector<string> resp) {
     667           0 :     XmppServerAddressParser addressParser(XMPP_SERVER_PORT, MAX_XMPP_SERVERS);
     668           0 :     std::vector<string>::iterator iter;
     669           0 :     int8_t count = -1;
     670             : 
     671             :     /* Apply only MAX_XMPP_SERVERS from list as the list is ordered */
     672           0 :     int8_t min_iter = std::min(static_cast<int>(resp.size()), MAX_XMPP_SERVERS);
     673           0 :     for (iter = resp.begin(); ++count < min_iter; iter++) {
     674           0 :         std::string server_ip;
     675             :         uint16_t server_port;
     676           0 :         addressParser.ParseAddress(*iter, &server_ip, &server_port);
     677           0 :         std::string str_port = integerToString(server_port);
     678             : 
     679           0 :         CONTROLLER_CONNECTIONS_TRACE(DiscoveryConnection, "XMPP ReConfig Apply Server Ip",
     680             :             count, server_ip, str_port);
     681             : 
     682           0 :         AgentXmppChannel *chnl = FindAgentXmppChannel(server_ip);
     683           0 :         if (chnl) {
     684           0 :             if (chnl->GetXmppChannel() &&
     685           0 :                 chnl->GetXmppChannel()->GetPeerState() == xmps::READY) {
     686           0 :                 CONTROLLER_CONNECTIONS_TRACE(DiscoveryConnection,
     687             :                     " XMPP ReConfig Server is READY and running, ignore", count,
     688             :                     chnl->GetXmppServer(), "");
     689           0 :                 continue;
     690             :             } else {
     691           0 :                 CONTROLLER_CONNECTIONS_TRACE(DiscoveryConnection,
     692             :                     " XMPP ReConfig Server is NOT_READY, ignore", count,
     693             :                     chnl->GetXmppServer(), "");
     694           0 :                 continue;
     695             :             }
     696             : 
     697             :         } else {
     698             : 
     699           0 :             for (uint8_t xs_idx = 0; xs_idx < MAX_XMPP_SERVERS; xs_idx++) {
     700             : 
     701           0 :                 if (agent_->controller_ifmap_xmpp_server(xs_idx).empty()) {
     702             : 
     703           0 :                     CONTROLLER_CONNECTIONS_TRACE(DiscoveryConnection,
     704             :                         "Set Xmpp ReConfig Channel",
     705             :                         xs_idx, server_ip, str_port);
     706             : 
     707           0 :                     agent_->set_controller_ifmap_xmpp_server(server_ip, xs_idx);
     708           0 :                     agent_->set_controller_ifmap_xmpp_port(server_port, xs_idx);
     709           0 :                     break;
     710             : 
     711           0 :                 } else if (agent_->controller_xmpp_channel(xs_idx)) {
     712             : 
     713           0 :                     if (AgentReConfigXmppServerConnectedExists(
     714           0 :                         agent_->controller_ifmap_xmpp_server(xs_idx), resp)) {
     715             : 
     716           0 :                         CONTROLLER_CONNECTIONS_TRACE(DiscoveryConnection,
     717             :                             "Retain Xmpp ReConfig Channel ", xs_idx,
     718             :                              agent_->controller_ifmap_xmpp_server(xs_idx), "");
     719           0 :                         continue;
     720             :                     }
     721             : 
     722           0 :                     CONTROLLER_CONNECTIONS_TRACE(DiscoveryConnection,
     723             :                         "ReSet Xmpp ReConfig Channel ", xs_idx,
     724             :                         agent_->controller_ifmap_xmpp_server(xs_idx),
     725             :                         server_ip);
     726             : 
     727           0 :                     DisConnectControllerIfmapServer(xs_idx);
     728           0 :                     agent_->set_controller_ifmap_xmpp_server(server_ip, xs_idx);
     729           0 :                     agent_->set_controller_ifmap_xmpp_port(server_port, xs_idx);
     730           0 :                     break;
     731             :                 }
     732             :             }
     733             :         }
     734           0 :     }
     735             : 
     736           0 :     XmppServerConnect();
     737           0 :     return true;
     738             : }
     739             : 
     740           0 : AgentDnsXmppChannel *VNController::FindAgentDnsXmppChannel(
     741             :                                    const std::string &server_ip) {
     742             : 
     743           0 :     uint8_t count = 0;
     744           0 :     while (count < MAX_XMPP_SERVERS) {
     745           0 :         AgentDnsXmppChannel *ch = agent_->dns_xmpp_channel(count);
     746           0 :         if (ch && (ch->GetXmppServer().compare(server_ip) == 0)) {
     747           0 :             return ch;
     748             :         }
     749           0 :         count++;
     750             :     }
     751             : 
     752           0 :     return NULL;
     753             : }
     754             : 
     755           2 : void VNController::DisConnectDnsServer(uint8_t idx) {
     756             : 
     757             :     // Managed Delete of XmppClient object, which deletes the
     758             :     // dependent XmppClientConnection object and
     759             :     // scoped_ptr XmppChannel
     760           2 :     XmppClient *xc = agent_->dns_xmpp_client(idx);
     761           2 :     xc->UnRegisterConnectionEvent(xmps::DNS);
     762           2 :     xc->Shutdown();
     763           2 :     agent_->set_dns_xmpp_client(NULL, idx);
     764             : 
     765             :     //cleanup AgentDnsXmppChannel
     766           2 :     delete agent_->dns_xmpp_channel(idx);
     767           2 :     agent_->set_dns_xmpp_channel(NULL, idx);
     768             : 
     769           2 :     agent_->dns_xmpp_init(idx)->Reset();
     770           2 :     delete agent_->dns_xmpp_init(idx);
     771           2 :     agent_->set_dns_xmpp_init(NULL, idx);
     772             : 
     773           2 :     DeleteConnectionInfo(agent_->dns_server(idx), true);
     774           2 :     agent_->reset_dns_server(idx);
     775           2 : }
     776             : 
     777           0 : bool VNController::ApplyDnsReConfigInternal(std::vector<string> resp) {
     778           0 :     XmppServerAddressParser addressParser(XMPP_DNS_SERVER_PORT, MAX_XMPP_SERVERS);
     779           0 :     std::vector<string>::iterator iter;
     780           0 :     int8_t count = -1;
     781             : 
     782             :     /* Apply only MAX_XMPP_SERVERS from list as the list is ordered */
     783           0 :     int8_t min_iter = std::min(static_cast<int>(resp.size()), MAX_XMPP_SERVERS);
     784           0 :     for (iter = resp.begin(); ++count < min_iter; iter++) {
     785           0 :         std::string server_ip;
     786             :         uint16_t server_port;
     787           0 :         addressParser.ParseAddress(*iter, &server_ip, &server_port);
     788           0 :         std::string str_port = integerToString(server_port);
     789             : 
     790           0 :         CONTROLLER_CONNECTIONS_TRACE(DiscoveryConnection,
     791             :             "DNS Server ReConfig Apply Server Ip",
     792             :             count, server_ip, str_port);
     793             : 
     794           0 :         AgentDnsXmppChannel *chnl = FindAgentDnsXmppChannel(server_ip);
     795           0 :         if (chnl) {
     796           0 :             if (chnl->GetXmppChannel() &&
     797           0 :                 chnl->GetXmppChannel()->GetPeerState() == xmps::READY) {
     798           0 :                 CONTROLLER_CONNECTIONS_TRACE(DiscoveryConnection,
     799             :                     "DNS Server is READY and running, ignore", count,
     800             :                     chnl->GetXmppServer(), "");
     801           0 :                 continue;
     802             :             } else {
     803           0 :                 CONTROLLER_CONNECTIONS_TRACE(DiscoveryConnection,
     804             :                     "DNS Server is NOT_READY, ignore", count,
     805             :                     chnl->GetXmppServer(), "");
     806           0 :                 continue;
     807             :             }
     808             :         } else {
     809             : 
     810           0 :             for (uint8_t xs_idx = 0; xs_idx < MAX_XMPP_SERVERS; xs_idx++) {
     811             : 
     812           0 :                 if (agent_->dns_server(xs_idx).empty()) {
     813             : 
     814           0 :                     CONTROLLER_CONNECTIONS_TRACE(DiscoveryConnection,
     815             :                         "Set Dns Xmpp Channel ",
     816             :                         xs_idx, server_ip, str_port);
     817             : 
     818           0 :                     agent_->set_dns_server(server_ip, xs_idx);
     819           0 :                     agent_->set_dns_server_port(server_port, xs_idx);
     820           0 :                     break;
     821             : 
     822           0 :                 } else if (agent_->dns_xmpp_channel(xs_idx)) {
     823             : 
     824           0 :                     if (AgentReConfigXmppServerConnectedExists(
     825           0 :                         agent_->dns_server(xs_idx), resp)) {
     826             : 
     827           0 :                         CONTROLLER_CONNECTIONS_TRACE(DiscoveryConnection,
     828             :                             "Retain Dns Xmpp Channel ", xs_idx,
     829             :                             agent_->dns_server(xs_idx), "");
     830           0 :                         continue;
     831             :                     }
     832             : 
     833           0 :                     CONTROLLER_CONNECTIONS_TRACE(DiscoveryConnection,
     834             :                         "ReSet Dns ReConfigChannel ", xs_idx,
     835             :                         agent_->dns_server(xs_idx), server_ip);
     836             : 
     837           0 :                     DisConnectDnsServer(xs_idx);
     838           0 :                     agent_->set_dns_server(server_ip, xs_idx);
     839           0 :                     agent_->set_dns_server_port(server_port, xs_idx);
     840           0 :                     break;
     841             :                }
     842             :            }
     843             :         }
     844           0 :     }
     845             : 
     846           0 :     DnsXmppServerConnect();
     847           0 :     return true;
     848             : }
     849             : 
     850             : /*
     851             :  * Returns the number of active agentxmppchannel.
     852             :  * AgentXmppChannel is identified as active if it has a BGP peer
     853             :  * attached to it.
     854             :  */
     855           0 : uint8_t VNController::ActiveXmppConnectionCount() {
     856           0 :     uint8_t active_xmpps = 0;
     857           0 :     for (uint8_t count = 0; count < MAX_XMPP_SERVERS; count++) {
     858           0 :         AgentXmppChannel *xc = agent_->controller_xmpp_channel(count);
     859           0 :        if (xc) {
     860             :            // Check if AgentXmppChannel has BGP peer
     861           0 :            if (xc->bgp_peer_id() != NULL)
     862           0 :                active_xmpps++;
     863             :        }
     864             :     }
     865             : 
     866           0 :     return active_xmpps;
     867             : }
     868             : 
     869           0 : AgentXmppChannel *VNController::GetActiveXmppChannel() {
     870           0 :     for (uint8_t count = 0; count < MAX_XMPP_SERVERS; count++) {
     871           0 :         AgentXmppChannel *xc = agent_->controller_xmpp_channel(count);
     872           0 :        if (xc) {
     873             :            // Check if AgentXmppChannel has BGP peer
     874           0 :            if (xc->bgp_peer_id() != NULL)
     875           0 :                return xc;
     876             :        }
     877             :     }
     878             : 
     879           0 :     return NULL;
     880             : }
     881             : 
     882           0 : void VNController::StartEndOfRibTxTimer() {
     883           0 :     uint8_t count = 0;
     884           0 :     while (count < MAX_XMPP_SERVERS) {
     885           0 :         if (agent_->controller_xmpp_channel(count))
     886           0 :             agent_->controller_xmpp_channel(count)->end_of_rib_tx_timer()->
     887           0 :                 Start(agent_->controller_xmpp_channel(count));
     888           0 :         count++;
     889             :     }
     890           0 : }
     891             : 
     892           0 : void VNController::StopEndOfRibTx() {
     893           0 :     uint8_t count = 0;
     894           0 :     while (count < MAX_XMPP_SERVERS) {
     895           0 :         if (agent_->controller_xmpp_channel(count)) {
     896           0 :             agent_->controller_xmpp_channel(count)->
     897           0 :                 end_of_rib_tx_timer()->Cancel();
     898           0 :             agent_->controller_xmpp_channel(count)->StopEndOfRibTxWalker();
     899             :         }
     900           0 :         count++;
     901             :     }
     902           0 : }
     903             : 
     904         370 : bool VNController::ControllerWorkQueueProcess(ControllerWorkQueueDataType data) {
     905             : 
     906             :     //DOM processing
     907             :     ControllerXmppDataType derived_xmpp_data =
     908         370 :         boost::dynamic_pointer_cast<ControllerXmppData>(data);
     909         370 :     if (derived_xmpp_data) {
     910           0 :         return XmppMessageProcess(derived_xmpp_data);
     911             :     }
     912             : 
     913             :     // VM Subscription message
     914             :     ControllerVmiSubscribeData *subscribe_data =
     915         370 :         boost::dynamic_pointer_cast<ControllerVmiSubscribeData>(data.get());
     916         370 :     if (subscribe_data && agent_ifmap_vm_export_.get()) {
     917         330 :         agent_ifmap_vm_export_->VmiEvent(subscribe_data);
     918         330 :         return true;
     919             :     }
     920             : 
     921             :     //ReConfig
     922             :     ControllerReConfigDataType reconfig_data =
     923          40 :         boost::dynamic_pointer_cast<ControllerReConfigData>(data);
     924          40 :     if (reconfig_data) {
     925           0 :         if (reconfig_data->service_name_.compare(
     926           0 :             g_vns_constants.XMPP_SERVER_DISCOVERY_SERVICE_NAME) == 0) {
     927           0 :             return ApplyControllerReConfigInternal(reconfig_data->server_list_);
     928           0 :         } else if (reconfig_data->service_name_.compare(
     929           0 :             g_vns_constants.DNS_SERVER_DISCOVERY_SERVICE_NAME) == 0) {
     930           0 :             return ApplyDnsReConfigInternal(reconfig_data->server_list_);
     931             :         } else {
     932           0 :             LOG(ERROR, "Unknown Service Name %s" << reconfig_data->service_name_);
     933           0 :             return true;
     934             :         }
     935             :     }
     936             : 
     937             :     ControllerDelPeerDataType del_peer_data =
     938          40 :         boost::dynamic_pointer_cast<ControllerDelPeerData>(data);
     939          40 :     if (del_peer_data) {
     940          21 :         DelPeerWalkDoneProcess(del_peer_data.get()->channel());
     941          21 :         return true;
     942             :     }
     943             : 
     944             :     AgentIfMapXmppChannel::EndOfConfigDataPtr end_of_config_data =
     945          19 :         boost::dynamic_pointer_cast<EndOfConfigData>(data);
     946          38 :     if (end_of_config_data &&
     947          19 :         (agent_->ifmap_xmpp_channel(agent_->ifmap_active_xmpp_server_index()) ==
     948          38 :          end_of_config_data->channel())) {
     949           0 :         end_of_config_data->channel()->ProcessEndOfConfig();
     950           0 :         return true;
     951             :     }
     952             : 
     953             :     //Connection retry for servers
     954             :     ControllerConnectRetryDataType connect_retry_data =
     955          19 :         boost::dynamic_pointer_cast<ControllerConnectRetryData>(data);
     956          19 :     if (connect_retry_data) {
     957           0 :         if (connect_retry_data->connect_xmpp_server()) {
     958           0 :             XmppServerConnect();
     959           0 :             return true;
     960             :         }
     961           0 :         if (connect_retry_data->connect_dns_xmpp_server()) {
     962           0 :             DnsXmppServerConnect();
     963           0 :             return true;
     964             :         }
     965             :     }
     966             : 
     967          19 :     return true;
     968         370 : }
     969             : 
     970           0 : bool VNController::XmppMessageProcess(ControllerXmppDataType data) {
     971           0 :     if (data->peer_id() == xmps::BGP) {
     972           0 :         if (data->config()) {
     973             :             AgentXmppChannel *peer =
     974           0 :                 agent_->controller_xmpp_channel(data->channel_id());
     975           0 :             if (peer) {
     976           0 :                 peer->ReceiveBgpMessage(data->dom());
     977             :             }
     978             :         } else {
     979             :             AgentXmppChannel *peer =
     980           0 :                 agent_->controller_xmpp_channel(data->channel_id());
     981           0 :             if (peer) {
     982           0 :                 AgentXmppChannel::HandleAgentXmppClientChannelEvent(peer,
     983             :                                                                     data->peer_state());
     984             :             }
     985             :         }
     986           0 :     } else if (data->peer_id() == xmps::CONFIG) {
     987             :         AgentIfMapXmppChannel *peer =
     988           0 :             agent_->ifmap_xmpp_channel(data->channel_id());
     989           0 :         if (peer) {
     990           0 :             peer->ReceiveConfigMessage(data->dom());
     991             :         }
     992           0 :     } else if (data->peer_id() == xmps::DNS) {
     993             :         AgentDnsXmppChannel *peer =
     994           0 :             agent_->dns_xmpp_channel(data->channel_id());
     995           0 :         if (data->config()) {
     996           0 :             if (peer) {
     997           0 :                 peer->ReceiveDnsMessage(data->dom());
     998             :             }
     999             :         } else {
    1000           0 :             if (peer) {
    1001           0 :                 AgentDnsXmppChannel::HandleXmppClientChannelEvent(peer,
    1002             :                                                                   data->peer_state());
    1003             :             }
    1004             :         }
    1005             :     }
    1006             : 
    1007           0 :     return true;
    1008             : }
    1009             : 
    1010         349 : void VNController::Enqueue(ControllerWorkQueueDataType data) {
    1011         349 :     work_queue_.Enqueue(data);
    1012         349 : }
    1013             : 
    1014           0 : bool VNController::RxXmppMessageTrace(uint8_t peer_index,
    1015             :                                       const std::string &to_address,
    1016             :                                       int port, int size,
    1017             :                                       const std::string &msg,
    1018             :                                       const XmppStanza::XmppMessage *xmppmsg) {
    1019           0 :     const std::string &to = xmppmsg->to;
    1020           0 :     if (to.find(XmppInit::kBgpPeer) != string::npos) {
    1021           0 :         CONTROLLER_RX_ROUTE_MESSAGE_TRACE(Message, peer_index, to_address,
    1022             :                                            port, size, msg);
    1023           0 :         return true;
    1024           0 :     } else if (to.find(XmppInit::kConfigPeer) != string::npos) {
    1025           0 :         CONTROLLER_RX_CONFIG_MESSAGE_TRACE(Message, peer_index, to_address,
    1026             :                                            port, size, msg);
    1027           0 :         return true;
    1028             :     }
    1029           0 :     return false;
    1030             : }
    1031             : 
    1032           0 : bool VNController::TxXmppMessageTrace(uint8_t peer_index,
    1033             :                                       const std::string &to_address,
    1034             :                                       int port, int size,
    1035             :                                       const std::string &msg,
    1036             :                                       const XmppStanza::XmppMessage *xmppmsg) {
    1037           0 :     CONTROLLER_TX_MESSAGE_TRACE(Message, peer_index, to_address,
    1038             :                                 port, size, msg);
    1039           0 :     return true;
    1040             : }
    1041             : 
    1042           0 : bool VNController::IsWorkQueueEmpty() const {
    1043           0 :     return (work_queue_.IsQueueEmpty() == 0);
    1044             : }
    1045             : 
    1046           0 : MacAddress VNController::GetTunnelMac(const autogen::EnetNextHopType &nh) {
    1047           0 :     return MacAddress(nh.mac);
    1048             : }
    1049             : 
    1050           0 : MacAddress VNController::GetTunnelMac(const autogen::NextHopType &nh) {
    1051           0 :     return MacAddress();
    1052             : }
    1053             : 
    1054           0 : TunnelType::TypeBmap VNController::GetTypeBitmap
    1055             : (const autogen::EnetTunnelEncapsulationListType &encap) {
    1056           0 :     TunnelType::TypeBmap bmap = 0;
    1057           0 :     for (autogen::EnetTunnelEncapsulationListType::const_iterator iter =
    1058           0 :          encap.begin(); iter != encap.end(); iter++) {
    1059             :         TunnelEncapType::Encap encap =
    1060           0 :             TunnelEncapType::TunnelEncapFromString(*iter);
    1061           0 :         if ((encap == TunnelEncapType::GRE) ||
    1062             :             (encap == TunnelEncapType::MPLS_O_GRE))
    1063           0 :             bmap |= (1 << TunnelType::MPLS_GRE);
    1064           0 :         if (encap == TunnelEncapType::MPLS_O_UDP)
    1065           0 :             bmap |= (1 << TunnelType::MPLS_UDP);
    1066           0 :         if (encap == TunnelEncapType::VXLAN)
    1067           0 :             bmap |= (1 << TunnelType::VXLAN);
    1068           0 :         if (encap == TunnelEncapType::NATIVE)
    1069           0 :             bmap |= (1 << TunnelType::NATIVE);
    1070           0 :         if (encap == TunnelEncapType::MPLS)
    1071           0 :             bmap |= (1 << TunnelType::MPLS_OVER_MPLS);
    1072             :     }
    1073           0 :     return bmap;
    1074             : }
    1075             : 
    1076           0 : TunnelType::TypeBmap VNController::GetTypeBitmap
    1077             : (const autogen::TunnelEncapsulationListType &encap) {
    1078           0 :     TunnelType::TypeBmap bmap = 0;
    1079           0 :     for (autogen::TunnelEncapsulationListType::const_iterator iter =
    1080           0 :          encap.begin(); iter != encap.end(); iter++) {
    1081             :         TunnelEncapType::Encap encap =
    1082           0 :             TunnelEncapType::TunnelEncapFromString(*iter);
    1083           0 :         if ((encap == TunnelEncapType::GRE) ||
    1084             :             (encap == TunnelEncapType::MPLS_O_GRE))
    1085           0 :             bmap |= (1 << TunnelType::MPLS_GRE);
    1086           0 :         if (encap == TunnelEncapType::MPLS_O_UDP)
    1087           0 :             bmap |= (1 << TunnelType::MPLS_UDP);
    1088           0 :         if (encap == TunnelEncapType::NATIVE)
    1089           0 :             bmap |= (1 << TunnelType::NATIVE);
    1090           0 :         if (encap == TunnelEncapType::MPLS)
    1091           0 :             bmap |= (1 << TunnelType::MPLS_OVER_MPLS);
    1092             :     }
    1093           0 :     return bmap;
    1094             : }
    1095             : 
    1096           0 : TunnelType::TypeBmap VNController::GetTypeBitmap
    1097             : (const autogen::McastTunnelEncapsulationListType &encap) {
    1098           0 :     TunnelType::TypeBmap bmap = 0;
    1099           0 :     for (autogen::McastTunnelEncapsulationListType::const_iterator iter =
    1100           0 :          encap.begin(); iter != encap.end(); iter++) {
    1101             :         TunnelEncapType::Encap encap =
    1102           0 :             TunnelEncapType::TunnelEncapFromString(*iter);
    1103           0 :         if ((encap == TunnelEncapType::GRE) ||
    1104             :             (encap == TunnelEncapType::MPLS_O_GRE))
    1105           0 :             bmap |= (1 << TunnelType::MPLS_GRE);
    1106           0 :         if (encap == TunnelEncapType::MPLS_O_UDP)
    1107           0 :             bmap |= (1 << TunnelType::MPLS_UDP);
    1108             :     }
    1109           0 :     return bmap;
    1110             : }
    1111             : 
    1112           0 : void VNController::GracefulRestartConfigListener() {
    1113           0 :     bool enable = agent_->oper_db()->global_system_config()->
    1114           0 :         gres_parameters().IsEnabled();
    1115             : 
    1116           0 :     for (uint8_t i = 0; i < MAX_XMPP_SERVERS; i++) {
    1117           0 :         if (agent_->ifmap_xmpp_channel(i)) {
    1118           0 :             agent_->ifmap_xmpp_channel(i)->end_of_config_timer()->
    1119           0 :                 GresEnabled(enable);
    1120             :         }
    1121           0 :         if (agent_->controller_xmpp_channel(i)) {
    1122           0 :             agent_->controller_xmpp_channel(i)->end_of_rib_tx_timer()->
    1123           0 :                 GresEnabled(enable);
    1124           0 :             agent_->controller_xmpp_channel(i)->end_of_rib_rx_timer()->
    1125           0 :                 GresEnabled(enable);
    1126             :         }
    1127             :     }
    1128           0 : }

Generated by: LCOV version 1.14