1 #include <boost/lexical_cast.hpp>
3 #include "snmp_pp/snmp_pp.h"
7 #include "subscriber.h"
10 #include "snmptable.h"
16 Switch::Switch(const Settings & settings,
18 const std::string & ip,
19 const std::string & readCommunity,
20 const std::string & writeCommunity,
22 : _settings(settings),
25 _readCommunity(readCommunity),
26 _writeCommunity(writeCommunity),
27 _uplinkPort(uplinkPort),
33 Switch::Switch(const Switch & rvalue)
34 : _settings(rvalue._settings),
37 _readCommunity(rvalue._readCommunity),
38 _writeCommunity(rvalue._writeCommunity),
39 _uplinkPort(rvalue._uplinkPort),
40 _nextUpACL(rvalue._nextUpACL),
41 _nextDownACL(rvalue._nextDownACL),
50 Switch & Switch::operator=(const Switch & rvalue)
53 _readCommunity = rvalue._readCommunity;
54 _writeCommunity = rvalue._writeCommunity;
55 _uplinkPort = rvalue._uplinkPort;
56 _nextUpACL = rvalue._nextUpACL;
57 _nextDownACL = rvalue._nextDownACL;
63 void Switch::addSubscriber(const Subscriber & subscriber)
65 _acls.push_back(ACL(_nextUpACL++,
66 _settings.upProfileId(),
69 subscriber.getUpShape(),
70 subscriber.getUpBurst(),
72 _acls.push_back(ACL(_nextDownACL++,
73 _settings.downProfileId(),
76 subscriber.getDownShape(),
77 subscriber.getDownBurst(),
83 IpAddress addr(_ip.c_str());
85 logger << "Switch::sync() - ivalid switch ip: '" << _ip << "'" << std::endl;
89 CTarget target(addr, _readCommunity.c_str(), _writeCommunity.c_str());
90 if (!target.valid()) {
91 logger << "Switch::sync() - failed to create target for the switch '" << _ip << "'" << std::endl;
95 target.set_version(version2c);
97 if (!checkProfiles(target)) {
98 logger << "Switch::sync() - no upload and download profiles defined for the switch '" << _ip << "'" << std::endl;
102 if (!dropACLs(target)) {
103 logger << "Switch::sync() - failed to drop ACLs for the switch '" << _ip << "'" << std::endl;
107 if (!createACLs(target)) {
108 logger << "Switch::sync() - failed to create ACLs for the switch '" << _ip << "'" << std::endl;
111 if (_settings.isDebug()) {
112 logger << "Switch::sync() - switch '" << _ip << "' synchronized successfully, ACLs: " << _acls.size() << std::endl;
116 bool Switch::checkProfiles(const CTarget & target)
118 SNMPTable table(_snmp, target, Oid(swACLEtherRuleProfileID));
119 if (!table.valid()) {
120 logger << "Switch::checkProfiles() - profiles SNMPTable is invalid for the switch '" << _ip << "'" << std::endl;
124 // Ok, just an empty table
127 if (table.valueExists(
128 static_cast<int>(_settings.upProfileId())
131 static_cast<int>(_settings.downProfileId())
138 bool Switch::dropACLs(const CTarget & target)
140 std::string upOidValue(swACLEtherRuleAccessID);
142 upOidValue += boost::lexical_cast<std::string>(_settings.upProfileId());
143 std::string downOidValue(swACLEtherRuleAccessID);
145 downOidValue += boost::lexical_cast<std::string>(_settings.downProfileId());
146 SNMPTable aclsUpTable(_snmp, target, Oid(upOidValue.c_str()));
147 SNMPTable aclsDownTable(_snmp, target, Oid(downOidValue.c_str()));
148 if (!aclsUpTable.valid()) {
149 logger << "Switch::dropACLs() - upload profile acls SNMPTable is invalid for the switch '" << _ip << "'" << std::endl;
152 if (!aclsDownTable.valid()) {
153 logger << "Switch::dropACLs() - download profile acls SNMPTable is invalid for the switch '" << _ip << "'" << std::endl;
156 if (!aclsUpTable.empty()) {
157 if (!dropACLsByTable(target, _settings.upProfileId(), aclsUpTable)) {
158 logger << "Switch::dropACLs() - failed to drop acls from upload table for the switch '" << _ip << "'" << std::endl;
162 if (!aclsDownTable.empty()) {
163 if (!dropACLsByTable(target, _settings.downProfileId(), aclsDownTable)) {
164 logger << "Switch::dropACLs() - failed to drop acls from download table for the switch '" << _ip << "'" << std::endl;
171 bool Switch::dropACLsByTable(const CTarget & target, unsigned profileId, const SNMPTable & table)
173 std::string dropACLOidPrefix(swACLEtherRuleRowStatus);
174 dropACLOidPrefix += ".";
175 dropACLOidPrefix += boost::lexical_cast<std::string>(profileId);
176 SNMPList aclsList(table.getList());
178 SNMPList::const_iterator it;
179 for (it = aclsList.begin(); it != aclsList.end(); ++it) {
181 if (int c = it->get_value(id) != SNMP_CLASS_SUCCESS) {
182 logger << "Switch::dropACLsByTable() - failed to get ACL id for the switch '" << _ip << "'. Error message: '" << Snmp::error_msg(c) << "'" << std::endl;
185 std::string dropACLOid(dropACLOidPrefix);
187 dropACLOid += boost::lexical_cast<std::string>(id);
188 Vb vb(Oid(dropACLOid.c_str()));
189 vb.set_value(int(6));
192 if (int c = _snmp.set(pdu, target) != SNMP_CLASS_SUCCESS) {
193 logger << "Switch::dropACLsByTable() - failed to invoke Snmp::set for the switch '" << _ip << "'. Error message: '" << Snmp::error_msg(c) << "'" << std::endl;
199 bool Switch::createACLs(const CTarget & target)
201 std::vector<ACL>::const_iterator it;
202 for (it = _acls.begin(); it != _acls.end(); ++it) {
205 if (int c = _snmp.set(pdu, target) != SNMP_CLASS_SUCCESS) {
206 logger << "Switch::createACLs() - failed to invoke Snmp::set for the switch '" << _ip << "'. Error message: '" << Snmp::error_msg(c) << "'" << std::endl;