LCOV - code coverage report
Current view: top level - bgp/routing-policy - routing_policy_action.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 0 174 0.0 %
Date: 2026-11-16 02:06:09 Functions: 0 20 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include "bgp/routing-policy/routing_policy_action.h"
       7             : 
       8             : #include <boost/foreach.hpp>
       9             : 
      10             : #include <algorithm>
      11             : #include <sstream>
      12             : 
      13             : #include "bgp/bgp_attr.h"
      14             : #include "bgp/bgp_server.h"
      15             : #include "bgp/community.h"
      16             : 
      17             : using std::copy;
      18             : using std::ostringstream;
      19             : using std::string;
      20             : using std::vector;
      21             : 
      22           0 : UpdateAsPath::UpdateAsPath(const vector<uint32_t> &asn_list)
      23           0 :     : asn_list_(asn_list) {
      24           0 : }
      25             : 
      26           0 : void UpdateAsPath::operator()(BgpAttr *attr) const {
      27           0 :     if (!attr)
      28           0 :         return;
      29           0 :     BgpAttrDB *attr_db = attr->attr_db();
      30           0 :     BgpServer *server = attr_db->server();
      31           0 :     if (!server->enable_4byte_as()) {
      32           0 :         const AsPath *as_path = attr->as_path();
      33           0 :         if (as_path) {
      34           0 :             const AsPathSpec &as_path_spec = as_path->path();
      35           0 :             AsPathSpec *as_path_spec_ptr = as_path_spec.Add(asn_list_);
      36           0 :             attr->set_as_path(as_path_spec_ptr);
      37           0 :             delete as_path_spec_ptr;
      38             :         } else {
      39           0 :             AsPathSpec as_path_spec;
      40           0 :             AsPathSpec *as_path_spec_ptr = as_path_spec.Add(asn_list_);
      41           0 :             attr->set_as_path(as_path_spec_ptr);
      42           0 :             delete as_path_spec_ptr;
      43           0 :         }
      44             :     } else {
      45           0 :         const AsPath4Byte *as_path = attr->aspath_4byte();
      46           0 :         if (as_path) {
      47           0 :             const AsPath4ByteSpec &as_path_spec = as_path->path();
      48           0 :             AsPath4ByteSpec *as_path_spec_ptr = as_path_spec.Add(asn_list_);
      49           0 :             attr->set_aspath_4byte(as_path_spec_ptr);
      50           0 :             delete as_path_spec_ptr;
      51             :         } else {
      52           0 :             AsPath4ByteSpec as_path_spec;
      53           0 :             AsPath4ByteSpec *as_path_spec_ptr = as_path_spec.Add(asn_list_);
      54           0 :             attr->set_aspath_4byte(as_path_spec_ptr);
      55           0 :             delete as_path_spec_ptr;
      56           0 :         }
      57             :     }
      58             : }
      59             : 
      60           0 : string UpdateAsPath::ToString() const {
      61           0 :     ostringstream oss;
      62           0 :     oss << "as-path expand [ ";
      63           0 :     BOOST_FOREACH(uint32_t asn, asn_list_) {
      64           0 :         oss << asn << ",";
      65             :     }
      66           0 :     oss.seekp(-1, oss.cur);
      67           0 :     oss << " ]";
      68           0 :     return oss.str();
      69           0 : }
      70             : 
      71           0 : bool UpdateAsPath::IsEqual(const RoutingPolicyAction &as_path) const {
      72           0 :     const UpdateAsPath in_as_path = static_cast<const UpdateAsPath&>(as_path);
      73           0 :     return (asn_list_ == in_as_path.asn_list_);
      74           0 : }
      75             : 
      76           0 : UpdateCommunity::UpdateCommunity(const std::vector<string> communities,
      77           0 :                                  string op) {
      78           0 :     BOOST_FOREACH(const string &community, communities) {
      79           0 :         uint32_t value = CommunityType::CommunityFromString(community);
      80           0 :         if (value) communities_.push_back(value);
      81             :     }
      82           0 :     std::sort(communities_.begin(), communities_.end());
      83             :     std::vector<uint32_t>::iterator it =
      84           0 :         std::unique(communities_.begin(), communities_.end());
      85           0 :     communities_.erase(it, communities_.end());
      86             : 
      87           0 :     if (strcmp(op.c_str(), "add") == 0) {
      88           0 :         op_ = ADD;
      89           0 :     } else if (strcmp(op.c_str(), "remove") == 0) {
      90           0 :         op_ = REMOVE;
      91           0 :     } else if (strcmp(op.c_str(), "set") == 0) {
      92           0 :         op_ = SET;
      93             :     }
      94           0 : }
      95             : 
      96           0 : void UpdateCommunity::operator()(BgpAttr *attr) const {
      97           0 :     if (!attr) return;
      98           0 :     const Community *comm = attr->community();
      99           0 :     BgpAttrDB *attr_db = attr->attr_db();
     100           0 :     BgpServer *server = attr_db->server();
     101           0 :     CommunityDB *comm_db = server->comm_db();
     102           0 :     CommunityPtr new_community = NULL;
     103           0 :     if (op_ == SET) {
     104           0 :         new_community = comm_db->SetAndLocate(comm, communities_);
     105           0 :     } else if (op_ == ADD) {
     106           0 :         new_community = comm_db->AppendAndLocate(comm, communities_);
     107           0 :     } else if (op_ == REMOVE) {
     108           0 :         if (comm) new_community = comm_db->RemoveAndLocate(comm, communities_);
     109             :     }
     110           0 :     attr->set_community(new_community);
     111           0 : }
     112             : 
     113           0 : string UpdateCommunity::ToString() const {
     114           0 :     ostringstream oss;
     115           0 :     if (op_ == SET) oss << "community set [ ";
     116           0 :     else if  (op_ == ADD) oss << "community add [ ";
     117           0 :     else if (op_ == REMOVE) oss << "community remove [ ";
     118             : 
     119           0 :     BOOST_FOREACH(uint32_t community, communities()) {
     120           0 :         string name = CommunityType::CommunityToString(community);
     121           0 :         oss << name << ",";
     122           0 :     }
     123           0 :     oss.seekp(-1, oss.cur);
     124           0 :     oss << " ]";
     125           0 :     return oss.str();
     126           0 : }
     127             : 
     128           0 : bool UpdateCommunity::IsEqual(const RoutingPolicyAction &community) const {
     129             :     const UpdateCommunity in_comm =
     130           0 :         static_cast<const UpdateCommunity&>(community);
     131           0 :     if (op_ == in_comm.op_)
     132           0 :         return (communities() == in_comm.communities());
     133           0 :     return false;
     134           0 : }
     135             : 
     136           0 : UpdateExtCommunity::UpdateExtCommunity(const std::vector<string> &communities,
     137           0 :                                        string op) {
     138           0 :     BOOST_FOREACH(const string &community, communities) {
     139             :         const ExtCommunity::ExtCommunityList list =
     140           0 :             ExtCommunity::ExtCommunityFromString(community);
     141           0 :         if (list.size())
     142           0 :             communities_.push_back(list[0]);
     143           0 :     }
     144           0 :     std::sort(communities_.begin(), communities_.end());
     145             :     ExtCommunity::ExtCommunityList::iterator it =
     146           0 :         std::unique(communities_.begin(), communities_.end());
     147           0 :     communities_.erase(it, communities_.end());
     148             : 
     149           0 :     if (strcmp(op.c_str(), "add") == 0) {
     150           0 :         op_ = ADD;
     151           0 :     } else if (strcmp(op.c_str(), "remove") == 0) {
     152           0 :         op_ = REMOVE;
     153           0 :     } else if (strcmp(op.c_str(), "set") == 0) {
     154           0 :         op_ = SET;
     155             :     }
     156           0 : }
     157             : 
     158           0 : void UpdateExtCommunity::operator()(BgpAttr *attr) const {
     159           0 :     if (!attr) return;
     160           0 :     const ExtCommunity *comm = attr->ext_community();
     161           0 :     BgpAttrDB *attr_db = attr->attr_db();
     162           0 :     BgpServer *server = attr_db->server();
     163           0 :     ExtCommunityDB *comm_db = server->extcomm_db();
     164           0 :     ExtCommunityPtr new_community = NULL;
     165           0 :     if (op_ == SET) {
     166           0 :         new_community = comm_db->SetAndLocate(comm, communities_);
     167           0 :     } else if (op_ == ADD) {
     168           0 :         new_community = comm_db->AppendAndLocate(comm, communities_);
     169           0 :     } else if (op_ == REMOVE) {
     170           0 :         new_community = comm_db->RemoveAndLocate(comm, communities_);
     171             :     }
     172           0 :     attr->set_ext_community(new_community);
     173           0 : }
     174             : 
     175           0 : string UpdateExtCommunity::ToString() const {
     176           0 :     ostringstream oss;
     177           0 :     if (op_ == SET) oss << "Extcommunity set [ ";
     178           0 :     else if  (op_ == ADD) oss << "Extcommunity add [ ";
     179           0 :     else if (op_ == REMOVE) oss << "Extcommunity remove [ ";
     180             : 
     181           0 :     BOOST_FOREACH(ExtCommunity::ExtCommunityValue community, communities()) {
     182           0 :         string name = ExtCommunity::ToString(community);
     183           0 :         oss << name << ",";
     184           0 :     }
     185           0 :     oss.seekp(-1, oss.cur);
     186           0 :     oss << " ]";
     187           0 :     return oss.str();
     188           0 : }
     189             : 
     190           0 : bool UpdateExtCommunity::IsEqual(const RoutingPolicyAction &community) const {
     191             :     const UpdateExtCommunity in_comm =
     192           0 :         static_cast<const UpdateExtCommunity&>(community);
     193           0 :     if (op_ == in_comm.op_)
     194           0 :         return (communities() == in_comm.communities());
     195           0 :     return false;
     196           0 : }
     197             : 
     198           0 : UpdateLocalPref::UpdateLocalPref(uint32_t local_pref)
     199           0 :     : local_pref_(local_pref) {
     200           0 : }
     201             : 
     202           0 : void UpdateLocalPref::operator()(BgpAttr *attr) const {
     203           0 :     attr->set_local_pref(local_pref_);
     204           0 : }
     205             : 
     206           0 : string UpdateLocalPref::ToString() const {
     207           0 :     ostringstream oss;
     208           0 :     oss << "local-pref " << local_pref_;
     209           0 :     return oss.str();
     210           0 : }
     211             : 
     212           0 : bool UpdateLocalPref::IsEqual(const RoutingPolicyAction &local_pref) const {
     213             :     const UpdateLocalPref in_lp =
     214           0 :         static_cast<const UpdateLocalPref&>(local_pref);
     215           0 :     return (local_pref_ == in_lp.local_pref_);
     216           0 : }
     217             : 
     218           0 : UpdateMed::UpdateMed(uint32_t med)
     219           0 :     : med_(med) {
     220           0 : }
     221             : 
     222           0 : void UpdateMed::operator()(BgpAttr *attr) const {
     223           0 :     attr->set_med(med_);
     224           0 : }
     225             : 
     226           0 : string UpdateMed::ToString() const {
     227           0 :     ostringstream oss;
     228           0 :     oss << "med " << med_;
     229           0 :     return oss.str();
     230           0 : }
     231             : 
     232           0 : bool UpdateMed::IsEqual(const RoutingPolicyAction &med) const {
     233             :     const UpdateMed in_med =
     234           0 :         static_cast<const UpdateMed&>(med);
     235           0 :     return (med_ == in_med.med_);
     236           0 : }

Generated by: LCOV version 1.14