2 ==================================================================================
4 Copyright (c) 2021 Samsung
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.
18 This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 platform project (RICP).
21 ==================================================================================
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"
40 func NewRestful() *Restful {
42 rh: resthooks.NewResthook(),
44 r.api = r.setupHandler()
48 func (r *Restful) setupHandler() *operations.A1API {
49 swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
54 api := operations.NewA1API(swaggerSpec)
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()
60 return a1_mediator.NewA1ControllerGetHealthcheckInternalServerError()
62 return a1_mediator.NewA1ControllerGetHealthcheckOK()
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())
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()
76 if r.rh.IsTypeAlready(err) || r.rh.IsTypeMismatch(err) {
77 return a1_mediator.NewA1ControllerCreatePolicyTypeBadRequest()
79 return a1_mediator.NewA1ControllerCreatePolicyTypeServiceUnavailable()
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)))
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
94 if err = r.rh.CreatePolicyInstance(models.PolicyTypeID(params.PolicyTypeID), models.PolicyInstanceID(params.PolicyInstanceID), params.Body, notificationDestination); err == nil {
96 return a1_mediator.NewA1ControllerCreateOrReplacePolicyInstanceAccepted()
98 if r.rh.IsValidJson(err) {
99 return a1_mediator.NewA1ControllerCreateOrReplacePolicyInstanceBadRequest()
101 return a1_mediator.NewA1ControllerCreateOrReplacePolicyInstanceServiceUnavailable()
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)
110 if r.rh.IsPolicyInstanceNotFound(err) {
111 return a1_mediator.NewA1ControllerGetPolicyInstanceNotFound()
113 return a1_mediator.NewA1ControllerGetPolicyInstanceServiceUnavailable()
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 {
120 return a1_mediator.NewA1ControllerGetAllInstancesForTypeOK().WithPayload(resp)
123 if r.rh.IsPolicyInstanceNotFound(err) {
124 return a1_mediator.NewA1ControllerGetPolicyInstanceNotFound()
126 return a1_mediator.NewA1ControllerGetAllInstancesForTypeServiceUnavailable()
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()
136 return a1_mediator.NewA1ControllerDeletePolicyTypeServiceUnavailable()
139 return a1_mediator.NewA1ControllerDeletePolicyTypeNoContent()
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()
150 return a1_mediator.NewA1ControllerGetPolicyInstanceStatusServiceUnavailable()
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()
159 return a1_mediator.NewA1ControllerDeletePolicyInstanceServiceUnavailable()
162 return a1_mediator.NewA1ControllerDeletePolicyInstanceAccepted()
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()
171 return a1_e_i_data_delivery.NewA1ControllerDataDeliveryOK()
178 func (r *Restful) Run() {
180 server := restapi.NewServer(r.api)
181 defer server.Shutdown()
183 server.Host = "0.0.0.0"
184 if err := server.Serve(); err != nil {
185 log.Fatal(err.Error())