blob: 94d08360615195477ccd005971717f7e9e98f123 [file] [log] [blame]
Khen Nursimulua7b842a2016-12-03 23:28:42 -05001#!/usr/bin/env python
2#
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -05003# Copyright 2017 the original author or authors.
Khen Nursimulua7b842a2016-12-03 23:28:42 -05004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050017import structlog
18import sys
Khen Nursimulua7b842a2016-12-03 23:28:42 -050019from constants import Constants as C
20
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050021URN_PREFIX = "urn:opencord:params:xml:ns:voltha:"
22log = structlog.get_logger()
23
Khen Nursimulua7b842a2016-12-03 23:28:42 -050024
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050025class Capabilities:
Khen Nursimulua7b842a2016-12-03 23:28:42 -050026 def __init__(self):
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050027 self.server_caps = set()
Khen Nursimulua7b842a2016-12-03 23:28:42 -050028 self.client_caps = set()
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050029 self.voltha_schemas = set()
30 self.schema_dir = None
Khen Nursimulua7b842a2016-12-03 23:28:42 -050031
32 def add_client_capability(self, cap):
33 self.client_caps.add(cap)
34
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050035 def set_server_capabilities(self, schemas):
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050036 # first add the basic capabilities
37 self.server_caps.add(C.NETCONF_BASE_10)
38 self.server_caps.add(C.NETCONF_BASE_11)
39 self.server_caps.add(C.NETCONF_MONITORING)
40 for schema in schemas:
41 self.server_caps.add(''.join([URN_PREFIX, schema]))
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050042 self.server_caps.add(''.join([
43 URN_PREFIX,
44 schema,
45 ':writable-running'])
46 )
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050047 self.voltha_schemas.add(schema)
Khen Nursimuluaaac7ee2016-12-11 22:03:52 -050048
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050049 def set_schema_dir(self, schema_dir):
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050050 self.schema_dir = schema_dir
Khen Nursimuluaaac7ee2016-12-11 22:03:52 -050051
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050052 def get_yang_schemas_definitions(self):
53 defs = []
54 for schema in self.voltha_schemas:
55 defs.append(
56 {
57 'id': schema,
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050058 'version': '2016-11-15',
59 # TODO: need to extract from voltha
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050060 'format': 'yang',
61 'location': 'NETCONF',
62 'namespace': ''.join([URN_PREFIX, schema])
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050063 }
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050064 )
65 return defs
Khen Nursimuluaaac7ee2016-12-11 22:03:52 -050066
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050067 def is_schema_supported(self, schema):
68 return schema in self.voltha_schemas
Khen Nursimuluaaac7ee2016-12-11 22:03:52 -050069
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050070 def get_schema_content(self, schema):
71 if self.schema_dir not in sys.path:
72 sys.path.insert(0, self.schema_dir)
Khen Nursimuluaaac7ee2016-12-11 22:03:52 -050073
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050074 try:
75 with open(''.join([self.schema_dir, '/', schema, '.yang']),
76 'r') as f:
77 content = f.read()
78 return content
79 except Exception as e:
80 log.error("error-opening-file", file=''.join([schema, '.yang']),
81 dir=self.schema_dir, exception=repr(e))