Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5,622 changes: 26 additions & 5,596 deletions app/Http/Controllers/Panel/AdminController.php

Large diffs are not rendered by default.

65 changes: 65 additions & 0 deletions app/Http/Requests/OltRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class OltRequest extends FormRequest
{
public function authorize(): bool
{
// Adjust authorization as needed (e.g. policies/permissions)
return $this->user()?->can('manage', \App\Models\Olt::class) ?? true;
}

public function rules(): array
{
return [
'name' => ['required', 'string', 'max:100'],
'ip_address' => ['required', 'ip'],
'brand' => ['nullable', 'string', 'max:50'],
'model' => ['nullable', 'string', 'max:100'],
'management_protocol' => ['required', 'in:ssh,telnet,snmp'],
'port' => ['nullable', 'integer', 'between:1,65535'],
'snmp_port' => ['nullable', 'integer', 'between:1,65535'],
'snmp_community' => ['nullable', 'string', 'max:500'],
'username' => ['nullable', 'string', 'max:100'],
'password' => ['nullable', 'string', 'max:200'],
'status' => ['required', 'in:active,inactive,maintenance'],
'onu_type' => ['nullable', 'in:epon,gpon,xpon'], // support ONU type validation
'total_ports' => ['nullable','integer','min:0'],
'max_onus' => ['nullable','integer','min:0'],
];
}

public function withValidator($validator)
{
$validator->after(function ($validator) {
$protocol = $this->input('management_protocol');

if (in_array($protocol, ['ssh', 'telnet']) && ! $this->filled('port')) {
$validator->errors()->add('port', 'Port is required for SSH/Telnet management protocol.');
}

if ($protocol === 'snmp' && ! $this->filled('snmp_port')) {
$validator->errors()->add('snmp_port', 'SNMP port is required for SNMP management protocol.');
}

// If SSH/Telnet selected ensure username/password present
if (in_array($protocol, ['ssh', 'telnet']) && (! $this->filled('username') || ! $this->filled('password'))) {
$validator->errors()->add('credentials', 'Username and password are required for SSH/Telnet devices.');
}
});
}

public function messages(): array
{
return [
'ip_address.ip' => 'Please provide a valid IPv4 or IPv6 address.',
'management_protocol.in' => 'Management protocol must be one of: ssh, telnet, snmp.',
'onu_type.in' => 'ONU type must be one of: epon, gpon, xpon.',
];
}
}
86 changes: 9 additions & 77 deletions app/Models/Olt.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,17 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* OLT (Optical Line Terminal) Model
*
* Represents an OLT device that manages multiple ONUs.
*
* @property int $id
* @property int|null $tenant_id
* @property string $name
* @property string $ip_address
* @property int $port
* @property string $management_protocol
* @property string $username
* @property string $password
* @property string|null $snmp_community
* @property string|null $snmp_version
* @property string|null $model
* @property string|null $location
* @property string $status
* @property \Illuminate\Support\Carbon|null $last_backup_at
* @property \Illuminate\Support\Carbon|null $last_health_check_at
* @property string $health_status
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
*/
class Olt extends Model
{
use HasFactory;

/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'tenant_id',
'name',
'brand',
'ip_address',
'port',
'telnet_port',
'management_protocol',
'port', // SSH/Telnet port
'management_protocol', // ssh, telnet, snmp
'username',
'password',
'snmp_community',
Expand All @@ -67,24 +37,15 @@ class Olt extends Model
'last_health_check_at',
];

/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'username',
'snmp_community',
];

/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'port' => 'integer',
'snmp_port' => 'integer',
'username' => 'encrypted',
'password' => 'encrypted',
'snmp_community' => 'encrypted',
Expand All @@ -94,67 +55,38 @@ class Olt extends Model
'updated_at' => 'datetime',
];

/**
* Get the tenant that owns the OLT.
*/
public function tenant(): BelongsTo
{
return $this->belongsTo(Tenant::class);
}

/**
* Get the ONUs for the OLT.
*/
public function onus(): HasMany
{
return $this->hasMany(Onu::class);
}

/**
* Get the backups for the OLT.
*/
public function backups(): HasMany
{
return $this->hasMany(OltBackup::class);
}

/**
* Scope a query to only include active OLTs.
*/
public function scopeActive($query)
{
return $query->where('status', 'active');
}

/**
* Scope a query to only include inactive OLTs.
*/
public function scopeInactive($query)
{
return $query->where('status', 'inactive');
}

/**
* Scope a query to only include OLTs in maintenance mode.
*/
public function scopeMaintenance($query)
{
return $query->where('status', 'maintenance');
}

/**
* Check if the OLT is active.
*/
public function isActive(): bool
{
return $this->status === 'active';
}

/**
* Check if the OLT can be connected to.
*/
public function canConnect(): bool
{
// Allow SNMP-only devices (no username/password), or SSH/Telnet requiring credentials
if ($this->management_protocol === 'snmp') {
return $this->isActive() && ! empty($this->ip_address);
}

return $this->isActive() && ! empty($this->ip_address) && ! empty($this->username) && ! empty($this->password);
}
}
}
Loading