Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ederson.brilhante committed Feb 19, 2018
2 parents 1e0a3e1 + e63d08c commit 3a4f4fd
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 57 deletions.
2 changes: 1 addition & 1 deletion docker-compose-sdn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ services:

odl:
container_name: netapi_odl
image: globocom/odl:4.4.0
image: globocom/odl:6.1.0
command: "/opt/opendaylight/bin/karaf server"
ports:
- "6653:6653"
Expand Down
1 change: 1 addition & 0 deletions fast_start_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export DJANGO_SETTINGS_MODULE='networkapi.settings_ci'

# Updates the SDN controller ip address
export REMOTE_CTRL_IP=$(nslookup netapi_odl | grep Address | tail -1 | awk '{print $2}')
echo "Found SDN controller at $REMOTE_CTRL_IP"

echo "Starting tests.."
python manage.py test "$@"
14 changes: 12 additions & 2 deletions networkapi/api_environment/facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from networkapi.equipamento.models import Equipamento
from networkapi.infrastructure.datatable import build_query_to_datatable_v3
from networkapi.plugins.factory import PluginFactory
from networkapi.api_equipment import exceptions as exceptions_eqpt
from networkapi.api_equipment import facade as facade_eqpt


log = logging.getLogger(__name__)
Expand Down Expand Up @@ -161,11 +163,19 @@ def delete_environment(env_ids):


def get_controller_by_envid(env_id):
""" Get all controllers from a given environment """

q_filter_environment = {
'equipmentcontrollerenvironment__environment': env_id
'equipmentcontrollerenvironment__environment': env_id,
'maintenance': 0
}

return Equipamento.objects.filter(Q(**q_filter_environment))
equips = Equipamento.objects.filter(Q(**q_filter_environment))

if facade_eqpt.all_equipments_are_in_maintenance(equips):
raise exceptions_eqpt.AllEquipmentsAreInMaintenanceException()

return equips


def list_flows_by_envid(env_id, flow_id=0):
Expand Down
2 changes: 1 addition & 1 deletion networkapi/api_environment/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,4 @@ def setup_eager_loading_default_vrf(queryset):
queryset = queryset.select_related(
'default_vrf',
)
return queryset
return queryset
6 changes: 4 additions & 2 deletions networkapi/api_environment/tasks/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ def async_add_flow(self, plugins, user_id, data):
""" Asynchronous flows insertion into environment equipment """

for plugin in plugins:
plugin.add_flow(data=data)
if plugin is not None:
plugin.add_flow(data=data)


@celery_app.task(bind=True, base=BaseTask, serializer='pickle')
def async_flush_environment(self, plugins, user_id, data):
""" Asynchronous flush and restore of flows of an environment """

for plugin in plugins:
plugin.update_all_flows(data=data)
if plugin is not None:
plugin.update_all_flows(data=data)
29 changes: 28 additions & 1 deletion networkapi/api_environment/tests/sanity/test_environment_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,40 @@ def test_get_success_one_environment_with_equipments(self):
expected_data = [
{'id': 1},
{'id': 2},
{'id': 10},
{'id': 7},
{'id': 8}
]
expected_data = json.dumps(expected_data, sort_keys=True)
self.compare_values(expected_data, data)

def test_get_success_one_environment_with_equipments_and_controllers(self):
"""
Test of success to get one environment with equipments and SDN
controllers
"""

response = self.client.get(
'/api/v3/environment/1/?include=equipments,sdn_controllers',
content_type='application/json',
HTTP_AUTHORIZATION=self.get_http_authorization('test'))

self.compare_status(200, response.status_code)

data = json.dumps(response.data['environments'][0]['equipments'],
sort_keys=True)
expected_data = [
{'id': 1},
{'id': 2},
{'id': 7},
{'id': 8}
]

expected_data = json.dumps(expected_data, sort_keys=True)
self.compare_values(expected_data, data)
assert isinstance(response.data["environments"][0]["sdn_controllers"],
list), "Wrong controller data type. Must be a list"
assert len(response.data["environments"][0]["sdn_controllers"]) > 0, \
"sdn_controllers must have at least one entry"

def test_get_success_one_environment_with_children(self):
"""Test of success to get one environment with children."""
Expand Down
136 changes: 104 additions & 32 deletions networkapi/api_interface/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

class InterfaceTypeSerializer(DynamicFieldsModelSerializer):

log.info("InterfaceTypeV3Serializer")

type = serializers.RelatedField(source='tipo')

class Meta:
Expand All @@ -36,45 +38,66 @@ class Meta:
'type',
)

default_fields = fields

basic_fields = fields

details_fields = fields


class PortChannelSerializer(DynamicFieldsModelSerializer):

log.info("PortChannelV3Serializer")

name = serializers.RelatedField(source='nome')

class Meta:
model = get_model('interface', 'PortChannel')

fields = (
'id',
'name',
'lacp',
)

