Merge pull request #235 from splietker/master
[osm/vim-emu.git] / src / emuvim / api / openstack / resources / port_chain.py
1 import random
2 import uuid
3 import logging
4
5
6 class PortChain(object):
7 def __init__(self, name):
8 self.id = str(uuid.uuid4())
9 self.tenant_id = "abcdefghijklmnopqrstuvwxyz123456"
10 self.name = name
11 self.description = ""
12 self.port_pair_groups = list()
13 self.flow_classifiers = list()
14 self.chain_parameters = dict()
15
16 # Cookie for internal identification of installed flows (e.g. to delete them)
17 self.cookie = random.randint(1, 0xffffffff)
18
19 def create_dict(self, compute):
20 representation = {
21 "name": self.name,
22 "tenant_id": self.tenant_id,
23 "description": self.description,
24 "flow_classifiers": self.flow_classifiers,
25 "port_pair_groups": self.port_pair_groups,
26 "id": self.id
27 }
28 return representation
29
30 def install(self, compute):
31 for flow_classifier_id in self.flow_classifiers:
32 flow_classifier = compute.find_flow_classifier_by_name_or_id(flow_classifier_id)
33 if flow_classifier:
34 pass
35 # TODO: for every flow classifier create match and pass it to setChain
36
37 for group_id in self.port_pair_groups:
38 port_pair_group = compute.find_port_pair_group_by_name_or_id(group_id)
39 for port_pair_id in port_pair_group.port_pairs:
40 port_pair = compute.find_port_pair_by_name_or_id(port_pair_id)
41
42 server_ingress = None
43 server_egress = None
44 for server in compute.computeUnits.values():
45 if port_pair.ingress.name in server.port_names:
46 server_ingress = server
47 elif port_pair.egress.name in server.port_names:
48 server_egress = server
49
50 # TODO: Not sure, if this should throw an error
51 if not server_ingress:
52 logging.warn("Neutron SFC: ingress port %s not connected." % str(port_pair.ingress.name))
53 continue
54 if not server_egress:
55 logging.warn("Neutron SFC: egress port %s not connected." % str(port_pair.egress.name))
56 continue
57
58 compute.dc.net.setChain(
59 server_ingress.name, server_egress.name,
60 port_pair.ingress.intf_name, port_pair.egress.intf_name,
61 cmd="add-flow", cookie=self.cookie, priority=10, bidirectional=False,
62 monitor=False
63 )
64
65 def uninstall(self, compute):
66 # TODO: implement
67 logging.warn("Removing flows is currently not implemented.")
68
69 def update(self):
70 # TODO: implement
71 logging.warn("Updating flows is currently not implemented.")