7c0a2b004a9fe514974f4d911f3ec02e51fadedc
[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 install(self, compute):
57 for flow_classifier_id in self.flow_classifiers:
58 flow_classifier = compute.find_flow_classifier_by_name_or_id(
59 flow_classifier_id)
60 if flow_classifier:
61 pass
62 # TODO: for every flow classifier create match and pass it to
63 # setChain
64
65 for group_id in self.port_pair_groups:
66 port_pair_group = compute.find_port_pair_group_by_name_or_id(
67 group_id)
68 for port_pair_id in port_pair_group.port_pairs:
69 port_pair = compute.find_port_pair_by_name_or_id(port_pair_id)
70
71 server_ingress = None
72 server_egress = None
73 for server in compute.computeUnits.values():
74 if port_pair.ingress.name in server.port_names or port_pair.ingress.id in server.port_names:
75 server_ingress = server
76 if port_pair.egress.name in server.port_names or port_pair.egress.id in server.port_names:
77 server_egress = server
78
79 # TODO: Not sure, if this should throw an error
80 if not server_ingress:
81 logging.warn("Neutron SFC: ingress port %s not connected." % str(
82 port_pair.ingress.name))
83 continue
84 if not server_egress:
85 logging.warn("Neutron SFC: egress port %s not connected." % str(
86 port_pair.egress.name))
87 continue
88
89 compute.dc.net.setChain(
90 server_ingress.name, server_egress.name,
91 port_pair.ingress.intf_name, port_pair.egress.intf_name,
92 cmd="add-flow", cookie=self.cookie, priority=10, bidirectional=False,
93 monitor=False
94 )
95
96 def uninstall(self, compute):
97 # TODO: implement
98 logging.warn("Removing flows is currently not implemented.")
99
100 def update(self):
101 # TODO: implement
102 logging.warn("Updating flows is currently not implemented.")