blob: a452c4f457730edc222eacd09cd2f8a21fb0bda2 [file] [log] [blame]
Matt Jeanneret40f28392019-12-04 18:21:46 -05001#
2# Copyright 2020 the original author or authors.
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#
16from __future__ import absolute_import
17from .mib_db_api import CREATED_KEY, MODIFIED_KEY
18import json
19from datetime import datetime
20import structlog
21from pyvoltha.common.utils.registry import registry
22from pyvoltha.common.config.config_backend import EtcdStore
23import six
24
25
26class MibTemplateDb(object):
27
28 BASE_PATH = 'service/voltha/omci_mibs/templates'
29 TEMPLATE_PATH = '{}/{}/{}'
30
31 def __init__(self, vendor_id, equipment_id, software_version, serial_number, mac_address):
32 self.log = structlog.get_logger()
33 self._jsonstring = b''
34
35 # lookup keys
36 self._vendor_id = vendor_id
37 self._equipment_id = equipment_id
38 self._software_version = software_version
39
40 # replacement values
41 self._serial_number = serial_number
42 self._mac_address = mac_address
43
44 self.args = registry('main').get_args()
45 host, port = self.args.etcd.split(':', 1)
46 self._kv_store = EtcdStore(host, port, MibTemplateDb.BASE_PATH)
47 self.loaded = False
48 self._load_template()
49
50 def get_template_instance(self):
51 # swap out tokens with specific data
52 fixup = self._jsonstring.decode('ascii')
53 fixup = fixup.replace('%SERIAL_NUMBER%', self._serial_number)
54 fixup = fixup.replace('%MAC_ADDRESS%', self._mac_address)
55
56 # convert to a dict() compatible with mib_db_dict
57 newdb = self._load_from_json(fixup)
58 now = datetime.utcnow()
59
60 # populate timestamps as if it was mib uploaded
61 for cls_id, cls_data in newdb.items():
62 if isinstance(cls_id, int):
63 for inst_id, inst_data in cls_data.items():
64 if isinstance(inst_id, int):
65 newdb[cls_id][inst_id][CREATED_KEY] = now
66 newdb[cls_id][inst_id][MODIFIED_KEY] = now
67
68 return newdb
69
70 def _load_template(self):
71 path = self._get_template_path()
72 try:
73 self._jsonstring = self._kv_store[path]
74 self.log.debug('found-template-data', path=path)
75 self.loaded = True
76 except KeyError:
77 self.log.warn('no-template-found', path=path)
78
79 def _get_template_path(self):
80 if not isinstance(self._vendor_id, six.string_types):
81 raise TypeError('Vendor ID is a string')
82
83 if not isinstance(self._equipment_id, six.string_types):
84 raise TypeError('Equipment ID is a string')
85
86 if not isinstance(self._software_version, six.string_types):
87 raise TypeError('Software Version is a string')
88
89 fmt = MibTemplateDb.TEMPLATE_PATH
90 return fmt.format(self._vendor_id, self._equipment_id, self._software_version)
91
92 def _load_from_json(self, jsondata):
93
94 def json_obj_parser(x):
95 if isinstance(x, dict):
96 results = dict()
97 for (key, value) in x.items():
98 try:
99 key = int(key)
100 except (ValueError, TypeError):
101 pass
102
103 results.update({key: value})
104 return results
105 return x
106
107 template_data = json.loads(jsondata, object_hook=json_obj_parser)
108 return template_data