From 4b3eca1e08603ed8c97d7b352e567e2e8570eac4 Mon Sep 17 00:00:00 2001
From: Christian Breunig <christian@breunig.cc>
Date: Mon, 25 Dec 2023 08:58:51 +0100
Subject: snmp: T5855: add GPL license header

(cherry picked from commit 2490f22408ad811ff9f63ec970d0167ecbf4ab59)
---
 src/migration-scripts/lldp/0-to-1 | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/src/migration-scripts/lldp/0-to-1 b/src/migration-scripts/lldp/0-to-1
index a936cbdfc..a99356062 100755
--- a/src/migration-scripts/lldp/0-to-1
+++ b/src/migration-scripts/lldp/0-to-1
@@ -1,4 +1,18 @@
 #!/usr/bin/env python3
+#
+# Copyright (C) 2020 VyOS maintainers and contributors
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 or later as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 # Delete "set service lldp interface <interface> location civic-based" option
 # as it was broken most of the time anyways
-- 
cgit v1.2.3


From 1902f252c4c270cf7981fecd486e293ec2182fd9 Mon Sep 17 00:00:00 2001
From: Christian Breunig <christian@breunig.cc>
Date: Mon, 25 Dec 2023 08:58:56 +0100
Subject: snmp: T5855: migrate "set service lldp snmp enable" to "set service
 lldp snmp"

(cherry picked from commit a9201e77110ce0695e2ba879304aef41b7ac9a0c)
---
 data/templates/lldp/lldpd.j2                       |  2 +-
 .../include/version/lldp-version.xml.i             |  2 +-
 interface-definitions/lldp.xml.in                  | 15 ++-----
 smoketest/configs/ospf-small                       |  3 ++
 smoketest/scripts/cli/test_service_lldp.py         | 16 +++++++-
 src/conf_mode/lldp.py                              |  5 +--
 src/conf_mode/snmp.py                              |  2 +-
 src/migration-scripts/lldp/1-to-2                  | 48 ++++++++++++++++++++++
 8 files changed, 75 insertions(+), 18 deletions(-)
 create mode 100755 src/migration-scripts/lldp/1-to-2

diff --git a/data/templates/lldp/lldpd.j2 b/data/templates/lldp/lldpd.j2
index 3c499197d..6ae063c4b 100644
--- a/data/templates/lldp/lldpd.j2
+++ b/data/templates/lldp/lldpd.j2
@@ -1,2 +1,2 @@
 ### Autogenerated by lldp.py ###
-DAEMON_ARGS="-M 4 {{ '-x' if snmp.enable is vyos_defined }} {{ '-c' if legacy_protocols.cdp is vyos_defined }} {{ '-e' if legacy_protocols.edp is vyos_defined }} {{ '-f' if legacy_protocols.fdp is vyos_defined }} {{ '-s' if legacy_protocols.sonmp is vyos_defined }}"
+DAEMON_ARGS="-M 4 {{ '-x' if snmp is vyos_defined }} {{ '-c' if legacy_protocols.cdp is vyos_defined }} {{ '-e' if legacy_protocols.edp is vyos_defined }} {{ '-f' if legacy_protocols.fdp is vyos_defined }} {{ '-s' if legacy_protocols.sonmp is vyos_defined }}"
diff --git a/interface-definitions/include/version/lldp-version.xml.i b/interface-definitions/include/version/lldp-version.xml.i
index 0deb73279..b41d80451 100644
--- a/interface-definitions/include/version/lldp-version.xml.i
+++ b/interface-definitions/include/version/lldp-version.xml.i
@@ -1,3 +1,3 @@
 <!-- include start from include/version/lldp-version.xml.i -->
-<syntaxVersion component='lldp' version='1'></syntaxVersion>
+<syntaxVersion component='lldp' version='2'></syntaxVersion>
 <!-- include end -->
diff --git a/interface-definitions/lldp.xml.in b/interface-definitions/lldp.xml.in
index 738bb11c1..25fb575b6 100644
--- a/interface-definitions/lldp.xml.in
+++ b/interface-definitions/lldp.xml.in
@@ -175,19 +175,12 @@
               <multi/>
             </properties>
           </leafNode>
-          <node name="snmp">
+          <leafNode name="snmp">
             <properties>
-              <help>SNMP parameters for LLDP</help>
+              <help>Enable SNMP queries of the LLDP database</help>
+              <valueless/>
             </properties>
-            <children>
-              <leafNode name="enable">
-                <properties>
-                  <help>Enable SNMP queries of the LLDP database</help>
-                  <valueless/>
-                </properties>
-              </leafNode>
-            </children>
-          </node>
+          </leafNode>
         </children>
       </node>
     </children>
