3402b8e1b84a805c028bb594e29322731a13f97c
[nonrtric/plt/sme.git] / capifcore / internal / securityapi / typeupdate.go
1 // -
2 //
3 //      ========================LICENSE_START=================================
4 //      O-RAN-SC
5 //      %%
6 //      Copyright (C) 2023: Nordix Foundation
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 package securityapi
21
22 import (
23         "fmt"
24         "strings"
25
26         "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
27 )
28
29 var securityMethods []publishserviceapi.SecurityMethod
30
31 func (newContext *ServiceSecurity) PrepareNewSecurityContext(services []publishserviceapi.ServiceAPIDescription) error {
32         securityMethods = []publishserviceapi.SecurityMethod{}
33         for i, securityInfo := range newContext.SecurityInfo {
34                 if securityInfo.InterfaceDetails != nil {
35                         addSecurityMethodsFromInterfaceDetails(securityInfo.InterfaceDetails.SecurityMethods, &securityInfo.PrefSecurityMethods)
36
37                 } else {
38                         checkNil := securityInfo.ApiId != nil && securityInfo.AefId != nil
39                         if checkNil {
40                                 service := getServiceByApiId(&services, securityInfo.ApiId)
41                                 if service != nil {
42                                         afpProfile := service.GetAefProfileById(securityInfo.AefId)
43                                         addSecurityMethodsFromAefProfile(afpProfile)
44                                 }
45
46                         }
47                 }
48
49                 if isSecuryMethodsEmpty() {
50                         return fmt.Errorf("not found compatible security method")
51                 }
52                 newContext.SecurityInfo[i].SelSecurityMethod = &securityMethods[0]
53         }
54         return nil
55 }
56
57 func isSecuryMethodsEmpty() bool {
58         return len(securityMethods) <= 0
59 }
60
61 func addSecurityMethodsFromInterfaceDetails(methodsFromInterface *[]publishserviceapi.SecurityMethod, prefMethods *[]publishserviceapi.SecurityMethod) {
62
63         if methodsFromInterface != nil {
64                 securityMethods = append(securityMethods, *methodsFromInterface...)
65         }
66         if prefMethods != nil {
67                 securityMethods = append(securityMethods, *prefMethods...)
68         }
69 }
70
71 func addSecurityMethodsFromAefProfile(afpProfile *publishserviceapi.AefProfile) {
72         if afpProfile.SecurityMethods != nil {
73                 securityMethods = append(securityMethods, *afpProfile.SecurityMethods...)
74         }
75 }
76
77 func getServiceByApiId(services *[]publishserviceapi.ServiceAPIDescription, apiId *string) *publishserviceapi.ServiceAPIDescription {
78
79         for _, service := range *services {
80                 if apiId != nil && strings.Compare(*service.ApiId, *apiId) == 0 {
81                         return &service
82                 }
83         }
84         return nil
85 }