blob: cb8a4ae4fa2a5556161acd8a7ec9bdba3dccf933 [file] [log] [blame]
Chip Bolingab8863d2018-03-22 14:50:31 -05001# Copyright 2017-present Adtran, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from voltha.core.flow_decomposer import *
16from evc import EVC
17from twisted.internet import defer
18from twisted.internet.defer import returnValue, inlineCallbacks
19
20log = structlog.get_logger()
21
22EVC_NAME_FORMAT = 'VOLTHA-UTILITY-{}' # format(flow.vlan_id)
23EVC_NAME_REGEX_ALL = EVC_NAME_FORMAT.format('*')
24
25
26_utility_evcs = {} # device-id -> flow dictionary
27 # |
28 # +-> untagged-vlan-id -> evcs
29
30
31class UtilityEVC(EVC):
32 """
33 Class to wrap orphan ingress ACLs EVC functionality
34 """
35 def __init__(self, flow_entry):
36 super(UtilityEVC, self).__init__(flow_entry)
37 self._downstream_flows = {flow_entry.flow_id} # Matching Downstream Flow IDs
38 self.service_evc = True
39
40 def __str__(self):
41 return "VOLTHA-UTILITY-{}: MEN: {}, VLAN: {}".format(self._name, self._men_ports, self._s_tag)
42
43 def _create_name(self):
44 #
45 # TODO: Take into account selection criteria and output to make the name
46 #
47 return EVC_NAME_FORMAT.format(self._flow.vlan_id)
48
49 @staticmethod
50 def create(flow_entry):
51 device_id = flow_entry.device_id
52 evc_table = _utility_evcs.get(device_id)
53
54 if evc_table is None:
55 _utility_evcs[device_id] = dict()
56 evc_table = _utility_evcs[device_id]
57
58 try:
59 evc = evc_table.get(flow_entry.vlan_id)
60
61 if evc is None:
62 # Create EVC and initial EVC Map
63 evc = UtilityEVC(flow_entry)
64 evc_table[flow_entry.vlan_id] = evc
65 else:
66 if flow_entry.flow_id in evc.downstream_flows: # TODO: Debug only to see if flow_ids are unique
67 pass
68 else:
69 evc.add_downstream_flows(flow_entry.flow_id)
70
71 return evc
72
73 except Exception as e:
74 log.exception('untagged-create', e=e)
75 return None
76
77 @property
78 def flow_entry(self):
79 return self._flow
80
81 @property
82 def downstream_flows(self):
83 return frozenset(self._downstream_flows)
84
85 def add_downstream_flows(self, flow_id):
86 self._downstream_flows.add(flow_id)
87
88 def remove_downstream_flows(self, flow_id):
89 self._downstream_flows.discard(flow_id)
90
91 @inlineCallbacks
92 def remove(self, remove_maps=True):
93 """
94 Remove EVC (and optional associated EVC-MAPs) from hardware
95 :param remove_maps: (boolean)
96 :return: (deferred)
97 """
98 log.info('removing', evc=self, remove_maps=remove_maps)
99
100 device_id = self._handler.device_id
101 flow_id = self._flow.id
102 evc_table = _utility_evcs.get(device_id)
103
104 if evc_table is None or flow_id not in evc_table:
105 returnValue('NOP')
106
107 # Remove flow reference
108 if self._flow.flow_id in self._downstream_flows:
109 del self._downstream_flows[self._flow.flow_id]
110
111 if len(self._downstream_flows) == 0:
112 # Use base class to clean up
113 returnValue(super(UtilityEVC, self).remove(remove_maps=True))
114
115 returnValue('More references')
116
117 @inlineCallbacks
118 def delete(self, delete_maps=True):
119 """
120 Remove from hardware and delete/clean-up EVC Object
121 """
122 log.info('deleting', evc=self, delete_maps=delete_maps)
123
124 try:
125 dl = [self.remove()]
126 if delete_maps:
127 for evc_map in self.evc_maps:
128 dl.append(evc_map.delete(None)) # TODO: implement bulk-flow procedures
129
130 yield defer.gatherResults(dl, consumeErrors=True)
131
132 except Exception as e:
133 log.exception('removal', e=e)
134
135 self._evc_maps = None
136 f, self._flow = self._flow, None
137 if f is not None and f.handler is not None:
138 f.handler.remove_evc(self)
139
140 def reflow(self, reflow_maps=True):
141 pass # TODO: Implement or use base class?
142
143 @staticmethod
144 def remove_all(client, regex_=EVC_NAME_REGEX_ALL):
145 """
146 Remove all matching EVCs from hardware
147 :param client: (ncclient) NETCONF Client to use
148 :param regex_: (String) Regular expression for name matching
149 :return: (deferred)
150 """
151 pass # TODO: ???