From 10a88cf669a0e458ec002e71284f82d6f75f6d1d Mon Sep 17 00:00:00 2001
From: Kirill Solomko <ksolomko@sipwise.com>
Date: Mon, 15 Jul 2024 19:36:29 +0200
Subject: [PATCH] MT#58964 /api/platforminfo add license_meta

* new Utils::License::get_license_meta($c) to
  fetch license meta ({} by default) that contains license
  related metadata such as current and max amount
  of subcsribers and license valid until date.
  currently the following data is fetched from /proc/ngcp
    check
    current_calls
    current_pbx_groups
    current_pbx_subscribers
    current_registered_subscribers
    current_subscribers
    license_valid_until
    max_calls
    max_pbx_groups
    max_pbx_subscribers
    max_registered_subscribers
    max_subscribers
    valid
* Controller::API::Root platforminfo now also returns
  license_meta

Change-Id: I323cdfd646335a408e0150ecd69ad950fa0461ab
---
 lib/NGCP/Panel/Controller/API/Root.pm |  4 ++-
 lib/NGCP/Panel/Utils/License.pm       | 50 ++++++++++++++++++++++++++-
 2 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/lib/NGCP/Panel/Controller/API/Root.pm b/lib/NGCP/Panel/Controller/API/Root.pm
index 92dbf30a0a..dc3b89728e 100644
--- a/lib/NGCP/Panel/Controller/API/Root.pm
+++ b/lib/NGCP/Panel/Controller/API/Root.pm
@@ -347,11 +347,13 @@ sub platforminfo :Path('/api/platforminfo') :CaptureArgs(0) {
         my $json_file = shift;
         my $json = '';
         my $licenses = NGCP::Panel::Utils::License::get_licenses($c);
+        my $license_meta = NGCP::Panel::Utils::License::get_license_meta($c);
         open(my $fh, '<', $json_file) || return;
         $json .= $_ while <$fh>;
         close $fh;
         my $data = decode_json($json) || return;
-        $data->{licenses} = $licenses;
+        $data->{licenses} = $licenses // [];
+        $data->{license_meta} = $license_meta // {};
         return to_json($data, {pretty => 1, canonical => 1});
     });
     $c->forward($c->view());
diff --git a/lib/NGCP/Panel/Utils/License.pm b/lib/NGCP/Panel/Utils/License.pm
index ae660d52d7..9986f15b68 100644
--- a/lib/NGCP/Panel/Utils/License.pm
+++ b/lib/NGCP/Panel/Utils/License.pm
@@ -3,6 +3,7 @@ use strict;
 use warnings;
 
 use Sipwise::Base;
+use List::Util qw(none);
 
 sub get_license_status {
     my ($ref) = @_;
@@ -75,7 +76,7 @@ sub get_licenses {
             next;
         };
         my $enabled = <$fh>;
-        chomp($enabled) if $enabled;
+        chomp($enabled) if defined $enabled;
         push @lics, $lf if $enabled && $enabled == 1;
         close $fh;
     }
@@ -84,6 +85,53 @@ sub get_licenses {
     return \@sorted_lics;
 }
 
+sub get_license_meta {
+    my $c = shift;
+
+    my $proc_dir = '/proc/ngcp';
+    unless (-d $proc_dir) {
+        $c->log->error("Failed to access $proc_dir");
+        return;
+    };
+
+    my $meta = {};
+    my @collect = qw(
+        check
+        current_calls
+        current_pbx_groups
+        current_pbx_subscribers
+        current_registered_subscribers
+        current_subscribers
+        license_valid_until
+        max_calls
+        max_pbx_groups
+        max_pbx_subscribers
+        max_registered_subscribers
+        max_subscribers
+        valid
+    );
+
+    opendir(my $dh, $proc_dir) || do {
+        $c->log->error("Failed to open ngcp dir $proc_dir: $!");
+        return;
+    };
+    while (readdir($dh)) {
+        my $lf = $_;
+        next if $lf =~ /^\.+$/;
+        next if none { $lf eq $_ } @collect;
+        open(my $fh, '<', "$proc_dir/$lf") || do {
+            $c->log->error("Failed to open license file $lf: $!");
+            next;
+        };
+        my $value = <$fh>;
+        chomp($value) if defined $value;
+        $meta->{$lf} = $value =~ /^-?\d+(\.\d+)?$/ ? $value+0 : $value;
+        close $fh;
+    }
+    closedir $dh;
+    return $meta;
+}
+
 1;
 
 =head1 NAME