Roll versions after J-Relase (master branch)
[nonrtric/plt/sme.git] / servicemanager / generate.sh
1 #!/bin/bash
2 # -
3 #   ========================LICENSE_START=================================
4 #   O-RAN-SC
5 #   %%
6 #   Copyright (C) 2024: OpenInfra Foundation Europe
7 #   %%
8 #   Licensed under the Apache License, Version 2.0 (the "License");
9 #   you may not use this file except in compliance with the License.
10 #   You may obtain a copy of the License at
11 #
12 #        http://www.apache.org/licenses/LICENSE-2.0
13 #
14 #   Unless required by applicable law or agreed to in writing, software
15 #   distributed under the License is distributed on an "AS IS" BASIS,
16 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 #   See the License for the specific language governing permissions and
18 #   limitations under the License.
19 #   ========================LICENSE_END===================================
20 #
21
22 make_internal_dirs () {
23     echo "Make the internal API directory structure"
24     mkdir -p internal/discoverserviceapi
25     mkdir -p internal/invokermanagementapi
26     mkdir -p internal/providermanagementapi
27     mkdir -p internal/publishserviceapi
28 }
29
30 set_up_dir_paths () {
31     echo "Set up dir paths"
32     cwd=$PWD
33     sme_dir=$(dirname "$cwd")
34     capifcore_dir="$sme_dir/capifcore"
35 }
36
37 curl_api_specs () {
38     echo "Curl down the API specs"
39     mkdir -p specs
40     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.222/29222-h60.zip -o specs/apidef.zip
41     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.122/29122-h70.zip -o specs/common29122apidef.zip
42     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.508/29508-h80.zip -o specs/common29508apidef.zip
43     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.510/29510-h70.zip -o specs/common29510apidef.zip
44     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.512/29512-h80.zip -o specs/common29512apidef.zip
45     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.514/29514-h60.zip -o specs/common29514apidef.zip
46     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.517/29517-h70.zip -o specs/common29517apidef.zip
47     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.518/29518-h70.zip -o specs/common29518apidef.zip
48     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.522/29522-h70.zip -o specs/common29522apidef.zip
49     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.523/29523-h80.zip -o specs/common29523apidef.zip
50     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.554/29554-h40.zip -o specs/common29554apidef.zip
51     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.571/29571-h70.zip -o specs/common29571apidef.zip
52     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.572/29572-h60.zip -o specs/common29572apidef.zip
53 }
54
55 spec_extraction () {
56     cd specs/
57     echo "Specifications extraction"
58     unzip -o apidef.zip
59     unzip -o common29122apidef.zip
60     unzip -o common29508apidef.zip
61     unzip -o common29510apidef.zip
62     unzip -o common29512apidef.zip
63     unzip -o common29514apidef.zip
64     unzip -o common29517apidef.zip
65     unzip -o common29518apidef.zip
66     unzip -o common29522apidef.zip
67     unzip -o common29523apidef.zip
68     unzip -o common29554apidef.zip
69     unzip -o common29571apidef.zip
70     unzip -o common29572apidef.zip
71 }
72
73 fix_with_sed () {
74     echo "Fixing with sed"
75
76     # Remove types that are not used by CAPIF that have dependencies to other specifications.
77     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\CivicAddress/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
78     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\ExternalMbsServiceArea/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
79     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\GeographicArea/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
80     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\GeoServiceArea/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
81     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\MbsMediaComp/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
82     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\MbsMediaCompRm/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
83     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\MbsMediaInfo/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
84     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\MbsServiceInfo/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
85     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\MbsSession/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
86     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\SpatialValidityCond/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
87
88     # Remove attributes that cannot be generated easily.
89     sed -i '/accessTokenError.*/,+3d' TS29571_CommonData.yaml
90     sed -i '/accessTokenRequest.*/,+3d' TS29571_CommonData.yaml
91     sed -i '/oneOf.*/,+2d' TS29222_CAPIF_Publish_Service_API.yaml
92     sed -i '/oneOf.*/,+2d' TS29222_CAPIF_Security_API.yaml
93
94     # Replace references to external specs that are collected to the common spec by the commoncollector
95     # <replacements_start>
96     sed -i 's/TS29572_Nlmf_Location/CommonData/g' TS29122_CommonData.yaml
97     sed -i 's/TS29554_Npcf_BDTPolicyControl/CommonData/g' TS29122_CommonData.yaml
98     sed -i 's/TS29514_Npcf_PolicyAuthorization/CommonData/g' TS29122_CommonData.yaml
99     sed -i 's/TS29514_Npcf_PolicyAuthorization/CommonData/g' TS29571_CommonData.yaml
100     sed -i 's/TS29572_Nlmf_Location/CommonData/g' TS29571_CommonData.yaml
101     sed -i 's/TS29572_Nlmf_Location/CommonData/g' TS29222_CAPIF_Publish_Service_API.yaml
102     sed -i 's/TS29520_Nnwdaf_EventsSubscription/CommonData/g' TS29222_CAPIF_Routing_Info_API.yaml
103     sed -i 's/TS29510_Nnrf_NFManagement/CommonData/g' TS29222_CAPIF_Routing_Info_API.yaml
104     sed -i 's/TS29523_Npcf_EventExposure/CommonData/g' TS29222_CAPIF_Events_API.yaml
105     # <new_replacement>
106
107     # This spec has references to itself that need to be removed
108     sed -i 's/TS29571_CommonData.yaml//g' TS29571_CommonData.yaml
109 }
110
111 gentools () {
112     echo "Fixing enums"
113
114     cd "$capifcore_dir/internal/gentools/enumfixer"
115     go build .
116     ./enumfixer -apidir="$sme_dir/servicemanager/specs"
117
118     echo "Gathering common references"
119     cd "$capifcore_dir/internal/gentools/commoncollector"
120     go build .
121     ./commoncollector -apidir="$sme_dir/servicemanager/specs"
122
123     echo "Fixing misc in specifications"
124     cd "$capifcore_dir/internal/gentools/specificationfixer"
125     go build .
126     ./specificationfixer -apidir="$sme_dir/servicemanager/specs"
127 }
128
129 generate_apis_from_spec () {
130     go install github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.10.1
131     PATH=$PATH:~/go/bin
132
133     cd $cwd
134
135     echo "Generating TS29122_CommonData"
136     mkdir -p internal/common29122
137     oapi-codegen --config "gogeneratorspecs/common29122/generator_settings.yaml" specs/TS29122_CommonData.yaml
138
139     echo "Generating aggregated CommonData"
140     mkdir -p internal/common
141     oapi-codegen --config "gogeneratorspecs/common/generator_settings.yaml" specs/CommonData.yaml
142
143     echo "Generating TS29571_CommonData"
144     mkdir -p internal/common29571
145     oapi-codegen --config "gogeneratorspecs/common29571/generator_settings.yaml" specs/TS29571_CommonData.yaml
146
147     echo "Generating TS29222_CAPIF_Publish_Service_API"
148     mkdir -p internal/publishserviceapi
149     oapi-codegen --config "gogeneratorspecs/publishserviceapi/generator_settings_types.yaml" specs/TS29222_CAPIF_Publish_Service_API.yaml
150     oapi-codegen --config "gogeneratorspecs/publishserviceapi/generator_settings_server.yaml" specs/TS29222_CAPIF_Publish_Service_API.yaml
151     oapi-codegen --config "gogeneratorspecs/publishserviceapi/generator_settings_client.yaml" specs/TS29222_CAPIF_Publish_Service_API.yaml
152
153     echo "Generating TS29222_CAPIF_API_Invoker_Management_API"
154     mkdir -p internal/invokermanagementapi
155     oapi-codegen --config "gogeneratorspecs/invokermanagementapi/generator_settings_types.yaml" specs/TS29222_CAPIF_API_Invoker_Management_API.yaml
156     oapi-codegen --config "gogeneratorspecs/invokermanagementapi/generator_settings_server.yaml" specs/TS29222_CAPIF_API_Invoker_Management_API.yaml
157     oapi-codegen --config "gogeneratorspecs/invokermanagementapi/generator_settings_client.yaml" specs/TS29222_CAPIF_API_Invoker_Management_API.yaml
158
159     echo "Generating TS29222_CAPIF_API_Provider_Management_API"
160     mkdir -p internal/providermanagementapi
161     oapi-codegen --config "gogeneratorspecs/providermanagementapi/generator_settings_types.yaml" specs/TS29222_CAPIF_API_Provider_Management_API.yaml
162     oapi-codegen --config "gogeneratorspecs/providermanagementapi/generator_settings_server.yaml" specs/TS29222_CAPIF_API_Provider_Management_API.yaml
163     oapi-codegen --config "gogeneratorspecs/providermanagementapi/generator_settings_client.yaml" specs/TS29222_CAPIF_API_Provider_Management_API.yaml
164
165     echo "Generating TS29222_CAPIF_Discover_Service_API"
166     mkdir -p internal/discoverserviceapi
167     oapi-codegen --config "gogeneratorspecs/discoverserviceapi/generator_settings_types.yaml" specs/TS29222_CAPIF_Discover_Service_API.yaml
168     oapi-codegen --config "gogeneratorspecs/discoverserviceapi/generator_settings_server.yaml" specs/TS29222_CAPIF_Discover_Service_API.yaml
169     oapi-codegen --config "gogeneratorspecs/discoverserviceapi/generator_settings_client.yaml" specs/TS29222_CAPIF_Discover_Service_API.yaml
170 }
171
172 generate_html2_from_spec() {
173     cd $cwd
174
175     echo "Generating Provider_Management HTML"
176     docker run --rm \
177         -u $(id -u ${USER}):$(id -g ${USER}) \
178         -v ${PWD}:/local openapitools/openapi-generator-cli generate \
179         -i /local/specs/TS29222_CAPIF_API_Provider_Management_API.yaml \
180         -g html2 \
181         -o /local/openapi/html2/Provider_Management
182     mv -v openapi/html2/Provider_Management/index.html openapi/html2/Provider_Management/Provider_Management.html
183
184     echo "Generating Publish_Service HTML"
185     docker run --rm \
186         -u $(id -u ${USER}):$(id -g ${USER}) \
187         -v ${PWD}:/local openapitools/openapi-generator-cli generate \
188         -i /local/specs/TS29222_CAPIF_Publish_Service_API.yaml \
189         -g html2 \
190         -o /local/openapi/html2/Publish_Service
191     mv -v openapi/html2/Publish_Service/index.html openapi/html2/Publish_Service/Publish_Service.html
192
193     echo "Generating Invoker_Management HTML"
194     docker run --rm \
195         -u $(id -u ${USER}):$(id -g ${USER}) \
196         -v ${PWD}:/local openapitools/openapi-generator-cli generate \
197         -i /local/specs/TS29222_CAPIF_API_Invoker_Management_API.yaml \
198         -g html2 \
199         -o /local/openapi/html2/Invoker_Management
200     mv -v openapi/html2/Invoker_Management/index.html openapi/html2/Invoker_Management/Invoker_Management.html
201
202     echo "Generating Discover_Service HTML"
203     docker run --rm \
204         -u $(id -u ${USER}):$(id -g ${USER}) \
205         -v ${PWD}:/local openapitools/openapi-generator-cli generate \
206         -i /local/specs/TS29222_CAPIF_Discover_Service_API.yaml \
207         -g html2 \
208         -o /local/openapi/html2/Discover_Service
209     mv -v openapi/html2/Discover_Service/index.html openapi/html2/Discover_Service/Discover_Service.html
210
211     echo "Generating Events HTML"
212     docker run --rm \
213         -u $(id -u ${USER}):$(id -g ${USER}) \
214         -v ${PWD}:/local openapitools/openapi-generator-cli generate \
215         -i /local/specs/TS29222_CAPIF_Events_API.yaml \
216         -g html2 \
217         -o /local/openapi/html2/Events
218     mv -v openapi/html2/Events/index.html openapi/html2/Events/Events.html
219
220     echo "Generating Security HTML"
221     docker run --rm \
222         -u $(id -u ${USER}):$(id -g ${USER}) \
223         -v ${PWD}:/local openapitools/openapi-generator-cli generate \
224         -i /local/specs/TS29222_CAPIF_Security_API.yaml \
225         -g html2 \
226         -o /local/openapi/html2/Security
227     mv -v openapi/html2/Security/index.html openapi/html2/Security/Security.html
228
229     mkdir -vp ../docs/openapi
230     find openapi/html2 -type f -name "*.html" -exec cp -v {} ../docs/openapi \;
231 }
232
233 run_tests () {
234     # Make sure that SERVICE_MANAGER_ENV is configured with the required .env file, e.g.
235     # export SERVICE_MANAGER_ENV=development
236     cd "$cwd"
237     go test ./...
238 }
239
240 # Main code block
241 echo $(date -u) "generate started"
242
243 # Check if the run-tests switch is provided as a command-line argument
244 RUN_TESTS=false
245 while [[ "$#" -gt 0 ]]; do
246     case "$1" in
247         -t|--run-tests)
248             RUN_TESTS=true
249             shift  # consume the switch
250             ;;
251         *)
252             echo "Unknown argument: $1"
253             exit 1
254             ;;
255     esac
256     shift
257 done
258
259 make_internal_dirs
260 set_up_dir_paths
261 curl_api_specs
262 spec_extraction
263 fix_with_sed
264 gentools
265 generate_apis_from_spec
266 generate_html2_from_spec
267
268 cd $cwd
269
270 # Check if the run-tests switch is enabled
271 if [ "$RUN_TESTS" = true ]; then
272     echo "Running tests!"
273     run_tests
274 else
275     echo "Not running tests."
276 fi
277
278 echo $(date -u) "generate completed"