default_fields = fields

basic_fields = (
'id',
'name',
)

details_fields = fields


class InterfaceV3Serializer(DynamicFieldsModelSerializer):

log.info("InterfaceV3Serializer")

protected = serializers.Field(source='protegida')
description = serializers.Field(source='descricao')
equipment = serializers.Field(source='equipamento')
native_vlan = serializers.Field(source='vlan_nativa')

interface_type = serializers.SerializerMethodField('get_interface_type')
equipamento = serializers.SerializerMethodField('get_equipment')
tipo = serializers.SerializerMethodField('get_interface_type')
channel = serializers.SerializerMethodField('get_channel')
front_interface = serializers.SerializerMethodField('get_front_interface')
back_interface = serializers.SerializerMethodField('get_back_interface')
ligacao_front = serializers.SerializerMethodField('get_front_interface')
ligacao_back = serializers.SerializerMethodField('get_back_interface')

def get_interface_type(self, obj):
return self.extends_serializer(obj, 'tipo_interface')
return self.extends_serializer(obj, 'tipo')

def get_channel(self, obj):
return self.extends_serializer(obj, 'port_channel')
return self.extends_serializer(obj, 'channel')

def get_front_interface(self, obj):
return self.extends_serializer(obj, 'interfaces')
return self.extends_serializer(obj, 'ligacao_front')

def get_back_interface(self, obj):
return self.extends_serializer(obj, 'interfaces')
return self.extends_serializer(obj, 'ligacao_back')

def get_equipment(self, obj):
return self.extends_serializer(obj, 'equipamento')

class Meta:
interface_model = get_app('interface', module_label='models')
Expand All @@ -84,49 +107,98 @@ class Meta:
'interface',
'protected',
'description',
'equipment',
'equipamento',
'description',
'native_vlan',
'interface_type',
'tipo',
'channel',
'front_interface',
'back_interface',
'ligacao_front',
'ligacao_back',
)

default_fields = (
default_fields = fields

basic_fields = (
'id',
'interface',
'protected',
'description',
'equipment',
'description',
'native_vlan',
'interface_type',
'channel',
'front_interface',
'back_interface',
'equipamento',
)

basic_fields = default_fields

details_fields = default_fields
details_fields = fields

def get_serializers(self):
"""Returns the mapping of serializers."""

equipment_serializers = get_app('api_equipment', module_label='serializers')

if not self.mapping:
self.mapping = {
'interface_type': {
'obj': 'interface_type_id'
'tipo__details': {
'serializer': InterfaceTypeSerializer,
'kwargs': {
'kind': 'details',
},
'obj': 'tipo'
},
'channel': {
'obj': 'channel_id'
},
'front_interface': {
'obj': 'front_interface_id'
'channel__basic': {
'serializer': PortChannelSerializer,
'kwargs': {
'kind': 'basic'
},
'obj': 'channel',
},
'back_interface': {
'obj': 'back_interface_id'
'ligacao_front__basic': {
'serializer': InterfaceV3Serializer,
'kwargs': {
'kind': 'basic'
},
'obj': 'ligacao_front',
},
'ligacao_front__details': {
'serializer': InterfaceV3Serializer,
'kwargs': {
'kind': 'details'
},
'obj': 'ligacao_front',
},
'ligacao_back__basic': {
'serializer': InterfaceV3Serializer,
'kwargs': {
'kind': 'basic'
},
'obj': 'ligacao_back',
},
'ligacao_back__details': {
'serializer': InterfaceV3Serializer,
'kwargs': {
'kind': 'details'
},
'obj': 'ligacao_back',
},
'equipamento': {
'obj': 'equipamento_id'
},
'equipamento__basic': {
'serializer': equipment_serializers.EquipmentV3Serializer,
'kwargs': {
'kind': 'basic'
},
'obj': 'equipamento',
},
'equipamento__details': {
'serializer': equipment_serializers.EquipmentV3Serializer,
'kwargs': {
'kind': 'details'
},
'obj': 'equipamento',
},

}

@staticmethod
def setup_eager_loading_channel(queryset):
log.info('Using setup_eager_loading_channel')
queryset = queryset.select_related('channel')
return queryset
9 changes: 4 additions & 5 deletions networkapi/plugins/SDN/ODL/fixtures/initial_equipments.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{
"fields": {
"marca": "1",
"nome": "Beryllium"
"nome": "Carbon"
},
"model": "equipamento.modelo",
"pk": 1
Expand Down Expand Up @@ -62,11 +62,10 @@
},
{
"fields": {
"ambiente": 1,
"equipamento": 10,
"is_router": false
"environment": 1,
"equipment": 10
},
"model": "equipamento.equipamentoambiente",
"model": "equipamento.equipmentcontrollerenvironment",
"pk": 3
},
{
Expand Down
Loading

0 comments on commit 3a4f4fd

Please sign in to comment.