NONRTRIC-946: Servicemanager - update README and fix stand-alone deployment
[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 run_tests () {
173     # Make sure that SERVICE_MANAGER_ENV is configured with the required .env file, e.g.
174     # export SERVICE_MANAGER_ENV=development
175     cd "$cwd"
176     go test ./...
177 }
178
179 # Main code block
180 echo $(date -u) "generate started"
181
182 # Check if the run-tests switch is provided as a command-line argument
183 RUN_TESTS=false
184 while [[ "$#" -gt 0 ]]; do
185     case "$1" in
186         -t|--run-tests)
187             RUN_TESTS=true
188             shift  # consume the switch
189             ;;
190         *)
191             echo "Unknown argument: $1"
192             exit 1
193             ;;
194     esac
195     shift
196 done
197
198 make_internal_dirs
199 set_up_dir_paths
200 curl_api_specs
201 spec_extraction
202 fix_with_sed
203 gentools
204 generate_apis_from_spec
205
206 # Check if the run-tests switch is enabled
207 if [ "$RUN_TESTS" = true ]; then
208     echo "Running tests!"
209     run_tests
210 else
211     echo "Not running tests."
212 fi
213
214 echo $(date -u) "generate completed"