Do not tag outputs with VLANs for SFC
[osm/vim-emu.git] / src / emuvim / api / openstack / resources / port_chain.py
1 # Copyright (c) 2015 SONATA-NFV and Paderborn University
2 # ALL RIGHTS RESERVED.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # Neither the name of the SONATA-NFV, Paderborn University
17 # nor the names of its contributors may be used to endorse or promote
18 # products derived from this software without specific prior written
19 # permission.
20 #
21 # This work has been performed in the framework of the SONATA project,
22 # funded by the European Commission under Grant number 671517 through
23 # the Horizon 2020 and 5G-PPP programmes. The authors would like to
24 # acknowledge the contributions of their colleagues of the SONATA
25 # partner consortium (www.sonata-nfv.eu).
26 import random
27 import uuid
28 import logging
29
30
31 class PortChain(object):
32 def __init__(self, name):
33 self.id = str(uuid.uuid4())
34 self.tenant_id = "abcdefghijklmnopqrstuvwxyz123456"
35 self.name = name
36 self.description = ""
37 self.port_pair_groups = list()
38 self.flow_classifiers = list()
39 self.chain_parameters = dict()
40
41 # Cookie for internal identification of installed flows (e.g. to delete
42 # them)
43 self.cookie = random.randint(1, 0xffffffff)
44
45 def create_dict(self, compute):
46 representation = {
47 "name": self.name,
48 "tenant_id": self.tenant_id,
49 "description": self.description,
50 "flow_classifiers": self.flow_classifiers,
51 "port_pair_groups": self.port_pair_groups,
52 "id": self.id
53 }
54 return representation
55
56 def _get_port_pair(self, port_pair_group_id, compute):
57 port_pair_group = compute.find_port_pair_group_by_name_or_id(port_pair_group_id)
58 if len(port_pair_group.port_pairs) != 1:
59 raise RuntimeError("Only port pair groups with a single port pair are supported!")
60 return compute.find_port_pair_by_name_or_id(port_pair_group.port_pairs[0])
61
62 def install(self, compute):
63
64 port_pair_chain = map(lambda port_pair_group_id: self._get_port_pair(port_pair_group_id, compute),
65 self.port_pair_groups)
66 ingress_ports = map(lambda port_pair: port_pair.ingress, port_pair_chain)
67 egress_ports = map(lambda port_pair: port_pair.ingress, port_pair_chain)
68 chain_start = ingress_ports[0]
69 chain_rest = ingress_ports[1:]
70
71 source_port_to_chain_start = []
72 for flow_classifier_id in self.flow_classifiers:
73 flow_classifier = compute.find_flow_classifier_by_name_or_id(flow_classifier_id)
74 if flow_classifier:
75 port = compute.find_port_by_name_or_id(flow_classifier.logical_source_port)
76 source_port_to_chain_start.append((port, chain_start))
77
78 chain = source_port_to_chain_start + zip(egress_ports, chain_rest)
79
80 for (egress_port, ingress_port) in chain:
81 server_egress = None
82 server_ingress = None
83 for server in compute.computeUnits.values():
84 if egress_port.name in server.port_names or egress_port.id in server.port_names:
85 server_egress = server
86 if ingress_port.name in server.port_names or ingress_port.id in server.port_names:
87 server_ingress = server
88
89 if not server_egress:
90 raise RuntimeError("Neutron SFC: egress port %s not connected to any server." %
91 egress_port.name)
92 if not server_ingress:
93 raise RuntimeError("Neutron SFC: ingress port %s not connected to any server." %
94 ingress_port.name)
95
96 compute.dc.net.setChain(
97 server_egress.name, server_ingress.name,
98 egress_port.intf_name, ingress_port.intf_name,
99 cmd="add-flow", cookie=self.cookie, priority=10, bidirectional=False,
100 monitor=False, skip_vlan_tag=True
101 )
102
103 def uninstall(self, compute):
104 # TODO: implement
105 logging.warn("Removing flows is currently not implemented.")
106
107 def update(self):
108 # TODO: implement
109 logging.warn("Updating flows is currently not implemented.")