blob: e4c149e8a8da6bb6f1756267e0403f310b8478a0 [file] [log] [blame]
Mohamed Abukar34e43832019-11-13 17:57:15 +02001/*
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
20package restful
21
22import (
Mohamed Abukar34e43832019-11-13 17:57:15 +020023 "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
41func NewRestful() *Restful {
42 r := &Restful{
43 helm: helm.NewHelm(),
44 cm: cm.NewCM(),
Mohamed Abukard9769772019-11-20 20:39:06 +020045 rh: resthooks.NewResthook(true),
Mohamed Abukar34e43832019-11-13 17:57:15 +020046 ready: false,
47 }
48 r.api = r.SetupHandler()
49 return r
50}
51
52func (r *Restful) Run() {
53 server := restapi.NewServer(r.api)
54 defer server.Shutdown()
Mohamed Abukard9769772019-11-20 20:39:06 +020055 server.Port = 8080
56 server.Host = "0.0.0.0"
Mohamed Abukar34e43832019-11-13 17:57:15 +020057
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
66func (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 Abukaraca8f3c2020-01-14 11:10:16 +020079
80 api.HealthGetHealthReadyHandler = health.GetHealthReadyHandlerFunc(
Mohamed Abukar34e43832019-11-13 17:57:15 +020081 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 Abukaraca8f3c2020-01-14 11:10:16 +020090
Mohamed Abukar34e43832019-11-13 17:57:15 +020091 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 Abukaraca8f3c2020-01-14 11:10:16 +020098
Mohamed Abukar34e43832019-11-13 17:57:15 +020099 api.AddSubscriptionHandler = operations.AddSubscriptionHandlerFunc(
100 func(params operations.AddSubscriptionParams) middleware.Responder {
101 return operations.NewAddSubscriptionCreated().WithPayload(r.rh.AddSubscription(*params.SubscriptionRequest))
102 })
Mohamed Abukaraca8f3c2020-01-14 11:10:16 +0200103
Mohamed Abukar34e43832019-11-13 17:57:15 +0200104 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 Abukaraca8f3c2020-01-14 11:10:16 +0200111
Mohamed Abukar34e43832019-11-13 17:57:15 +0200112 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 Abukaraca8f3c2020-01-14 11:10:16 +0200128
Mohamed Abukar34e43832019-11-13 17:57:15 +0200129 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 Abukaraca8f3c2020-01-14 11:10:16 +0200136
Mohamed Abukar34e43832019-11-13 17:57:15 +0200137 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 Abukaraca8f3c2020-01-14 11:10:16 +0200144
Mohamed Abukar34e43832019-11-13 17:57:15 +0200145 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 Abukaraca8f3c2020-01-14 11:10:16 +0200156
Mohamed Abukar34e43832019-11-13 17:57:15 +0200157 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 Abukaraca8f3c2020-01-14 11:10:16 +0200165
Mohamed Abukar34e43832019-11-13 17:57:15 +0200166 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 Abukaraca8f3c2020-01-14 11:10:16 +0200178 return xapp.NewGetAllXappConfigOK().WithPayload(r.cm.UploadConfigAll())
Mohamed Abukar34e43832019-11-13 17:57:15 +0200179 })
Mohamed Abukaraca8f3c2020-01-14 11:10:16 +0200180
181 api.XappGetConfigElementHandler = xapp.GetConfigElementHandlerFunc(
182 func(params xapp.GetConfigElementParams) middleware.Responder {
183 return xapp.NewGetConfigElementOK().WithPayload(r.cm.UploadConfigElement(params.Element))
Mohamed Abukar34e43832019-11-13 17:57:15 +0200184 })
Mohamed Abukaraca8f3c2020-01-14 11:10:16 +0200185
Mohamed Abukar34e43832019-11-13 17:57:15 +0200186 api.XappModifyXappConfigHandler = xapp.ModifyXappConfigHandlerFunc(
187 func(params xapp.ModifyXappConfigParams) middleware.Responder {
188 result, err := r.cm.UpdateConfigMap(*params.XAppConfig)
Mohamed Abukaraca8f3c2020-01-14 11:10:16 +0200189 if err != nil {
Mohamed Abukar34e43832019-11-13 17:57:15 +0200190 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 Abukar34e43832019-11-13 17:57:15 +0200199
200 return api
201}
202
203func (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
211func (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 Abukaraca8f3c2020-01-14 11:10:16 +0200218 time.Sleep(time.Duration(5) * time.Second)
Mohamed Abukar34e43832019-11-13 17:57:15 +0200219 if result, _ := r.helm.Status(name); result.Instances != nil {
220 r.rh.PublishSubscription(result, models.EventTypeDeployed)
221 break
222 }
Mohamed Abukar34e43832019-11-13 17:57:15 +0200223 }
224}
225
226func (r *Restful) PublishXappDeleteEvent(xapp models.Xapp) {
227 r.rh.PublishSubscription(xapp, models.EventTypeUndeployed)
228}