More work to decommission rogue_path

This commit is contained in:
2023-09-15 22:57:32 +10:00
parent c1d6d48a3c
commit 708d9a9f67
12 changed files with 218 additions and 231 deletions

View File

@@ -372,6 +372,99 @@ class Address extends Model
/* METHODS */
/**
* Create an FTN address associated with a system
*
* @param string $address
* @param System $so
* @return self
* @throws \Exception
*/
public static function createFTN(string $address,System $so): ?self
{
$ftn = self::parseFTN($address);
if (! $ftn['d']) {
// See if we have a default domain for this domain
if ($ftn['z']) {
$zo = Zone::where('zone_id',$ftn['z'])->where('default',TRUE)->single();
if ($zo)
$ftn['d'] = $zo->domain->name;
else {
Log::alert(sprintf('%s:! Refusing to create address [%s] no domain available',self::LOGKEY,$address));
return NULL;
}
}
}
Log::debug(sprintf('%s:- Creating AKA [%s] for [%s]',self::LOGKEY,$address,$so->name));
// Check Domain exists
Domain::unguard();
$do = Domain::singleOrNew(['name'=>$ftn['d']]);
Domain::reguard();
if (! $do->exists) {
$do->public = TRUE;
$do->active = TRUE;
$do->notes = 'Auto created';
$do->save();
}
// If the zone is zero, see if this is a flatten domain, and if so, find an NC
if (($ftn['z'] === 0) && $do->flatten) {
$nc = self::findZone($do,$ftn['n'],0,0);
if ($nc) {
Log::info(sprintf('%s:- Setting zone to [%d]',self::LOGKEY,$nc->zone->zone_id));
$ftn['z'] = $nc->zone->zone_id;
}
}
// Create zone
Zone::unguard();
$zo = Zone::singleOrNew(['domain_id'=>$do->id,'zone_id'=>$ftn['z']]);
Zone::reguard();
if (! $zo->exists) {
$zo->active = TRUE;
$zo->notes = 'Auto created';
$zo->system_id = System::createUnknownSystem()->id;
$do->zones()->save($zo);
}
if (! $zo->active || ! $do->active) {
Log::alert(sprintf('%s:! Refusing to create address [%s] in disabled zone or domain',self::LOGKEY,$address));
return NULL;
}
// Create Address, assigned to $so
$o = new self;
$o->active = TRUE;
$o->zone_id = $zo->id;
$o->region_id = 0;
$o->host_id = $ftn['n'];
$o->node_id = $ftn['f'];
$o->point_id = $ftn['p'];
$o->role = $ftn['p'] ? self::NODE_POINT : self::NODE_UNKNOWN;
try {
$so->addresses()->save($o);
} catch (\Exception $e) {
Log::error(sprintf('%s:! ERROR creating address [%s]',self::LOGKEY,get_class($e)));
return NULL;
}
return $o;
}
/**
* Find a record in the DB for a node string, eg: 10:1/1.0
*
@@ -382,45 +475,12 @@ class Address extends Model
* @return Address|null
* @throws \Exception
*/
public static function findFTN(string $address,bool $create=FALSE,System $so=NULL,bool $trashed=FALSE): ?self
public static function findFTN(string $address,bool $trashed=FALSE): ?self
{
$ftn = self::parseFTN($address);
$o = NULL;
// Are we looking for a region address
if (($ftn['f'] === 0) && $ftn['p'] === 0) {
$o = (new self)
->select('addresses.*')
->join('zones',['zones.id'=>'addresses.zone_id'])
->join('domains',['domains.id'=>'zones.domain_id'])
->when($trashed,function($query) {
$query->trashed();
},function($query) {
$query->active();
})
->where('zones.zone_id',$ftn['z'])
->where(function($q) use ($ftn) {
return $q
->where(function($q) use ($ftn) {
return $q->where('region_id',$ftn['n'])
->where('host_id',$ftn['n']);
});
})
->where('node_id',$ftn['f'])
->where('point_id',$ftn['p'])
->when($ftn['d'],function($query,$domain) {
$query->where('domains.name',$domain);
})
->when((! $ftn['d']),function($query) {
$query->where('zones.default',TRUE);
})
->single();
if ($o && $o->system->active)
return $o;
}
// Look for a normal address
$o = ($x=(new self)
$query = (new self)
->select('addresses.*')
->join('zones',['zones.id'=>'addresses.zone_id'])
->join('domains',['domains.id'=>'zones.domain_id'])
@@ -430,32 +490,39 @@ class Address extends Model
$query->active();
})
->where('zones.zone_id',$ftn['z'])
->where(function($q) use ($ftn) {
return $q->where(function($qq) use ($ftn) {
return $qq
->where('region_id',$ftn['n'])
->where('host_id',$ftn['n']);
})
->orWhere(function($qq) use ($ftn) {
return $qq
->where('host_id',$ftn['n']);
});
})
->where('node_id',$ftn['f'])
->where('point_id',$ftn['p'])
->when($ftn['d'],function($query,$domain) {
$query->where('domains.name',$domain);
})
->when((! $ftn['d']),function($query) {
},function($query) {
$query->where('zones.default',TRUE);
}))
->single();
});
if ($o && $o->system->active)
return $o;
$q = $query->clone();
// Are we looking for a region address
if (($ftn['f'] === 0) && ($ftn['p'] === 0))
$o = $query
->where('region_id',$ftn['n'])
->where('host_id',$ftn['n'])
->single();
// Look for a normal address
if (! $o)
$o = $q
->where(function($q) use ($ftn) {
return $q
->where(function($q) use ($ftn) {
return $q
->where('region_id',$ftn['n'])
->where('host_id',$ftn['n']);
})
->orWhere('host_id',$ftn['n']);
})
->single();
// Check and see if we are a flattened domain, our address might be available with a different zone.
if ($ftn['p'] === 0) {
if (! $o && ($ftn['p'] === 0)) {
if ($ftn['d'])
$do = Domain::where(['name'=>$ftn['d']])->single();
else {
@@ -463,81 +530,8 @@ class Address extends Model
$do = $zo?->domain;
}
if ($do && $do->flatten) {
if ($do && $do->flatten)
$o = self::findZone($do,$ftn['n'],$ftn['f'],$ftn['p'],$trashed);
if ($o && $o->system->active)
return $o;
}
}
if ($create) {
if (! $so) {
System::unguard();
$so = System::firstOrCreate([
'name' => 'Discovered System',
'sysop' => 'Unknown',
'location' => '',
'active' => TRUE,
]);
System::reguard();
}
if (! $ftn['d']) {
Log::alert(sprintf('%s:! Refusing to create address [%s] no domain available',self::LOGKEY,$address));
return NULL;
}
Log::debug(sprintf('%s:Creating AKA [%s] for [%s]',self::LOGKEY,$address,$so->name));
// Check Domain exists
Domain::unguard();
$do = Domain::singleOrNew(['name'=>$ftn['d']]);
Domain::reguard();
if (! $do->exists) {
$do->public = TRUE;
$do->active = TRUE;
$do->notes = 'Auto created';
$do->save();
}
// Create zone
Zone::unguard();
$zo = Zone::singleOrNew(['domain_id'=>$do->id,'zone_id'=>$ftn['z']]);
Zone::reguard();
if (! $zo->exists) {
$zo->active = TRUE;
$zo->notes = 'Auto created';
$zo->system_id = System::where('name','Discovered System')->first()->id;
$do->zones()->save($zo);
}
if (! $zo->active || ! $do->active) {
Log::alert(sprintf('%s:! Refusing to create address [%s] in disabled zone or domain',self::LOGKEY,$address));
return NULL;
}
// Create Address, assigned to $so
$o = new self;
$o->active = TRUE;
$o->zone_id = $zo->id;
$o->region_id = 0;
$o->host_id = $ftn['n'];
$o->node_id = $ftn['f'];
$o->point_id = $ftn['p'];
$o->role = $ftn['p'] ? self::NODE_POINT : self::NODE_UNKNOWN;
try {
$so->addresses()->save($o);
} catch (\Exception $e) {
Log::error(sprintf('%s:! ERROR creating address [%s] (%s)',self::LOGKEY,$x->toSql(),get_class($e)),['bindings'=>$x->getBindings()]);
return NULL;
}
}
return ($o && $o->system->active) ? $o : NULL;