iTop网络关联关系的过度设计

iTop 的 lnkConnectableCIToNetworkDevice 可能存在过度设计,通过一个枚举属性,引入上联下联的概念,然后为一对连接设备创建了两个连接关系,如以下代码所示,AfterInsert 里,添加了一个反向的连接关系。

protected function AddConnectedNetworkDevice()
{
	$oDevice = MetaModel::GetObject('ConnectableCI', $this->Get('connectableci_id'));
	if (is_object($oDevice) && (get_class($oDevice) == 'NetworkDevice'))
	{
		$sOQL = "SELECT  lnkConnectableCIToNetworkDevice WHERE connectableci_id = :device AND networkdevice_id = :network AND network_port = :nwport AND device_port = :devport";
		$oConnectionSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL),
						array(),
						array(
							'network' => $this->Get('connectableci_id'),
							'device' => $this->Get('networkdevice_id'),
							'devport' => $this->Get('network_port'),
							'nwport' => $this->Get('device_port'),
							)
		);	
		if ($oConnectionSet->Count() == 0)
		{
			$sLink = $this->Get('connection_type');
			$sConnLink = ($sLink == 'uplink') ? 'downlink' : 'uplink';

			$oNewLink = new lnkConnectableCIToNetworkDevice();
			$oNewLink->Set('networkdevice_id', $this->Get('connectableci_id'));
			$oNewLink->Set('connectableci_id', $this->Get('networkdevice_id'));
			$oNewLink->Set('network_port', $this->Get('device_port'));
			$oNewLink->Set('device_port', $this->Get('network_port'));
			$oNewLink->Set('connection_type', $sConnLink);
			$oNewLink->DBInsert();	
		}
	}
}
protected function AfterInsert()
{
    $this->AddConnectedNetworkDevice();
    parent::AfterInsert();
}

不仅新增,更新,删除也都要修改,非常复杂,也难以理解。

我认为可以有更清晰的方法来区分上下游关系。ConnectableCI 里定义了 networkdevice_list 属性,可以理解为 上游设备列表,而 NetworkDevice 里定义了 connectableci_list 可以理解为 下游设备列表NetworkDevice 本身又是 ConnectableCI 的子类,继承了 networkdevice_list 属性,但是 networkdevice_list 属性却没有在 details 里显示,而是给连接搞了个复杂的上下联属性。其实只要把 NetworkDevicenetworkdevcie_list 属性显示出来,就非常清晰了,可以翻译为:

"Class:ConnectableCI/Attribute:networkdevice_list" => "上游网络设备",
"Class:NetworkDevice/Attribute:connectableci_list" => "下游设备",

relations 关系也不必搞的现在这样复杂

<relations>
<relation id="impacts">
  <neighbours>
	<neighbour id="connectableci">
	  <query_down>SELECT ConnectableCI AS d JOIN lnkConnectableCIToNetworkDevice AS l1 ON l1.connectableci_id = d.id WHERE l1.networkdevice_id = :this-&gt;id AND l1.connection_type='downlink'</query_down>
	  <query_up>SELECT NetworkDevice AS nw JOIN lnkConnectableCIToNetworkDevice AS l1 ON l1.networkdevice_id = nw.id WHERE l1.connectableci_id = :this-&gt;id AND l1.connection_type='downlink'</query_up>
	</neighbour>
  </neighbours>
</relation>
</relations>

直接改为

<relations>
<relation id="impacts">
  <neighbours>
	<neighbour id="connectableci">
	  <attribute>connectableci_list</attribute>
	</neighbour>
  </neighbours>
</relation>
</relations>

即可。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注