2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
27 "gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/models"
28 "gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/restapi"
29 "gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/restapi/operations"
30 "gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/restapi/operations/health"
31 "gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/restapi/operations/xapp"
32 "github.com/go-openapi/loads"
33 "github.com/go-openapi/runtime/middleware"
35 "gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/appmgr"
36 "gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/cm"
37 "gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/helm"
38 "gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/resthooks"
41 func NewRestful() *Restful {
45 rh: resthooks.NewResthook(true),
48 r.api = r.SetupHandler()
52 func (r *Restful) Run() {
53 server := restapi.NewServer(r.api)
54 defer server.Shutdown()
56 server.Host = "0.0.0.0"
58 appmgr.Logger.Info("Xapp manager started ... serving on %s:%d\n", server.Host, server.Port)
61 if err := server.Serve(); err != nil {
62 log.Fatal(err.Error())
66 func (r *Restful) SetupHandler() *operations.AppManagerAPI {
67 swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
69 appmgr.Logger.Error(err.Error())
72 api := operations.NewAppManagerAPI(swaggerSpec)
74 // URL: /ric/v1/health
75 api.HealthGetHealthAliveHandler = health.GetHealthAliveHandlerFunc(
76 func(params health.GetHealthAliveParams) middleware.Responder {
77 return health.NewGetHealthAliveOK()
80 api.HealthGetHealthReadyHandler = health.GetHealthReadyHandlerFunc(
81 func(params health.GetHealthReadyParams) middleware.Responder {
82 return health.NewGetHealthReadyOK()
85 // URL: /ric/v1/subscriptions
86 api.GetSubscriptionsHandler = operations.GetSubscriptionsHandlerFunc(
87 func(params operations.GetSubscriptionsParams) middleware.Responder {
88 return operations.NewGetSubscriptionsOK().WithPayload(r.rh.GetAllSubscriptions())
91 api.GetSubscriptionByIDHandler = operations.GetSubscriptionByIDHandlerFunc(
92 func(params operations.GetSubscriptionByIDParams) middleware.Responder {
93 if result, found := r.rh.GetSubscriptionById(params.SubscriptionID); found {
94 return operations.NewGetSubscriptionByIDOK().WithPayload(&result)
96 return operations.NewGetSubscriptionByIDNotFound()
99 api.AddSubscriptionHandler = operations.AddSubscriptionHandlerFunc(
100 func(params operations.AddSubscriptionParams) middleware.Responder {
101 return operations.NewAddSubscriptionCreated().WithPayload(r.rh.AddSubscription(*params.SubscriptionRequest))
104 api.ModifySubscriptionHandler = operations.ModifySubscriptionHandlerFunc(
105 func(params operations.ModifySubscriptionParams) middleware.Responder {
106 if _, ok := r.rh.ModifySubscription(params.SubscriptionID, *params.SubscriptionRequest); ok {
107 return operations.NewModifySubscriptionOK()
109 return operations.NewModifySubscriptionBadRequest()
112 api.DeleteSubscriptionHandler = operations.DeleteSubscriptionHandlerFunc(
113 func(params operations.DeleteSubscriptionParams) middleware.Responder {
114 if _, ok := r.rh.DeleteSubscription(params.SubscriptionID); ok {
115 return operations.NewDeleteSubscriptionNoContent()
117 return operations.NewDeleteSubscriptionBadRequest()
121 api.XappGetAllXappsHandler = xapp.GetAllXappsHandlerFunc(
122 func(params xapp.GetAllXappsParams) middleware.Responder {
123 if result, err := r.helm.StatusAll(); err == nil {
124 return xapp.NewGetAllXappsOK().WithPayload(result)
126 return xapp.NewGetAllXappsInternalServerError()
129 api.XappListAllXappsHandler = xapp.ListAllXappsHandlerFunc(
130 func(params xapp.ListAllXappsParams) middleware.Responder {
131 if result := r.helm.SearchAll(); err == nil {
132 return xapp.NewListAllXappsOK().WithPayload(result)
134 return xapp.NewListAllXappsInternalServerError()
137 api.XappGetXappByNameHandler = xapp.GetXappByNameHandlerFunc(
138 func(params xapp.GetXappByNameParams) middleware.Responder {
139 if result, err := r.helm.Status(params.XAppName); err == nil {
140 return xapp.NewGetXappByNameOK().WithPayload(&result)
142 return xapp.NewGetXappByNameNotFound()
145 api.XappGetXappInstanceByNameHandler = xapp.GetXappInstanceByNameHandlerFunc(
146 func(params xapp.GetXappInstanceByNameParams) middleware.Responder {
147 if result, err := r.helm.Status(params.XAppName); err == nil {
148 for _, v := range result.Instances {
149 if *v.Name == params.XAppInstanceName {
150 return xapp.NewGetXappInstanceByNameOK().WithPayload(v)
154 return xapp.NewGetXappInstanceByNameNotFound()
157 api.XappDeployXappHandler = xapp.DeployXappHandlerFunc(
158 func(params xapp.DeployXappParams) middleware.Responder {
159 if result, err := r.helm.Install(*params.XappDescriptor); err == nil {
160 go r.PublishXappCreateEvent(params)
161 return xapp.NewDeployXappCreated().WithPayload(&result)
163 return xapp.NewUndeployXappInternalServerError()
166 api.XappUndeployXappHandler = xapp.UndeployXappHandlerFunc(
167 func(params xapp.UndeployXappParams) middleware.Responder {
168 if result, err := r.helm.Delete(params.XAppName); err == nil {
169 go r.PublishXappDeleteEvent(result)
170 return xapp.NewUndeployXappNoContent()
172 return xapp.NewUndeployXappInternalServerError()
175 // URL: /ric/v1/config
176 api.XappGetAllXappConfigHandler = xapp.GetAllXappConfigHandlerFunc(
177 func(params xapp.GetAllXappConfigParams) middleware.Responder {
178 return xapp.NewGetAllXappConfigOK().WithPayload(r.cm.UploadConfigAll())
181 api.XappGetConfigElementHandler = xapp.GetConfigElementHandlerFunc(
182 func(params xapp.GetConfigElementParams) middleware.Responder {
183 return xapp.NewGetConfigElementOK().WithPayload(r.cm.UploadConfigElement(params.Element))
186 api.XappModifyXappConfigHandler = xapp.ModifyXappConfigHandlerFunc(
187 func(params xapp.ModifyXappConfigParams) middleware.Responder {
188 result, err := r.cm.UpdateConfigMap(*params.XAppConfig)
190 if err.Error() != "Validation failed!" {
191 return xapp.NewModifyXappConfigInternalServerError()
193 return xapp.NewModifyXappConfigUnprocessableEntity()
196 r.rh.PublishSubscription(models.Xapp{}, models.EventTypeModified)
197 return xapp.NewModifyXappConfigOK().WithPayload(result)
203 func (r *Restful) NotifyClients() {
205 if xapps, err := r.helm.StatusAll(); err == nil {
206 r.rh.NotifyClients(xapps, models.EventTypeRestarted)
211 func (r *Restful) PublishXappCreateEvent(params xapp.DeployXappParams) {
212 name := *params.XappDescriptor.XappName
213 if params.XappDescriptor.ReleaseName != "" {
214 name = params.XappDescriptor.ReleaseName
217 for i := 0; i < 5; i++ {
218 time.Sleep(time.Duration(5) * time.Second)
219 if result, _ := r.helm.Status(name); result.Instances != nil {
220 r.rh.PublishSubscription(result, models.EventTypeDeployed)
226 func (r *Restful) PublishXappDeleteEvent(xapp models.Xapp) {
227 r.rh.PublishSubscription(xapp, models.EventTypeUndeployed)