Open Kilda Java Documentation
context.py
Go to the documentation of this file.
1 # Copyright 2017 Telstra Open Source
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 #
15 
16 import logging.config
17 import os
18 import pathlib
19 
20 from kilda.traffexam import const
21 from kilda.traffexam import common
22 from kilda.traffexam import exc
23 
24 
25 class Context(object):
26  is_debug = False
27 
28  root = os.path.join(os.sep, 'var', 'run', const.PROJECT_NAME)
29  service = None
30  _init_done = False
31 
32  def __init__(self, iface, rest_bind):
33  self.iface = iface
34  self.rest_bind = rest_bind
35 
38  self._acquired_resources = []
39 
40  self.set_root(self.root)
41  self.set_debug_mode(self.is_debug)
42 
43  self._init_done = True
44 
45  def close(self):
46  for resource in self._acquired_resources:
47  resource.release()
48 
49  def path(self, *chunks):
50  return self.root.joinpath(*chunks)
51 
52  def acquire_resources(self, *resources):
53  for allocator in resources:
54  self._acquired_resources.insert(0, allocator(self))
55 
57  name = '{}-{}.lock'.format(self.iface.name, self.iface.index)
58  return str(self.path(name))
59 
61  return '{}{}.{}'.format(
62  const.IF_PREFIX, self.iface.name, self.iface.index)
63 
64  def make_bridge_name(self):
65  return '{}gw.{}'.format(const.IF_PREFIX, self.iface.index)
66 
68  return '{}nsgw.{}'.format(const.IF_PREFIX, self.iface.index)
69 
70  def set_root(self, root):
71  self.root = pathlib.Path(root)
72  if not self._init_done:
73  self.root.mkdir(parents=True, exist_ok=True)
74  return self
75 
77  stderr = logging.StreamHandler()
78  stderr.setFormatter('%(asctime)s %(levelname)s %(name)s - %(message)s')
79 
80  log = logging.getLogger()
81  log.addHandler(stderr)
82  log.setLevel(logging.INFO)
83  return self
84 
85  def set_logging_config(self, config, incremental=False):
86  try:
87  logging.config.fileConfig(
88  config, disable_existing_loggers=not incremental)
89  except (IOError, OSError) as e:
90  raise exc.InvalidLoggingConfigError(config, e)
91  return self
92 
93  def set_debug_mode(self, mode):
94  self.is_debug = mode
95  return self
96 
97  def set_service_adapter(self, adapter):
98  self.service = adapter
99  return self
100 
101 
102 class ContextConsumer(object):
103  def __init__(self, context):
104  self.context = context
def __init__(self, iface, rest_bind)
Definition: context.py:32
def path(self, chunks)
Definition: context.py:49
def set_logging_config(self, config, incremental=False)
Definition: context.py:85
def set_debug_mode(self, mode)
Definition: context.py:93
def set_service_adapter(self, adapter)
Definition: context.py:97
def set_root(self, root)
Definition: context.py:70
def acquire_resources(self, resources)
Definition: context.py:52
def make_network_namespace_name(self)
Definition: context.py:60