Skip to content

Commit

Permalink
UTIL type=[ALLMERGER] - duplicate code reduction - output improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
swaschkut committed Mar 30, 2022
1 parent 0e17f09 commit 6f33c2a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 73 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ UTILS:
* UTIL develop display-xpath-for-node-filter | add argument fullxpath - to display also for template info the full. output
* UTIL develop display-xpath-value | introduce argument displayattributename - to only display the attribute, not full content
* UTIL type=service | introduce actions=split-large-service-groups:MAXnumber
* UTIL type=[ALLMERGER] - duplicate code reduction - output improvements

BUGFIX:
* class DeviceGroup / Template - remove $xmlroot as already defined in trait XmlConvertible
Expand Down
133 changes: 60 additions & 73 deletions utils/lib/MERGER.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ function addressgroup_merging()
else
$childDeviceGroups = array();

PH::print_stdout( "\n\n***********************************************\n" );
PH::print_stdout( " - upper level search status : " . boolYesNo($this->upperLevelSearch) . "" );
if( is_string($findLocation) )
PH::print_stdout( " - location 'shared' found" );
Expand Down Expand Up @@ -989,9 +990,6 @@ function addressgroup_merging()
if( count( $child_hashMap ) >0 )
PH::print_stdout( "Duplicates ChildDG removal is now done. Number of objects after cleanup: '{$store->countAddresses()}' (removed/created {$countChildRemoved}/{$countChildCreated} addresses)\n" );

PH::print_stdout( "\n\n***********************************************\n" );

PH::print_stdout( "\n" );
}
}

Expand All @@ -1007,6 +1005,7 @@ function address_merging()
else
$childDeviceGroups = array();

PH::print_stdout( "\n\n***********************************************\n" );
PH::print_stdout( " - upper level search status : " . boolYesNo($this->upperLevelSearch) . "" );
if( is_string($findLocation) )
PH::print_stdout( " - location 'shared' found" );
Expand All @@ -1021,10 +1020,7 @@ function address_merging()
// Building a hash table of all address objects with same value
//
if( $this->upperLevelSearch )
{
$objectsToSearchThrough = $store->nestedPointOfView();
#$objectsToSearchThrough = $store->nestedPointOfView_sven();
}
else
$objectsToSearchThrough = $store->addressObjects();

Expand All @@ -1048,6 +1044,11 @@ function address_merging()
continue;

$value = $object->value();

// if object is /32, let's remove it to match equivalent non /32 syntax
if( $object->isType_ipNetmask() && strpos($object->value(), '/32') !== FALSE )
$value = substr($value, 0, strlen($value) - 3);

$value = $object->type() . '-' . $value;

#PH::print_stdout( "add objNAME: " . $object->name() . " DG: " . $object->owner->owner->name() . "" );
Expand Down Expand Up @@ -1133,24 +1134,10 @@ function address_merging()
// Hashes with single entries have no duplicate, let's remove them
//
$countConcernedObjects = 0;
foreach( $hashMap as $index => &$hash )
{
if( count($hash) == 1 && !isset($upperHashMap[$index]) && !isset($child_hashMap[$index]) && !isset(reset($hash)->ancestor) )
unset($hashMap[$index]);
else
$countConcernedObjects += count($hash);
}
unset($hash);
self::removeSingleEntries( $hashMap, $child_hashMap, $upperHashMap, $countConcernedObjects);

$countConcernedChildObjects = 0;
foreach( $child_hashMap as $index => &$hash )
{
if( count($hash) == 1 && !isset($upperHashMap[$index]) && !isset($hashMap[$index]) && !isset(reset($hash)->ancestor) )
unset($child_hashMap[$index]);
else
$countConcernedChildObjects += count($hash);
}
unset($hash);
self::removeSingleEntries( $child_hashMap, $hashMap, $upperHashMap, $countConcernedChildObjects);



Expand Down Expand Up @@ -1298,7 +1285,10 @@ function address_merging()
$text .= " value: '{$ancestor->value()}' ";
PH::print_stdout($text);

$tmpstring = "|->ERROR ancestor: '" . $object->_PANC_shortName() . "' cannot be merged";
if( $this->upperLevelSearch )
$tmpstring = "|->ERROR ancestor: '" . $object->_PANC_shortName() . "' cannot be merged. ";
else
$tmpstring = "|-> ancestor: '" . $object->_PANC_shortName() . "' you did not allow to merged";
self::deletedObjectSetRemoved( $index, $tmpstring );

continue;
Expand Down Expand Up @@ -1424,7 +1414,7 @@ function address_merging()
$tmp_address = $store->newAddress($pickedObject->name(), $pickedObject->type(), $pickedObject->value(), $pickedObject->description());
}
else
$tmp_address = "[".$tmp_DG_name."] - ".$pickedObject->name();
$tmp_address = "[".$tmp_DG_name."] - ".$pickedObject->name(). " {new}";

