Updates for G Maintenance release
[nonrtric/plt/sme.git] / capifcore / internal / providermanagementapi / typeupdate.go
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 package providermanagementapi
22
23 import (
24         "fmt"
25         "strings"
26
27         "github.com/google/uuid"
28 )
29
30 var uuidFunc = getUUID
31
32 func (ed *APIProviderEnrolmentDetails) UpdateFuncs(registeredProvider APIProviderEnrolmentDetails) error {
33         for pos, function := range *ed.ApiProvFuncs {
34                 if function.ApiProvFuncId == nil {
35                         (*ed.ApiProvFuncs)[pos].ApiProvFuncId = getFuncId(function.ApiProvFuncRole, function.ApiProvFuncInfo)
36                 } else {
37                         if !registeredProvider.IsFunctionRegistered(*function.ApiProvFuncId) {
38                                 return fmt.Errorf("function with ID %s is not registered for the provider", *function.ApiProvFuncId)
39                         }
40                 }
41         }
42         return nil
43 }
44
45 func (ed *APIProviderEnrolmentDetails) PrepareNewProvider() {
46         ed.ApiProvDomId = ed.getDomainId()
47
48         ed.registerFunctions()
49
50 }
51
52 func (ed *APIProviderEnrolmentDetails) getDomainId() *string {
53         var idAsString string
54         if ed.ApiProvDomInfo != nil {
55                 idAsString = strings.ReplaceAll(*ed.ApiProvDomInfo, " ", "_")
56         } else {
57                 idAsString = uuidFunc()
58         }
59         newId := "domain_id_" + idAsString
60         return &newId
61 }
62
63 func (ed *APIProviderEnrolmentDetails) registerFunctions() {
64         if ed.ApiProvFuncs == nil {
65                 return
66         }
67         for i, provFunc := range *ed.ApiProvFuncs {
68                 (*ed.ApiProvFuncs)[i].ApiProvFuncId = getFuncId(provFunc.ApiProvFuncRole, provFunc.ApiProvFuncInfo)
69         }
70 }
71
72 func getFuncId(role ApiProviderFuncRole, funcInfo *string) *string {
73         var idPrefix string
74         switch role {
75         case ApiProviderFuncRoleAPF:
76                 idPrefix = "APF_id_"
77         case ApiProviderFuncRoleAMF:
78                 idPrefix = "AMF_id_"
79         case ApiProviderFuncRoleAEF:
80                 idPrefix = "AEF_id_"
81         }
82         var id string
83         if funcInfo != nil {
84                 id = strings.ReplaceAll(*funcInfo, " ", "_")
85         } else {
86                 id = uuidFunc()
87         }
88         idAsString := idPrefix + id
89         return &idAsString
90 }
91
92 func getUUID() string {
93         return uuid.NewString()
94 }