1 package FS::part_export::trango;
3 =head1 FS::part_export::trango
5 This export sends SNMP SETs to a router using the Net::SNMP package. It requires the following custom fields to be defined on a router. If any of the required custom fields are not present, then the export will exit quietly.
7 =head1 Required custom fields
11 =item trango_address - IP address (or hostname) of the Trango AP.
13 =item trango_comm - R/W SNMP community of the Trango AP.
15 =item trango_ap_type - Trango AP Model. Currently 'access5830' is the only supported option.
19 =head1 Optional custom fields
23 =item trango_baseid - Base ID of the Trango AP. See L</"Generating SU IDs">.
25 =item trango_apid - AP ID of the Trango AP. See L</"Generating SU IDs">.
29 =head1 Generating SU IDs
31 This export will/must generate a unique SU ID for each service exported to a Trango AP. It can be done such that SU IDs are globally unique, unique per Base ID, or unique per Base ID/AP ID pair. This is accomplished by setting neither trango_baseid and trango_apid, only trango_baseid, or both trango_baseid and trango_apid, respectively. An SU ID will be generated if the FS::svc_broadband virtual field specified by suid_field export option is unset, otherwise the existing value will be used.
35 This export has been tested with the Trango Access5830 AP.
42 use vars qw(@ISA %info $me $DEBUG $trango_mib $counter_dir);
44 use FS::UID qw(dbh datasrc);
45 use FS::Record qw(qsearch qsearchs);
46 use FS::part_export::snmp;
49 use File::CounterFile;
50 use Data::Dumper qw(Dumper);
52 @ISA = qw(FS::part_export::snmp);
54 tie my %options, 'Tie::IxHash', (
56 'label' => 'Trango SU ID field',
57 'default' => 'trango_suid',
58 'notes' => 'Name of the FS::svc_broadband virtual field that will contain the SU ID.',
61 'label' => 'Trango MAC address field',
63 'notes' => 'Name of the FS::svc_broadband virtual field that will contain the SU\'s MAC address.',
68 'svc' => 'svc_broadband',
69 'desc' => 'Sends SNMP SETs to a Trango AP.',
70 'options' => \%options,
71 'notes' => 'Requires Net::SNMP. See the documentation for FS::part_export::trango for required virtual fields and usage information.',
74 $me= '[' . __PACKAGE__ . ']';
79 'snmpversion' => 'snmpv1',
83 'oid' => '1.3.6.1.4.1.5454.1.20.3.5.1',
85 'value' => \&_trango_access5830_sudbDeleteOrAddId,
88 'oid' => '1.3.6.1.4.1.5454.1.20.3.5.2',
89 'type' => 'HEX_STRING',
90 'value' => \&_trango_access5830_sudbAddMac,
93 'oid' => '1.3.6.1.4.1.5454.1.20.3.5.7',
100 'oid' => '1.3.6.1.4.1.5454.1.20.3.5.1',
102 'value' => \&_trango_access5830_sudbDeleteOrAddId,
105 'oid' => '1.3.6.1.4.1.5454.1.20.3.5.8',
111 { # sudbDeleteOrAddID
112 'oid' => '1.3.6.1.4.1.5454.1.20.3.5.1',
114 'value' => \&_trango_access5830_sudbDeleteOrAddId,
117 'oid' => '1.3.6.1.4.1.5454.1.20.3.5.8',
121 { # sudbDeleteOrAddID
122 'oid' => '1.3.6.1.4.1.5454.1.20.3.5.1',
124 'value' => \&_trango_access5830_sudbDeleteOrAddId,
127 'oid' => '1.3.6.1.4.1.5454.1.20.3.5.2',
128 'type' => 'HEX_STRING',
129 'value' => \&_trango_access5830_sudbAddMac,
132 'oid' => '1.3.6.1.4.1.5454.1.20.3.5.7',
138 { # sudbDeleteOrAddID
139 'oid' => '1.3.6.1.4.1.5454.1.20.3.5.1',
141 'value' => \&_trango_access5830_sudbDeleteOrAddId,
144 'oid' => '1.3.6.1.4.1.5454.1.20.3.5.8',
150 { # sudbDeleteOrAddID
151 'oid' => '1.3.6.1.4.1.5454.1.20.3.5.1',
153 'value' => \&_trango_access5830_sudbDeleteOrAddId,
156 'oid' => '1.3.6.1.4.1.5454.1.20.3.5.2',
157 'type' => 'HEX_STRING',
158 'value' => \&_trango_access5830_sudbAddMac,
161 'oid' => '1.3.6.1.4.1.5454.1.20.3.5.7',
171 sub _field_prefix { 'trango'; }
173 sub _req_router_fields {
175 $_[0]->_field_prefix . '_' . $_
176 } (qw(address comm ap_type suid_field));
181 return('FS::part_export::snmp::snmp_cmd');
187 my ($self, $action, $router) = (shift, shift, shift);
188 my ($svc_broadband) = shift;
189 my $old = shift if $action eq 'replace';
190 my $field_prefix = $self->_field_prefix;
193 my $ap_type = $router->getfield($field_prefix . '_ap_type');
195 unless (exists $trango_mib->{$ap_type}) {
196 return "Unsupported Trango AP type '$ap_type'";
199 $error = $self->_check_suid(
200 $action, $router, $svc_broadband, ($old) ? $old : ()
202 return $error if $error;
204 $error = $self->_check_mac(
205 $action, $router, $svc_broadband, ($old) ? $old : ()
207 return $error if $error;
209 my $ap_mib = $trango_mib->{$ap_type};
212 '-hostname' => $router->getfield($field_prefix.'_address'),
213 '-version' => $ap_mib->{'snmpversion'},
214 '-community' => $router->getfield($field_prefix.'_comm'),
217 my @varbindlist = ();
219 foreach my $oid (@{$ap_mib->{'varbinds'}->{$action}}) {
220 warn "[debug]$me Processing OID '" . $oid->{'oid'} . "'" if $DEBUG;
222 if (ref($oid->{'value'}) eq 'CODE') {
224 $value = &{$oid->{'value'}}(
225 $self, $action, $router, $svc_broadband,
226 (($old) ? $old : ()),
229 return "While processing OID '" . $oid->{'oid'} . "':" . $@
232 $value = $oid->{'value'};
235 warn "[debug]$me Value for OID '" . $oid->{'oid'} . "': " if $DEBUG;
237 if (defined $value) { # Skip OIDs with undefined values.
238 push @varbindlist, ($oid->{'oid'}, $oid->{'type'}, $value);
243 push @$args, ('-varbindlist', @varbindlist);
251 my ($self, $action, $router, $svc_broadband) = (shift, shift, shift, shift);
252 my $old = shift if $action eq 'replace';
255 my $suid_field = $self->option('suid_field');
256 unless (grep {$_ eq $suid_field} $svc_broadband->fields) {
257 return "Missing Trango SU ID field. "
258 . "See the trango export options for more info.";
261 my $suid = $svc_broadband->getfield($suid_field);
262 if ($action eq 'replace') {
263 my $old_suid = $old->getfield($suid_field);
265 if ($old_suid ne '' and $old_suid ne $suid) {
266 return 'Cannot change Trango SU ID';
270 if (not $suid =~ /^\d+$/ and $action ne 'delete') {
271 my $new_suid = eval { $self->_get_next_suid($router); };
272 return "Error while getting next Trango SU ID: $@" if ($@);
274 warn "[debug]$me Got new SU ID: $new_suid" if $DEBUG;
275 $svc_broadband->set($suid_field, $new_suid);
277 #FIXME: Probably a bad hack.
278 # We need to update the SU ID field in the database.
280 my $oldAutoCommit = $FS::UID::AutoCommit;
281 local $FS::svc_Common::noexport_hack = 1;
282 local $FS::UID::AutoCommit = 0;
285 my $svcnum = $svc_broadband->svcnum;
287 my $old_svc = qsearchs('svc_broadband', { svcnum => $svcnum });
289 return "Unable to retrieve svc_broadband with svcnum '$svcnum";
292 my $svcpart = $svc_broadband->svcpart
293 ? $svc_broadband->svcpart
294 : $svc_broadband->cust_svc->svcpart;
296 my $new_svc = new FS::svc_broadband {
298 $suid_field => $new_suid,
302 $error = $new_svc->check;
304 $dbh->rollback if $oldAutoCommit;
305 return "Error while updating the Trango SU ID: $error" if $error;
308 warn "[debug]$me Updating svc_broadband with SU ID '$new_suid'...\n" .
309 &Dumper($new_svc) if $DEBUG;
311 $error = eval { $new_svc->replace($old_svc); };
315 $dbh->rollback if $oldAutoCommit;
316 return "Error while updating the Trango SU ID: $error" if $error;
319 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
329 my ($self, $action, $router, $svc_broadband) = (shift, shift, shift, shift);
330 my $old = shift if $action eq 'replace';
332 my $mac_field = $self->option('mac_field');
333 unless (grep {$_ eq $mac_field} $svc_broadband->fields) {
334 return "Missing Trango MAC address field. "
335 . "See the trango export options for more info.";
338 my $mac_addr = $svc_broadband->getfield($mac_field);
339 unless (length(join('', $mac_addr =~ /[0-9a-fA-F]/g)) == 12) {
340 return "Invalid Trango MAC address: $mac_addr";
349 my ($self, $router) = (shift, shift);
351 my $counter_dir = '/usr/local/etc/freeside/export.'. datasrc . '/trango';
352 my $baseid = $router->getfield('trango_baseid');
353 my $apid = $router->getfield('trango_apid');
355 my $counter_file_suffix = '';
357 $counter_file_suffix .= "_B$baseid";
359 $counter_file_suffix .= "_A$apid";
363 my $counter_file = $counter_dir . '/SUID' . $counter_file_suffix;
365 warn "[debug]$me Using SUID counter file '$counter_file'";
368 mkdir $counter_dir, 0700 unless -d $counter_dir;
370 my $cf = new File::CounterFile($counter_file, 0);
374 die "Error generating next Trango SU ID: $@" if (not $suid or $@);
382 # Trango-specific subroutines for generating varbind values.
384 # All subs should die on error, and return undef to decline. OIDs that
385 # decline will not be added to varbinds.
387 sub _trango_access5830_sudbDeleteOrAddId {
389 my ($self, $action, $router) = (shift, shift, shift);
390 my ($svc_broadband) = shift;
391 my $old = shift if $action eq 'replace';
393 my $suid = $svc_broadband->getfield($self->option('suid_field'));
396 unless ($suid =~ /^\d+$/) {
397 if ($action eq 'delete') {
398 # Silently ignore. If we don't have a valid SU ID now, we probably
402 die "Invalid Trango SU ID '$suid'";
410 sub _trango_access5830_sudbAddMac {
412 my ($self, $action, $router) = (shift, shift, shift);
413 my ($svc_broadband) = shift;
414 my $old = shift if $action eq 'replace';
416 my $mac_addr = $svc_broadband->getfield($self->option('mac_field'));
417 $mac_addr = join('', $mac_addr =~ /[0-9a-fA-F]/g);
420 die "Invalid Trango MAC address '$mac_addr'" unless (length($mac_addr)==12);