diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 545841f1..615b0b9a 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -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 diff --git a/utils/lib/MERGER.php b/utils/lib/MERGER.php index f60854b3..be37172d 100644 --- a/utils/lib/MERGER.php +++ b/utils/lib/MERGER.php @@ -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" ); @@ -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" ); } } @@ -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" ); @@ -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(); @@ -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() . "" ); @@ -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); @@ -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; @@ -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++; } @@ -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" ); } } @@ -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" ); @@ -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" ); } } @@ -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" ); @@ -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" ); @@ -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 @@ -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++; } @@ -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; } @@ -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" ); - } } @@ -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" ); @@ -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" ); @@ -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; @@ -2810,6 +2783,8 @@ function tag_merging() if( $this->apiMode ) $tmp_tag->API_sync(); } + else + $tmp_tag = "[".$tmp_DG_name."] - ".$pickedObject->name()." {new}"; $countChildCreated++; } @@ -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" ); - } } @@ -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']) ) @@ -2889,7 +2862,7 @@ function merger_final_step() function exportCSVToHtml() { - $headers = 'valuekeptremoved'; + $headers = 'IDvaluekept (create)removed'; $lines = ''; @@ -2939,6 +2912,8 @@ function exportCSVToHtml() else $lines .= ""; + $lines .= $encloseFunction( (string)$count ); + $lines .= $encloseFunction( (string)$index ); if( isset( $line['kept'] ) ) @@ -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); + } } \ No newline at end of file