Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 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 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 15 | from __future__ import absolute_import |
| 16 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 17 | import os |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 18 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 19 | from multistructlog import create_logger |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 20 | from xosconfig import Config |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 21 | |
| 22 | log = create_logger(Config().get("logging")) |
| 23 | |
| 24 | |
| 25 | class ModelLoadClient(object): |
Scott Baker | 7ff8ad9 | 2019-02-15 17:02:41 -0800 | [diff] [blame] | 26 | REQUIRE_CLEAN = 0 |
| 27 | AUTOMATICALLY_CLEAN = 1 |
| 28 | PURGE = 2 |
| 29 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 30 | def __init__(self, api): |
| 31 | self.api = api |
| 32 | |
| 33 | def upload_models(self, name, dir, version="unknown"): |
| 34 | request = self.api.dynamicload_pb2.LoadModelsRequest(name=name, version=version) |
| 35 | |
| 36 | for fn in os.listdir(dir): |
| 37 | if fn.endswith(".xproto"): |
| 38 | item = request.xprotos.add() |
| 39 | item.filename = fn |
| 40 | item.contents = open(os.path.join(dir, fn)).read() |
| 41 | |
| 42 | models_fn = os.path.join(dir, "models.py") |
| 43 | if os.path.exists(models_fn): |
| 44 | item = request.decls.add() |
| 45 | item.filename = "models.py" |
| 46 | item.contents = open(models_fn).read() |
| 47 | |
| 48 | attic_dir = os.path.join(dir, "attic") |
| 49 | if os.path.exists(attic_dir): |
| 50 | log.warn( |
| 51 | "Attics are deprecated, please use the legacy=True option in xProto" |
| 52 | ) |
| 53 | for fn in os.listdir(attic_dir): |
| 54 | if fn.endswith(".py"): |
| 55 | item = request.attics.add() |
| 56 | item.filename = fn |
| 57 | item.contents = open(os.path.join(attic_dir, fn)).read() |
| 58 | |
| 59 | api_convenience_dir = os.path.join(dir, "convenience") |
| 60 | if os.path.exists(api_convenience_dir): |
| 61 | for fn in os.listdir(api_convenience_dir): |
| 62 | if fn.endswith(".py") and "test" not in fn: |
| 63 | item = request.convenience_methods.add() |
| 64 | item.filename = fn |
| 65 | item.contents = open(os.path.join(api_convenience_dir, fn)).read() |
| 66 | |
Scott Baker | 8cc464d | 2019-02-06 16:51:42 -0800 | [diff] [blame] | 67 | # migrations directory is a sibling to the models directory |
| 68 | migrations_dir = os.path.join(dir, "..", "migrations") |
| 69 | if os.path.exists(migrations_dir): |
| 70 | for fn in os.listdir(migrations_dir): |
| 71 | if fn.endswith(".py") and "test" not in fn: |
| 72 | item = request.migrations.add() |
| 73 | item.filename = fn |
| 74 | item.contents = open(os.path.join(migrations_dir, fn)).read() |
| 75 | |
Matteo Scandolo | 12651d7 | 2019-02-21 15:15:29 -0800 | [diff] [blame] | 76 | # loading core requested version from synchronizer config |
| 77 | core_version = Config.get("core_version") |
| 78 | if core_version is None: |
| 79 | log.warn("Core version is not set in the config file") |
| 80 | |
| 81 | request.core_version = core_version |
| 82 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 83 | result = self.api.dynamicload.LoadModels(request) |
Scott Baker | 7ff8ad9 | 2019-02-15 17:02:41 -0800 | [diff] [blame] | 84 | |
| 85 | return result |
| 86 | |
| 87 | def unload_models(self, name, version="unknown", cleanup_behavior=REQUIRE_CLEAN): |
| 88 | request = self.api.dynamicload_pb2.UnloadModelsRequest( |
| 89 | name=name, |
| 90 | version=version, |
| 91 | cleanup_behavior=cleanup_behavior) |
| 92 | result = self.api.dynamicload.UnloadModels(request) |
| 93 | |
| 94 | return result |