Merge "CLI for OSM"
[osm/SO.git] / examples / ping_pong_ns / rift / mano / examples / ping_pong_nsd.py
1 #!/usr/bin/env python3
2
3 #
4 # Copyright 2016 RIFT.IO Inc
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
19
20 import argparse
21 import os
22 import shutil
23 import sys
24 import uuid
25
26 import gi
27 gi.require_version('RwYang', '1.0')
28 gi.require_version('RwVnfdYang', '1.0')
29 gi.require_version('VnfdYang', '1.0')
30 gi.require_version('RwNsdYang', '1.0')
31 gi.require_version('NsdYang', '1.0')
32
33
34 from gi.repository import (
35 RwNsdYang,
36 NsdYang,
37 RwVnfdYang,
38 VnfdYang,
39 RwYang,
40 )
41
42
43 try:
44 import rift.mano.config_data.config as config_data
45 except ImportError:
46 # Load modules from common which are not yet installed
47 path = os.path.abspath(os.path.dirname(os.path.abspath(__file__)) + "../../../common/python/rift/mano")
48 sys.path.append(path)
49
50 import config_data.config as config_data
51
52
53 NUM_PING_INSTANCES = 1
54 MAX_VNF_INSTANCES_PER_NS = 10
55 use_epa = False
56 aws = False
57 pingcount = NUM_PING_INSTANCES
58 use_ping_cloud_init_file = ""
59 use_pong_cloud_init_file = ""
60
61 PING_USERDATA_FILE = '''#cloud-config
62 password: fedora
63 chpasswd: { expire: False }
64 ssh_pwauth: True
65 runcmd:
66 - [ systemctl, daemon-reload ]
67 - [ systemctl, enable, ping.service ]
68 - [ systemctl, start, --no-block, ping.service ]
69 - [ ifup, eth1 ]
70 '''
71
72 PONG_USERDATA_FILE = '''#cloud-config
73 password: fedora
74 chpasswd: { expire: False }
75 ssh_pwauth: True
76 runcmd:
77 - [ systemctl, daemon-reload ]
78 - [ systemctl, enable, pong.service ]
79 - [ systemctl, start, --no-block, pong.service ]
80 - [ ifup, eth1 ]
81 '''
82
83
84 class UnknownVNFError(Exception):
85 pass
86
87
88 class ManoDescriptor(object):
89 def __init__(self, name):
90 self.name = name
91 self.descriptor = None
92
93 def write_to_file(self, module_list, outdir, output_format):
94 model = RwYang.Model.create_libncx()
95 for module in module_list:
96 model.load_module(module)
97
98 if output_format == 'json':
99 with open('%s/%s.json' % (outdir, self.name), "w") as fh:
100 fh.write(self.descriptor.to_json(model))
101 elif output_format.strip() == 'xml':
102 with open('%s/%s.xml' % (outdir, self.name), "w") as fh:
103 fh.write(self.descriptor.to_xml_v2(model))
104 elif output_format.strip() == 'yaml':
105 with open('%s/%s.yaml' % (outdir, self.name), "w") as fh:
106 fh.write(self.descriptor.to_yaml(model))
107 else:
108 raise Exception("Invalid output format for the descriptor")
109
110 def get_json(self, module_list):
111 model = RwYang.Model.create_libncx()
112 for module in module_list:
113 model.load_module(module)
114 print(self.descriptor.to_json(model))
115
116
117 class VirtualNetworkFunction(ManoDescriptor):
118 def __init__(self, name, instance_count=1):
119 self.vnfd_catalog = None
120 self.vnfd = None
121 self.instance_count = instance_count
122 self._placement_groups = []
123 super(VirtualNetworkFunction, self).__init__(name)
124
125 def add_placement_group(self, group):
126 self._placement_groups.append(group)
127
128 def compose(self, image_name, cloud_init="", cloud_init_file="", endpoint=None, mon_params=[],
129 mon_port=8888, mgmt_port=8888, num_vlr_count=1, num_ivlr_count=1,
130 num_vms=1, image_md5sum=None, mano_ut=False):
131 self.descriptor = RwVnfdYang.YangData_Vnfd_VnfdCatalog()
132 self.id = str(uuid.uuid1())
133 vnfd = self.descriptor.vnfd.add()
134 vnfd.id = self.id
135 vnfd.name = self.name
136 vnfd.short_name = self.name
137 vnfd.vendor = 'RIFT.io'
138 vnfd.logo = 'rift_logo.png'
139 vnfd.description = 'This is an example RIFT.ware VNF'
140 vnfd.version = '1.0'
141
142 self.vnfd = vnfd
143
144 if mano_ut is True:
145 internal_vlds = []
146 for i in range(num_ivlr_count):
147 internal_vld = vnfd.internal_vld.add()
148 internal_vld.id = 'ivld%s' % i
149 internal_vld.name = 'fabric%s' % i
150 internal_vld.short_name = 'fabric%s' % i
151 internal_vld.description = 'Virtual link for internal fabric%s' % i
152 internal_vld.type_yang = 'ELAN'
153 internal_vlds.append(internal_vld)
154
155 for i in range(num_vlr_count):
156 cp = vnfd.connection_point.add()
157 cp.type_yang = 'VPORT'
158 cp.name = '%s/cp%d' % (self.name, i)
159
160 if endpoint is not None:
161 endp = VnfdYang.YangData_Vnfd_VnfdCatalog_Vnfd_HttpEndpoint(
162 path=endpoint, port=mon_port, polling_interval_secs=2
163 )
164 vnfd.http_endpoint.append(endp)
165
166 # Monitoring params
167 for monp_dict in mon_params:
168 monp = VnfdYang.YangData_Vnfd_VnfdCatalog_Vnfd_MonitoringParam.from_dict(monp_dict)
169 monp.http_endpoint_ref = endpoint
170 vnfd.monitoring_param.append(monp)
171
172
173 for i in range(num_vms):
174 # VDU Specification
175 vdu = vnfd.vdu.add()
176 vdu.id = 'iovdu_%s' % i
177 vdu.name = 'iovdu_%s' % i
178 vdu.count = 1
179 # vdu.mgmt_vpci = '0000:00:20.0'
180
181 # specify the VM flavor
182 if use_epa:
183 vdu.vm_flavor.vcpu_count = 4
184 vdu.vm_flavor.memory_mb = 1024
185 vdu.vm_flavor.storage_gb = 4
186 else:
187 vdu.vm_flavor.vcpu_count = 1
188 vdu.vm_flavor.memory_mb = 512
189 vdu.vm_flavor.storage_gb = 4
190
191 # Management interface
192 mgmt_intf = vnfd.mgmt_interface
193 mgmt_intf.vdu_id = vdu.id
194 mgmt_intf.port = mgmt_port
195 mgmt_intf.dashboard_params.path = endpoint
196 mgmt_intf.dashboard_params.port = mgmt_port
197
198 if cloud_init_file and len(cloud_init_file):
199 vdu.cloud_init_file = cloud_init_file
200 else:
201 vdu.cloud_init = cloud_init
202 if aws:
203 vdu.cloud_init += " - [ systemctl, restart, --no-block, elastic-network-interfaces.service ]\n"
204
205 # sepcify the guest EPA
206 if use_epa:
207 vdu.guest_epa.trusted_execution = False
208 vdu.guest_epa.mempage_size = 'LARGE'
209 vdu.guest_epa.cpu_pinning_policy = 'DEDICATED'
210 vdu.guest_epa.cpu_thread_pinning_policy = 'PREFER'
211 vdu.guest_epa.numa_node_policy.node_cnt = 2
212 vdu.guest_epa.numa_node_policy.mem_policy = 'STRICT'
213
214 node = vdu.guest_epa.numa_node_policy.node.add()
215 node.id = 0
216 node.memory_mb = 512
217 node.vcpu = [0, 1]
218
219 node = vdu.guest_epa.numa_node_policy.node.add()
220 node.id = 1
221 node.memory_mb = 512
222 node.vcpu = [2, 3]
223
224 # specify the vswitch EPA
225 vdu.vswitch_epa.ovs_acceleration = 'DISABLED'
226 vdu.vswitch_epa.ovs_offload = 'DISABLED'
227
228 # Specify the hypervisor EPA
229 vdu.hypervisor_epa.type_yang = 'PREFER_KVM'
230
231 # Specify the host EPA
232 # vdu.host_epa.cpu_model = 'PREFER_SANDYBRIDGE'
233 # vdu.host_epa.cpu_arch = 'PREFER_X86_64'
234 # vdu.host_epa.cpu_vendor = 'PREFER_INTEL'
235 # vdu.host_epa.cpu_socket_count = 2
236 # vdu.host_epa.cpu_core_count = 8
237 # vdu.host_epa.cpu_core_thread_count = 2
238 # vdu.host_epa.cpu_feature = ['PREFER_AES', 'REQUIRE_VME', 'PREFER_MMX','REQUIRE_SSE2']
239
240 if aws:
241 vdu.image = 'rift-ping-pong'
242 else:
243 vdu.image = image_name
244 if image_md5sum is not None:
245 vdu.image_checksum = image_md5sum
246
247 if mano_ut is True:
248 for i in range(num_ivlr_count):
249 internal_cp = vdu.internal_connection_point.add()
250 if vnfd.name.find("ping") >= 0:
251 cp_name = "ping"
252 else:
253 cp_name = "pong"
254 internal_cp.name = cp_name + "/icp{}".format(i)
255 internal_cp.id = cp_name + "/icp{}".format(i)
256 internal_cp.type_yang = 'VPORT'
257 internal_vlds[i].internal_connection_point_ref.append(internal_cp.id)
258
259 internal_interface = vdu.internal_interface.add()
260 internal_interface.name = 'fab%d' % i
261 internal_interface.vdu_internal_connection_point_ref = internal_cp.id
262 internal_interface.virtual_interface.type_yang = 'VIRTIO'
263
264 # internal_interface.virtual_interface.vpci = '0000:00:1%d.0'%i
265
266 for i in range(num_vlr_count):
267 external_interface = vdu.external_interface.add()
268 external_interface.name = 'eth%d' % i
269 external_interface.vnfd_connection_point_ref = '%s/cp%d' % (self.name, i)
270 if use_epa:
271 external_interface.virtual_interface.type_yang = 'VIRTIO'
272 else:
273 external_interface.virtual_interface.type_yang = 'VIRTIO'
274 # external_interface.virtual_interface.vpci = '0000:00:2%d.0'%i
275
276 for group in self._placement_groups:
277 placement_group = vnfd.placement_groups.add()
278 placement_group.name = group.name
279 placement_group.requirement = group.requirement
280 placement_group.strategy = group.strategy
281 if group.vdu_list:
282 ### Add specific VDUs to placement group
283 for vdu in group.vdu_list:
284 member_vdu = placement_group.member_vdus.add()
285 member_vdu.member_vdu_ref = vdu.id
286 else:
287 ### Add all VDUs to placement group
288 for vdu in vnfd.vdu:
289 member_vdu = placement_group.member_vdus.add()
290 member_vdu.member_vdu_ref = vdu.id
291
292
293 def write_to_file(self, outdir, output_format):
294 dirpath = "%s/%s" % (outdir, self.name)
295 if not os.path.exists(dirpath):
296 os.makedirs(dirpath)
297 super(VirtualNetworkFunction, self).write_to_file(['vnfd', 'rw-vnfd'],
298 dirpath,
299 output_format)
300 self.add_scripts(outdir)
301
302 def add_scripts(self, outdir):
303 script_dir = os.path.join(outdir, self.name, 'cloud_init')
304 try:
305 os.makedirs(script_dir)
306 except OSError:
307 if not os.path.isdir(script_dir):
308 raise
309
310 if 'ping' in self.name:
311 script_file = os.path.join(script_dir, 'ping_cloud_init.cfg')
312 cfg = PING_USERDATA_FILE
313 else:
314 script_file = os.path.join(script_dir, 'pong_cloud_init.cfg')
315 cfg = PONG_USERDATA_FILE
316
317 with open(script_file, "w") as f:
318 f.write("{}".format(cfg))
319
320
321 class NetworkService(ManoDescriptor):
322 def __init__(self, name):
323 super(NetworkService, self).__init__(name)
324 self._scale_groups = []
325 self.vnfd_config = {}
326 self._placement_groups = []
327
328 def ping_config(self, mano_ut, use_ns_init_conf):
329 suffix = ''
330 if mano_ut:
331 ping_cfg = r'''
332 #!/bin/bash
333
334 echo "!!!!!!!! Executed ping Configuration !!!!!!!!!"
335 '''
336 else:
337 ping_cfg = r'''
338 #!/bin/bash
339
340 # Rest API config
341 ping_mgmt_ip='<rw_mgmt_ip>'
342 ping_mgmt_port=18888
343
344 # VNF specific configuration
345 pong_server_ip='<rw_connection_point_name pong_vnfd%s/cp0>'
346 ping_rate=5
347 server_port=5555
348
349 # Make rest API calls to configure VNF
350 curl -D /dev/stdout \
351 -H "Accept: application/vnd.yang.data+xml" \
352 -H "Content-Type: application/vnd.yang.data+json" \
353 -X POST \
354 -d "{\"ip\":\"$pong_server_ip\", \"port\":$server_port}" \
355 http://${ping_mgmt_ip}:${ping_mgmt_port}/api/v1/ping/server
356 rc=$?
357 if [ $rc -ne 0 ]
358 then
359 echo "Failed to set server info for ping!"
360 exit $rc
361 fi
362
363 curl -D /dev/stdout \
364 -H "Accept: application/vnd.yang.data+xml" \
365 -H "Content-Type: application/vnd.yang.data+json" \
366 -X POST \
367 -d "{\"rate\":$ping_rate}" \
368 http://${ping_mgmt_ip}:${ping_mgmt_port}/api/v1/ping/rate
369 rc=$?
370 if [ $rc -ne 0 ]
371 then
372 echo "Failed to set ping rate!"
373 exit $rc
374 fi
375
376 ''' % suffix
377 if use_ns_init_conf:
378 ping_cfg += "exit 0\n"
379 else:
380 ping_cfg +='''
381 output=$(curl -D /dev/stdout \
382 -H "Accept: application/vnd.yang.data+xml" \
383 -H "Content-Type: application/vnd.yang.data+json" \
384 -X POST \
385 -d "{\"enable\":true}" \
386 http://${ping_mgmt_ip}:${ping_mgmt_port}/api/v1/ping/adminstatus/state)
387 if [[ $output == *"Internal Server Error"* ]]
388 then
389 echo $output
390 exit 3
391 else
392 echo $output
393 fi
394
395 exit 0
396 '''
397 return ping_cfg
398
399 def pong_config(self, mano_ut, use_ns_init_conf):
400 suffix = ''
401 if mano_ut:
402 pong_cfg = r'''
403 #!/bin/bash
404
405 echo "!!!!!!!! Executed pong Configuration !!!!!!!!!"
406 '''
407 else:
408 pong_cfg = r'''
409 #!/bin/bash
410
411 # Rest API configuration
412 pong_mgmt_ip='<rw_mgmt_ip>'
413 pong_mgmt_port=18889
414 # username=<rw_username>
415 # password=<rw_password>
416
417 # VNF specific configuration
418 pong_server_ip='<rw_connection_point_name pong_vnfd%s/cp0>'
419 server_port=5555
420
421 # Make Rest API calls to configure VNF
422 curl -D /dev/stdout \
423 -H "Accept: application/vnd.yang.data+xml" \
424 -H "Content-Type: application/vnd.yang.data+json" \
425 -X POST \
426 -d "{\"ip\":\"$pong_server_ip\", \"port\":$server_port}" \
427 http://${pong_mgmt_ip}:${pong_mgmt_port}/api/v1/pong/server
428 rc=$?
429 if [ $rc -ne 0 ]
430 then
431 echo "Failed to set server(own) info for pong!"
432 exit $rc
433 fi
434
435 ''' % suffix
436
437 if use_ns_init_conf:
438 pong_cfg += "exit 0\n"
439 else:
440 pong_cfg +='''
441 curl -D /dev/stdout \
442 -H "Accept: application/vnd.yang.data+xml" \
443 -H "Content-Type: application/vnd.yang.data+json" \
444 -X POST \
445 -d "{\"enable\":true}" \
446 http://${pong_mgmt_ip}:${pong_mgmt_port}/api/v1/pong/adminstatus/state
447 rc=$?
448 if [ $rc -ne 0 ]
449 then
450 echo "Failed to enable pong service!"
451 exit $rc
452 fi
453
454 exit 0
455 '''
456 return pong_cfg
457
458 def pong_fake_juju_config(self, vnf_config):
459
460 if vnf_config:
461 # Select "script" configuration
462 vnf_config.juju.charm = 'clearwater-aio-proxy'
463
464 # Set the initital-config
465 vnf_config.create_initial_config_primitive()
466 init_config = VnfdYang.InitialConfigPrimitive.from_dict({
467 "seq": 1,
468 "name": "config",
469 "parameter": [
470 {"name": "proxied_ip", "value": "<rw_mgmt_ip>"},
471 ]
472 })
473 vnf_config.initial_config_primitive.append(init_config)
474
475 init_config_action = VnfdYang.InitialConfigPrimitive.from_dict({
476 "seq": 2,
477 "name": "action1",
478 "parameter": [
479 {"name": "Pong Connection Point", "value": "pong_vnfd/cp0"},
480 ]
481 })
482 vnf_config.initial_config_primitive.append(init_config_action)
483 init_config_action = VnfdYang.InitialConfigPrimitive.from_dict({
484 "seq": 3,
485 "name": "action2",
486 "parameter": [
487 {"name": "Ping Connection Point", "value": "ping_vnfd/cp0"},
488 ]
489 })
490 vnf_config.initial_config_primitive.append(init_config_action)
491
492 # Config parameters can be taken from config.yaml and
493 # actions from actions.yaml in the charm
494 # Config to set the home domain
495 vnf_config.create_service_primitive()
496 config = VnfdYang.ServicePrimitive.from_dict({
497 "name": "config",
498 "parameter": [
499 {"name": "home_domain", "data_type": "STRING"},
500 {"name": "base_number", "data_type": "STRING"},
501 {"name": "number_count", "data_type": "INTEGER"},
502 {"name": "password", "data_type": "STRING"},
503 ]
504 })
505 vnf_config.service_primitive.append(config)
506
507 config = VnfdYang.ServicePrimitive.from_dict({
508 "name": "create-update-user",
509 # "user-defined-script":"/tmp/test.py",
510 "parameter": [
511 {"name": "number", "data_type": "STRING", "mandatory": True},
512 {"name": "password", "data_type": "STRING", "mandatory": True},
513 ]
514 })
515 vnf_config.service_primitive.append(config)
516
517 config = VnfdYang.ServicePrimitive.from_dict({
518 "name": "delete-user",
519 "parameter": [
520 {"name": "number", "data_type": "STRING", "mandatory": True},
521 ]
522 })
523 vnf_config.service_primitive.append(config)
524
525 def default_config(self, const_vnfd, vnfd, mano_ut, use_ns_init_conf):
526 vnf_config = vnfd.vnfd.vnf_configuration
527
528 vnf_config.config_attributes.config_priority = 0
529 vnf_config.config_attributes.config_delay = 0
530
531 # Select "script" configuration
532 vnf_config.script.script_type = 'bash'
533
534 if vnfd.name == 'pong_vnfd' or vnfd.name == 'pong_vnfd_with_epa' or vnfd.name == 'pong_vnfd_aws':
535 vnf_config.config_attributes.config_priority = 1
536 vnf_config.config_template = self.pong_config(mano_ut, use_ns_init_conf)
537 # First priority config delay will delay the entire NS config delay
538 if mano_ut is False:
539 vnf_config.config_attributes.config_delay = 60
540 else:
541 # This is PONG and inside mano_ut
542 # This is test only
543 vnf_config.config_attributes.config_delay = 10
544 # vnf_config.config_template = self.pong_config(vnf_config, use_ns_init_conf)
545
546 if vnfd.name == 'ping_vnfd' or vnfd.name == 'ping_vnfd_with_epa' or vnfd.name == 'ping_vnfd_aws':
547 vnf_config.config_attributes.config_priority = 2
548 vnf_config.config_template = self.ping_config(mano_ut, use_ns_init_conf)
549
550 def ns_config(self, nsd, vnfd_list, mano_ut):
551 # Used by scale group
552 if mano_ut:
553 nsd.service_primitive.add().from_dict(
554 {
555 "name": "ping config",
556 "user_defined_script": "{}".format(os.path.join(
557 os.environ['RIFT_ROOT'],
558 'modules/core/mano',
559 'examples/ping_pong_ns/rift/mano/examples',
560 'ping_config_ut.sh'))
561 })
562 else:
563 nsd.service_primitive.add().from_dict(
564 {
565 "name": "ping config",
566 "user_defined_script": "ping_config.py"
567 })
568
569 def ns_initial_config(self, nsd):
570 nsd.initial_config_primitive.add().from_dict(
571 {
572 "seq": 1,
573 "name": "start traffic",
574 "user_defined_script": "start_traffic.py",
575 "parameter": [
576 {
577 'name': 'userid',
578 'value': 'rift',
579 },
580 ],
581 }
582 )
583
584 def add_scale_group(self, scale_group):
585 self._scale_groups.append(scale_group)
586
587 def add_placement_group(self, placement_group):
588 self._placement_groups.append(placement_group)
589
590 def create_mon_params(self, vnfds):
591 NsdMonParam = NsdYang.YangData_Nsd_NsdCatalog_Nsd_MonitoringParam
592 param_id = 1
593 for vnfd_obj in vnfds:
594 for mon_param in vnfd_obj.vnfd.monitoring_param:
595 nsd_monp = NsdMonParam.from_dict({
596 'id': str(param_id),
597 'name': mon_param.name,
598 'aggregation_type': "AVERAGE",
599 'value_type': mon_param.value_type,
600 'vnfd_monitoring_param': [
601 {'vnfd_id_ref': vnfd_obj.vnfd.id,
602 'vnfd_monitoring_param_ref': mon_param.id}]
603 })
604
605 self.nsd.monitoring_param.append(nsd_monp)
606 param_id += 1
607
608
609
610
611 def compose(self, vnfd_list, cpgroup_list, mano_ut, use_ns_init_conf=True):
612
613 if mano_ut:
614 # Disable NS initial config primitive
615 use_ns_init_conf=False
616
617 self.descriptor = RwNsdYang.YangData_Nsd_NsdCatalog()
618 self.id = str(uuid.uuid1())
619 nsd = self.descriptor.nsd.add()
620 self.nsd = nsd
621 nsd.id = self.id
622 nsd.name = self.name
623 nsd.short_name = self.name
624 nsd.vendor = 'RIFT.io'
625 nsd.logo = 'rift_logo.png'
626 nsd.description = 'Toy NS'
627 nsd.version = '1.0'
628 nsd.input_parameter_xpath.append(
629 NsdYang.YangData_Nsd_NsdCatalog_Nsd_InputParameterXpath(
630 xpath="/nsd:nsd-catalog/nsd:nsd/nsd:vendor",
631 )
632 )
633
634 ip_profile = nsd.ip_profiles.add()
635 ip_profile.name = "InterVNFLink"
636 ip_profile.description = "Inter VNF Link"
637 ip_profile.ip_profile_params.ip_version = "ipv4"
638 ip_profile.ip_profile_params.subnet_address = "31.31.31.0/24"
639 ip_profile.ip_profile_params.gateway_address = "31.31.31.210"
640
641 vld_id = 1
642 for cpgroup in cpgroup_list:
643 vld = nsd.vld.add()
644 vld.id = 'ping_pong_vld%s' % vld_id
645 vld_id += 1
646 vld.name = 'ping_pong_vld' # hard coded
647 vld.short_name = vld.name
648 vld.vendor = 'RIFT.io'
649 vld.description = 'Toy VL'
650 vld.version = '1.0'
651 vld.type_yang = 'ELAN'
652 vld.ip_profile_ref = 'InterVNFLink'
653 for cp in cpgroup:
654 cpref = vld.vnfd_connection_point_ref.add()
655 cpref.member_vnf_index_ref = cp[0]
656 cpref.vnfd_id_ref = cp[1]
657 cpref.vnfd_connection_point_ref = cp[2]
658
659 vnfd_index_map = {}
660 member_vnf_index = 1
661 for vnfd in vnfd_list:
662 for i in range(vnfd.instance_count):
663 constituent_vnfd = nsd.constituent_vnfd.add()
664 constituent_vnfd.member_vnf_index = member_vnf_index
665 vnfd_index_map[vnfd] = member_vnf_index
666
667 # Set the start by default to false for ping vnfd,
668 # if scaling is enabled
669 if (len(self._scale_groups) and
670 vnfd.descriptor.vnfd[0].name == 'ping_vnfd'):
671 constituent_vnfd.start_by_default = False
672
673 constituent_vnfd.vnfd_id_ref = vnfd.descriptor.vnfd[0].id
674 self.default_config(constituent_vnfd, vnfd, mano_ut,
675 use_ns_init_conf,)
676 member_vnf_index += 1
677
678 # Enable config primitives if either mano_ut or
679 # scale groups are enabled
680 if mano_ut or len(self._scale_groups):
681 self.ns_config(nsd, vnfd_list, mano_ut)
682
683 # Add NS initial config to start traffic
684 if use_ns_init_conf:
685 self.ns_initial_config(nsd)
686
687 for scale_group in self._scale_groups:
688 group_desc = nsd.scaling_group_descriptor.add()
689 group_desc.name = scale_group.name
690 group_desc.max_instance_count = scale_group.max_count
691 group_desc.min_instance_count = scale_group.min_count
692 for vnfd, count in scale_group.vnfd_count_map.items():
693 member = group_desc.vnfd_member.add()
694 member.member_vnf_index_ref = vnfd_index_map[vnfd]
695 member.count = count
696
697 for trigger in scale_group.config_action:
698 config_action = group_desc.scaling_config_action.add()
699 config_action.trigger = trigger
700 config = scale_group.config_action[trigger]
701 config_action.ns_config_primitive_name_ref = config['ns-config-primitive-name-ref']
702
703 for placement_group in self._placement_groups:
704 group = nsd.placement_groups.add()
705 group.name = placement_group.name
706 group.strategy = placement_group.strategy
707 group.requirement = placement_group.requirement
708 for member_vnfd in placement_group.vnfd_list:
709 member = group.member_vnfd.add()
710 member.vnfd_id_ref = member_vnfd.descriptor.vnfd[0].id
711 member.member_vnf_index_ref = vnfd_index_map[member_vnfd]
712
713 # self.create_mon_params(vnfd_list)
714
715 def write_config(self, outdir, vnfds):
716
717 converter = config_data.ConfigPrimitiveConvertor()
718 yaml_data = converter.extract_nsd_config(self.nsd)
719
720 ns_config_dir = os.path.join(outdir, self.name, "ns_config")
721 os.makedirs(ns_config_dir, exist_ok=True)
722 vnf_config_dir = os.path.join(outdir, self.name, "vnf_config")
723 os.makedirs(vnf_config_dir, exist_ok=True)
724
725 if len(yaml_data):
726 with open('%s/%s.yaml' % (ns_config_dir, self.id), "w") as fh:
727 fh.write(yaml_data)
728
729 for i, vnfd in enumerate(vnfds, start=1):
730 yaml_data = converter.extract_vnfd_config(vnfd)
731
732 if len(yaml_data):
733 with open('%s/%s__%s.yaml' % (vnf_config_dir, vnfd.id, i), "w") as fh:
734 fh.write(yaml_data)
735
736 def write_initial_config_script(self, outdir):
737 script_name = 'start_traffic.py'
738
739 src_path = os.path.dirname(os.path.abspath(os.path.realpath(__file__)))
740 script_src = os.path.join(src_path, script_name)
741 if not os.path.exists(script_src):
742 src_path = os.path.join(os.environ['RIFT_ROOT'],
743 'modules/core/mano/examples/ping_pong_ns/rift/mano/examples')
744 script_src = os.path.join(src_path, script_name)
745
746 dest_path = os.path.join(outdir, 'scripts')
747 os.makedirs(dest_path, exist_ok=True)
748
749 shutil.copy2(script_src, dest_path)
750
751 def write_to_file(self, outdir, output_format):
752 dirpath = os.path.join(outdir, self.name)
753 if not os.path.exists(dirpath):
754 os.makedirs(dirpath)
755
756 super(NetworkService, self).write_to_file(["nsd", "rw-nsd"],
757 dirpath,
758 output_format)
759
760 # Write the initial config script
761 self.write_initial_config_script(dirpath)
762
763
764 def get_ping_mon_params(path):
765 return [
766 {
767 'id': '1',
768 'name': 'ping-request-tx-count',
769 'http_endpoint_ref': path,
770 'json_query_method': "NAMEKEY",
771 'value_type': "INT",
772 'description': 'no of ping requests',
773 'group_tag': 'Group1',
774 'widget_type': 'COUNTER',
775 'units': 'packets'
776 },
777
778 {
779 'id': '2',
780 'name': 'ping-response-rx-count',
781 'http_endpoint_ref': path,
782 'json_query_method': "NAMEKEY",
783 'value_type': "INT",
784 'description': 'no of ping responses',
785 'group_tag': 'Group1',
786 'widget_type': 'COUNTER',
787 'units': 'packets'
788 },
789 ]
790
791
792 def get_pong_mon_params(path):
793 return [
794 {
795 'id': '1',
796 'name': 'ping-request-rx-count',
797 'http_endpoint_ref': path,
798 'json_query_method': "NAMEKEY",
799 'value_type': "INT",
800 'description': 'no of ping requests',
801 'group_tag': 'Group1',
802 'widget_type': 'COUNTER',
803 'units': 'packets'
804 },
805
806 {
807 'id': '2',
808 'name': 'ping-response-tx-count',
809 'http_endpoint_ref': path,
810 'json_query_method': "NAMEKEY",
811 'value_type': "INT",
812 'description': 'no of ping responses',
813 'group_tag': 'Group1',
814 'widget_type': 'COUNTER',
815 'units': 'packets'
816 },
817 ]
818
819
820 class ScaleGroup(object):
821 def __init__(self, name, min_count=1, max_count=1):
822 self.name = name
823 self.min_count = min_count
824 self.max_count = max_count
825 self.vnfd_count_map = {}
826 self.config_action = {}
827
828 def add_vnfd(self, vnfd, vnfd_count):
829 self.vnfd_count_map[vnfd] = vnfd_count
830
831 def add_config(self):
832 self.config_action['post_scale_out']= {'ns-config-primitive-name-ref':
833 'ping config'}
834
835 class PlacementGroup(object):
836 def __init__(self, name):
837 self.name = name
838 self.strategy = ''
839 self.requirement = ''
840
841 def add_strategy(self, strategy):
842 self.strategy = strategy
843
844 def add_requirement(self, requirement):
845 self.requirement = requirement
846
847 class NsdPlacementGroup(PlacementGroup):
848 def __init__(self, name):
849 self.vnfd_list = []
850 super(NsdPlacementGroup, self).__init__(name)
851
852 def add_member(self, vnfd):
853 self.vnfd_list.append(vnfd)
854
855
856 class VnfdPlacementGroup(PlacementGroup):
857 def __init__(self, name):
858 self.vdu_list = []
859 super(VnfdPlacementGroup, self).__init__(name)
860
861 def add_member(self, vdu):
862 self.vdu_list.append(vdu)
863
864
865
866
867 def generate_ping_pong_descriptors(fmt="json",
868 write_to_file=False,
869 out_dir="./",
870 pingcount=NUM_PING_INSTANCES,
871 external_vlr_count=1,
872 internal_vlr_count=1,
873 num_vnf_vms=1,
874 ping_md5sum=None,
875 pong_md5sum=None,
876 mano_ut=False,
877 use_scale_group=False,
878 ping_fmt=None,
879 pong_fmt=None,
880 nsd_fmt=None,
881 use_mon_params=True,
882 ping_userdata=None,
883 pong_userdata=None,
884 ex_ping_userdata=None,
885 ex_pong_userdata=None,
886 use_placement_group=True,
887 use_ns_init_conf=True,
888 ):
889 # List of connection point groups
890 # Each connection point group refers to a virtual link
891 # the CP group consists of tuples of connection points
892 cpgroup_list = []
893 for i in range(external_vlr_count):
894 cpgroup_list.append([])
895
896 suffix = ''
897 ping = VirtualNetworkFunction("ping_vnfd%s" % (suffix), pingcount)
898
899 if use_placement_group:
900 ### Add group name Eris
901 group = VnfdPlacementGroup('Eris')
902 group.add_strategy('COLOCATION')
903 group.add_requirement('''Place this VM on the Kuiper belt object Eris''')
904 ping.add_placement_group(group)
905
906 # ping = VirtualNetworkFunction("ping_vnfd", pingcount)
907 if not ping_userdata:
908 ping_userdata = PING_USERDATA_FILE
909
910 if ex_ping_userdata:
911 ping_userdata = '''\
912 {ping_userdata}
913 {ex_ping_userdata}
914 '''.format(
915 ping_userdata=ping_userdata,
916 ex_ping_userdata=ex_ping_userdata
917 )
918
919 ping.compose(
920 "Fedora-x86_64-20-20131211.1-sda-ping.qcow2",
921 ping_userdata,
922 use_ping_cloud_init_file,
923 "api/v1/ping/stats",
924 get_ping_mon_params("api/v1/ping/stats") if use_mon_params else [],
925 mon_port=18888,
926 mgmt_port=18888,
927 num_vlr_count=external_vlr_count,
928 num_ivlr_count=internal_vlr_count,
929 num_vms=num_vnf_vms,
930 image_md5sum=ping_md5sum,
931 mano_ut=mano_ut,
932 )
933
934 pong = VirtualNetworkFunction("pong_vnfd%s" % (suffix))
935
936 if use_placement_group:
937 ### Add group name Weywot
938 group = VnfdPlacementGroup('Weywot')
939 group.add_strategy('COLOCATION')
940 group.add_requirement('''Place this VM on the Kuiper belt object Weywot''')
941 pong.add_placement_group(group)
942
943
944 # pong = VirtualNetworkFunction("pong_vnfd")
945
946 if not pong_userdata:
947 pong_userdata = PONG_USERDATA_FILE
948
949 if ex_pong_userdata:
950 pong_userdata = '''\
951 {pong_userdata}
952 {ex_pong_userdata}
953 '''.format(
954 pong_userdata=pong_userdata,
955 ex_pong_userdata=ex_pong_userdata
956 )
957
958
959 pong.compose(
960 "Fedora-x86_64-20-20131211.1-sda-pong.qcow2",
961 pong_userdata,
962 use_pong_cloud_init_file,
963 "api/v1/pong/stats",
964 get_pong_mon_params("api/v1/pong/stats") if use_mon_params else [],
965 mon_port=18889,
966 mgmt_port=18889,
967 num_vlr_count=external_vlr_count,
968 num_ivlr_count=internal_vlr_count,
969 num_vms=num_vnf_vms,
970 image_md5sum=pong_md5sum,
971 mano_ut=mano_ut,
972 )
973
974 # Initialize the member VNF index
975 member_vnf_index = 1
976
977 # define the connection point groups
978 for index, cp_group in enumerate(cpgroup_list):
979 desc_id = ping.descriptor.vnfd[0].id
980 filename = 'ping_vnfd{}/cp{}'.format(suffix, index)
981
982 for idx in range(pingcount):
983 cp_group.append((
984 member_vnf_index,
985 desc_id,
986 filename,
987 ))
988
989 member_vnf_index += 1
990
991 desc_id = pong.descriptor.vnfd[0].id
992 filename = 'pong_vnfd{}/cp{}'.format(suffix, index)
993
994 cp_group.append((
995 member_vnf_index,
996 desc_id,
997 filename,
998 ))
999
1000 member_vnf_index += 1
1001
1002 vnfd_list = [ping, pong]
1003
1004 nsd_catalog = NetworkService("ping_pong_nsd%s" % (suffix))
1005
1006 if use_scale_group:
1007 group = ScaleGroup("ping_group", max_count=10)
1008 group.add_vnfd(ping, 1)
1009 group.add_config()
1010 nsd_catalog.add_scale_group(group)
1011
1012 if use_placement_group:
1013 ### Add group name Orcus
1014 group = NsdPlacementGroup('Orcus')
1015 group.add_strategy('COLOCATION')
1016 group.add_requirement('''Place this VM on the Kuiper belt object Orcus''')
1017
1018 for member_vnfd in vnfd_list:
1019 group.add_member(member_vnfd)
1020
1021 nsd_catalog.add_placement_group(group)
1022
1023 ### Add group name Quaoar
1024 group = NsdPlacementGroup('Quaoar')
1025 group.add_strategy('COLOCATION')
1026 group.add_requirement('''Place this VM on the Kuiper belt object Quaoar''')
1027
1028 for member_vnfd in vnfd_list:
1029 group.add_member(member_vnfd)
1030
1031 nsd_catalog.add_placement_group(group)
1032
1033
1034 nsd_catalog.compose(vnfd_list,
1035 cpgroup_list,
1036 mano_ut,
1037 use_ns_init_conf=use_ns_init_conf,)
1038
1039 if write_to_file:
1040 ping.write_to_file(out_dir, ping_fmt if ping_fmt is not None else fmt)
1041 pong.write_to_file(out_dir, pong_fmt if ping_fmt is not None else fmt)
1042 nsd_catalog.write_config(out_dir, vnfd_list)
1043 nsd_catalog.write_to_file(out_dir, ping_fmt if nsd_fmt is not None else fmt)
1044
1045 return (ping, pong, nsd_catalog)
1046
1047
1048 def main(argv=sys.argv[1:]):
1049 global outdir, output_format, use_epa, aws, use_ping_cloud_init_file, use_pong_cloud_init_file
1050 parser = argparse.ArgumentParser()
1051 parser.add_argument('-o', '--outdir', default='.')
1052 parser.add_argument('-f', '--format', default='json')
1053 parser.add_argument('-e', '--epa', action="store_true", default=False)
1054 parser.add_argument('-a', '--aws', action="store_true", default=False)
1055 parser.add_argument('-n', '--pingcount', default=NUM_PING_INSTANCES)
1056 parser.add_argument('--ping-image-md5')
1057 parser.add_argument('--pong-image-md5')
1058 parser.add_argument('--ping-cloud-init', default=None)
1059 parser.add_argument('--pong-cloud-init', default=None)
1060 args = parser.parse_args()
1061 outdir = args.outdir
1062 output_format = args.format
1063 use_epa = args.epa
1064 aws = args.aws
1065 pingcount = args.pingcount
1066 use_ping_cloud_init_file = args.ping_cloud_init
1067 use_pong_cloud_init_file = args.pong_cloud_init
1068
1069 generate_ping_pong_descriptors(args.format, True, args.outdir, pingcount,
1070 ping_md5sum=args.ping_image_md5, pong_md5sum=args.pong_image_md5,
1071 mano_ut=False,
1072 use_scale_group=False,)
1073
1074 if __name__ == "__main__":
1075 main()