3 // ========================LICENSE_START=================================
6 // Copyright (C) 2023: Nordix Foundation
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
12 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
26 "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
29 var securityMethods []publishserviceapi.SecurityMethod
31 func (newContext *ServiceSecurity) PrepareNewSecurityContext(services []publishserviceapi.ServiceAPIDescription) error {
32 securityMethods = []publishserviceapi.SecurityMethod{}
33 for i, securityInfo := range newContext.SecurityInfo {
35 if securityInfo.InterfaceDetails != nil {
36 addSecurityMethodsFromInterfaceDetails(securityInfo.InterfaceDetails.SecurityMethods, &securityInfo.PrefSecurityMethods)
39 checkNil := securityInfo.ApiId != nil && securityInfo.AefId != nil
41 service := getServiceByApiId(&services, securityInfo.ApiId)
42 afpProfile := service.GetAefProfileById(securityInfo.AefId)
44 addSecurityMethodsFromAefProfile(afpProfile)
48 if isSecuryMethodsEmpty() {
49 return fmt.Errorf("not found compatible security method")
51 newContext.SecurityInfo[i].SelSecurityMethod = &securityMethods[0]
56 func isSecuryMethodsEmpty() bool {
57 return len(securityMethods) <= 0
60 func addSecurityMethodsFromInterfaceDetails(methodsFromInterface *[]publishserviceapi.SecurityMethod, prefMethods *[]publishserviceapi.SecurityMethod) {
62 if methodsFromInterface != nil {
63 securityMethods = append(securityMethods, *methodsFromInterface...)
65 if prefMethods != nil {
66 securityMethods = append(securityMethods, *prefMethods...)
70 func addSecurityMethodsFromAefProfile(afpProfile *publishserviceapi.AefProfile) {
71 if afpProfile.SecurityMethods != nil {
72 securityMethods = append(securityMethods, *afpProfile.SecurityMethods...)
76 func getServiceByApiId(services *[]publishserviceapi.ServiceAPIDescription, apiId *string) *publishserviceapi.ServiceAPIDescription {
78 for _, service := range *services {
79 if apiId != nil && strings.Compare(*service.ApiId, *apiId) == 0 {