3 #include <boost/lexical_cast.hpp>
5 #include "snmp_pp/snmp_pp.h"
9 #include "subscriber.h"
12 #include "snmptable.h"
16 using SSMD::SNMPTable;
18 Switch::Switch(const Settings & settings,
20 const std::string & ip,
21 const std::string & readCommunity,
22 const std::string & writeCommunity,
24 : _settings(settings),
27 _readCommunity(readCommunity),
28 _writeCommunity(writeCommunity),
29 _uplinkPort(uplinkPort),
36 Switch::Switch(const Switch & rvalue)
37 : _settings(rvalue._settings),
40 _readCommunity(rvalue._readCommunity),
41 _writeCommunity(rvalue._writeCommunity),
42 _uplinkPort(rvalue._uplinkPort),
43 _nextUpACL(rvalue._nextUpACL),
44 _nextDownACL(rvalue._nextDownACL),
46 _aclsCreated(rvalue._aclsCreated)
53 IpAddress addr(_ip.c_str());
55 logger << "Switch::~Switch() - ivalid switch ip: '" << _ip << "'" << std::endl;
59 CTarget target(addr, _readCommunity.c_str(), _writeCommunity.c_str());
60 if (!target.valid()) {
61 logger << "Switch::~Switch() - failed to create target for the switch '" << _ip << "'" << std::endl;
65 target.set_version(version2c);
67 if (!checkProfiles(target)) {
68 logger << "Switch::~Switch() - no upload and download profiles defined for the switch '" << _ip << "'" << std::endl;
72 if (!dropACLs(target, std::cerr)) {
73 logger << "Switch::~Switch() - failed to drop ACLs for the switch '" << _ip << "'" << std::endl;
79 Switch & Switch::operator=(const Switch & rvalue)
82 _readCommunity = rvalue._readCommunity;
83 _writeCommunity = rvalue._writeCommunity;
84 _uplinkPort = rvalue._uplinkPort;
85 _nextUpACL = rvalue._nextUpACL;
86 _nextDownACL = rvalue._nextDownACL;
88 _aclsCreated = rvalue._aclsCreated;
93 void Switch::addSubscriber(const Subscriber & subscriber)
95 _acls.push_back(ACL(_nextUpACL++,
96 _settings.upProfileId(),
99 subscriber.getUpShape(),
100 subscriber.getUpBurst(),
102 _acls.push_back(ACL(_nextDownACL++,
103 _settings.downProfileId(),
106 subscriber.getDownShape(),
107 subscriber.getDownBurst(),
113 IpAddress addr(_ip.c_str());
115 logger << "Switch::sync() - ivalid switch ip: '" << _ip << "'" << std::endl;
119 CTarget target(addr, _readCommunity.c_str(), _writeCommunity.c_str());
120 if (!target.valid()) {
121 logger << "Switch::sync() - failed to create target for the switch '" << _ip << "'" << std::endl;
125 target.set_version(version2c);
127 if (!checkProfiles(target)) {
128 logger << "Switch::sync() - no upload and download profiles defined for the switch '" << _ip << "'" << std::endl;
133 std::string fileName(_ip + ".sh");
134 std::string newFileName(fileName + ".new");
135 std::ofstream script(newFileName.c_str());
136 script << "#!/bin/sh\n";
137 if (!dropACLs(target, script)) {
138 logger << "Switch::sync() - failed to drop ACLs for the switch '" << _ip << "'" << std::endl;
142 if (!createACLs(target, script)) {
143 logger << "Switch::sync() - failed to create ACLs for the switch '" << _ip << "'" << std::endl;
147 rename(newFileName.c_str(), fileName.c_str());
150 if (_settings.isDebug()) {
151 logger << "Switch::sync() - switch '" << _ip << "' synchronized successfully, ACLs: " << _acls.size() << std::endl;
155 bool Switch::checkProfiles(const CTarget & target)
157 SNMPTable table(_snmp, target, Oid(swACLEtherRuleProfileID));
158 if (!table.valid()) {
159 logger << "Switch::checkProfiles() - profiles SNMPTable is invalid for the switch '" << _ip << "'" << std::endl;
163 // Ok, just an empty table
166 if (table.valueExists(
167 static_cast<int>(_settings.upProfileId())
170 static_cast<int>(_settings.downProfileId())
177 bool Switch::dropACLs(const CTarget & target, std::ostream & stream)
179 std::string upOidValue(swACLEtherRuleAccessID);
181 upOidValue += boost::lexical_cast<std::string>(_settings.upProfileId());
182 std::string downOidValue(swACLEtherRuleAccessID);
184 downOidValue += boost::lexical_cast<std::string>(_settings.downProfileId());
185 SNMPTable aclsUpTable(_snmp, target, Oid(upOidValue.c_str()));
186 SNMPTable aclsDownTable(_snmp, target, Oid(downOidValue.c_str()));
187 if (!aclsUpTable.valid()) {
188 logger << "Switch::dropACLs() - upload profile acls SNMPTable is invalid for the switch '" << _ip << "'" << std::endl;
191 if (!aclsDownTable.valid()) {
192 logger << "Switch::dropACLs() - download profile acls SNMPTable is invalid for the switch '" << _ip << "'" << std::endl;
195 if (!aclsUpTable.empty()) {
196 if (!dropACLsByTable(target, _settings.upProfileId(), aclsUpTable, stream)) {
197 logger << "Switch::dropACLs() - failed to drop acls from upload table for the switch '" << _ip << "'" << std::endl;
201 if (!aclsDownTable.empty()) {
202 if (!dropACLsByTable(target, _settings.downProfileId(), aclsDownTable, stream)) {
203 logger << "Switch::dropACLs() - failed to drop acls from download table for the switch '" << _ip << "'" << std::endl;
210 bool Switch::dropACLsByTable(const CTarget & target, unsigned profileId, const SNMPTable & table, std::ostream & stream)
212 std::string dropACLOidPrefix(swACLEtherRuleRowStatus);
213 dropACLOidPrefix += ".";
214 dropACLOidPrefix += boost::lexical_cast<std::string>(profileId);
215 SNMPList aclsList(table.getList());
216 SNMPList::const_iterator it(aclsList.begin());
217 size_t chunks = aclsList.size() / _settings.maxACLPerPDU() + 1;
218 for (size_t i = 0; i < chunks && it != aclsList.end(); ++i) {
220 stream << "snmpset -v2c -cgts_community_w " << _ip;
221 for (size_t j = 0; j < _settings.maxACLPerPDU() && it != aclsList.end(); ++j, ++it) {
223 if (int c = it->get_value(id) != SNMP_CLASS_SUCCESS) {
224 logger << "Switch::dropACLsByTable() - failed to get ACL id for the switch '" << _ip << "'. Error message: '" << Snmp::error_msg(c) << "'" << std::endl;
227 std::string dropACLOid(dropACLOidPrefix);
229 dropACLOid += boost::lexical_cast<std::string>(id);
230 Vb vb(Oid(dropACLOid.c_str()));
231 vb.set_value(int(6));
233 stream << " " << dropACLOid << " i 6";
236 if (int c = _snmp.set(pdu, target) != SNMP_CLASS_SUCCESS) {
237 if (c != SNMP_ERROR_TOO_BIG) {
238 logger << "Switch::dropACLsByTable() - failed to invoke Snmp::set for the switch '" << _ip << "'. Error message: '" << Snmp::error_msg(c) << "'" << std::endl;
246 bool Switch::createACLs(const CTarget & target, std::ostream & stream)
248 std::vector<ACL>::const_iterator it;
250 for (it = _acls.begin(); it != _acls.end(); ++it) {
253 if (int c = _snmp.set(pdu, target) != SNMP_CLASS_SUCCESS) {
254 if (c != SNMP_ERROR_TOO_BIG) {
255 logger << "Switch::createACLs() - failed to invoke Snmp::set for the switch '" << _ip << "'. Error message: '" << Snmp::error_msg(c) << "'. Error occured at creation of " << (pos + 1) << " from " << _acls.size() << " ACL's" << std::endl;
256 logger << "Switch::createACLs() - ACL dump: " << *it << std::endl;
263 stream << "snmpset -v2c -cgts_community_w " << _ip << " " << *it << "\n";