Script to create all templates needed to run E2E NS Option2
[demo.git] / tutorials / 5GE2ENetworkSlicing / Option2 / template-creation.py
1 #!/usr/bin/env python3
2
3 # ============LICENSE_START===================================================
4 #  Copyright (C) 2021 Samsung Electronics
5 # ============================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=====================================================
20
21 from onapsdk.sdc.category_management import ServiceCategory
22 from onapsdk.sdc.service import Service
23 from onapsdk.sdc.properties import Property, ParameterError
24 from onapsdk.sdc.vf import Vf
25 from onapsdk.sdc.vsp import Vsp
26 from onapsdk.sdc.vfc import Vfc
27 from onapsdk.sdc.vendor import Vendor
28
29 from time import sleep
30 import onapsdk.constants as const
31 from onapsdk.exceptions import APIError, ResourceNotFound
32 import logging
33
34 vsp = Vsp("test1")
35 vsp.create()
36
37 logger = logging.getLogger("")
38 logger.setLevel(logging.DEBUG)
39 fh = logging.StreamHandler()
40 fh_formatter = logging.Formatter('%(asctime)s %(levelname)s %(lineno)d:%(filename)s(%(process)d) - %(message)s')
41 fh.setFormatter(fh_formatter)
42 logger.addHandler(fh)
43
44 SUFFIX = ''
45
46 def create_name(name, suffix=SUFFIX):
47     """ helper function to create uniqe name by appending
48         predefined suffix, may be helpful during testing
49         as ONAP does not allow for easy deeltion of services"""
50     return name + suffix
51
52
53 def create_service_category(category_names):
54     for cn in category_names:
55         logger.info('creating service category [%s]', cn)
56         ServiceCategory.create(name=cn)
57
58
59 def create_vendor(vendor_name):
60     vendor = Vendor(vendor_name)
61     vendor.create()
62     try:
63         vendor.onboard()
64     except APIError as e:
65         logger.warn("Exception during vendor onboarding, ", e)
66         raise e
67
68     return vendor
69
70
71 def create_vsp(name, vendor, onboard=False):
72     logger.info("creating vsp: [%s:%s]", name, vendor)
73     retry = 0
74     done = False
75
76     vsp = Vsp(name=name, vendor=vendor)
77     if onboard:
78         while not done:
79             try:
80                 vsp.create()
81                 vsp.onboard()
82             except ResourceNotFound as e:
83                 logger.warn(f"Failed to onboard {name}", e)
84                 retry = retry + 1
85                 if retry >= 5:
86                     raise e
87             except APIError as e:
88                 logger.warn("Exception during vsp onboarding, ", e)
89                 raise e
90             else:
91                 done = True
92
93     return vsp
94
95
96 def create_vf(name, category, vendor, onboard=False):
97     logger.info("create vf: [%s:%s]", name, category)
98
99     vfc = Vfc('AllottedResource')  # seemd incorrect
100     vf = Vf(name=name, category=category, vendor=vendor)
101     vf.create()
102     if vf.status == const.DRAFT:
103         vf.add_resource(vfc)
104         if onboard:
105             onboard_vf(vf)
106     return vf
107
108
109 def onboard_vf(vf):
110     retry = 0
111     done = False
112     to = 2
113
114     while not done:
115         try:
116             vf.onboard()
117         except ResourceNotFound as e:
118             retry += 1
119             if retry > 5:
120                 raise e
121             sleep(to)
122             to = 2 * to + 1
123         else:
124             done = True
125     logger.info("onboarded vf: [%s]", vf.name)
126
127
128 def create_service(name, category, vnfs=[], properties=[], role=None, service_type=None):
129     logger.info("create service: [%s:%s]", name, category)
130     retry = 0
131     done = False
132
133     srvc = Service(name=name, category=category, properties=properties, role=role, service_type=service_type)
134     srvc.create()
135
136     while not done:
137         try:
138             if srvc.status == const.DRAFT:
139                 for vnf in vnfs:
140                     srvc.add_resource(vnf)
141
142             if srvc.status != const.DISTRIBUTED:
143                 srvc.onboard()
144         except ResourceNotFound as e:
145             retry += 1
146             if retry > 5:
147                 raise e
148         else:
149             done = True
150
151     return srvc
152
153 def create_service_1(name, category, vnfs=[], properties=[], role=None, service_type=None):
154     logger.info("create service: [%s:%s]", name, category)
155     retry = 0
156     done = False
157
158     srvc = Service(name=name, category=category, properties=properties, role=role, service_type=service_type)
159     srvc.create()
160
161     while not done:
162         try:
163             if srvc.status == const.DRAFT:
164                 for vnf in vnfs:
165                     srvc.add_resource(vnf)
166                     for c in srvc.components:
167                         if c.name == 'SLICE_AR 0':
168                             cp = get_component_property(c, 'allottedresource0_providing_service_invariant_uuid')
169                             if cp:
170                                 logger.info('setting value on property [%s]', cp)
171                                 cp.value = "{\\\"get_input\\\":\\\"slice_ar0_allottedresource0_providing_service_invariant_uuid\\\"}"
172                             else:
173                                 raise ParameterError('no property providing_service_invariant_uuid found')
174
175                             cp = get_component_property(c, 'allottedresource0_providing_service_uuid')
176                             if cp:
177                                 cp.value = "{\\\"get_input\\\":\\\"slice_ar0_allottedresource0_providing_service_uuid\\\"}"
178                             else:
179                                 raise ParameterError('no property providing_service_uuid found')
180
181                     break
182             if srvc.status != const.DISTRIBUTED:
183                 srvc.onboard()
184         except ResourceNotFound as e:
185             retry += 1
186             if retry > 5:
187                 raise e
188         else:
189             done = True
190
191     return srvc
192
193
194 def get_component_property(component, name):
195     prop = None
196     try:
197         prop = list(filter(lambda x: x.name == name, component.properties))
198         if prop:
199             prop = prop[0]
200         else:
201             raise ParameterError('no property found')
202     except ParameterError as e:
203         logger.warn("component [%s] has no property [%s]", component.name, name)
204         raise e
205
206     logger.info("retrived property [%s] for component [%s]", prop.name if prop else 'null', component.name)
207
208     return prop
209
210
211 vendor = create_vendor('aaaa')
212 vsp = create_vsp('test1', vendor)
213
214 # create custom categories
215 create_service_category(['CST', 'ServiceProfile', 'AN SliceProfile', 'AN NF Slice Profile',
216                          'CN SliceProfile', 'TN SliceProfile', 'NST', 'TN BH NSST', 'Alloted Resource',
217                          'TN MH NSST', 'TN FH NSST', 'TN Network Requirement', 'AN NF NSST', 'CN NSST', 'AN NSST'])
218
219 props = [Property('ConnectionLink', 'string', value='{\\"get_input\\":\\"ConnectionLink\\"}'),
220          Property('jitter', 'string', value='10'),
221          Property('latency', 'integer', value=10),
222          Property('maxBandwith', 'integer', value=1000)]
223
224 srv = create_service(create_name('TN_Network_Requirement'), 'TN Network Requirement', properties=props)
225 vf = create_vf(create_name('TN_Network_Req_AR'), 'Alloted Resource', vendor)
226 for c in vf.components:
227     if c.name == 'AllottedResource 0':
228         c.get_property('providing_service_invariant_uuid').value = srv.unique_uuid
229         c.get_property('providing_service_uuid').value = srv.identifier
230         c.get_property('providing_service_name').value = srv.name
231         break
232 onboard_vf(vf)
233
234 props = [Property('pLMNIdList', 'string', value='39-00'),
235         Property('jitter', 'string', value='10'),
236         Property('latency', 'integer', value=10),
237         Property('maxBandwith', 'integer', value=1000)]
238
239 # 3
240 srv = create_service(create_name('Tn_ONAP_internal_BH'), 'TN BH NSST', vnfs=[vf], role='ONAP_internal',
241                     properties=props)
242 # 6
243 vf_tn_bh_ar = create_vf(create_name('Tn_BH_AR'), 'Alloted Resource', vendor)
244 for c in vf_tn_bh_ar.components:
245     if c.name == 'AllottedResource 0':
246         c.get_property('providing_service_invariant_uuid').value = srv.unique_uuid
247         c.get_property('providing_service_uuid').value = srv.identifier
248         c.get_property('providing_service_name').value = srv.name
249         break
250 onboard_vf(vf_tn_bh_ar)
251
252 # 4
253 props = [Property('anNSSCap', 'org.openecomp.datatypes.NSSCapabilities')]
254 srv = create_service(create_name('EmbbAn_NF'), 'AN NF NSST', role='huawei', service_type='embb', properties=props)
255 # 7
256 vf_embban_nf_ar = create_vf(create_name('EmbbAn_NF_AR'), 'Alloted Resource', vendor)
257 for c in vf_embban_nf_ar.components:
258     if c.name == 'AllottedResource 0':
259         c.get_property('providing_service_invariant_uuid').value = srv.unique_uuid
260         c.get_property('providing_service_uuid').value = srv.identifier
261         c.get_property('providing_service_name').value = srv.name
262         break
263
264 onboard_vf(vf_embban_nf_ar)
265
266 # 5
267 props = [Property('activityFactor', 'org.openecomp.datatypes.NSSCapabilities', '30'),
268         Property('areaTrafficCapDL', 'org.openecomp.datatypes.NSSCapabilities', '800'),
269         Property('areaTrafficCapUL', 'org.openecomp.datatypes.NSSCapabilities', '800'),
270         Property('expDataRateDL', 'org.openecomp.datatypes.NSSCapabilities', '1000'),
271         Property('expDataReateUL', 'org.openecomp.datatypes.NSSCapabilities', '1000')]
272
273 srv = Service(name=create_name('EmbbCn_External'),
274             category='CN NSST',
275             role='huawei',
276             service_type='embb',
277             properties=[Property('aname', 'org.openecomp.datatypes.NSSCapabilities',
278                                 value="[{\\\"latency\\\":35,\\\"maxNumberofUEs\\\":1005,\\\"resourceSharingLevel\\\":\\\"Shared\\\",\\\"sST\\\":\\\"eMBB\\\",\\\"activityFactor\\\":30,\\\"areaTrafficCapDL\\\":800,\\\"areaTrafficCapUL\\\":1000}]")])
279
280 srv.create()
281
282 if srv.status == const.DRAFT:
283     srv.add_deployment_artifact(artifact_type="WORKFLOW", artifact_name="eMBB.zip", artifact="./eMBB.zip",
284                                 artifact_label="abc")
285
286 if srv.status != const.DISTRIBUTED:
287     done = False
288     retry = 0
289     to = 1
290     while not done:
291         try:
292             srv.onboard()
293         except ResourceNotFound as e:
294             retry += 1
295             if retry > 5:
296                 raise e
297             to = 2 * to + 1
298             sleep(to)
299         else:
300             done = True
301
302
303 # 8
304 vf_embbcn_external_ar = create_vf(create_name('EmbbCn_External_AR'), 'Alloted Resource', vendor)
305 for c in vf_embbcn_external_ar.components:
306     if c.name == 'AllottedResource 0':
307         c.get_property('providing_service_invariant_uuid').value = srv.unique_uuid
308         c.get_property('providing_service_uuid').value = srv.identifier
309         c.get_property('providing_service_name').value = srv.name
310         break
311 onboard_vf(vf_embbcn_external_ar)
312
313 logger.info("create service EmbbNst_O2")
314 # 9
315 srv = create_service(create_name('EmbbNst_O2'),
316                      'NST',
317                      role='option2',
318                      vnfs = [vf_embbcn_external_ar, vf_embban_nf_ar, vf_tn_bh_ar],
319                      properties=[Property('aname', 'org.openecomp.datatypes.NSSCapabilities',
320                                 value="[{\\\"latency\\\":20,\
321                                             \\\"maxNumberofUEs\\\":1000,\
322                                             \\\"maxNumberofConns\\\":100000,\
323                                             \\\"resourceSharingLevel\\\":\\\"Shared\\\",\
324                                             \\\"sST\\\":\\\"eMBB\\\",\
325                                             \\\"activityFactor\\\":60,\
326                                             \\\"availability\\\":0.6,\
327                                             \\\"dLThptPerSlice\\\":1000,\
328                                             \\\"uLThptPerSlice\\\":1000,\
329                                             \\\"jitter\\\":10,\
330                                             \\\"survivalTime\\\":10,\
331                                             \\\"ueMobilityLevel\\\":\\\"stationary\\\",\
332                                             \\\"pLMNIdList\\\":\\\"39-00\\\",\
333                                             \\\"reliability\\\":\\\"99%\\\"}]")])
334
335 # 10
336 logger.info("create Slice_AR")
337 vf = create_vf(create_name('Slice_AR'), 'Allotted Resource', vendor)
338 for c in vf.components:
339     if c.name == 'AllottedResource 0':
340         cp = get_component_property(c, 'providing_service_invariant_uuid')
341         if cp:
342             logger.info('setting value on property [%s]', cp)
343             cp.value = "{\\\"get_input\\\":\\\"allottedresource0_providing_service_invariant_uuid\\\"}"
344         else:
345             raise ParameterError('no property providing_service_invariant_uuid found')
346
347         cp = get_component_property(c, 'providing_service_uuid')
348         if cp:
349             cp.value = "{\\\"get_input\\\":\\\"allottedresource0_providing_service_uuid\\\"}"
350         else:
351             raise ParameterError('no property providing_service_uuid found')
352
353         break
354 onboard_vf(vf)
355
356 # 11
357 slice_profile = '[{' \
358                 '\\"activityFactor\\":{\\"get_input\\":\\"anSP_activityFactor\\"},' \
359                 '\\"areaTrafficCapDL\\":{\\"get_input\\":\\"anSP_areaTrafficCapDL\\"},' \
360                 '\\"areaTrafficCapUL\\":{\\"get_input\\":\\"anSP_areaTrafficCapUL\\"},' \
361                 '\\"cSAvailabilityTarget\\":{\\"get_input\\":\\"anSP_cSAvailabilityTarget\\"},' \
362                 '\\"cSRealibilityMeanTime\\":{\\"get_input\\":\\"anSP_cSRealibilityMeanTime\\"}}]'
363 an_slice_profile = [Property('anSP', 'org.openecomp.datatypes.SliceProfile', slice_profile),
364                     Property('ipAddress', 'string', '{\\"get_input\\":\\"ipAddress\\"}'),
365                     Property('logicInterfaceId', 'string', '{\\"get_input\\":\\"logicInterfaceId\\"}'),
366                     Property('nextHopInfo', 'string', '{\\"get_input\\":\\"nextHopInfo\\"}')]
367
368 logger.info("create service Slice Profile AN O2")
369 srv_slice_profile_an_o2 = create_service_1(create_name('SliceProfile_AN_O2'),
370                                   'AN SliceProfile',
371                                   properties = an_slice_profile,
372                                   vnfs = [vf])
373
374 # 12
375 logger.info('create service SliceProfile_TN')
376 tn_slice_profile = [Property('jitter', 'string', '{\\"get_input\\":\\"jitter\\"}'),
377                     Property('latency', 'integer', '{\\"get_input\\":\\"latency\\"}'),
378                     Property('pLMNIdList', 'string', '{\\"get_input\\":\\"pLMNIdList\\"}'),
379                     Property('sNSSAI', 'string', '{\\"get_input\\":\\"sNSSAI\\"}'),
380                     Property('sST', 'integer', '{\\"get_input\\":\\"sST\\"}'),
381                     Property('maxBandwidth', 'integer', '{\\"get_input\\":\\"maxBandwidth\\"}')]
382
383
384 srv_slice_profile_tn = create_service_1(create_name('SliceProfile_TN'),
385                                       'TN SliceProfile',
386                                       vnfs = [vf],
387                                       properties = tn_slice_profile)
388
389 # 13
390 logger.info('create slice SliceProfile_CN')
391 cn_slice_profile = [Property('ipAddress', 'string', '{\\"get_input\\":\\"ipAddress\\"}'),
392                     Property('logicInterfaceId', 'string', '{\\"get_input\\":\\"logicInterfaceId\\"}'),
393                     Property('nextHopInfo', 'string', '{\\"get_input\\":\\"nextHopInfo\\"}')]
394 srv_slice_profile_cn = create_service_1(create_name('SliceProfile_CN'),
395                                       'CN SliceProfile',
396                                       vnfs = [ vf ],
397                                       properties = cn_slice_profile)
398
399 # 14
400 logger.info('create service ServiceProfile O2')
401 service_profile = '[{' \
402                 '\\"resourceSharingLevel\\":{\\"get_input\\":\\"spProp_resourceSharingLevel\\"},' \
403                 '\\"sNSSAI\\":{\\"get_input\\":\\"spProp_sNSSAI\\"},' \
404                 '\\"coverageAreaTAList\\":{\\"get_input\\":\\"spProp_coverageAreaTAList\\"},' \
405                 '\\"sST\\":{\\"get_input\\":\\"spProp_sST\\"},' \
406                 '\\"dLThptPerUE\\":{\\"get_input\\":\\"spProp_dLThptPerUE\\"},' \
407     '\\"uEMobilityLevel\\":{\\"get_input\\":\\"spProp_uEMobilityLevel\\"},'\
408     '\\"latency\\":{\\"get_input\\":\\"spProp_latency\\"},' \
409     '\\"uLThptPerUE\\":{\\"get_input\\":\\"spProp_uLThptPerUE\\"},' \
410     '\\"maxNumberofUEs\\":{\\"get_input\\":\\"spProp_maxNumberofUEs\\"}' \
411     '}]'
412 service_props = [Property('spProp', 'org.openecomp.datatypes.ServiceProfile', slice_profile)]
413 srv_profile_o2 = create_service_1(create_name('ServiceProfile_O2'),
414                                 'ServiceProfile',
415                                 properties=service_props,
416                                 vnfs = [vf, srv_slice_profile_cn, srv_slice_profile_tn, srv_slice_profile_an_o2],
417                                 role = 'option2')
418
419
420 # 15
421 logger.info('create service CST O2')
422 props = '[{' \
423                  '\\"coverageAreaList\\":{\\"get_input\\":\\"csProp_coverageAreaList\\"},' \
424                 '\\"expDataRateDL\\":{\\"get_input\\":\\"csProp_expDataRateDL\\"},' \
425                 '\\"expDataRateUL\\":{\\"get_input\\":\\"csProp_expDataRateUL\\"},' \
426                 '\\"latency\\":{\\"get_input\\":\\"csProp_latency\\"},' \
427                 '\\"maxNumberofUEs\\":{\\"get_input\\":\\"csProp_maxNumberofEUs\\"},' \
428                 '\\"resourceSharingLevel\\":{\\"get_input\\":\\"csProp_resourceSharingLevel\\"},' \
429                 '\\"uEMobilityLevel\\":{\\"get_input\\":\\"csProp_uEMobilityLevel\\"},' \
430                 '\\"useInterval\\":{\\"get_input\\":\\"csProp_useInterval\\"},' \
431         '}]'
432 srv = create_service_1(create_name('CST_O2'),
433                      'CST',
434                      role = 'option2',
435                      service_type = 'embb',
436                      vnfs = [srv_profile_o2],
437                      properties = [Property('csProp', 'org.openecomp.datatypes.CSProperties', props)])