From 06e85fc11a006a15757d12fc3bf3d402c4d37ffd Mon Sep 17 00:00:00 2001
From: Lluis Gifre <lluis.gifre@cttc.es>
Date: Fri, 4 Mar 2022 11:32:23 +0100
Subject: [PATCH] Compute:
NBI IETF L2VPN:
- Added missing configuration attributes (BGP AS, BGP Route Target)
---
 .../nbi_plugins/ietf_l2vpn/Constants.py       |  4 ++-
 .../ietf_l2vpn/L2VPN_SiteNetworkAccesses.py   | 25 ++++++++++++++-----
 2 files changed, 22 insertions(+), 7 deletions(-)
diff --git a/src/compute/service/rest_server/nbi_plugins/ietf_l2vpn/Constants.py b/src/compute/service/rest_server/nbi_plugins/ietf_l2vpn/Constants.py
index 2473d2f9c..83bf52d0c 100644
--- a/src/compute/service/rest_server/nbi_plugins/ietf_l2vpn/Constants.py
+++ b/src/compute/service/rest_server/nbi_plugins/ietf_l2vpn/Constants.py
@@ -12,8 +12,10 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-DEFAULT_MTU = 1512
+DEFAULT_MTU              = 1512
 DEFAULT_ADDRESS_FAMILIES = ['IPV4']
+DEFAULT_BGP_AS           = 65000
+DEFAULT_BGP_ROUTE_TARGET = '{:d}:{:d}'.format(DEFAULT_BGP_AS, 333)
 
 # Bearer mappings:
 # device_uuid:endpoint_uuid => (
diff --git a/src/compute/service/rest_server/nbi_plugins/ietf_l2vpn/L2VPN_SiteNetworkAccesses.py b/src/compute/service/rest_server/nbi_plugins/ietf_l2vpn/L2VPN_SiteNetworkAccesses.py
index 8ab8581b1..6811dadac 100644
--- a/src/compute/service/rest_server/nbi_plugins/ietf_l2vpn/L2VPN_SiteNetworkAccesses.py
+++ b/src/compute/service/rest_server/nbi_plugins/ietf_l2vpn/L2VPN_SiteNetworkAccesses.py
@@ -29,7 +29,7 @@ from .schemas.site_network_access import SCHEMA_SITE_NETWORK_ACCESS
 from .tools.Authentication import HTTP_AUTH
 from .tools.HttpStatusCodes import HTTP_NOCONTENT, HTTP_SERVERERROR
 from .tools.Validator import validate_message
-from .Constants import BEARER_MAPPINGS, DEFAULT_ADDRESS_FAMILIES, DEFAULT_MTU
+from .Constants import BEARER_MAPPINGS, DEFAULT_ADDRESS_FAMILIES, DEFAULT_BGP_AS, DEFAULT_BGP_ROUTE_TARGET, DEFAULT_MTU
 
 LOGGER = logging.getLogger(__name__)
 
@@ -64,7 +64,7 @@ def process_site_network_access(context_client : ContextClient, site_network_acc
         endpoint_id.endpoint_uuid.uuid = endpoint_uuid
 
     for config_rule in service.service_config.config_rules:                 # pylint: disable=no-member
-        if config_rule.resource_key != 'settings': continue
+        if config_rule.resource_key != '/settings': continue
         json_settings = json.loads(config_rule.resource_value)
 
         if 'mtu' not in json_settings:                                      # missing, add it
@@ -79,19 +79,33 @@ def process_site_network_access(context_client : ContextClient, site_network_acc
             msg = 'Specified AddressFamilies({:s}) differs from Service AddressFamilies({:s})'
             raise Exception(msg.format(str(json_settings['address_families']), str(DEFAULT_ADDRESS_FAMILIES)))
 
+        if 'bgp_as' not in json_settings:                                   # missing, add it
+            json_settings['bgp_as'] = DEFAULT_BGP_AS
+        elif json_settings['bgp_as'] != DEFAULT_BGP_AS:                     # differs, raise exception
+            msg = 'Specified BgpAs({:s}) differs from Service BgpAs({:s})'
+            raise Exception(msg.format(str(json_settings['bgp_as']), str(DEFAULT_BGP_AS)))
+
+        if 'bgp_route_target' not in json_settings:                         # missing, add it
+            json_settings['bgp_route_target'] = DEFAULT_BGP_ROUTE_TARGET
+        elif json_settings['bgp_route_target'] != DEFAULT_BGP_ROUTE_TARGET: # differs, raise exception
+            msg = 'Specified BgpRouteTarget({:s}) differs from Service BgpRouteTarget({:s})'
+            raise Exception(msg.format(str(json_settings['bgp_route_target']), str(DEFAULT_BGP_ROUTE_TARGET)))
+
         config_rule.resource_value = json.dumps(json_settings, sort_keys=True)
         break
     else:
         # not found, add it
         config_rule = service.service_config.config_rules.add()             # pylint: disable=no-member
         config_rule.action = ConfigActionEnum.CONFIGACTION_SET
-        config_rule.resource_key = 'settings'
+        config_rule.resource_key = '/settings'
         config_rule.resource_value = json.dumps({
-            'mtu': DEFAULT_MTU,
+            'mtu'             : DEFAULT_MTU,
             'address_families': DEFAULT_ADDRESS_FAMILIES,
+            'bgp_as'          : DEFAULT_BGP_AS,
+            'bgp_route_target': DEFAULT_BGP_ROUTE_TARGET,
         }, sort_keys=True)
 
-    endpoint_settings_key = 'device[{:s}]/endpoint[{:s}]/settings'.format(device_uuid, endpoint_uuid)
+    endpoint_settings_key = '/device[{:s}]/endpoint[{:s}]/settings'.format(device_uuid, endpoint_uuid)
     for config_rule in service.service_config.config_rules:                 # pylint: disable=no-member
         if config_rule.resource_key != endpoint_settings_key: continue
         json_settings = json.loads(config_rule.resource_value)
@@ -150,7 +164,6 @@ def process_site_network_access(context_client : ContextClient, site_network_acc
             'vlan_id': cvlan_id,
             'address_ip': address_ip,
             'address_prefix': address_prefix,
-
         }, sort_keys=True)
 
     return service
-- 
GitLab