Refactor providermaagement
[nonrtric/plt/sme.git] / capifcore / internal / providermanagementapi / typevalidation.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         "errors"
25         "fmt"
26         "strings"
27 )
28
29 func (ri RegistrationInformation) Validate() error {
30         if len(strings.TrimSpace(ri.ApiProvPubKey)) == 0 {
31                 return errors.New("RegistrationInformation missing required apiProvPubKey")
32         }
33         return nil
34 }
35
36 func (fd APIProviderFunctionDetails) Validate() error {
37         switch role := fd.ApiProvFuncRole; role {
38         case ApiProviderFuncRoleAEF:
39         case ApiProviderFuncRoleAPF:
40         case ApiProviderFuncRoleAMF:
41         default:
42                 return errors.New("APIProviderFunctionDetails missing required apiProvFuncRole")
43         }
44
45         return fd.RegInfo.Validate()
46 }
47
48 func (pd APIProviderEnrolmentDetails) Validate() error {
49         if len(strings.TrimSpace(pd.RegSec)) == 0 {
50                 return errors.New("APIProviderEnrolmentDetails missing required regSec")
51         }
52         if pd.ApiProvFuncs != nil {
53                 return pd.validateFunctions()
54         }
55         return nil
56 }
57
58 func (pd APIProviderEnrolmentDetails) validateFunctions() error {
59         for _, function := range *pd.ApiProvFuncs {
60                 err := function.Validate()
61                 if err != nil {
62                         return fmt.Errorf("apiProvFuncs contains invalid function: %s", err)
63                 }
64         }
65         return nil
66 }