Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | ================================================================================== |
| 3 | Copyright (c) 2019 AT&T Intellectual Property. |
| 4 | Copyright (c) 2019 Nokia |
| 5 | |
| 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 |
| 9 | |
| 10 | http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | |
| 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 | ================================================================================== |
| 18 | */ |
| 19 | |
| 20 | package restful |
| 21 | |
| 22 | import ( |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 23 | "log" |
| 24 | "os" |
| 25 | "time" |
| 26 | |
| 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" |
| 34 | |
| 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" |
| 39 | ) |
| 40 | |
| 41 | func NewRestful() *Restful { |
| 42 | r := &Restful{ |
| 43 | helm: helm.NewHelm(), |
| 44 | cm: cm.NewCM(), |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 45 | rh: resthooks.NewResthook(true), |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 46 | ready: false, |
| 47 | } |
| 48 | r.api = r.SetupHandler() |
| 49 | return r |
| 50 | } |
| 51 | |
| 52 | func (r *Restful) Run() { |
| 53 | server := restapi.NewServer(r.api) |
| 54 | defer server.Shutdown() |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 55 | server.Port = 8080 |
| 56 | server.Host = "0.0.0.0" |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 57 | |
| 58 | appmgr.Logger.Info("Xapp manager started ... serving on %s:%d\n", server.Host, server.Port) |
| 59 | |
| 60 | go r.NotifyClients() |
| 61 | if err := server.Serve(); err != nil { |
| 62 | log.Fatal(err.Error()) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func (r *Restful) SetupHandler() *operations.AppManagerAPI { |
| 67 | swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON) |
| 68 | if err != nil { |
| 69 | appmgr.Logger.Error(err.Error()) |
| 70 | os.Exit(1) |
| 71 | } |
| 72 | api := operations.NewAppManagerAPI(swaggerSpec) |
| 73 | |
| 74 | // URL: /ric/v1/health |
| 75 | api.HealthGetHealthAliveHandler = health.GetHealthAliveHandlerFunc( |
| 76 | func(params health.GetHealthAliveParams) middleware.Responder { |
| 77 | return health.NewGetHealthAliveOK() |
| 78 | }) |
Mohamed Abukar | aca8f3c | 2020-01-14 11:10:16 +0200 | [diff] [blame^] | 79 | |
| 80 | api.HealthGetHealthReadyHandler = health.GetHealthReadyHandlerFunc( |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 81 | func(params health.GetHealthReadyParams) middleware.Responder { |
| 82 | return health.NewGetHealthReadyOK() |
| 83 | }) |
| 84 | |
| 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()) |
| 89 | }) |
Mohamed Abukar | aca8f3c | 2020-01-14 11:10:16 +0200 | [diff] [blame^] | 90 | |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 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) |
| 95 | } |
| 96 | return operations.NewGetSubscriptionByIDNotFound() |
| 97 | }) |
Mohamed Abukar | aca8f3c | 2020-01-14 11:10:16 +0200 | [diff] [blame^] | 98 | |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 99 | api.AddSubscriptionHandler = operations.AddSubscriptionHandlerFunc( |
| 100 | func(params operations.AddSubscriptionParams) middleware.Responder { |
| 101 | return operations.NewAddSubscriptionCreated().WithPayload(r.rh.AddSubscription(*params.SubscriptionRequest)) |
| 102 | }) |
Mohamed Abukar | aca8f3c | 2020-01-14 11:10:16 +0200 | [diff] [blame^] | 103 | |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 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() |
| 108 | } |
| 109 | return operations.NewModifySubscriptionBadRequest() |
| 110 | }) |
Mohamed Abukar | aca8f3c | 2020-01-14 11:10:16 +0200 | [diff] [blame^] | 111 | |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 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() |
| 116 | } |
| 117 | return operations.NewDeleteSubscriptionBadRequest() |
| 118 | }) |
| 119 | |
| 120 | // URL: /ric/v1/xapp |
| 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) |
| 125 | } |
| 126 | return xapp.NewGetAllXappsInternalServerError() |
| 127 | }) |
Mohamed Abukar | aca8f3c | 2020-01-14 11:10:16 +0200 | [diff] [blame^] | 128 | |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 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) |
| 133 | } |
| 134 | return xapp.NewListAllXappsInternalServerError() |
| 135 | }) |
Mohamed Abukar | aca8f3c | 2020-01-14 11:10:16 +0200 | [diff] [blame^] | 136 | |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 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) |
| 141 | } |
| 142 | return xapp.NewGetXappByNameNotFound() |
| 143 | }) |
Mohamed Abukar | aca8f3c | 2020-01-14 11:10:16 +0200 | [diff] [blame^] | 144 | |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 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) |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | return xapp.NewGetXappInstanceByNameNotFound() |
| 155 | }) |
Mohamed Abukar | aca8f3c | 2020-01-14 11:10:16 +0200 | [diff] [blame^] | 156 | |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 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) |
| 162 | } |
| 163 | return xapp.NewUndeployXappInternalServerError() |
| 164 | }) |
Mohamed Abukar | aca8f3c | 2020-01-14 11:10:16 +0200 | [diff] [blame^] | 165 | |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 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() |
| 171 | } |
| 172 | return xapp.NewUndeployXappInternalServerError() |
| 173 | }) |
| 174 | |
| 175 | // URL: /ric/v1/config |
| 176 | api.XappGetAllXappConfigHandler = xapp.GetAllXappConfigHandlerFunc( |
| 177 | func(params xapp.GetAllXappConfigParams) middleware.Responder { |
Mohamed Abukar | aca8f3c | 2020-01-14 11:10:16 +0200 | [diff] [blame^] | 178 | return xapp.NewGetAllXappConfigOK().WithPayload(r.cm.UploadConfigAll()) |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 179 | }) |
Mohamed Abukar | aca8f3c | 2020-01-14 11:10:16 +0200 | [diff] [blame^] | 180 | |
| 181 | api.XappGetConfigElementHandler = xapp.GetConfigElementHandlerFunc( |
| 182 | func(params xapp.GetConfigElementParams) middleware.Responder { |
| 183 | return xapp.NewGetConfigElementOK().WithPayload(r.cm.UploadConfigElement(params.Element)) |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 184 | }) |
Mohamed Abukar | aca8f3c | 2020-01-14 11:10:16 +0200 | [diff] [blame^] | 185 | |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 186 | api.XappModifyXappConfigHandler = xapp.ModifyXappConfigHandlerFunc( |
| 187 | func(params xapp.ModifyXappConfigParams) middleware.Responder { |
| 188 | result, err := r.cm.UpdateConfigMap(*params.XAppConfig) |
Mohamed Abukar | aca8f3c | 2020-01-14 11:10:16 +0200 | [diff] [blame^] | 189 | if err != nil { |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 190 | if err.Error() != "Validation failed!" { |
| 191 | return xapp.NewModifyXappConfigInternalServerError() |
| 192 | } else { |
| 193 | return xapp.NewModifyXappConfigUnprocessableEntity() |
| 194 | } |
| 195 | } |
| 196 | r.rh.PublishSubscription(models.Xapp{}, models.EventTypeModified) |
| 197 | return xapp.NewModifyXappConfigOK().WithPayload(result) |
| 198 | }) |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 199 | |
| 200 | return api |
| 201 | } |
| 202 | |
| 203 | func (r *Restful) NotifyClients() { |
| 204 | r.helm.Initialize() |
| 205 | if xapps, err := r.helm.StatusAll(); err == nil { |
| 206 | r.rh.NotifyClients(xapps, models.EventTypeRestarted) |
| 207 | r.ready = true |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | func (r *Restful) PublishXappCreateEvent(params xapp.DeployXappParams) { |
| 212 | name := *params.XappDescriptor.XappName |
| 213 | if params.XappDescriptor.ReleaseName != "" { |
| 214 | name = params.XappDescriptor.ReleaseName |
| 215 | } |
| 216 | |
| 217 | for i := 0; i < 5; i++ { |
Mohamed Abukar | aca8f3c | 2020-01-14 11:10:16 +0200 | [diff] [blame^] | 218 | time.Sleep(time.Duration(5) * time.Second) |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 219 | if result, _ := r.helm.Status(name); result.Instances != nil { |
| 220 | r.rh.PublishSubscription(result, models.EventTypeDeployed) |
| 221 | break |
| 222 | } |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | func (r *Restful) PublishXappDeleteEvent(xapp models.Xapp) { |
| 227 | r.rh.PublishSubscription(xapp, models.EventTypeUndeployed) |
| 228 | } |