a08b720db81f67a2da1fecc2a4e26b5f84ee5f59
[dcaegen2/platform.git] / adapter / acumos / aoconversion / spec_gen.py
1 # ============LICENSE_START====================================================
2 # org.onap.dcae
3 # =============================================================================
4 # Copyright (c) 2019-2020 AT&T Intellectual Property. All rights reserved.
5 # Copyright (c) 2021 highstreet technologies GmbH. All rights reserved.
6 # =============================================================================
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #      http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END======================================================
19
20 """
21 Generates DCAE component specs
22 """
23
24
25 import json
26 from jsonschema import validate
27 from aoconversion import utils
28
29
30 def _get_format_version(target_name, data_formats):
31     """
32     search through the data formats for name, make sure we have it, and retrieve the version
33     """
34     # the df must exist, since the data formats were generated from the same metadata, or dataformats call blew up.
35     # So we don't do error checking here
36     for df in data_formats:
37         if df["self"]["name"] == target_name:
38             return df["self"]["version"]
39
40
41 def _generate_spec(model_name, meta, dcae_cs_schema, data_formats, docker_uri):
42     """
43     Function that generates the component spec from the model metadata and docker info
44     Broken out to be unit-testable.
45     """
46
47     spec = {
48         "self": {
49             "version": "1.0.0",  # hopefully we get this from somewhere and not hardcode this
50             "name": model_name,
51             "description": "Automatically generated from Acumos model",
52             "component_type": "docker",
53         },
54         "services": {"calls": [], "provides": []},
55         "streams": {"subscribes": [], "publishes": []},
56         "parameters": [],
57         "auxilary": {"healthcheck": {"type": "http", "endpoint": "/healthcheck"}},
58         "artifacts": [{"type": "docker image", "uri": docker_uri}],
59     }
60
61     # from https://pypi.org/project/acumos-dcae-model-runner/
62     # each model method gets a subscruber and a publisher, using the methood name
63     pstype = "message_router"
64     for method in meta["methods"]:
65
66         df_in_name = utils.validate_format(meta, method, "input")
67         subscriber = {
68             "config_key": "{0}_subscriber".format(method),
69             "format": df_in_name,
70             "version": _get_format_version(df_in_name, data_formats),
71             "type": pstype,
72         }
73
74         spec["streams"]["subscribes"].append(subscriber)
75
76         df_out_name = utils.validate_format(meta, method, "output")
77
78         publisher = {
79             "config_key": "{0}_publisher".format(method),
80             "format": df_out_name,
81             "version": _get_format_version(df_out_name, data_formats),
82             "type": pstype,
83         }
84
85         spec["streams"]["publishes"].append(publisher)
86
87     # Validate that we have a valid spec
88     validate(instance=spec, schema=dcae_cs_schema)
89
90     return spec
91
92
93 def generate_spec(model_repo_path, model_name, data_formats, docker_uri):
94     """
95     Generate and write the component spec to disk
96     Returns the spec
97     """
98     spec = _generate_spec(
99         model_name, utils.get_metadata(model_repo_path, model_name), utils.component_schema.get(), data_formats, docker_uri
100     )
101     fname = "{0}_dcae_component_specification.json".format(model_name)
102     with open("{0}/{1}".format(model_repo_path, fname), "w") as f:
103         f.write(json.dumps(spec))
104
105     return spec