subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | ================================================================================== |
| 3 | Copyright (c) 2021 Samsung |
| 4 | |
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations under the License. |
| 16 | |
| 17 | This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 18 | platform project (RICP). |
| 19 | ================================================================================== |
| 20 | */ |
| 21 | package restful |
| 22 | |
| 23 | import ( |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 24 | "log" |
| 25 | "os" |
| 26 | |
naman.gupta | ab3e9ee | 2022-02-18 18:03:36 +0530 | [diff] [blame] | 27 | "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1" |
| 28 | "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models" |
| 29 | "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/restapi" |
| 30 | "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/restapi/operations" |
naman.gupta | 159ddb7 | 2022-11-08 19:52:05 +0530 | [diff] [blame] | 31 | "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/restapi/operations/a1_e_i_data_delivery" |
naman.gupta | ab3e9ee | 2022-02-18 18:03:36 +0530 | [diff] [blame] | 32 | "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/restapi/operations/a1_mediator" |
| 33 | "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/resthooks" |
subhash kumar singh | 6017d6a | 2021-10-26 12:14:26 +0000 | [diff] [blame] | 34 | "github.com/go-openapi/loads" |
| 35 | "github.com/go-openapi/runtime/middleware" |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 36 | ) |
| 37 | |
| 38 | func NewRestful() *Restful { |
| 39 | r := &Restful{ |
| 40 | rh: resthooks.NewResthook(), |
| 41 | } |
| 42 | r.api = r.setupHandler() |
| 43 | return r |
| 44 | } |
| 45 | |
| 46 | func (r *Restful) setupHandler() *operations.A1API { |
| 47 | swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON) |
| 48 | if err != nil { |
| 49 | os.Exit(1) |
| 50 | } |
| 51 | |
| 52 | api := operations.NewA1API(swaggerSpec) |
naman.gupta | a5d9928 | 2022-11-17 18:20:03 +0530 | [diff] [blame] | 53 | |
| 54 | api.A1MediatorA1ControllerGetHealthcheckHandler = a1_mediator.A1ControllerGetHealthcheckHandlerFunc(func(param a1_mediator.A1ControllerGetHealthcheckParams) middleware.Responder { |
| 55 | a1.Logger.Debug("handler for get Health Check of A1") |
| 56 | resp := r.rh.GetA1Health() |
| 57 | if resp == false { |
| 58 | return a1_mediator.NewA1ControllerGetHealthcheckInternalServerError() |
| 59 | } |
| 60 | return a1_mediator.NewA1ControllerGetHealthcheckOK() |
| 61 | }) |
| 62 | |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 63 | api.A1MediatorA1ControllerGetAllPolicyTypesHandler = a1_mediator.A1ControllerGetAllPolicyTypesHandlerFunc(func(param a1_mediator.A1ControllerGetAllPolicyTypesParams) middleware.Responder { |
naman.gupta | ab3e9ee | 2022-02-18 18:03:36 +0530 | [diff] [blame] | 64 | a1.Logger.Debug("handler for get all policy type") |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 65 | return a1_mediator.NewA1ControllerGetAllPolicyTypesOK().WithPayload(r.rh.GetAllPolicyType()) |
| 66 | }) |
naman.gupta | ab3e9ee | 2022-02-18 18:03:36 +0530 | [diff] [blame] | 67 | |
| 68 | api.A1MediatorA1ControllerCreatePolicyTypeHandler = a1_mediator.A1ControllerCreatePolicyTypeHandlerFunc(func(params a1_mediator.A1ControllerCreatePolicyTypeParams) middleware.Responder { |
naman.gupta | 6864473 | 2022-02-24 14:41:55 +0530 | [diff] [blame] | 69 | a1.Logger.Debug("handler for Create policy type ") |
naman.gupta | ab3e9ee | 2022-02-18 18:03:36 +0530 | [diff] [blame] | 70 | if err = r.rh.CreatePolicyType(models.PolicyTypeID(params.PolicyTypeID), *params.Body); err == nil { |
| 71 | //Increase prometheus counter |
| 72 | return a1_mediator.NewA1ControllerCreatePolicyTypeCreated() |
| 73 | } |
| 74 | if r.rh.IsTypeAlready(err) || r.rh.IsTypeMismatch(err) { |
| 75 | return a1_mediator.NewA1ControllerCreatePolicyTypeBadRequest() |
| 76 | } |
| 77 | return a1_mediator.NewA1ControllerCreatePolicyTypeServiceUnavailable() |
| 78 | |
| 79 | }) |
| 80 | |
naman.gupta | 6864473 | 2022-02-24 14:41:55 +0530 | [diff] [blame] | 81 | api.A1MediatorA1ControllerGetPolicyTypeHandler = a1_mediator.A1ControllerGetPolicyTypeHandlerFunc(func(params a1_mediator.A1ControllerGetPolicyTypeParams) middleware.Responder { |
naman.gupta | 3ad6de4 | 2022-04-26 21:59:05 +0530 | [diff] [blame] | 82 | a1.Logger.Debug("handler for get policy type from policytypeID") |
naman.gupta | 6864473 | 2022-02-24 14:41:55 +0530 | [diff] [blame] | 83 | return a1_mediator.NewA1ControllerGetPolicyTypeOK().WithPayload(r.rh.GetPolicyType(models.PolicyTypeID(params.PolicyTypeID))) |
| 84 | }) |
| 85 | |
naman.gupta | 3ad6de4 | 2022-04-26 21:59:05 +0530 | [diff] [blame] | 86 | api.A1MediatorA1ControllerCreateOrReplacePolicyInstanceHandler = a1_mediator.A1ControllerCreateOrReplacePolicyInstanceHandlerFunc(func(params a1_mediator.A1ControllerCreateOrReplacePolicyInstanceParams) middleware.Responder { |
| 87 | a1.Logger.Debug("handler for create policy type instance ") |
| 88 | if err = r.rh.CreatePolicyInstance(models.PolicyTypeID(params.PolicyTypeID), models.PolicyInstanceID(params.PolicyInstanceID), params.Body); err == nil { |
| 89 | return a1_mediator.NewA1ControllerCreateOrReplacePolicyInstanceAccepted() |
| 90 | } |
| 91 | if r.rh.IsValidJson(err) { |
| 92 | return a1_mediator.NewA1ControllerCreateOrReplacePolicyInstanceBadRequest() |
| 93 | } |
| 94 | return a1_mediator.NewA1ControllerCreateOrReplacePolicyInstanceServiceUnavailable() |
| 95 | |
| 96 | }) |
| 97 | |
naman.gupta | 6579f78 | 2022-05-02 15:34:31 +0530 | [diff] [blame] | 98 | api.A1MediatorA1ControllerGetPolicyInstanceHandler = a1_mediator.A1ControllerGetPolicyInstanceHandlerFunc(func(params a1_mediator.A1ControllerGetPolicyInstanceParams) middleware.Responder { |
| 99 | a1.Logger.Debug("handler for get policy instance from policytypeID") |
| 100 | if resp, err := r.rh.GetPolicyInstance(models.PolicyTypeID(params.PolicyTypeID), models.PolicyInstanceID(params.PolicyInstanceID)); err == nil { |
| 101 | return a1_mediator.NewA1ControllerGetPolicyInstanceOK().WithPayload(resp) |
| 102 | } |
| 103 | if r.rh.IsPolicyInstanceNotFound(err) { |
Naman Gupta/Single vRAN Call Control /SRI-Bangalore/Engineer/Samsung Electronics | 584d755 | 2022-09-27 10:32:15 +0530 | [diff] [blame] | 104 | return a1_mediator.NewA1ControllerGetPolicyInstanceNotFound() |
naman.gupta | 6579f78 | 2022-05-02 15:34:31 +0530 | [diff] [blame] | 105 | } |
Naman Gupta/Single vRAN Call Control /SRI-Bangalore/Engineer/Samsung Electronics | 584d755 | 2022-09-27 10:32:15 +0530 | [diff] [blame] | 106 | return a1_mediator.NewA1ControllerGetPolicyInstanceServiceUnavailable() |
naman.gupta | 6579f78 | 2022-05-02 15:34:31 +0530 | [diff] [blame] | 107 | }) |
| 108 | |
naman.gupta | 05a0f58 | 2022-05-05 15:59:51 +0530 | [diff] [blame] | 109 | api.A1MediatorA1ControllerGetAllInstancesForTypeHandler = a1_mediator.A1ControllerGetAllInstancesForTypeHandlerFunc(func(params a1_mediator.A1ControllerGetAllInstancesForTypeParams) middleware.Responder { |
| 110 | a1.Logger.Debug("handler for get all policy instance") |
naman.gupta | 44f5c8a | 2022-05-25 23:31:47 +0530 | [diff] [blame] | 111 | if resp, err := r.rh.GetAllPolicyInstance(models.PolicyTypeID(params.PolicyTypeID)); err == nil { |
| 112 | if resp != nil { |
| 113 | return a1_mediator.NewA1ControllerGetAllInstancesForTypeOK().WithPayload(resp) |
| 114 | } |
| 115 | } |
| 116 | if r.rh.IsPolicyInstanceNotFound(err) { |
| 117 | return a1_mediator.NewA1ControllerGetPolicyInstanceNotFound() |
| 118 | } |
| 119 | return a1_mediator.NewA1ControllerGetAllInstancesForTypeServiceUnavailable() |
| 120 | |
naman.gupta | 05a0f58 | 2022-05-05 15:59:51 +0530 | [diff] [blame] | 121 | }) |
| 122 | |
naman.gupta | b513650 | 2022-10-18 00:25:14 +0530 | [diff] [blame] | 123 | api.A1MediatorA1ControllerDeletePolicyTypeHandler = a1_mediator.A1ControllerDeletePolicyTypeHandlerFunc(func(params a1_mediator.A1ControllerDeletePolicyTypeParams) middleware.Responder { |
| 124 | a1.Logger.Debug("handler for delete policy type") |
| 125 | if err := r.rh.DeletePolicyType(models.PolicyTypeID(params.PolicyTypeID)); err != nil { |
| 126 | if r.rh.CanPolicyTypeBeDeleted(err) { |
| 127 | return a1_mediator.NewA1ControllerDeletePolicyTypeBadRequest() |
| 128 | } |
| 129 | return a1_mediator.NewA1ControllerDeletePolicyTypeServiceUnavailable() |
| 130 | } |
| 131 | |
| 132 | return a1_mediator.NewA1ControllerDeletePolicyTypeNoContent() |
| 133 | |
| 134 | }) |
| 135 | |
naman.gupta | dd7944b | 2022-10-26 14:38:04 +0530 | [diff] [blame] | 136 | api.A1MediatorA1ControllerGetPolicyInstanceStatusHandler = a1_mediator.A1ControllerGetPolicyInstanceStatusHandlerFunc(func(params a1_mediator.A1ControllerGetPolicyInstanceStatusParams) middleware.Responder { |
| 137 | a1.Logger.Debug("handler for get policy instance status") |
| 138 | if resp, err := r.rh.GetPolicyInstanceStatus(models.PolicyTypeID(params.PolicyTypeID), models.PolicyInstanceID(params.PolicyInstanceID)); err == nil { |
| 139 | return a1_mediator.NewA1ControllerGetPolicyInstanceStatusOK().WithPayload(resp) |
| 140 | } |
| 141 | if r.rh.IsPolicyInstanceNotFound(err) { |
| 142 | return a1_mediator.NewA1ControllerGetPolicyInstanceStatusNotFound() |
| 143 | } |
| 144 | return a1_mediator.NewA1ControllerGetPolicyInstanceStatusServiceUnavailable() |
| 145 | }) |
| 146 | |
naman.gupta | 52ef1de | 2022-11-02 21:45:05 +0530 | [diff] [blame] | 147 | api.A1MediatorA1ControllerDeletePolicyInstanceHandler = a1_mediator.A1ControllerDeletePolicyInstanceHandlerFunc(func(params a1_mediator.A1ControllerDeletePolicyInstanceParams) middleware.Responder { |
| 148 | a1.Logger.Debug("handler for delete policy instance") |
| 149 | if err := r.rh.DeletePolicyInstance(models.PolicyTypeID(params.PolicyTypeID), models.PolicyInstanceID(params.PolicyInstanceID)); err != nil { |
| 150 | if r.rh.CanPolicyInstanceBeDeleted(err) { |
| 151 | return a1_mediator.NewA1ControllerDeletePolicyInstanceNotFound() |
| 152 | } |
| 153 | return a1_mediator.NewA1ControllerDeletePolicyInstanceServiceUnavailable() |
| 154 | } |
| 155 | |
| 156 | return a1_mediator.NewA1ControllerDeletePolicyInstanceAccepted() |
| 157 | |
| 158 | }) |
naman.gupta | 159ddb7 | 2022-11-08 19:52:05 +0530 | [diff] [blame] | 159 | |
| 160 | api.A1eiDataDeliveryA1ControllerDataDeliveryHandler = a1_e_i_data_delivery.A1ControllerDataDeliveryHandlerFunc(func(params a1_e_i_data_delivery.A1ControllerDataDeliveryParams) middleware.Responder { |
| 161 | a1.Logger.Debug("handler for EI data delivery") |
| 162 | if err = r.rh.DataDelivery(params.Body); err != nil { |
| 163 | return a1_e_i_data_delivery.NewA1ControllerDataDeliveryNotFound() |
| 164 | } |
| 165 | return a1_e_i_data_delivery.NewA1ControllerDataDeliveryOK() |
| 166 | }) |
| 167 | |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 168 | return api |
| 169 | |
| 170 | } |
| 171 | |
| 172 | func (r *Restful) Run() { |
| 173 | |
| 174 | server := restapi.NewServer(r.api) |
| 175 | defer server.Shutdown() |
naman.gupta | 1a41ce7 | 2022-10-18 00:14:49 +0530 | [diff] [blame] | 176 | server.Port = 10000 |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 177 | server.Host = "0.0.0.0" |
| 178 | if err := server.Serve(); err != nil { |
| 179 | log.Fatal(err.Error()) |
| 180 | } |
| 181 | } |