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