diff --git a/smoketest/configs/ospf-small b/smoketest/configs/ospf-small
index 767f4e21f..b3002b1af 100644
--- a/smoketest/configs/ospf-small
+++ b/smoketest/configs/ospf-small
@@ -81,6 +81,9 @@ service {
     lldp {
         interface all {
         }
+        snmp {
+            enable
+        }
     }
     snmp {
         community public {
diff --git a/smoketest/scripts/cli/test_service_lldp.py b/smoketest/scripts/cli/test_service_lldp.py
index ee26844ab..7e30b43f5 100755
--- a/smoketest/scripts/cli/test_service_lldp.py
+++ b/smoketest/scripts/cli/test_service_lldp.py
@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
 #
-# Copyright (C) 2022 VyOS maintainers and contributors
+# Copyright (C) 2022-2023 VyOS maintainers and contributors
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License version 2 or later as
@@ -122,6 +122,20 @@ class TestServiceLLDP(VyOSUnitTestSHIM.TestCase):
         self.assertIn(f'configure ports {interface} med location elin "{elin}"', config)
         self.assertIn(f'configure system interface pattern "{interface}"', config)
 
+    def test_06_lldp_snmp(self):
+        self.cli_set(base_path + ['snmp'])
+
+        # verify - can not start lldp snmp without snmp beeing configured
+        with self.assertRaises(ConfigSessionError):
+            self.cli_commit()
+        self.cli_set(['service', 'snmp'])
+        self.cli_commit()
+
+        # SNMP required process to be started with -x option
+        tmp = read_file('/etc/default/lldpd')
+        self.assertIn('-x', tmp)
+
+        self.cli_delete(['service', 'snmp'])
 
 if __name__ == '__main__':
     unittest.main(verbosity=2)
diff --git a/src/conf_mode/lldp.py b/src/conf_mode/lldp.py
index c2e87d171..3c647a0e8 100755
--- a/src/conf_mode/lldp.py
+++ b/src/conf_mode/lldp.py
@@ -86,9 +86,9 @@ def verify(lldp):
                     raise ConfigError(f'Must define both longitude and latitude for "{interface}" location!')
 
     # check options
-    if 'snmp' in lldp and 'enable' in lldp['snmp']:
+    if 'snmp' in lldp:
         if 'system_snmp_enabled' not in lldp:
-            raise ConfigError('SNMP must be configured to enable LLDP SNMP')
+            raise ConfigError('SNMP must be configured to enable LLDP SNMP!')
 
 
 def generate(lldp):
@@ -121,4 +121,3 @@ if __name__ == '__main__':
     except ConfigError as e:
         print(e)
         exit(1)
-
diff --git a/src/conf_mode/snmp.py b/src/conf_mode/snmp.py
index 38b5d52f9..f65d0d6bc 100755
--- a/src/conf_mode/snmp.py
+++ b/src/conf_mode/snmp.py
@@ -54,7 +54,7 @@ def get_config(config=None):
     if not conf.exists(base):
         snmp.update({'deleted' : ''})
 
-    if conf.exists(['service', 'lldp', 'snmp', 'enable']):
+    if conf.exists(['service', 'lldp', 'snmp']):
         snmp.update({'lldp_snmp' : ''})
 
     if 'deleted' in snmp:
diff --git a/src/migration-scripts/lldp/1-to-2 b/src/migration-scripts/lldp/1-to-2
new file mode 100755
index 000000000..35efb25db
--- /dev/null
+++ b/src/migration-scripts/lldp/1-to-2
@@ -0,0 +1,48 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2023 VyOS maintainers and contributors
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 or later as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# T5855: migrate "set service lldp snmp enable" -> `set service lldp snmp"
+
+import sys
+
+from vyos.configtree import ConfigTree
+
+if len(sys.argv) < 2:
+    print("Must specify file name!")
+    sys.exit(1)
+
+file_name = sys.argv[1]
+
+with open(file_name, 'r') as f:
+    config_file = f.read()
+
+config = ConfigTree(config_file)
+base = ['service', 'lldp']
+if not config.exists(base):
+    # Nothing to do
+    sys.exit(0)
+
+if config.exists(base + ['snmp']):
+    enabled = config.exists(base + ['snmp', 'enable'])
+    config.delete(base + ['snmp'])
+    if enabled: config.set(base + ['snmp'])
+
+try:
+    with open(file_name, 'w') as f:
+        f.write(config.to_string())
+except OSError as e:
+    print("Failed to save the modified config: {}".format(e))
+    sys.exit(1)
-- 
cgit v1.2.3