Newer
Older
# Copyright 2022-2025 ETSI SDG TeraFlowSDN (TFS) (https://tfs.etsi.org/)
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This file includes original contributions from Telefonica Innovación Digital S.L.
from datetime import datetime
from src.helpers import tfs_connector, cisco_connector
from src.Constants import DEFAULT_LOGGING_LEVEL, TFS_IP, TFS_L2VPN_SUPPORT, IXIA_IP, SRC_PATH, TEMPLATES_PATH, DUMMY_MODE, DUMP_TEMPLATES, PLANNER_ENABLED, NRP_ENABLED, UPLOAD_TYPE, NBI_L2_PATH, NBI_L3_PATH
# Configure logging to provide clear and informative log messages
logging.basicConfig(
level=DEFAULT_LOGGING_LEVEL,
format='%(levelname)s - %(message)s')
class NSController:
"""
Network Slice Controller (NSC) - A class to manage network slice creation,
modification, and deletion across different network domains.
This controller handles the translation, mapping, and realization of network
slice intents from different formats (3GPP and IETF) to network-specific
configurations.
Key Functionalities:
- Intent Processing: Translate and process network slice intents
- Slice Management: Create, modify, and delete network slices
- NRP (Network Resource Partition) Mapping: Match slice requirements with available resources
- Slice Realization: Convert intents to specific network configurations (L2VPN, L3VPN)
"""
def __init__(self, controller_type = "TFS", tfs_ip=TFS_IP, ixia_ip =IXIA_IP, need_l2vpn_support=TFS_L2VPN_SUPPORT):
"""
Initialize the Network Slice Controller.
Args:
controller_type (str): Flag to determine if configurations
should be uploaded to Teraflow or IXIA system.
need_l2vpn_support (bool, optional): Flag to determine if additional
L2VPN configuration support is required. Defaults to False.
Attributes:
answer (dict): Stores slice creation responses
start_time (float): Tracks slice setup start time
end_time (float): Tracks slice setup end time
need_l2vpn_support (bool): Flag for additional L2VPN configuration support
"""
self.path = ""
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
self.answer = {}
self.cool_answer = {}
self.start_time = 0
self.end_time = 0
self.setup_time = 0
self.need_l2vpn_support = need_l2vpn_support
# Internal templates and views
self.__gpp_template = ""
self.__ietf_template = ""
self.__teraflow_template = ""
self.__nrp_view = ""
self.subnet=""
# API Methods
def add_flow(self, intent):
"""
Create a new transport network slice.
Args:
intent (dict): Network slice intent in 3GPP or IETF format
Returns:
Result of the Network Slice Controller (NSC) operation
API Endpoint:
POST /slice
Raises:
ValueError: If no transport network slices are found
Exception: For unexpected errors during slice creation process
"""
return self.nsc(intent)
def get_flows(self,slice_id=None):
"""
Retrieve transport network slice information.
This method allows retrieving:
- All transport network slices
- A specific slice by its ID
Args:
slice_id (str, optional): Unique identifier of a specific slice.
Defaults to None.
Returns:
dict or list:
- If slice_id is provided: Returns the specific slice details
- If slice_id is None: Returns a list of all slices
- Returns an error response if no slices are found
API Endpoint:
GET /slice/{id}
Raises:
ValueError: If no transport network slices are found
Exception: For unexpected errors during file processing
"""
try:
# Read slice database from JSON file
with open(os.path.join(SRC_PATH, "slice_ddbb.json"), 'r') as file:
content = json.load(file)
# If specific slice ID is provided, find and return matching slice
if slice_id:
for slice in content:
if slice["slice_id"] == slice_id:
return slice
# If no slices exist, raise an error
if len(content) == 0:
raise ValueError("Transport network slices not found")
# Return all slices if no specific ID is given
return [slice for slice in content if slice.get("controller") == self.controller_type]
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
except ValueError as e:
# Handle case where no slices are found
return self.__send_response(False, code=404, message=str(e))
except Exception as e:
# Handle unexpected errors
return self.__send_response(False, code=500, message=str(e))
def modify_flow(self,slice_id, intent):
"""
Modify an existing transport network slice.
Args:
slice_id (str): Unique identifier of the slice to modify
intent (dict): New intent configuration for the slice
Returns:
Result of the Network Slice Controller (NSC) operation
API Endpoint:
PUT /slice/{id}
"""
return self.nsc(intent, slice_id)
def delete_flows(self, slice_id=None):
"""
Delete transport network slice(s).
This method supports:
- Deleting a specific slice by ID
- Deleting all slices
- Optional cleanup of L2VPN configurations
Args:
slice_id (str, optional): Unique identifier of slice to delete.
Defaults to None.
Returns:
dict: Response indicating successful deletion or error details
API Endpoint:
DELETE /slice/{id}
Raises:
ValueError: If no slices are found to delete
Exception: For unexpected errors during deletion process
Notes:
- If controller_type is TFS, attempts to delete from Teraflow
- If need_l2vpn_support is True, performs additional L2VPN cleanup
"""
try:
# Read current slice database
with open(os.path.join(SRC_PATH, "slice_ddbb.json"), 'r') as file:
content = json.load(file)
id = None
# Delete specific slice if slice_id is provided
if slice_id:
for i, slice in enumerate(content):
if slice["slice_id"] == slice_id and slice.get("controller") == self.controller_type:
Loading
Loading full blame...