Build DNS/NTP service related configuration in netplan_config

refs: AETHER-1996

Change-Id: I16a4932f3340b305ea3f70127e973e2c6da3bf3f
diff --git a/scripts/nbhelper.py b/scripts/nbhelper.py
index d0aa8fa..b941cfb 100644
--- a/scripts/nbhelper.py
+++ b/scripts/nbhelper.py
@@ -211,6 +211,7 @@
 
         # build item lists
         self.build_prefix()
+        self.prefixes[self.data.prefix] = self
 
     @classmethod
     def all_prefixes(cls):
@@ -563,6 +564,40 @@
                 "addresses": [ip.address for ip in virtual_if_ips],
             }
 
+        # If the object is mgmtserver, it needs to have DNS/NTP server configs
+        if self.data["device_role"]["name"] == "Router":
+            services = list(netboxapi.ipam.services.filter(device_id=self.id))
+            service_names = list(map(lambda x: x.name, services))
+
+            if "dns" in service_names:
+                unbound_listen_ips = []
+                unbound_allow_ips = []
+
+                for ip, intf in self.interfaces_by_ip.items():
+                    if ip != primary_ip and intf.mgmt_only == False:
+                        unbound_listen_ips.append(ip)
+
+                for prefix in NBPrefix.all_prefixes().values():
+                    if prefix.data.description:
+                        unbound_allow_ips.append(prefix.data.prefix)
+                        ntp_client_allow.append(prefix.data.prefix)
+
+                if unbound_listen_ips:
+                    self.netplan_config["unbound_listen_ips"] = unbound_listen_ips
+
+                if unbound_allow_ips:
+                    self.netplan_config["unbound_allow_ips"] = unbound_allow_ips
+
+            if "ntp" in service_names:
+                ntp_client_allow = []
+
+                for prefix in NBPrefix.all_prefixes().values():
+                    if prefix.data.description:
+                        ntp_client_allow.append(prefix.data.prefix)
+
+                if ntp_client_allow:
+                    self.netplan_config["ntp_client_allow"] = ntp_client_allow
+
         return self.netplan_config