$countChildCreated++;
}
Expand Down Expand Up @@ -1484,14 +1474,11 @@ function address_merging()
}

}



PH::print_stdout( "\n\nDuplicates removal is now done. Number of objects after cleanup: '{$store->countAddresses()}' (removed {$countRemoved} addresses)\n" );
if( count( $child_hashMap ) >0 )
PH::print_stdout( "Duplicates ChildDG removal is now done. Number of objects after cleanup: '{$store->countAddresses()}' (removed/created {$countChildRemoved}/{$countChildCreated} addresses)\n" );

PH::print_stdout( "\n\n***********************************************\n" );

PH::print_stdout( "\n\nDuplicates removal is now done. Number of objects after cleanup: '{$store->countAddresses()}' (removed {$countRemoved} addresses)\n" );

}
}
Expand All @@ -1508,6 +1495,7 @@ function servicegroup_merging()
else
$childDeviceGroups = array();

PH::print_stdout( "\n\n***********************************************\n" );
PH::print_stdout( " - upper level search status : " . boolYesNo($this->upperLevelSearch) . "" );
if( is_string($findLocation) )
PH::print_stdout( " - location 'shared' found" );
Expand Down Expand Up @@ -1855,9 +1843,6 @@ function servicegroup_merging()

PH::print_stdout( "\n\nDuplicates removal is now done. Number of objects after cleanup: '{$store->countServiceGroups()}' (removed {$countRemoved} groups)\n" );

PH::print_stdout( "\n\n***********************************************\n" );

PH::print_stdout( "\n" );
}
}

Expand All @@ -1873,6 +1858,7 @@ function service_merging()
else
$childDeviceGroups = array();

PH::print_stdout( "\n\n***********************************************\n" );
PH::print_stdout( " - upper level search status : " . boolYesNo($this->upperLevelSearch) . "" );
if( is_string($findLocation) )
PH::print_stdout( " - location 'shared' found" );
Expand Down Expand Up @@ -1988,23 +1974,10 @@ function service_merging()
// Hashes with single entries have no duplicate, let's remove them
//
$countConcernedObjects = 0;
foreach( $hashMap as $index => &$hash )
{
if( count($hash) == 1 && !isset($upperHashMap[$index]) && !isset(reset($hash)->ancestor) )
unset($hashMap[$index]);
else
$countConcernedObjects += count($hash);
}
unset($hash);
self::removeSingleEntries( $hashMap, $child_hashMap, $upperHashMap, $countConcernedObjects);

$countConcernedChildObjects = 0;
foreach( $child_hashMap as $index => &$hash )
{
if( count($hash) == 1 && !isset($upperHashMap[$index]) && !isset(reset($hash)->ancestor) )
unset($child_hashMap[$index]);
else
$countConcernedChildObjects += count($hash);
}
unset($hash);
self::removeSingleEntries( $child_hashMap, $hashMap, $upperHashMap, $countConcernedChildObjects);


PH::print_stdout( " - found " . count($hashMap) . " duplicates values totalling {$countConcernedObjects} service objects which are duplicate" );
Expand Down Expand Up @@ -2138,6 +2111,12 @@ function service_merging()
$text .= " value: '{$ancestor->getDestPort()}' ";
PH::print_stdout( $text );

if( $this->upperLevelSearch )
$tmpstring = "|->ERROR ancestor: '" . $object->_PANC_shortName() . "' cannot be merged. ";
else
$tmpstring = "|-> ancestor: '" . $object->_PANC_shortName() . "' you did not allow to merged";
self::deletedObjectSetRemoved( $index, $tmpstring );

continue;
}
else
Expand Down Expand Up @@ -2251,7 +2230,7 @@ function service_merging()
$tmp_service = $store->newService($pickedObject->name(), $pickedObject->protocol(), $pickedObject->getDestPort(), $pickedObject->description(), $pickedObject->getSourcePort());
}
else
$tmp_service = "[".$tmp_DG_name."] - ".$pickedObject->name();
$tmp_service = "[".$tmp_DG_name."] - ".$pickedObject->name()." {new}";

$countChildCreated++;
}
Expand Down Expand Up @@ -2355,6 +2334,12 @@ function service_merging()
$text .= " value: '{$ancestor->value()}' ";
PH::print_stdout($text);

if( $this->upperLevelSearch )
$tmpstring = "|->ERROR ancestor: '" . $object->_PANC_shortName() . "' cannot be merged. ";
else
$tmpstring = "|-> ancestor: '" . $object->_PANC_shortName() . "' you did not allow to merged";
self::deletedObjectSetRemoved( $index, $tmpstring );

continue;
}

Expand Down Expand Up @@ -2409,9 +2394,6 @@ function service_merging()
PH::print_stdout( "\n\nDuplicates removal is now done. Number of objects after cleanup: '{$store->countServices()}' (removed {$countRemoved} services)\n" );
if( count( $child_hashMap ) >0 )
PH::print_stdout( "Duplicates ChildDG removal is now done. Number of objects after cleanup: '{$store->countServices()}' (removed/created {$countChildRemoved}/{$countChildCreated} services)\n" );

