Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 3 | # Copyright 2017 the original author or authors. |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 4 | # |
| 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 Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 17 | import structlog |
| 18 | import sys |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 19 | from constants import Constants as C |
| 20 | |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 21 | URN_PREFIX = "urn:opencord:params:xml:ns:voltha:" |
| 22 | log = structlog.get_logger() |
| 23 | |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 24 | |
Khen Nursimulu | 8ffb893 | 2017-01-26 13:40:49 -0500 | [diff] [blame] | 25 | class Capabilities: |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 26 | def __init__(self): |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 27 | self.server_caps = set() |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 28 | self.client_caps = set() |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 29 | self.voltha_schemas = set() |
| 30 | self.schema_dir = None |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 31 | |
| 32 | def add_client_capability(self, cap): |
| 33 | self.client_caps.add(cap) |
| 34 | |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 35 | def set_server_capabilities(self, schemas): |
| 36 | # prefix = "urn:ietf:params:netconf:capability:" |
| 37 | # first add the basic capabilities |
| 38 | self.server_caps.add(C.NETCONF_BASE_10) |
| 39 | self.server_caps.add(C.NETCONF_BASE_11) |
| 40 | self.server_caps.add(C.NETCONF_MONITORING) |
| 41 | for schema in schemas: |
| 42 | self.server_caps.add(''.join([URN_PREFIX, schema])) |
Khen Nursimulu | 8ffb893 | 2017-01-26 13:40:49 -0500 | [diff] [blame] | 43 | self.server_caps.add(''.join([ |
| 44 | URN_PREFIX, |
| 45 | schema, |
| 46 | ':writable-running']) |
| 47 | ) |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 48 | self.voltha_schemas.add(schema) |
Khen Nursimulu | aaac7ee | 2016-12-11 22:03:52 -0500 | [diff] [blame] | 49 | |
Khen Nursimulu | 8ffb893 | 2017-01-26 13:40:49 -0500 | [diff] [blame] | 50 | def set_schema_dir(self, schema_dir): |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 51 | self.schema_dir = schema_dir |
Khen Nursimulu | aaac7ee | 2016-12-11 22:03:52 -0500 | [diff] [blame] | 52 | |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 53 | def get_yang_schemas_definitions(self): |
| 54 | defs = [] |
| 55 | for schema in self.voltha_schemas: |
| 56 | defs.append( |
| 57 | { |
| 58 | 'id': schema, |
Khen Nursimulu | 8ffb893 | 2017-01-26 13:40:49 -0500 | [diff] [blame] | 59 | 'version': '2016-11-15', |
| 60 | # TODO: need to extract from voltha |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 61 | 'format': 'yang', |
| 62 | 'location': 'NETCONF', |
| 63 | 'namespace': ''.join([URN_PREFIX, schema]) |
Khen Nursimulu | 8ffb893 | 2017-01-26 13:40:49 -0500 | [diff] [blame] | 64 | } |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 65 | ) |
| 66 | return defs |
Khen Nursimulu | aaac7ee | 2016-12-11 22:03:52 -0500 | [diff] [blame] | 67 | |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 68 | def is_schema_supported(self, schema): |
| 69 | return schema in self.voltha_schemas |
Khen Nursimulu | aaac7ee | 2016-12-11 22:03:52 -0500 | [diff] [blame] | 70 | |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 71 | def get_schema_content(self, schema): |
| 72 | if self.schema_dir not in sys.path: |
| 73 | sys.path.insert(0, self.schema_dir) |
Khen Nursimulu | aaac7ee | 2016-12-11 22:03:52 -0500 | [diff] [blame] | 74 | |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 75 | try: |
| 76 | with open(''.join([self.schema_dir, '/', schema, '.yang']), |
| 77 | 'r') as f: |
| 78 | content = f.read() |
| 79 | return content |
| 80 | except Exception as e: |
| 81 | log.error("error-opening-file", file=''.join([schema, '.yang']), |
| 82 | dir=self.schema_dir, exception=repr(e)) |