TT#38200 Add support for new NGCP::Template perl module

The perl Template::Toolkit is very rich, but its "function" support is a
bit poor. The ways to do it are either via MACRO directives, or by
simulating them with one function per file and then using PROCESS on
these. The problem is that this is very clunky, does not support
nesting, as we'd need different "argument" names for each "function",
and it's quite cumbersome to use, need to assign aguments passed
beforehand, and then assign back a designated return value from another
variable. This is also one of the reasons some of the functions are not
encapsulated, and have been inlined in various loops, because it was not
possible to cleanly PROCESS them from those call sites.

Instead we should use its native support for perl objects and perl
subroutines, which exposes these as proper methods of a designated
variable, and have none of the above mentioned problems. So we'll switch
from constructs such as:

  argv.arg-a = variable;
  argv.arg-b = 'value';
  PROCESS 'path-to-library-dir/function'
  result = out

into:

  result = ngcp.function(variable, 'value');

In addition this might actually be faster, as it does not require
processing additional files, and it's all just native perl code.

This will be exposed within the NGCP templates as the ngcp object, and
new member functions will start replacing our old and clunky native
Template PROCESS-style library.

Change-Id: Id2f0d181c695a9dd074646881b7d9de3478570af
changes/75/21875/4
Guillem Jover 8 years ago
parent acb6ea8682
commit 696ac4c247

@ -1,13 +1,16 @@
# for syntax checks
BASH_SCRIPTS = scripts/* functions/* etc/ngcp-config/ngcpcfg.cfg helper/build_config sbin/ngcpcfg helper/tt2-wrapper
PERL_SCRIPTS = helper/sort-yml \
helper/sync-db \
helper/tt2-daemon \
helper/validate-yml helper/fileformat_version \
sbin/ngcp-network \
sbin/ngcp-network-validator \
sbin/ngcp-sync-constants \
sbin/ngcp-sync-grants
PERL_SCRIPTS = \
lib/NGCP/Template.pm \
helper/sort-yml \
helper/sync-db \
helper/tt2-daemon \
helper/validate-yml helper/fileformat_version \
sbin/ngcp-network \
sbin/ngcp-network-validator \
sbin/ngcp-sync-constants \
sbin/ngcp-sync-grants \
# EOL
RESULTS ?= results
all: docs
@ -58,7 +61,7 @@ perlcheck:
@echo "Checking for perl syntax errors:"; \
for SCRIPT in $(PERL_SCRIPTS); do \
test -r $${SCRIPT} || continue ; \
perl -CSD -w -c $${SCRIPT} || exit ; \
perl -CSD -Ilib -w -c $${SCRIPT} || exit ; \
done; \
echo "-> perl check done."; \

@ -10,7 +10,9 @@ helper/tt2-daemon usr/share/ngcp-ngcpcfg/helper/
helper/tt2-wrapper usr/share/ngcp-ngcpcfg/helper/
helper/validate-yml usr/share/ngcp-ngcpcfg/helper/
hooks/ usr/share/ngcp-ngcpcfg/
lib/* usr/lib/ngcp-ngcpcfg/
lib/NGCP usr/share/perl5/
lib/get_* usr/lib/ngcp-ngcpcfg/
lib/has_* usr/lib/ngcp-ngcpcfg/
sbin/ngcp-network usr/sbin/
sbin/ngcp-network-validator usr/sbin/
sbin/ngcp-sync-constants usr/sbin/

@ -13,6 +13,8 @@ use YAML::XS qw(LoadFile);
use Clone 'clone';
use Getopt::Long;
use NGCP::Template;
my $quiet = 0;
GetOptions("q|quiet" => \$quiet);
@ -81,10 +83,12 @@ sub handle_connections {
RELATIVE => 1,
EVAL_PERL => 1
});
my $tt_config = clone($config);
$tt_config->{ngcp} = NGCP::Template->new($tt_config);
open my $fh, '<', $template or
print { $client } "Unable to open file '$template' for reading: $ERRNO\n";
$tt->process($fh, clone($config), $client) or
$tt->process($fh, $tt_config, $client) or
print { $client } $tt->error;
close $fh;

@ -0,0 +1,59 @@
package NGCP::Template 1.000;
use strict;
use warnings;
use List::Util qw(any);
sub new
{
my ($this, $config) = @_;
my $class = ref $this || $this;
my $self = {
config => $config,
};
return bless $self, $class;
}
1;
__END__
=encoding UTF-8
=head1 NAME
NGCP::Template - NGCP module for the Template::Tolkit framework
=head1 VERSION
Version 1.000
=head1 DESCRIPTION
This module provides the methods for the ngcp object that can be used within
the NGCP templates. This makes it easier to use instead of the old library
of code executed via the C<PROCESS> directive.
=head1 AUTHOR
Guillem Jover, C<< <gjover@sipwise.com> >>
=head1 LICENSE
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
=cut

@ -16,6 +16,7 @@ def ngcpcfgcli(tmpdir, *args):
'NGCPCFG': 'fixtures/ngcpcfg.cfg',
'SCRIPTS': '../scripts/',
'HELPER': '../helper/',
'PERL5LIB': '../lib/',
'NGCP_SOCKETFILE': '/tmp/ngcpcfg.socket',
}
testenv.update(env)

Loading…
Cancel
Save