PH::print_stdout( "\n\n***********************************************\n" );

}
}

Expand All @@ -2424,6 +2406,7 @@ function tag_merging()
$parentStore = $tmp_location['parentStore'];
$childDeviceGroups = $tmp_location['childDeviceGroups'];

PH::print_stdout( "\n\n***********************************************\n" );
PH::print_stdout( " - upper level search status : " . boolYesNo($this->upperLevelSearch) . "" );
if( is_string($findLocation) )
PH::print_stdout( " - location 'shared' found" );
Expand Down Expand Up @@ -2545,23 +2528,10 @@ function tag_merging()
// Hashes with single entries have no duplicate, let's remove them
//
$countConcernedObjects = 0;
foreach( $hashMap as $index => &$hash )
{
if( count($hash) == 1 && !isset($upperHashMap[$index]) && !isset(reset($hash)->ancestor) )
unset($hashMap[$index]);
else
$countConcernedObjects += count($hash);
}
unset($hash);
self::removeSingleEntries( $hashMap, $child_hashMap, $upperHashMap, $countConcernedObjects);

$countConcernedChildObjects = 0;
foreach( $child_hashMap as $index => &$hash )
{
if( count($hash) == 1 && !isset($upperHashMap[$index]) && !isset(reset($hash)->ancestor) )
unset($child_hashMap[$index]);
else
$countConcernedChildObjects += count($hash);
}
unset($hash);
self::removeSingleEntries( $child_hashMap, $hashMap, $upperHashMap, $countConcernedChildObjects);


PH::print_stdout( " - found " . count($hashMap) . " duplicates values totalling {$countConcernedObjects} tag objects which are duplicate" );
Expand Down Expand Up @@ -2702,7 +2672,10 @@ function tag_merging()
$text .= " color: '{$ancestor->getColor()}' ";
PH::print_stdout($text);

$tmpstring = "|->ERROR ancestor: '" . $object->_PANC_shortName() . "' cannot be merged";
if( $this->upperLevelSearch )
$tmpstring = "|->ERROR ancestor: '" . $object->_PANC_shortName() . "' cannot be merged. ";
else
$tmpstring = "|-> ancestor: '" . $object->_PANC_shortName() . "' you did not allow to merged";
self::deletedObjectSetRemoved( $index, $tmpstring );

continue;
Expand Down Expand Up @@ -2810,6 +2783,8 @@ function tag_merging()
if( $this->apiMode )
$tmp_tag->API_sync();
}
else
$tmp_tag = "[".$tmp_DG_name."] - ".$pickedObject->name()." {new}";

$countChildCreated++;
}
Expand Down Expand Up @@ -2855,8 +2830,6 @@ function tag_merging()
if( count( $child_hashMap ) >0 )
PH::print_stdout( "Duplicates ChildDG removal is now done. Number of objects after cleanup: '{$store->count()}' (removed/created {$countChildRemoved}/{$countChildCreated} tags)\n" );

PH::print_stdout( "\n\n***********************************************\n" );

}
}

Expand All @@ -2869,7 +2842,7 @@ function merger_final_step()
{
PH::print_stdout(" * script was called with argument 'exportCSV' - please wait for calculation");

$tmp_string = "value,kept,removed";
$tmp_string = "value,kept(create),removed";
foreach( $this->deletedObjects as $obj_index => $object_name )
{
if( isset($object_name['kept']) )
Expand All @@ -2889,7 +2862,7 @@ function merger_final_step()

function exportCSVToHtml()
{
$headers = '<th>value</th><th>kept</th><th>removed</th>';
$headers = '<th>ID</th><th>value</th><th>kept (create)</th><th>removed</th>';


$lines = '';
Expand Down Expand Up @@ -2939,6 +2912,8 @@ function exportCSVToHtml()
else
$lines .= "<tr bgcolor=\"#DDDDDD\">";

$lines .= $encloseFunction( (string)$count );

$lines .= $encloseFunction( (string)$index );

if( isset( $line['kept'] ) )
Expand Down Expand Up @@ -3006,4 +2981,16 @@ private function deletedObjectSetRemoved( $index, $tmpstring )

$this->deletedObjects[$index]['removed'] .= $tmpstring;
}

private function removeSingleEntries( &$hashMap, $other_hashMap, $upperHashMap, &$countObjects = 0)
{
foreach( $hashMap as $index => &$hash )
{
if( count($hash) == 1 && !isset($upperHashMap[$index]) && !isset($other_hashMap[$index]) && !isset(reset($hash)->ancestor) )
unset($hashMap[$index]);
else
$countObjects += count($hash);
}
unset($hash);
}
}

0 comments on commit 6f33c2a

Please sign in to comment.