blob: 571d2a06fde83dfb6346e5e8d3321b43c54edc70 [file] [log] [blame]
elinuxhenrik3d374362022-10-18 16:51:52 +02001// -
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
21package invokermanagement
22
23import (
24 "net/http"
25 "path"
26 "strconv"
27 "strings"
28 "sync"
29
30 publishapi "oransc.org/nonrtric/sme/internal/publishserviceapi"
31
32 "oransc.org/nonrtric/sme/internal/common29122"
33 invokerapi "oransc.org/nonrtric/sme/internal/invokermanagementapi"
34
35 "oransc.org/nonrtric/sme/internal/publishservice"
36
37 "github.com/labstack/echo/v4"
38)
39
40//go:generate mockery --name InvokerRegister
41type InvokerRegister interface {
42 IsInvokerRegistered(invokerId string) bool
43 VerifyInvokerSecret(invokerId, secret string) bool
44}
45
46type InvokerManager struct {
47 onboardedInvokers map[string]invokerapi.APIInvokerEnrolmentDetails
48 apiRegister publishservice.APIRegister
49 nextId int64
50 lock sync.Mutex
51}
52
53func NewInvokerManager(apiRegister publishservice.APIRegister) *InvokerManager {
54 return &InvokerManager{
55 onboardedInvokers: make(map[string]invokerapi.APIInvokerEnrolmentDetails),
56 apiRegister: apiRegister,
57 nextId: 1000,
58 }
59}
60
61func (im *InvokerManager) IsInvokerRegistered(invokerId string) bool {
62 im.lock.Lock()
63 defer im.lock.Unlock()
64
65 _, registered := im.onboardedInvokers[invokerId]
66 return registered
67}
68
69func (im *InvokerManager) VerifyInvokerSecret(invokerId, secret string) bool {
70 im.lock.Lock()
71 defer im.lock.Unlock()
72
73 verified := false
74 if invoker, registered := im.onboardedInvokers[invokerId]; registered {
75 verified = *invoker.OnboardingInformation.OnboardingSecret == secret
76 }
77 return verified
78}
79
80func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error {
81 var newInvoker invokerapi.APIInvokerEnrolmentDetails
82 err := ctx.Bind(&newInvoker)
83 if err != nil {
84 return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for invoker")
85 }
86
87 shouldReturn, coreError := im.validateInvoker(newInvoker, ctx)
88 if shouldReturn {
89 return coreError
90 }
91
92 im.lock.Lock()
93 defer im.lock.Unlock()
94
95 newInvoker.ApiInvokerId = im.getId(newInvoker.ApiInvokerInformation)
96 onboardingSecret := "onboarding_secret_"
97 if newInvoker.ApiInvokerInformation != nil {
98 onboardingSecret = onboardingSecret + strings.ReplaceAll(*newInvoker.ApiInvokerInformation, " ", "_")
99 } else {
100 onboardingSecret = onboardingSecret + *newInvoker.ApiInvokerId
101 }
102 newInvoker.OnboardingInformation.OnboardingSecret = &onboardingSecret
103
104 im.onboardedInvokers[*newInvoker.ApiInvokerId] = newInvoker
105
106 uri := ctx.Request().Host + ctx.Request().URL.String()
107 ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, *newInvoker.ApiInvokerId))
108 err = ctx.JSON(http.StatusCreated, newInvoker)
109 if err != nil {
110 // Something really bad happened, tell Echo that our handler failed
111 return err
112 }
113
114 return nil
115}
116
117func (im *InvokerManager) DeleteOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
118 im.lock.Lock()
119 defer im.lock.Unlock()
120
121 delete(im.onboardedInvokers, onboardingId)
122
123 return ctx.NoContent(http.StatusNoContent)
124}
125
126func (im *InvokerManager) PutOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
127 var invoker invokerapi.APIInvokerEnrolmentDetails
128 err := ctx.Bind(&invoker)
129 if err != nil {
130 return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for invoker")
131 }
132
133 if onboardingId != *invoker.ApiInvokerId {
134 return sendCoreError(ctx, http.StatusBadRequest, "Invoker ApiInvokerId not matching")
135 }
136
137 shouldReturn, coreError := im.validateInvoker(invoker, ctx)
138 if shouldReturn {
139 return coreError
140 }
141
142 im.lock.Lock()
143 defer im.lock.Unlock()
144
145 if _, ok := im.onboardedInvokers[onboardingId]; ok {
146 im.onboardedInvokers[*invoker.ApiInvokerId] = invoker
147 } else {
148 return sendCoreError(ctx, http.StatusNotFound, "The invoker to update has not been onboarded")
149 }
150
151 err = ctx.JSON(http.StatusOK, invoker)
152 if err != nil {
153 // Something really bad happened, tell Echo that our handler failed
154 return err
155 }
156
157 return nil
158}
159
160func (im *InvokerManager) ModifyIndApiInvokeEnrolment(ctx echo.Context, onboardingId string) error {
161 return ctx.NoContent(http.StatusNotImplemented)
162}
163
164func (im *InvokerManager) validateInvoker(invoker invokerapi.APIInvokerEnrolmentDetails, ctx echo.Context) (bool, error) {
165 if invoker.NotificationDestination == "" {
166 return true, sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required NotificationDestination")
167 }
168
169 if invoker.OnboardingInformation.ApiInvokerPublicKey == "" {
170 return true, sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required OnboardingInformation.ApiInvokerPublicKey")
171 }
172
173 if !im.areAPIsRegistered(invoker.ApiList) {
174 return true, sendCoreError(ctx, http.StatusBadRequest, "Some APIs needed by invoker are not registered")
175 }
176
177 return false, nil
178}
179
180func (im *InvokerManager) areAPIsRegistered(apis *invokerapi.APIList) bool {
181 if apis == nil {
182 return true
183 }
184 return im.apiRegister.AreAPIsRegistered((*[]publishapi.ServiceAPIDescription)(apis))
185}
186
187func (im *InvokerManager) getId(invokerInfo *string) *string {
188 idAsString := "api_invoker_id_"
189 if invokerInfo != nil {
190 idAsString = idAsString + strings.ReplaceAll(*invokerInfo, " ", "_")
191 } else {
192 idAsString = idAsString + strconv.FormatInt(im.nextId, 10)
193 im.nextId = im.nextId + 1
194 }
195 return &idAsString
196}
197
198// This function wraps sending of an error in the Error format, and
199// handling the failure to marshal that.
200func sendCoreError(ctx echo.Context, code int, message string) error {
201 pd := common29122.ProblemDetails{
202 Cause: &message,
203 Status: &code,
204 }
205 err := ctx.JSON(code, pd)
206 return err
207}