4 # Copyright 2016 RIFT.IO Inc
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
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')
34 from gi
.repository
import (
44 import rift
.mano
.config_data
.config
as config_data
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")
50 import config_data
.config
as config_data
53 NUM_PING_INSTANCES
= 1
54 MAX_VNF_INSTANCES_PER_NS
= 10
57 pingcount
= NUM_PING_INSTANCES
58 use_ping_cloud_init_file
= ""
59 use_pong_cloud_init_file
= ""
61 PING_USERDATA_FILE
= '''#cloud-config
63 chpasswd: { expire: False }
66 - [ systemctl, daemon-reload ]
67 - [ systemctl, enable, ping.service ]
68 - [ systemctl, start, --no-block, ping.service ]
72 PONG_USERDATA_FILE
= '''#cloud-config
74 chpasswd: { expire: False }
77 - [ systemctl, daemon-reload ]
78 - [ systemctl, enable, pong.service ]
79 - [ systemctl, start, --no-block, pong.service ]
84 class UnknownVNFError(Exception):
88 class ManoDescriptor(object):
89 def __init__(self
, name
):
91 self
.descriptor
= None
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
)
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
))
108 raise Exception("Invalid output format for the descriptor")
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
))
117 class VirtualNetworkFunction(ManoDescriptor
):
118 def __init__(self
, name
, instance_count
=1):
119 self
.vnfd_catalog
= None
121 self
.instance_count
= instance_count
122 self
._placement
_groups
= []
123 super(VirtualNetworkFunction
, self
).__init
__(name
)
125 def add_placement_group(self
, group
):
126 self
._placement
_groups
.append(group
)
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()
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'
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
)
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
)
160 if endpoint
is not None:
161 endp
= VnfdYang
.YangData_Vnfd_VnfdCatalog_Vnfd_HttpEndpoint(
162 path
=endpoint
, port
=mon_port
, polling_interval_secs
=2
164 vnfd
.http_endpoint
.append(endp
)
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
)
173 for i
in range(num_vms
):
176 vdu
.id = 'iovdu_%s' % i
177 vdu
.name
= 'iovdu_%s' % i
179 # vdu.mgmt_vpci = '0000:00:20.0'
181 # specify the VM flavor
183 vdu
.vm_flavor
.vcpu_count
= 4
184 vdu
.vm_flavor
.memory_mb
= 1024
185 vdu
.vm_flavor
.storage_gb
= 4
187 vdu
.vm_flavor
.vcpu_count
= 1
188 vdu
.vm_flavor
.memory_mb
= 512
189 vdu
.vm_flavor
.storage_gb
= 4
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
198 if cloud_init_file
and len(cloud_init_file
):
199 vdu
.cloud_init_file
= cloud_init_file
201 vdu
.cloud_init
= cloud_init
203 vdu
.cloud_init
+= " - [ systemctl, restart, --no-block, elastic-network-interfaces.service ]\n"
205 # sepcify the guest 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'
214 node
= vdu
.guest_epa
.numa_node_policy
.node
.add()
219 node
= vdu
.guest_epa
.numa_node_policy
.node
.add()
224 # specify the vswitch EPA
225 vdu
.vswitch_epa
.ovs_acceleration
= 'DISABLED'
226 vdu
.vswitch_epa
.ovs_offload
= 'DISABLED'
228 # Specify the hypervisor EPA
229 vdu
.hypervisor_epa
.type_yang
= 'PREFER_KVM'
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']
241 vdu
.image
= 'rift-ping-pong'
243 vdu
.image
= image_name
244 if image_md5sum
is not None:
245 vdu
.image_checksum
= image_md5sum
248 for i
in range(num_ivlr_count
):
249 internal_cp
= vdu
.internal_connection_point
.add()
250 if vnfd
.name
.find("ping") >= 0:
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)
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'
264 # internal_interface.virtual_interface.vpci = '0000:00:1%d.0'%i
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
)
271 external_interface
.virtual_interface
.type_yang
= 'VIRTIO'
273 external_interface
.virtual_interface
.type_yang
= 'VIRTIO'
274 # external_interface.virtual_interface.vpci = '0000:00:2%d.0'%i
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
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
287 ### Add all VDUs to placement group
289 member_vdu
= placement_group
.member_vdus
.add()
290 member_vdu
.member_vdu_ref
= vdu
.id
293 def write_to_file(self
, outdir
, output_format
):
294 dirpath
= "%s/%s" % (outdir
, self
.name
)
295 if not os
.path
.exists(dirpath
):
297 super(VirtualNetworkFunction
, self
).write_to_file(['vnfd', 'rw-vnfd'],
300 self
.add_scripts(outdir
)
302 def add_scripts(self
, outdir
):
303 script_dir
= os
.path
.join(outdir
, self
.name
, 'cloud_init')
305 os
.makedirs(script_dir
)
307 if not os
.path
.isdir(script_dir
):
310 if 'ping' in self
.name
:
311 script_file
= os
.path
.join(script_dir
, 'ping_cloud_init.cfg')
312 cfg
= PING_USERDATA_FILE
314 script_file
= os
.path
.join(script_dir
, 'pong_cloud_init.cfg')
315 cfg
= PONG_USERDATA_FILE
317 with
open(script_file
, "w") as f
:
318 f
.write("{}".format(cfg
))
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
= []
328 def ping_config(self
, mano_ut
, use_ns_init_conf
):
334 echo "!!!!!!!! Executed ping Configuration !!!!!!!!!"
341 ping_mgmt_ip='<rw_mgmt_ip>'
344 # VNF specific configuration
345 pong_server_ip='<rw_connection_point_name pong_vnfd%s/cp0>'
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" \
354 -d "{\"ip\":\"$pong_server_ip\", \"port\":$server_port}" \
355 http://${ping_mgmt_ip}:${ping_mgmt_port}/api/v1/ping/server
359 echo "Failed to set server info for ping!"
363 curl -D /dev/stdout \
364 -H "Accept: application/vnd.yang.data+xml" \
365 -H "Content-Type: application/vnd.yang.data+json" \
367 -d "{\"rate\":$ping_rate}" \
368 http://${ping_mgmt_ip}:${ping_mgmt_port}/api/v1/ping/rate
372 echo "Failed to set ping rate!"
378 ping_cfg
+= "exit 0\n"
381 output=$(curl -D /dev/stdout \
382 -H "Accept: application/vnd.yang.data+xml" \
383 -H "Content-Type: application/vnd.yang.data+json" \
385 -d "{\"enable\":true}" \
386 http://${ping_mgmt_ip}:${ping_mgmt_port}/api/v1/ping/adminstatus/state)
387 if [[ $output == *"Internal Server Error"* ]]
399 def pong_config(self
, mano_ut
, use_ns_init_conf
):
405 echo "!!!!!!!! Executed pong Configuration !!!!!!!!!"
411 # Rest API configuration
412 pong_mgmt_ip='<rw_mgmt_ip>'
414 # username=<rw_username>
415 # password=<rw_password>
417 # VNF specific configuration
418 pong_server_ip='<rw_connection_point_name pong_vnfd%s/cp0>'
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" \
426 -d "{\"ip\":\"$pong_server_ip\", \"port\":$server_port}" \
427 http://${pong_mgmt_ip}:${pong_mgmt_port}/api/v1/pong/server
431 echo "Failed to set server(own) info for pong!"
438 pong_cfg
+= "exit 0\n"
441 curl -D /dev/stdout \
442 -H "Accept: application/vnd.yang.data+xml" \
443 -H "Content-Type: application/vnd.yang.data+json" \
445 -d "{\"enable\":true}" \
446 http://${pong_mgmt_ip}:${pong_mgmt_port}/api/v1/pong/adminstatus/state
450 echo "Failed to enable pong service!"
458 def pong_fake_juju_config(self
, vnf_config
):
461 # Select "script" configuration
462 vnf_config
.juju
.charm
= 'clearwater-aio-proxy'
464 # Set the initital-config
465 vnf_config
.create_initial_config_primitive()
466 init_config
= VnfdYang
.InitialConfigPrimitive
.from_dict({
470 {"name": "proxied_ip", "value": "<rw_mgmt_ip>"},
473 vnf_config
.initial_config_primitive
.append(init_config
)
475 init_config_action
= VnfdYang
.InitialConfigPrimitive
.from_dict({
479 {"name": "Pong Connection Point", "value": "pong_vnfd/cp0"},
482 vnf_config
.initial_config_primitive
.append(init_config_action
)
483 init_config_action
= VnfdYang
.InitialConfigPrimitive
.from_dict({
487 {"name": "Ping Connection Point", "value": "ping_vnfd/cp0"},
490 vnf_config
.initial_config_primitive
.append(init_config_action
)
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({
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"},
505 vnf_config
.service_primitive
.append(config
)
507 config
= VnfdYang
.ServicePrimitive
.from_dict({
508 "name": "create-update-user",
509 # "user-defined-script":"/tmp/test.py",
511 {"name": "number", "data_type": "STRING", "mandatory": True},
512 {"name": "password", "data_type": "STRING", "mandatory": True},
515 vnf_config
.service_primitive
.append(config
)
517 config
= VnfdYang
.ServicePrimitive
.from_dict({
518 "name": "delete-user",
520 {"name": "number", "data_type": "STRING", "mandatory": True},
523 vnf_config
.service_primitive
.append(config
)
525 def default_config(self
, const_vnfd
, vnfd
, mano_ut
, use_ns_init_conf
):
526 vnf_config
= vnfd
.vnfd
.vnf_configuration
528 vnf_config
.config_attributes
.config_priority
= 0
529 vnf_config
.config_attributes
.config_delay
= 0
531 # Select "script" configuration
532 vnf_config
.script
.script_type
= 'bash'
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
539 vnf_config
.config_attributes
.config_delay
= 60
541 # This is PONG and inside mano_ut
543 vnf_config
.config_attributes
.config_delay
= 10
544 # vnf_config.config_template = self.pong_config(vnf_config, use_ns_init_conf)
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
)
550 def ns_config(self
, nsd
, vnfd_list
, mano_ut
):
551 # Used by scale group
553 nsd
.service_primitive
.add().from_dict(
555 "name": "ping config",
556 "user_defined_script": "{}".format(os
.path
.join(
557 os
.environ
['RIFT_ROOT'],
559 'examples/ping_pong_ns/rift/mano/examples',
560 'ping_config_ut.sh'))
563 nsd
.service_primitive
.add().from_dict(
565 "name": "ping config",
566 "user_defined_script": "ping_config.py"
569 def ns_initial_config(self
, nsd
):
570 nsd
.initial_config_primitive
.add().from_dict(
573 "name": "start traffic",
574 "user_defined_script": "start_traffic.py",
584 def add_scale_group(self
, scale_group
):
585 self
._scale
_groups
.append(scale_group
)
587 def add_placement_group(self
, placement_group
):
588 self
._placement
_groups
.append(placement_group
)
590 def create_mon_params(self
, vnfds
):
591 NsdMonParam
= NsdYang
.YangData_Nsd_NsdCatalog_Nsd_MonitoringParam
593 for vnfd_obj
in vnfds
:
594 for mon_param
in vnfd_obj
.vnfd
.monitoring_param
:
595 nsd_monp
= NsdMonParam
.from_dict({
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}]
605 self
.nsd
.monitoring_param
.append(nsd_monp
)
611 def compose(self
, vnfd_list
, cpgroup_list
, mano_ut
, use_ns_init_conf
=True):
614 # Disable NS initial config primitive
615 use_ns_init_conf
=False
617 self
.descriptor
= RwNsdYang
.YangData_Nsd_NsdCatalog()
618 self
.id = str(uuid
.uuid1())
619 nsd
= self
.descriptor
.nsd
.add()
623 nsd
.short_name
= self
.name
624 nsd
.vendor
= 'RIFT.io'
625 nsd
.logo
= 'rift_logo.png'
626 nsd
.description
= 'Toy NS'
628 nsd
.input_parameter_xpath
.append(
629 NsdYang
.YangData_Nsd_NsdCatalog_Nsd_InputParameterXpath(
630 xpath
="/nsd:nsd-catalog/nsd:nsd/nsd:vendor",
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"
642 for cpgroup
in cpgroup_list
:
644 vld
.id = 'ping_pong_vld%s' % vld_id
646 vld
.name
= 'ping_pong_vld' # hard coded
647 vld
.short_name
= vld
.name
648 vld
.vendor
= 'RIFT.io'
649 vld
.description
= 'Toy VL'
651 vld
.type_yang
= 'ELAN'
652 vld
.ip_profile_ref
= 'InterVNFLink'
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]
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
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
673 constituent_vnfd
.vnfd_id_ref
= vnfd
.descriptor
.vnfd
[0].id
674 self
.default_config(constituent_vnfd
, vnfd
, mano_ut
,
676 member_vnf_index
+= 1
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
)
683 # Add NS initial config to start traffic
685 self
.ns_initial_config(nsd
)
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
]
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']
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
]
713 # self.create_mon_params(vnfd_list)
715 def write_config(self
, outdir
, vnfds
):
717 converter
= config_data
.ConfigPrimitiveConvertor()
718 yaml_data
= converter
.extract_nsd_config(self
.nsd
)
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)
726 with
open('%s/%s.yaml' % (ns_config_dir
, self
.id), "w") as fh
:
729 for i
, vnfd
in enumerate(vnfds
, start
=1):
730 yaml_data
= converter
.extract_vnfd_config(vnfd
)
733 with
open('%s/%s__%s.yaml' % (vnf_config_dir
, vnfd
.id, i
), "w") as fh
:
736 def write_initial_config_script(self
, outdir
):
737 script_name
= 'start_traffic.py'
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
)
746 dest_path
= os
.path
.join(outdir
, 'scripts')
747 os
.makedirs(dest_path
, exist_ok
=True)
749 shutil
.copy2(script_src
, dest_path
)
751 def write_to_file(self
, outdir
, output_format
):
752 dirpath
= os
.path
.join(outdir
, self
.name
)
753 if not os
.path
.exists(dirpath
):
756 super(NetworkService
, self
).write_to_file(["nsd", "rw-nsd"],
760 # Write the initial config script
761 self
.write_initial_config_script(dirpath
)
764 def get_ping_mon_params(path
):
768 'name': 'ping-request-tx-count',
769 'http_endpoint_ref': path
,
770 'json_query_method': "NAMEKEY",
772 'description': 'no of ping requests',
773 'group_tag': 'Group1',
774 'widget_type': 'COUNTER',
780 'name': 'ping-response-rx-count',
781 'http_endpoint_ref': path
,
782 'json_query_method': "NAMEKEY",
784 'description': 'no of ping responses',
785 'group_tag': 'Group1',
786 'widget_type': 'COUNTER',
792 def get_pong_mon_params(path
):
796 'name': 'ping-request-rx-count',
797 'http_endpoint_ref': path
,
798 'json_query_method': "NAMEKEY",
800 'description': 'no of ping requests',
801 'group_tag': 'Group1',
802 'widget_type': 'COUNTER',
808 'name': 'ping-response-tx-count',
809 'http_endpoint_ref': path
,
810 'json_query_method': "NAMEKEY",
812 'description': 'no of ping responses',
813 'group_tag': 'Group1',
814 'widget_type': 'COUNTER',
820 class ScaleGroup(object):
821 def __init__(self
, name
, min_count
=1, max_count
=1):
823 self
.min_count
= min_count
824 self
.max_count
= max_count
825 self
.vnfd_count_map
= {}
826 self
.config_action
= {}
828 def add_vnfd(self
, vnfd
, vnfd_count
):
829 self
.vnfd_count_map
[vnfd
] = vnfd_count
831 def add_config(self
):
832 self
.config_action
['post_scale_out']= {'ns-config-primitive-name-ref':
835 class PlacementGroup(object):
836 def __init__(self
, name
):
839 self
.requirement
= ''
841 def add_strategy(self
, strategy
):
842 self
.strategy
= strategy
844 def add_requirement(self
, requirement
):
845 self
.requirement
= requirement
847 class NsdPlacementGroup(PlacementGroup
):
848 def __init__(self
, name
):
850 super(NsdPlacementGroup
, self
).__init
__(name
)
852 def add_member(self
, vnfd
):
853 self
.vnfd_list
.append(vnfd
)
856 class VnfdPlacementGroup(PlacementGroup
):
857 def __init__(self
, name
):
859 super(VnfdPlacementGroup
, self
).__init
__(name
)
861 def add_member(self
, vdu
):
862 self
.vdu_list
.append(vdu
)
867 def generate_ping_pong_descriptors(fmt
="json",
870 pingcount
=NUM_PING_INSTANCES
,
871 external_vlr_count
=1,
872 internal_vlr_count
=1,
877 use_scale_group
=False,
884 ex_ping_userdata
=None,
885 ex_pong_userdata
=None,
886 use_placement_group
=True,
887 use_ns_init_conf
=True,
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
893 for i
in range(external_vlr_count
):
894 cpgroup_list
.append([])
897 ping
= VirtualNetworkFunction("ping_vnfd%s" % (suffix
), pingcount
)
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
)
906 # ping = VirtualNetworkFunction("ping_vnfd", pingcount)
907 if not ping_userdata
:
908 ping_userdata
= PING_USERDATA_FILE
915 ping_userdata
=ping_userdata
,
916 ex_ping_userdata
=ex_ping_userdata
920 "Fedora-x86_64-20-20131211.1-sda-ping.qcow2",
922 use_ping_cloud_init_file
,
924 get_ping_mon_params("api/v1/ping/stats") if use_mon_params
else [],
927 num_vlr_count
=external_vlr_count
,
928 num_ivlr_count
=internal_vlr_count
,
930 image_md5sum
=ping_md5sum
,
934 pong
= VirtualNetworkFunction("pong_vnfd%s" % (suffix
))
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
)
944 # pong = VirtualNetworkFunction("pong_vnfd")
946 if not pong_userdata
:
947 pong_userdata
= PONG_USERDATA_FILE
954 pong_userdata
=pong_userdata
,
955 ex_pong_userdata
=ex_pong_userdata
960 "Fedora-x86_64-20-20131211.1-sda-pong.qcow2",
962 use_pong_cloud_init_file
,
964 get_pong_mon_params("api/v1/pong/stats") if use_mon_params
else [],
967 num_vlr_count
=external_vlr_count
,
968 num_ivlr_count
=internal_vlr_count
,
970 image_md5sum
=pong_md5sum
,
974 # Initialize the member VNF index
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
)
982 for idx
in range(pingcount
):
989 member_vnf_index
+= 1
991 desc_id
= pong
.descriptor
.vnfd
[0].id
992 filename
= 'pong_vnfd{}/cp{}'.format(suffix
, index
)
1000 member_vnf_index
+= 1
1002 vnfd_list
= [ping
, pong
]
1004 nsd_catalog
= NetworkService("ping_pong_nsd%s" % (suffix
))
1007 group
= ScaleGroup("ping_group", max_count
=10)
1008 group
.add_vnfd(ping
, 1)
1010 nsd_catalog
.add_scale_group(group
)
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''')
1018 for member_vnfd
in vnfd_list
:
1019 group
.add_member(member_vnfd
)
1021 nsd_catalog
.add_placement_group(group
)
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''')
1028 for member_vnfd
in vnfd_list
:
1029 group
.add_member(member_vnfd
)
1031 nsd_catalog
.add_placement_group(group
)
1034 nsd_catalog
.compose(vnfd_list
,
1037 use_ns_init_conf
=use_ns_init_conf
,)
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
)
1045 return (ping
, pong
, nsd_catalog
)
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
1065 pingcount
= args
.pingcount
1066 use_ping_cloud_init_file
= args
.ping_cloud_init
1067 use_pong_cloud_init_file
= args
.pong_cloud_init
1069 generate_ping_pong_descriptors(args
.format
, True, args
.outdir
, pingcount
,
1070 ping_md5sum
=args
.ping_image_md5
, pong_md5sum
=args
.pong_image_md5
,
1072 use_scale_group
=False,)
1074 if __name__
== "__main__":