Initial commit
[ta/config-manager.git] / cmdatahandlers / src / cmdatahandlers / time / config.py
1 # Copyright 2019 Nokia
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 from cmdatahandlers.api import configerror
16 from cmdatahandlers.api import config
17 from cmdatahandlers.api import utils
18
19 class Config(config.Config):
20     def __init__(self, confman):
21         super(Config, self).__init__(confman)
22         self.ROOT='cloud.time'
23         self.DOMAIN='time'
24
25     def init(self):
26         pass
27
28     def validate(self):
29         self.validate_root()
30
31         if 'zone' not in self.config[self.ROOT]:
32             raise configerror.ConfigError('No zone configuration found')
33
34         if 'ntp_servers' not in self.config[self.ROOT]:
35             raise configerror.ConfigError('No ntp servers found')
36
37         if 'auth_type' not in self.config[self.ROOT]:
38             self.config[self.ROOT]['auth_type'] = 'crypto'
39
40         if 'serverkeys_path' not in self.config[self.ROOT]:
41             self.config[self.ROOT]['serverkeys_path'] = ''
42
43         self._validate_time_zone()
44         self._validate_ntp_servers()
45
46     def _validate_time_zone(self):
47         import pytz
48         try:
49             zone = self.get_zone()
50             pytz.timezone(zone)
51         except:
52             raise configerror.ConfigError('The timezone %s is not valid' % zone)
53
54     def _validate_ntp_servers(self):
55         servers = self.get_ntp_servers()
56         utils.validate_list_items_unique(servers)
57
58         for server in servers:
59             utils.validate_ipv4_address(server)
60
61
62     """ get the time zone
63
64         Return:
65
66         A string representing time zone
67
68         Raise:
69
70         ConfigError in-case of an error
71     """
72     def get_zone(self):
73         self.validate_root()
74         return self.config[self.ROOT]['zone']
75
76     """ get the ntp servers
77
78         Return:
79
80         A list of ntp server addresses.
81
82         Raise:
83
84         ConfigError in-case of an error
85     """
86     def get_ntp_servers(self):
87         self.validate_root()
88         return self.config[self.ROOT]['ntp_servers']
89     
90     def get_auth_type(self):
91         self.validate_root()
92         return self.config[self.ROOT]['auth_type']
93
94     def get_time_config(self):
95         self.validate_root()
96         return self.config[self.ROOT]
97
98     def set_config(self, **args):
99         if args['auth_type'] is not None:
100             self.config[self.ROOT]['auth_type'] = args['auth_type']
101         if args['servers'] is not None:
102             self.config[self.ROOT]['ntp_servers'] = args['servers']
103         if args['keyfile_url'] is not None:
104             self.config[self.ROOT]['serverkeys_path'] = args['keyfile_url']
105         if args['zone'] is not None:
106             self.config[self.ROOT]['zone'] = args['zone']
107         self.validate()
108         return self.config[self.ROOT]