Seed code
[nonrtric/plt/sme.git] / internal / providermanagement / providermanagement.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2022: 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 providermanagement
22
23 import (
24         "net/http"
25         "path"
26         "strings"
27         "sync"
28
29         "github.com/labstack/echo/v4"
30
31         "oransc.org/nonrtric/sme/internal/common29122"
32         provapi "oransc.org/nonrtric/sme/internal/providermanagementapi"
33
34         log "github.com/sirupsen/logrus"
35 )
36
37 //go:generate mockery --name ServiceRegister
38 type ServiceRegister interface {
39         IsFunctionRegistered(aefId string) bool
40 }
41
42 type ProviderManager struct {
43         onboardedProviders map[string]provapi.APIProviderEnrolmentDetails
44         lock               sync.Mutex
45 }
46
47 func NewProviderManager() *ProviderManager {
48         return &ProviderManager{
49                 onboardedProviders: make(map[string]provapi.APIProviderEnrolmentDetails),
50         }
51 }
52
53 func (pm *ProviderManager) IsFunctionRegistered(aefId string) bool {
54         registered := false
55 out:
56         for _, provider := range pm.onboardedProviders {
57                 for _, registeredFunc := range *provider.ApiProvFuncs {
58                         if *registeredFunc.ApiProvFuncId == aefId {
59                                 registered = true
60                                 break out
61                         }
62                 }
63         }
64
65         return registered
66 }
67
68 func (pm *ProviderManager) PostRegistrations(ctx echo.Context) error {
69         var newProvider provapi.APIProviderEnrolmentDetails
70         err := ctx.Bind(&newProvider)
71         if err != nil {
72                 return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for provider")
73         }
74
75         if newProvider.ApiProvDomInfo == nil || *newProvider.ApiProvDomInfo == "" {
76                 return sendCoreError(ctx, http.StatusBadRequest, "Provider missing required ApiProvDomInfo")
77         }
78
79         pm.lock.Lock()
80         defer pm.lock.Unlock()
81
82         newProvider.ApiProvDomId = pm.getDomainId(newProvider.ApiProvDomInfo)
83
84         pm.registerFunctions(newProvider.ApiProvFuncs)
85         pm.onboardedProviders[*newProvider.ApiProvDomId] = newProvider
86
87         uri := ctx.Request().Host + ctx.Request().URL.String()
88         ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, *newProvider.ApiProvDomId))
89         err = ctx.JSON(http.StatusCreated, newProvider)
90         if err != nil {
91                 // Something really bad happened, tell Echo that our handler failed
92                 return err
93         }
94
95         return nil
96 }
97
98 func (pm *ProviderManager) DeleteRegistrationsRegistrationId(ctx echo.Context, registrationId string) error {
99         pm.lock.Lock()
100         defer pm.lock.Unlock()
101
102         log.Debug(pm.onboardedProviders)
103         if _, ok := pm.onboardedProviders[registrationId]; ok {
104                 log.Debug("Deleting provider", registrationId)
105                 delete(pm.onboardedProviders, registrationId)
106         }
107
108         return ctx.NoContent(http.StatusNoContent)
109 }
110
111 func (pm *ProviderManager) PutRegistrationsRegistrationId(ctx echo.Context, registrationId string) error {
112         registeredProvider, ok := pm.onboardedProviders[registrationId]
113         if !ok {
114                 return sendCoreError(ctx, http.StatusBadRequest, "Provider must be onboarded before updating it")
115
116         }
117
118         var updatedProvider provapi.APIProviderEnrolmentDetails
119         err := ctx.Bind(&updatedProvider)
120         if err != nil {
121                 return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for provider")
122         }
123
124         for _, function := range *updatedProvider.ApiProvFuncs {
125                 if function.ApiProvFuncId == nil {
126                         function.ApiProvFuncId = pm.getFuncId(function.ApiProvFuncRole, function.ApiProvFuncInfo)
127                         registeredFuncs := *registeredProvider.ApiProvFuncs
128                         newFuncs := append(registeredFuncs, function)
129                         registeredProvider.ApiProvFuncs = &newFuncs
130                         pm.onboardedProviders[*registeredProvider.ApiProvDomId] = registeredProvider
131                 }
132         }
133
134         err = ctx.JSON(http.StatusOK, registeredProvider)
135         if err != nil {
136                 // Something really bad happened, tell Echo that our handler failed
137                 return err
138         }
139
140         return nil
141 }
142
143 func (pm *ProviderManager) ModifyIndApiProviderEnrolment(ctx echo.Context, registrationId string) error {
144         return ctx.NoContent(http.StatusNotImplemented)
145 }
146
147 func (pm *ProviderManager) registerFunctions(provFuncs *[]provapi.APIProviderFunctionDetails) {
148         if provFuncs == nil {
149                 return
150         }
151         for i, provFunc := range *provFuncs {
152                 (*provFuncs)[i].ApiProvFuncId = pm.getFuncId(provFunc.ApiProvFuncRole, provFunc.ApiProvFuncInfo)
153         }
154 }
155
156 func (pm *ProviderManager) getDomainId(domainInfo *string) *string {
157         idAsString := "domain_id_" + strings.ReplaceAll(*domainInfo, " ", "_")
158         return &idAsString
159 }
160
161 func (pm *ProviderManager) getFuncId(role provapi.ApiProviderFuncRole, funcInfo *string) *string {
162         var idPrefix string
163         switch role {
164         case provapi.ApiProviderFuncRoleAPF:
165                 idPrefix = "APF_id_"
166         case provapi.ApiProviderFuncRoleAMF:
167                 idPrefix = "AMF_id_"
168         case provapi.ApiProviderFuncRoleAEF:
169                 idPrefix = "AEF_id_"
170         default:
171                 idPrefix = "function_id_"
172         }
173         idAsString := idPrefix + strings.ReplaceAll(*funcInfo, " ", "_")
174         return &idAsString
175 }
176
177 // This function wraps sending of an error in the Error format, and
178 // handling the failure to marshal that.
179 func sendCoreError(ctx echo.Context, code int, message string) error {
180         pd := common29122.ProblemDetails{
181                 Cause:  &message,
182                 Status: &code,
183         }
184         err := ctx.JSON(code, pd)
185         return err
186 }