Policy status notification handling - initial rollup
[ric-plt/a1.git] / pkg / restful / restful.go
1 /*
2 ==================================================================================
3
4         Copyright (c) 2021 Samsung
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          This source code is part of the near-RT RIC (RAN Intelligent Controller)
19          platform project (RICP).
20
21 ==================================================================================
22 */
23 package restful
24
25 import (
26         "log"
27         "os"
28
29         "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1"
30         "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models"
31         "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/restapi"
32         "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/restapi/operations"
33         "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/restapi/operations/a1_e_i_data_delivery"
34         "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/restapi/operations/a1_mediator"
35         "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/resthooks"
36         "github.com/go-openapi/loads"
37         "github.com/go-openapi/runtime/middleware"
38 )
39
40 func NewRestful() *Restful {
41         r := &Restful{
42                 rh: resthooks.NewResthook(),
43         }
44         r.api = r.setupHandler()
45         return r
46 }
47
48 func (r *Restful) setupHandler() *operations.A1API {
49         swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
50         if err != nil {
51                 os.Exit(1)
52         }
53
54         api := operations.NewA1API(swaggerSpec)
55
56         api.A1MediatorA1ControllerGetHealthcheckHandler = a1_mediator.A1ControllerGetHealthcheckHandlerFunc(func(param a1_mediator.A1ControllerGetHealthcheckParams) middleware.Responder {
57                 a1.Logger.Debug("handler for get Health Check of A1")
58                 resp := r.rh.GetA1Health()
59                 if resp == false {
60                         return a1_mediator.NewA1ControllerGetHealthcheckInternalServerError()
61                 }
62                 return a1_mediator.NewA1ControllerGetHealthcheckOK()
63         })
64
65         api.A1MediatorA1ControllerGetAllPolicyTypesHandler = a1_mediator.A1ControllerGetAllPolicyTypesHandlerFunc(func(param a1_mediator.A1ControllerGetAllPolicyTypesParams) middleware.Responder {
66                 a1.Logger.Debug("handler for get all policy type")
67                 return a1_mediator.NewA1ControllerGetAllPolicyTypesOK().WithPayload(r.rh.GetAllPolicyType())
68         })
69
70         api.A1MediatorA1ControllerCreatePolicyTypeHandler = a1_mediator.A1ControllerCreatePolicyTypeHandlerFunc(func(params a1_mediator.A1ControllerCreatePolicyTypeParams) middleware.Responder {
71                 a1.Logger.Debug("handler for Create policy type ")
72                 if err = r.rh.CreatePolicyType(models.PolicyTypeID(params.PolicyTypeID), *params.Body); err == nil {
73                         //Increase prometheus counter
74                         return a1_mediator.NewA1ControllerCreatePolicyTypeCreated()
75                 }
76                 if r.rh.IsTypeAlready(err) || r.rh.IsTypeMismatch(err) {
77                         return a1_mediator.NewA1ControllerCreatePolicyTypeBadRequest()
78                 }
79                 return a1_mediator.NewA1ControllerCreatePolicyTypeServiceUnavailable()
80
81         })
82
83         api.A1MediatorA1ControllerGetPolicyTypeHandler = a1_mediator.A1ControllerGetPolicyTypeHandlerFunc(func(params a1_mediator.A1ControllerGetPolicyTypeParams) middleware.Responder {
84                 a1.Logger.Debug("handler for get policy type from policytypeID")
85                 return a1_mediator.NewA1ControllerGetPolicyTypeOK().WithPayload(r.rh.GetPolicyType(models.PolicyTypeID(params.PolicyTypeID)))
86         })
87
88         api.A1MediatorA1ControllerCreateOrReplacePolicyInstanceHandler = a1_mediator.A1ControllerCreateOrReplacePolicyInstanceHandlerFunc(func(params a1_mediator.A1ControllerCreateOrReplacePolicyInstanceParams) middleware.Responder {
89                 a1.Logger.Debug("handler for create policy type instance ")
90                 var notificationDestination string
91                 if params.NotificationDestination != nil {
92                         notificationDestination = *params.NotificationDestination
93                 }
94                 if err = r.rh.CreatePolicyInstance(models.PolicyTypeID(params.PolicyTypeID), models.PolicyInstanceID(params.PolicyInstanceID), params.Body, notificationDestination); err == nil {
95
96                         return a1_mediator.NewA1ControllerCreateOrReplacePolicyInstanceAccepted()
97                 }
98                 if r.rh.IsValidJson(err) {
99                         return a1_mediator.NewA1ControllerCreateOrReplacePolicyInstanceBadRequest()
100                 }
101                 return a1_mediator.NewA1ControllerCreateOrReplacePolicyInstanceServiceUnavailable()
102
103         })
104
105         api.A1MediatorA1ControllerGetPolicyInstanceHandler = a1_mediator.A1ControllerGetPolicyInstanceHandlerFunc(func(params a1_mediator.A1ControllerGetPolicyInstanceParams) middleware.Responder {
106                 a1.Logger.Debug("handler for get policy instance from policytypeID")
107                 if resp, err := r.rh.GetPolicyInstance(models.PolicyTypeID(params.PolicyTypeID), models.PolicyInstanceID(params.PolicyInstanceID)); err == nil {
108                         return a1_mediator.NewA1ControllerGetPolicyInstanceOK().WithPayload(resp)
109                 }
110                 if r.rh.IsPolicyInstanceNotFound(err) {
111                         return a1_mediator.NewA1ControllerGetPolicyInstanceNotFound()
112                 }
113                 return a1_mediator.NewA1ControllerGetPolicyInstanceServiceUnavailable()
114         })
115
116         api.A1MediatorA1ControllerGetAllInstancesForTypeHandler = a1_mediator.A1ControllerGetAllInstancesForTypeHandlerFunc(func(params a1_mediator.A1ControllerGetAllInstancesForTypeParams) middleware.Responder {
117                 a1.Logger.Debug("handler for get all policy instance")
118                 if resp, err := r.rh.GetAllPolicyInstance(models.PolicyTypeID(params.PolicyTypeID)); err == nil {
119                         if resp != nil {
120                                 return a1_mediator.NewA1ControllerGetAllInstancesForTypeOK().WithPayload(resp)
121                         }
122                 }
123                 if r.rh.IsPolicyInstanceNotFound(err) {
124                         return a1_mediator.NewA1ControllerGetPolicyInstanceNotFound()
125                 }
126                 return a1_mediator.NewA1ControllerGetAllInstancesForTypeServiceUnavailable()
127
128         })
129
130         api.A1MediatorA1ControllerDeletePolicyTypeHandler = a1_mediator.A1ControllerDeletePolicyTypeHandlerFunc(func(params a1_mediator.A1ControllerDeletePolicyTypeParams) middleware.Responder {
131                 a1.Logger.Debug("handler for delete policy type")
132                 if err := r.rh.DeletePolicyType(models.PolicyTypeID(params.PolicyTypeID)); err != nil {
133                         if r.rh.CanPolicyTypeBeDeleted(err) {
134                                 return a1_mediator.NewA1ControllerDeletePolicyTypeBadRequest()
135                         }
136                         return a1_mediator.NewA1ControllerDeletePolicyTypeServiceUnavailable()
137                 }
138
139                 return a1_mediator.NewA1ControllerDeletePolicyTypeNoContent()
140
141         })
142
143         api.A1MediatorA1ControllerGetPolicyInstanceStatusHandler = a1_mediator.A1ControllerGetPolicyInstanceStatusHandlerFunc(func(params a1_mediator.A1ControllerGetPolicyInstanceStatusParams) middleware.Responder {
144                 a1.Logger.Debug("handler for get policy instance status")
145                 if resp, err := r.rh.GetPolicyInstanceStatus(models.PolicyTypeID(params.PolicyTypeID), models.PolicyInstanceID(params.PolicyInstanceID)); err == nil {
146                         return a1_mediator.NewA1ControllerGetPolicyInstanceStatusOK().WithPayload(resp)
147                 } else if r.rh.IsPolicyInstanceNotFound(err) {
148                         return a1_mediator.NewA1ControllerGetPolicyInstanceStatusNotFound()
149                 }
150                 return a1_mediator.NewA1ControllerGetPolicyInstanceStatusServiceUnavailable()
151         })
152
153         api.A1MediatorA1ControllerDeletePolicyInstanceHandler = a1_mediator.A1ControllerDeletePolicyInstanceHandlerFunc(func(params a1_mediator.A1ControllerDeletePolicyInstanceParams) middleware.Responder {
154                 a1.Logger.Debug("handler for delete policy instance")
155                 if err := r.rh.DeletePolicyInstance(models.PolicyTypeID(params.PolicyTypeID), models.PolicyInstanceID(params.PolicyInstanceID)); err != nil {
156                         if r.rh.CanPolicyInstanceBeDeleted(err) {
157                                 return a1_mediator.NewA1ControllerDeletePolicyInstanceNotFound()
158                         }
159                         return a1_mediator.NewA1ControllerDeletePolicyInstanceServiceUnavailable()
160                 }
161
162                 return a1_mediator.NewA1ControllerDeletePolicyInstanceAccepted()
163
164         })
165
166         api.A1eiDataDeliveryA1ControllerDataDeliveryHandler = a1_e_i_data_delivery.A1ControllerDataDeliveryHandlerFunc(func(params a1_e_i_data_delivery.A1ControllerDataDeliveryParams) middleware.Responder {
167                 a1.Logger.Debug("handler for EI data delivery")
168                 if err = r.rh.DataDelivery(params.Body); err != nil {
169                         return a1_e_i_data_delivery.NewA1ControllerDataDeliveryNotFound()
170                 }
171                 return a1_e_i_data_delivery.NewA1ControllerDataDeliveryOK()
172         })
173
174         return api
175
176 }
177
178 func (r *Restful) Run() {
179
180         server := restapi.NewServer(r.api)
181         defer server.Shutdown()
182         server.Port = 10000
183         server.Host = "0.0.0.0"
184         if err := server.Serve(); err != nil {
185                 log.Fatal(err.Error())
186         }
187 }