Skip to content

Commit

Permalink
Merge pull request #13 from hadik3r/master
Browse files Browse the repository at this point in the history
Add PSA_placement
  • Loading branch information
hadik3r authored Nov 14, 2017
2 parents 7747615 + 6a06132 commit 6220474
Show file tree
Hide file tree
Showing 30 changed files with 2,096 additions and 1 deletion.
45 changes: 45 additions & 0 deletions son-ssm-examples/PSA_placement/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright (c) 2015 SONATA-NFV
# ALL RIGHTS RESERVED.
# 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.
# Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
# nor the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written
# permission.
# This work has been performed in the framework of the SONATA project,
# funded by the European Commission under Grant number 671517 through
# the Horizon 2020 and 5G-PPP programmes. The authors would like to
# acknowledge the contributions of their colleagues of the SONATA
# partner consortium (www.sonata-nfv.eu).

FROM python:3.4-slim
MAINTAINER SONATA

# configrurations
ENV mongo_host mongo
ENV mongo_port 27017
ENV broker_host amqp://guest:guest@broker:5672/%2F
ENV broker_exchange son-kernel

ADD son-ssm-examples/PSA_placement /placement
ADD son-mano-framework/son-mano-base /son-mano-base
ADD son-sm-template /son-sm-template
ADD utils/delayedstart.sh /delayedstart.sh

WORKDIR /son-mano-base
Run python setup.py install

WORKDIR /son-sm-template
RUN python setup.py install

WORKDIR /placement
RUN python setup.py develop

CMD ["placement"]
2 changes: 2 additions & 0 deletions son-ssm-examples/PSA_placement/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Include the license file
include ../../LICENSE
59 changes: 59 additions & 0 deletions son-ssm-examples/PSA_placement/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# PSA Pilot Placement SSM
Placement SSM to be used for PSA pilot.

## Requires
* Docker
* Python3.4
* RabbitMQ

## Implementation
* Implemented in Python 3.4
* Dependencies: amqp-storm
* The main implementation can be found in: `son-ssm-examples/PSA_placement/placement/placement.py`

## How to run it

