Add validation for performance tuning option
[ta/cm-plugins.git] / validators / src / VersionValidation.py
1 #!/usr/bin/python
2 # Copyright 2019 Nokia
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
16 import logging
17 import json
18
19 from cmframework.apis import cmvalidator
20 from cmdatahandlers.api import validation
21
22
23 class VersionValidation(cmvalidator.CMValidator):
24     domain = 'cloud.version'
25     version = [2, 0, 5]
26
27     # Should be same as 'version' in release build
28     devel_version = [2, 0, 5]
29
30     # Example:
31     # {1: 'This is the first change requiring new template version (1.1.0)',
32     #  2: 'This is the second change requiring new template version (1.2.0)',
33     #  3: 'This is the third change requiring new template version (1.3.0)'}
34     change_log = {}
35
36     def get_subscription_info(self):
37         logging.debug('get_subscription info called')
38         return r'^cloud\.version$'
39
40     def validate_set(self, dict_key_value):
41         logging.debug('validate_set called with %s' % str(dict_key_value))
42
43         for key, value in dict_key_value.iteritems():
44             version = json.loads(value)
45             if key == self.domain:
46                 self.validate_version(version)
47             else:
48                 raise validation.ValidationError('Unexpected configuration %s' % key)
49
50     def validate_delete(self, prop):
51         logging.debug('validate_delete called with %s' % str(prop))
52         raise validation.ValidationError('%s cannot be deleted' % self.domain)
53
54     def validate_version(self, version_str):
55         if not version_str:
56             raise validation.ValidationError('Missing configuration template version')
57         if not isinstance(version_str, basestring):
58             raise validation.ValidationError('Version configuration should be a string')
59         data = version_str.split('.')
60         if len(data) != 3:
61             raise validation.ValidationError('Invalid version data syntax in configuration')
62         version = []
63         for i in data:
64             if not i.isdigit():
65                 raise validation.ValidationError('Version data does not consist of numbers')
66             version.append(int(i))
67         if self.version != self.devel_version and version == self.devel_version:
68             msg = 'Accepting development version %s' % version_str
69             logging.warning(msg)
70         elif version[0] != self.version[0]:
71             reason = 'Major configuration template version mismatch (%s does not match with %s)' \
72                      % (version_str, str(self.version))
73             raise validation.ValidationError(reason)
74         elif version[1] != self.version[1]:
75             reason = 'Configuration template version mismatch (%s does not match with %s)' \
76                      % (version_str, str(self.version))
77             self.log_changes(version[1])
78             raise validation.ValidationError(reason)
79         elif version[2] != self.version[2]:
80             msg = 'Minor configuration template version mismatch, check the latest template changes'
81             logging.warning(msg)
82
83     def log_changes(self, version):
84         for key, log in self.change_log.iteritems():
85             if key > version:
86                 logging.warning('Changes in template version %s.%s.0: %s' % (str(self.version[0]),
87                                                                              str(key), log))