Merge "Acumos Adapter updates to support v3 schema"
[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 import json
25 from jsonschema import validate
26 from aoconversion import utils
27
28
29 def _get_format_version(target_name, data_formats):
30     """
31     search through the data formats for name, make sure we have it, and retrieve the version
32     """
33     # the df must exist, since the data formats were generated from the same metadata, or dataformats call blew up.
34     # So we don't do error checking here
35     for df in data_formats:
36         if df["self"]["name"] == target_name:
37             return df["self"]["version"]
38
39
40 def _generate_spec(model_name, meta, dcae_cs_schema, data_formats, model_version):
41     """
42     Function that generates the component spec from the model metadata and docker info
43     Broken out to be unit-testable.
44     """
45     docker_uri = "{}:{}".format(model_name, model_version)
46     spec = {
47         "self": {
48             "version": "1.0.0",  # hopefully we get this from somewhere and not hardcode this
49             "name": model_name,
50             "description": "Automatically generated from Acumos model",
51             "component_type": "docker",
52         },
53         "services": {"calls": [], "provides": []},
54         "streams": {"subscribes": [], "publishes": []},
55         "parameters": [],
56         "auxilary": {"helm": {"service": {"type": "ClusterIP",
57                                           "name": model_name,
58                                           "has_internal_only_ports": "true",
59                                           "ports": [{"name": "http", "port": 8443, "plain_port": 8080,
60                                                      "port_protocol": "http"}]}},
61                      "healthcheck": {"type": "HTTP", "interval": "15s", "timeout": "1s", "port": 8080,
62                                      "endpoint": "/healthcheck"}},
63         "artifacts": [{"type": "docker image", "uri": docker_uri}],
64     }
65
66     # from https://pypi.org/project/acumos-dcae-model-runner/
67     # each model method gets a subscruber and a publisher, using the methood name
68     pstype = "message_router"
69     for method in meta["methods"]:
70         df_in_name = utils.validate_format(meta, method, "input")
71         subscriber = {
72             "config_key": "{0}_subscriber".format(method),
73             "format": df_in_name,
74             "version": _get_format_version(df_in_name, data_formats),
75             "type": pstype,
76         }
77
78         spec["streams"]["subscribes"].append(subscriber)
79
80         df_out_name = utils.validate_format(meta, method, "output")
81
82         publisher = {
83             "config_key": "{0}_publisher".format(method),
84             "format": df_out_name,
85             "version": _get_format_version(df_out_name, data_formats),
86             "type": pstype,
87         }
88
89         publisher = {
90             "config_key": "{0}_publisher".format(method),
91             "format": df_out_name,
92             "version": _get_format_version(df_out_name, data_formats),
93             "type": pstype,
94         }
95         parameter_subscriber = {
96             "name": "streams_subscribes",
97             "value": "{{\"{0}_subscriber\":{{\"dmaap_info\":{{\"topic_url\":\"http://message-router:3904/events/unauthenticated.{1}_In\"}},\"type\":\"message_router\"}}}}".format(
98                 method, model_name),
99
100             "description": "standard http port collector will open for listening;",
101             "sourced_at_deployment": False,
102             "policy_editable": False,
103             "designer_editable": False
104         }
105         paramter_publisher = {
106             "name": "streams_publishes",
107             "value": "{{\"{0}_publisher\":{{\"dmaap_info\":{{\"topic_url\":\"http://message-router:3904/events/unauthenticated.{1}_Out\"}},\"type\":\"message_router\"}}}}".format(
108                 method, model_name),
109             "description": "standard http port collector will open for listening;",
110             "sourced_at_deployment": False,
111             "policy_editable": False,
112             "designer_editable": False
113         }
114         spec["streams"]["publishes"].append(publisher)
115         spec["parameters"].append(parameter_subscriber)
116         spec["parameters"].append(paramter_publisher)
117
118     # Validate that we have a valid spec
119     validate(instance=spec, schema=dcae_cs_schema)
120     return spec
121
122
123 def generate_spec(model_repo_path, model_name, data_formats, model_version):
124     """
125     Generate and write the component spec to disk
126     Returns the spec
127     """
128     spec = _generate_spec(
129         model_name, utils.get_metadata(model_repo_path, model_name), utils.component_schema.get(), data_formats,
130         model_version
131     )
132     fname = "{0}_dcae_component_specification.json".format(model_name)
133     with open("{0}/{1}".format(model_repo_path, fname), "w") as f:
134         f.write(json.dumps(spec))
135
136     return spec