* To run the placement SSM locally, you need:
* a running RabbitMQ broker (see general README.md of [son-mano framework repository](https://github.com/sonata-nfv/son-mano-framework) for info on how to do this)
* a running Service Specific Registry (SMR) connected to the broker (see general README.md of [SMR repository](https://github.com/sonata-nfv/son-mano-framework) for info on how to do this)

* Run the placement SSM (in a Docker container):
* (do in `son-sm/`)
* `docker build -t sonssmpsaplacement1 -f son-ssm-examples/PSA_placement/Dockerfile .`
* `docker run --name sonssmpsaplacement1 --net=sonata --network-alias=sonssmpsaplacement1 sonssmpsaplacement1`

* Or: Run the placement SSM (directly in your terminal not in a Docker container):
* (In `son-sm/son-sm-template/`)
* `python3.4 setup.py install`
* (In `son-sm/son-ssm-examples/PSA_placement/`)
* `python3.4 setup.py develop`
* (In `son-sm/`)
* `python3.4 son-ssm-examples/PSA_placement/placement/placement.py`

## How to test it
* Do the following; each in a separate terminal.
1. Run the fake_SMR
2. Run the placement container
3. In son-sm/son-ssm-examples/PSA_placement/placement/test run python3.4 placementtrigger.py

* The expected results are as follows:

* In the placementtrigger terminal:

```
{'status': 'COMPLETED', 'mapping': {'vnfd2': {'vim': '1234'}, 'vnfd1': {'vim': '1234'}}, 'error': None}
```

* In placement terminal:

```
INFO:son-sm-base:Starting sonssmpsaplacement1 ...
INFO:son-sm-base:Sending registration request...
INFO:son-sm-base:sonssmpsaplacement1 registered with uuid:23345
DEBUG:PSA-ssm-placement:Received registration ok event.
INFO:PSA-ssm-placement:Subscribed to placement.ssm.1234
INFO:PSA-ssm-placement:Placement started
INFO:PSA-ssm-placement:Mapping algorithm started.
INFO:PSA-ssm-placement:Mapping succeeded: {'vnfd1': {'vim': '1234'}, 'vnfd2': {'vim': '1234'}}
INFO:PSA-ssm-placement:The mapping calculation has succeeded.
```
Empty file.
8 changes: 8 additions & 0 deletions son-ssm-examples/PSA_placement/placement/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from placement import placement


def main():
placement.main()

if __name__ == '__main__':
main()
164 changes: 164 additions & 0 deletions son-ssm-examples/PSA_placement/placement/placement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
"""
Copyright (c) 2015 SONATA-NFV
ALL RIGHTS RESERVED.
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.
Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
nor the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written
permission.
This work has been performed in the framework of the SONATA project,
funded by the European Commission under Grant number 671517 through
the Horizon 2020 and 5G-PPP programmes. The authors would like to
acknowledge the contributions of their colleagues of the SONATA
partner consortium (www.sonata-nfv.eu).
"""

import logging
import yaml
from sonsmbase.smbase import sonSMbase

logging.basicConfig(level=logging.INFO)
LOG = logging.getLogger("PSA-ssm-placement")
LOG.setLevel(logging.DEBUG)
logging.getLogger("son-mano-base:messaging").setLevel(logging.INFO)


class PlacementSSM(sonSMbase):

def __init__(self):

"""
:param specific_manager_type: specifies the type of specific manager that could be either fsm or ssm.
:param service_name: the name of the service that this specific manager belongs to.
:param function_name: the name of the function that this specific manager belongs to, will be null in SSM case
:param specific_manager_name: the actual name of specific manager (e.g., scaling, placement)
:param id_number: the specific manager id number which is used to distinguish between multiple SSM/FSM
that are created for the same objective (e.g., scaling with algorithm 1 and 2)
:param version: version
:param description: description
"""
self.specific_manager_type = 'ssm'
self.service_name = 'psa'
self.specific_manager_name = 'placement'
self.id_number = '1'
self.version = 'v0.1'
self.description = "Placement SSM"

super(self.__class__, self).__init__(specific_manager_type= self.specific_manager_type,
service_name= self.service_name,
specific_manager_name = self.specific_manager_name,
id_number = self.id_number,
version = self.version,
description = self.description)


def on_registration_ok(self):

LOG.debug("Received registration ok event.")

# For testing, here we set the service uuid.
# In the actual scenario this should be set by SLM and SMR during the SSM instantiation.
self.sfuuid = '1234'

# Register to placement topic.
topic = 'placement.ssm.' + self.sfuuid

self.manoconn.register_async_endpoint(self.on_place,topic= topic)

LOG.info("Subscribed to {0}".format(topic))

def on_place(self, ch, method, properties, payload):
"""
This method organises the placement calculation, and
provides the response for the SLM.
"""

LOG.info("Placement started")
message = yaml.load(payload)
topology = message['topology']
nsd = message['nsd']
functions = message['vnfds']
nap = message['nap']

mapping = self.placement_alg(nsd, functions, topology, nap)

if mapping is None:
LOG.info("The mapping calculation has failed.")
message = {}
message['error'] = 'Unable to perform placement.'
message['status'] = 'ERROR'

else:
LOG.info("The mapping calculation has succeeded.")
message = {}
message['error'] = None
message['status'] = "COMPLETED"
message['mapping'] = mapping

is_dict = isinstance(message, dict)
LOG.info("Type Dict: " + str(is_dict))

payload = yaml.dump(message)
self.manoconn.notify('placement.ssm.' + self.sfuuid,
payload,
correlation_id=properties.correlation_id)

return

def placement_alg(self, nsd, functions, topology, nap):
"""
This is the default placement algorithm that is used if the SLM
is responsible to perform the placement
"""
LOG.info("Mapping algorithm started.")
mapping = {}

ingress = nap['ingress'][0]['location']
egress = nap['egress'][0]['location']

total_core = 0
total_memory = 0

for vnfd in functions:
needed_cpu = vnfd['virtual_deployment_units'][0]['resource_requirements']['cpu']['vcpus']
needed_mem = vnfd['virtual_deployment_units'][0]['resource_requirements']['memory']['size']

total_core += needed_cpu
total_memory += needed_mem

for vim in topology:

if total_core <= (vim['core_total'] - vim['core_used']) and total_memory <= (vim['memory_total'] - vim['memory_used']):

#place all VNF in one POP
for vnfd in functions:
print('VNF ' + vnfd['name'] + ' mapped on VIM ' + vim['vim_uuid'])
mapping[vnfd['name']] = {}
mapping[vnfd['name']]['vim'] = vim['vim_uuid']
break

# Check if all VNFs have been mapped
if len(mapping.keys()) == len(functions):
LOG.info("Mapping succeeded: " + str(mapping))
return mapping
else:
return None

def main():
PlacementSSM()

if __name__ == '__main__':
main()
76 changes: 76 additions & 0 deletions son-ssm-examples/PSA_placement/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""
Copyright (c) 2015 SONATA-NFV
ALL RIGHTS RESERVED.
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.
Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
nor the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written
permission.
This work has been performed in the framework of the SONATA project,
funded by the European Commission under Grant number 671517 through
the Horizon 2020 and 5G-PPP programmes. The authors would like to
acknowledge the contributions of their colleagues of the SONATA
partner consortium (www.sonata-nfv.eu).
"""

# Always prefer setuptools over distutils
from setuptools import setup, find_packages
# To use a consistent encoding
from codecs import open
from os import path

here = path.abspath(path.dirname(__file__))

# Get the long description from the README file
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = f.read()

setup(
name='placement',

# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version='0.0.1',

description='Placement SSM example',
long_description=long_description,

# The project's main homepage.
url='https://github.com/sonata-nfv/son-mano-framework/tree/master/son-mano-specificmanager',

# Author details
author='Hadi Razzaghi Kouchaksaraei',
author_email='[email protected]',

# Choose your license
license='Apache 2.0',

# What does your project relate to?
keywords='NFV orchestrator',

packages=find_packages("placement"),
install_requires=['pika', 'pytest'],
setup_requires=['pytest-runner'],

# To provide executable scripts, use entry points in preference to the
# "scripts" keyword. Entry points provide cross-platform support and allow
# pip to create the appropriate form of executable for the target platform.
entry_points={
'console_scripts': ['placement=placement.__main__:main'],
},
)
Loading

0 comments on commit 6220474

Please sign in to comment.