ca9f5ce4aa6481f3907bf63f9be558b6e9a00014
[nonrtric/plt/sme.git] / r1-sme-manager / generate-service-publish-and-discovery.sh
1 # -
2 #   ========================LICENSE_START=================================
3 #   O-RAN-SC
4 #   %%
5 #   Copyright (C) 2023: Nordix Foundation
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 #!/bin/bash
22
23 make_internal_dirs () {
24     echo "Make the internal directory structure"
25     mkdir -p internal/config
26     mkdir -p internal/discoverservice
27     mkdir -p internal/discoverserviceapi
28     mkdir -p internal/eventsapi
29     mkdir -p internal/eventservice
30     mkdir -p internal/helmmanagement
31     mkdir -p internal/invokermanagement
32     mkdir -p internal/invokermanagementapi
33     mkdir -p internal/keycloak
34     mkdir -p internal/loggingapi
35     mkdir -p internal/providermanagement
36     mkdir -p internal/providermanagementapi
37     mkdir -p internal/publishservice
38     mkdir -p internal/publishserviceapi
39     mkdir -p internal/restclient
40 }
41
42 tear_down () {
43     echo "Tear down the internal directory"
44     rm -rf internal/
45 }
46
47 set_up_dir_paths () {
48     echo "Set up dir paths"
49     cwd=$PWD
50     sme_dir=$(dirname "$cwd")
51     capifcore_dir="$sme_dir/capifcore"
52 }
53
54 copy_test_wrappers () {
55     echo "Copy the test wrappers"
56     cp -v $capifcore_dir/internal/config/*.go internal/config
57     cp -v $capifcore_dir/internal/discoverservice/*.go internal/discoverservice
58     cp -v $capifcore_dir/internal/eventsapi/type*.go internal/eventsapi
59     cp -v $capifcore_dir/internal/eventservice/*.go internal/eventservice
60     cp -v $capifcore_dir/internal/helmmanagement/*.go internal/helmmanagement
61     cp -v $capifcore_dir/internal/invokermanagement/*.go internal/invokermanagement
62     cp -v $capifcore_dir/internal/invokermanagementapi/type*.go internal/invokermanagementapi
63     cp -v $capifcore_dir/internal/keycloak/*.go internal/keycloak
64     cp -v $capifcore_dir/internal/providermanagement/*.go internal/providermanagement
65     cp -v $capifcore_dir/internal/providermanagementapi/*.go internal/providermanagementapi
66     cp -v $capifcore_dir/internal/publishservice/*.go internal/publishservice
67     cp -v $capifcore_dir/internal/publishserviceapi/type*.go internal/publishserviceapi
68     cp -v $capifcore_dir/internal/restclient/*.go internal/restclient
69 }
70
71 curl_api_specs () {
72     echo "Curl down the API specs"
73     mkdir -p specs
74     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.222/29222-h60.zip -o specs/apidef.zip
75     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.122/29122-h70.zip -o specs/common29122apidef.zip
76     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.508/29508-h80.zip -o specs/common29508apidef.zip
77     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.510/29510-h70.zip -o specs/common29510apidef.zip
78     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.512/29512-h80.zip -o specs/common29512apidef.zip
79     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.514/29514-h60.zip -o specs/common29514apidef.zip
80     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.517/29517-h70.zip -o specs/common29517apidef.zip
81     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.518/29518-h70.zip -o specs/common29518apidef.zip
82     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.522/29522-h70.zip -o specs/common29522apidef.zip
83     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.523/29523-h80.zip -o specs/common29523apidef.zip
84     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.554/29554-h40.zip -o specs/common29554apidef.zip
85     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.571/29571-h70.zip -o specs/common29571apidef.zip
86     curl https://www.3gpp.org/ftp/Specs/archive/29_series/29.572/29572-h60.zip -o specs/common29572apidef.zip
87 }
88
89 jar_extraction () {
90     cd specs/
91     echo "Jar extraction"
92     jar xvf apidef.zip
93     jar xvf common29122apidef.zip
94     jar xvf common29508apidef.zip
95     jar xvf common29510apidef.zip
96     jar xvf common29512apidef.zip
97     jar xvf common29514apidef.zip
98     jar xvf common29517apidef.zip
99     jar xvf common29518apidef.zip
100     jar xvf common29522apidef.zip
101     jar xvf common29523apidef.zip
102     jar xvf common29554apidef.zip
103     jar xvf common29571apidef.zip
104     jar xvf common29572apidef.zip
105 }
106
107
108 fix_with_sed () {
109     echo "Fixing with sed"
110
111     # Remove types that are not used by CAPIF that have dependencies to other specifications.
112     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\CivicAddress/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
113     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\ExternalMbsServiceArea/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
114     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\GeographicArea/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
115     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\GeoServiceArea/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
116     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\MbsMediaComp/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
117     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\MbsMediaCompRm/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
118     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\MbsMediaInfo/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
119     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\MbsServiceInfo/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
120     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\MbsSession/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
121     sed -i -e 'H;x;/^\(  *\)\n\1/{s/\n.*//;x;d;}' -e 's/.*//;x;/\SpatialValidityCond/{s/^\( *\).*/ \1/;x;d;}' TS29571_CommonData.yaml
122
123     # Remove attributes that can not be generated easily.
124     sed -i '/accessTokenError.*/,+3d' TS29571_CommonData.yaml
125     sed -i '/accessTokenRequest.*/,+3d' TS29571_CommonData.yaml
126     sed -i '/oneOf.*/,+2d' TS29222_CAPIF_Publish_Service_API.yaml
127     sed -i '/oneOf.*/,+2d' TS29222_CAPIF_Security_API.yaml
128
129     # Replace references to external specs that are collected to the common spec by the commoncollector
130     # <replacements_start>
131     sed -i 's/TS29572_Nlmf_Location/CommonData/g' TS29122_CommonData.yaml
132     sed -i 's/TS29554_Npcf_BDTPolicyControl/CommonData/g' TS29122_CommonData.yaml
133     sed -i 's/TS29514_Npcf_PolicyAuthorization/CommonData/g' TS29122_CommonData.yaml
134     sed -i 's/TS29514_Npcf_PolicyAuthorization/CommonData/g' TS29571_CommonData.yaml
135     sed -i 's/TS29572_Nlmf_Location/CommonData/g' TS29571_CommonData.yaml
136     sed -i 's/TS29572_Nlmf_Location/CommonData/g' TS29222_CAPIF_Publish_Service_API.yaml
137     sed -i 's/TS29520_Nnwdaf_EventsSubscription/CommonData/g' TS29222_CAPIF_Routing_Info_API.yaml
138     sed -i 's/TS29510_Nnrf_NFManagement/CommonData/g' TS29222_CAPIF_Routing_Info_API.yaml
139     sed -i 's/TS29523_Npcf_EventExposure/CommonData/g' TS29222_CAPIF_Events_API.yaml
140     # <new_replacement>
141
142     # This spec has references to itself that need to be removed
143     sed -i 's/TS29571_CommonData.yaml//g' TS29571_CommonData.yaml
144 }
145
146 gentools () {
147     echo "Fixing enums"
148
149     cd "$capifcore_dir/internal/gentools/enumfixer"
150     go build .
151     ./enumfixer -apidir="$sme_dir/r1-sme-manager/specs"
152
153     echo "Gathering common references"
154     cd "$capifcore_dir/internal/gentools/commoncollector"
155     go build .
156     ./commoncollector -apidir="$sme_dir/r1-sme-manager/specs"
157
158     echo "Fixing misc in specifications"
159     cd "$capifcore_dir/internal/gentools/specificationfixer"
160     go build .
161     ./specificationfixer -apidir="$sme_dir/r1-sme-manager/specs"
162 }
163
164 code_generation () {
165     go install github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.10.1
166     PATH=$PATH:~/go/bin
167
168     cd $cwd
169
170     echo "Generating TS29122_CommonData"
171     mkdir -p internal/common29122
172     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/common29122/generator_settings.yaml" specs/TS29122_CommonData.yaml
173
174     echo "Generating aggregated CommonData"
175     mkdir -p internal/common
176     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/common/generator_settings.yaml" specs/CommonData.yaml
177
178     echo "Generating TS29571_CommonData"
179     mkdir -p internal/common29571
180     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/common29571/generator_settings.yaml" specs/TS29571_CommonData.yaml
181
182     echo "Generating TS29222_CAPIF_Publish_Service_API"
183     mkdir -p internal/publishserviceapi
184     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/publishserviceapi/generator_settings_types.yaml" specs/TS29222_CAPIF_Publish_Service_API.yaml
185     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/publishserviceapi/generator_settings_server.yaml" specs/TS29222_CAPIF_Publish_Service_API.yaml
186
187     echo "Generating TS29222_CAPIF_API_Invoker_Management_API"
188     mkdir -p internal/invokermanagementapi
189     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/invokermanagementapi/generator_settings_types.yaml" specs/TS29222_CAPIF_API_Invoker_Management_API.yaml
190     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/invokermanagementapi/generator_settings_server.yaml" specs/TS29222_CAPIF_API_Invoker_Management_API.yaml
191
192     echo "Generating TS29222_CAPIF_API_Provider_Management_API"
193     mkdir -p internal/providermanagementapi
194     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/providermanagementapi/generator_settings_types.yaml" specs/TS29222_CAPIF_API_Provider_Management_API.yaml
195     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/providermanagementapi/generator_settings_server.yaml" specs/TS29222_CAPIF_API_Provider_Management_API.yaml
196
197     echo "Generating TS29222_CAPIF_Discover_Service_API"
198     mkdir -p internal/discoverserviceapi
199     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/discoverserviceapi/generator_settings_types.yaml" specs/TS29222_CAPIF_Discover_Service_API.yaml
200     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/discoverserviceapi/generator_settings_server.yaml" specs/TS29222_CAPIF_Discover_Service_API.yaml
201
202     echo "Generating TS29222_CAPIF_Logging_API_Invocation_API"
203     mkdir -p internal/loggingapi
204     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/loggingapi/generator_settings_types.yaml" specs/TS29222_CAPIF_Logging_API_Invocation_API.yaml
205     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/loggingapi/generator_settings_server.yaml" specs/TS29222_CAPIF_Logging_API_Invocation_API.yaml
206
207     echo "Generating TS29222_CAPIF_Routing_Info_API"
208     mkdir -p internal/routinginfoapi
209     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/routinginfoapi/generator_settings_types.yaml" specs/TS29222_CAPIF_Routing_Info_API.yaml
210     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/routinginfoapi/generator_settings_server.yaml" specs/TS29222_CAPIF_Routing_Info_API.yaml
211
212     echo "Generating TS29222_CAPIF_Access_Control_Policy_API"
213     mkdir -p internal/accesscontrolpolicyapi
214     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/accesscontrolpolicyapi/generator_settings_types.yaml" specs/TS29222_CAPIF_Access_Control_Policy_API.yaml
215     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/accesscontrolpolicyapi/generator_settings_server.yaml" specs/TS29222_CAPIF_Access_Control_Policy_API.yaml
216
217     echo "Generating TS29222_CAPIF_Events_API"
218     mkdir -p internal/eventsapi
219     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/eventsapi/generator_settings_types.yaml" specs/TS29222_CAPIF_Events_API.yaml
220     oapi-codegen --config "$capifcore_dir/gogeneratorspecs/eventsapi/generator_settings_server.yaml" specs/TS29222_CAPIF_Events_API.yaml
221
222     echo "Clean up"
223     rm -rf specs
224 }
225
226
227 fix_package_imports () {
228     echo "Fix package imports"
229     if find "$cwd/internal" -type f -exec sed -i 's/oransc.org\/nonrtric\/capifcore/oransc.org\/nonrtric\/r1-sme-manager/g' {} +; then
230         echo "Package import replacement successful"
231     else
232         echo "Package import replacement failed"
233     fi
234 }
235
236 generate_mocks () {
237     echo "Generating mocks"
238     go generate ./...
239 }
240
241 running_tests () {
242     echo "Running tests"
243     cd internal
244     go clean -testcache
245     go test ./publishservice ./discoverservice
246 }
247
248 # Main code block
249
250 tear_down
251 make_internal_dirs
252 set_up_dir_paths
253 copy_test_wrappers
254 curl_api_specs
255 jar_extraction
256 fix_with_sed
257 gentools
258 code_generation
259 fix_package_imports
260 generate_mocks
261 running_tests