Stop setting 8.8.8.8 as default DNS server
[snaps.git] / snaps / config / qos.py
1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  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 import enum
16
17
18 class Consumer(enum.Enum):
19     """
20     QoS Specification consumer types
21     """
22     front_end = 'front-end'
23     back_end = 'back-end'
24     both = 'both'
25
26
27 class QoSConfig(object):
28     def __init__(self, **kwargs):
29         """
30         Constructor
31         :param name: the qos's name (required)
32         :param consumer: the qos's consumer type of the enum type Consumer
33                          (required)
34         :param specs: dict of key/values
35         """
36
37         self.name = kwargs.get('name')
38
39         if kwargs.get('consumer'):
40             self.consumer = map_consumer(kwargs['consumer'])
41         else:
42             self.consumer = None
43
44         self.specs = kwargs.get('specs')
45         if not self.specs:
46             self.specs = dict()
47
48         if not self.name or not self.consumer:
49             raise QoSConfigError(
50                 "The attributes name and consumer are required")
51
52
53 def map_consumer(consumer):
54     """
55     Takes a the protocol value maps it to the Consumer enum. When None return
56     None
57     :param consumer: the value to map to the Enum
58     :return: the Protocol enum object
59     :raise: Exception if value is invalid
60     """
61     if not consumer:
62         return None
63     elif isinstance(consumer, Consumer):
64         return consumer
65     elif isinstance(consumer, str):
66         proto_str = str(consumer)
67         if proto_str == 'front-end':
68             return Consumer.front_end
69         elif proto_str == 'back-end':
70             return Consumer.back_end
71         elif proto_str == 'both':
72             return Consumer.both
73         else:
74             raise QoSConfigError('Invalid Consumer - ' + proto_str)
75     else:
76         if consumer.value == 'front-end':
77             return Consumer.front_end
78         elif consumer.value == 'back-end':
79             return Consumer.back_end
80         elif consumer.value == 'both':
81             return Consumer.both
82         else:
83             raise QoSConfigError('Invalid Consumer - ' + consumer.value)
84
85
86 class QoSConfigError(Exception):
87     """
88     Exception to be thrown when an qos settings are incorrect
89     """
90
91     def __init__(self, message):
92         Exception.__init__(self, message)