1 # ============LICENSE_START====================================================
3 # =============================================================================
4 # Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
5 # =============================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END======================================================
20 Generates DCAE component specs
25 from jsonschema import validate
27 from aoconversion import utils
30 def _get_dcae_cs_schema():
32 "https://gerrit.onap.org/r/gitweb?p=dcaegen2/platform/cli.git;a=blob_plain;f=component-json-schemas/component-specification/dcae-cli-v2/component-spec-schema.json;hb=HEAD"
37 def _get_format_version(target_name, data_formats):
39 search through the data formats for name, make sure we have it, and retrieve the version
41 # the df must exist, since the data formats were generated from the same metadata, or dataformats call blew up.
42 # So we don't do error checking here
43 for df in data_formats:
44 if df["self"]["name"] == target_name:
45 return df["self"]["version"]
48 def _generate_spec(model_name, meta, dcae_cs_schema, data_formats, docker_uri):
50 Function that generates the component spec from the model metadata and docker info
51 Broken out to be unit-testable.
56 "version": "1.0.0", # hopefully we get this from somewhere and not hardcode this
58 "description": "Automatically generated from Acumos model",
59 "component_type": "docker",
61 "services": {"calls": [], "provides": []},
62 "streams": {"subscribes": [], "publishes": []},
64 "auxilary": {"healthcheck": {"type": "http", "endpoint": "/healthcheck"}},
65 "artifacts": [{"type": "docker image", "uri": docker_uri}],
68 # from https://pypi.org/project/acumos-dcae-model-runner/
69 # each model method gets a subscruber and a publisher, using the methood name
70 pstype = "message_router"
71 for method in meta["methods"]:
73 df_in_name = meta["methods"][method]["input"]
75 "config_key": "{0}_subscriber".format(method),
77 "version": _get_format_version(df_in_name, data_formats),
81 spec["streams"]["subscribes"].append(subscriber)
83 df_out_name = meta["methods"][method]["output"]
86 "config_key": "{0}_publisher".format(method),
87 "format": df_out_name,
88 "version": _get_format_version(df_out_name, data_formats),
92 spec["streams"]["publishes"].append(publisher)
94 # Validate that we have a valid spec
95 validate(instance=spec, schema=dcae_cs_schema)
100 def generate_spec(model_repo_path, model_name, data_formats, docker_uri):
102 Generate and write the component spec to disk
105 spec = _generate_spec(
106 model_name, utils.get_metadata(model_repo_path, model_name), _get_dcae_cs_schema(), data_formats, docker_uri
108 fname = "{0}_dcae_component_specification.json".format(model_name)
109 with open("{0}/{1}".format(model_repo_path, fname), "w") as f:
110 f.write(json.dumps(spec))