blob: d8d1e9269e85791235bb7ad1d3e156cc19ce8c56 [file] [log] [blame]
subhash kumar singh327d9db2021-09-30 19:07:18 +00001/*
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*/
21package restful
22
23import (
subhash kumar singh327d9db2021-09-30 19:07:18 +000024 "log"
25 "os"
26
naman.guptaab3e9ee2022-02-18 18:03:36 +053027 "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.gupta159ddb72022-11-08 19:52:05 +053031 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/restapi/operations/a1_e_i_data_delivery"
naman.guptaab3e9ee2022-02-18 18:03:36 +053032 "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 singh6017d6a2021-10-26 12:14:26 +000034 "github.com/go-openapi/loads"
35 "github.com/go-openapi/runtime/middleware"
subhash kumar singh327d9db2021-09-30 19:07:18 +000036)
37
38func NewRestful() *Restful {
39 r := &Restful{
40 rh: resthooks.NewResthook(),
41 }
42 r.api = r.setupHandler()
43 return r
44}
45
46func (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)
53 api.A1MediatorA1ControllerGetAllPolicyTypesHandler = a1_mediator.A1ControllerGetAllPolicyTypesHandlerFunc(func(param a1_mediator.A1ControllerGetAllPolicyTypesParams) middleware.Responder {
naman.guptaab3e9ee2022-02-18 18:03:36 +053054 a1.Logger.Debug("handler for get all policy type")
subhash kumar singh327d9db2021-09-30 19:07:18 +000055 return a1_mediator.NewA1ControllerGetAllPolicyTypesOK().WithPayload(r.rh.GetAllPolicyType())
56 })
naman.guptaab3e9ee2022-02-18 18:03:36 +053057
58 api.A1MediatorA1ControllerCreatePolicyTypeHandler = a1_mediator.A1ControllerCreatePolicyTypeHandlerFunc(func(params a1_mediator.A1ControllerCreatePolicyTypeParams) middleware.Responder {
naman.gupta68644732022-02-24 14:41:55 +053059 a1.Logger.Debug("handler for Create policy type ")
naman.guptaab3e9ee2022-02-18 18:03:36 +053060 if err = r.rh.CreatePolicyType(models.PolicyTypeID(params.PolicyTypeID), *params.Body); err == nil {
61 //Increase prometheus counter
62 return a1_mediator.NewA1ControllerCreatePolicyTypeCreated()
63 }
64 if r.rh.IsTypeAlready(err) || r.rh.IsTypeMismatch(err) {
65 return a1_mediator.NewA1ControllerCreatePolicyTypeBadRequest()
66 }
67 return a1_mediator.NewA1ControllerCreatePolicyTypeServiceUnavailable()
68
69 })
70
naman.gupta68644732022-02-24 14:41:55 +053071 api.A1MediatorA1ControllerGetPolicyTypeHandler = a1_mediator.A1ControllerGetPolicyTypeHandlerFunc(func(params a1_mediator.A1ControllerGetPolicyTypeParams) middleware.Responder {
naman.gupta3ad6de42022-04-26 21:59:05 +053072 a1.Logger.Debug("handler for get policy type from policytypeID")
naman.gupta68644732022-02-24 14:41:55 +053073 return a1_mediator.NewA1ControllerGetPolicyTypeOK().WithPayload(r.rh.GetPolicyType(models.PolicyTypeID(params.PolicyTypeID)))
74 })
75
naman.gupta3ad6de42022-04-26 21:59:05 +053076 api.A1MediatorA1ControllerCreateOrReplacePolicyInstanceHandler = a1_mediator.A1ControllerCreateOrReplacePolicyInstanceHandlerFunc(func(params a1_mediator.A1ControllerCreateOrReplacePolicyInstanceParams) middleware.Responder {
77 a1.Logger.Debug("handler for create policy type instance ")
78 if err = r.rh.CreatePolicyInstance(models.PolicyTypeID(params.PolicyTypeID), models.PolicyInstanceID(params.PolicyInstanceID), params.Body); err == nil {
79 return a1_mediator.NewA1ControllerCreateOrReplacePolicyInstanceAccepted()
80 }
81 if r.rh.IsValidJson(err) {
82 return a1_mediator.NewA1ControllerCreateOrReplacePolicyInstanceBadRequest()
83 }
84 return a1_mediator.NewA1ControllerCreateOrReplacePolicyInstanceServiceUnavailable()
85
86 })
87
naman.gupta6579f782022-05-02 15:34:31 +053088 api.A1MediatorA1ControllerGetPolicyInstanceHandler = a1_mediator.A1ControllerGetPolicyInstanceHandlerFunc(func(params a1_mediator.A1ControllerGetPolicyInstanceParams) middleware.Responder {
89 a1.Logger.Debug("handler for get policy instance from policytypeID")
90 if resp, err := r.rh.GetPolicyInstance(models.PolicyTypeID(params.PolicyTypeID), models.PolicyInstanceID(params.PolicyInstanceID)); err == nil {
91 return a1_mediator.NewA1ControllerGetPolicyInstanceOK().WithPayload(resp)
92 }
93 if r.rh.IsPolicyInstanceNotFound(err) {
Naman Gupta/Single vRAN Call Control /SRI-Bangalore/Engineer/Samsung Electronics584d7552022-09-27 10:32:15 +053094 return a1_mediator.NewA1ControllerGetPolicyInstanceNotFound()
naman.gupta6579f782022-05-02 15:34:31 +053095 }
Naman Gupta/Single vRAN Call Control /SRI-Bangalore/Engineer/Samsung Electronics584d7552022-09-27 10:32:15 +053096 return a1_mediator.NewA1ControllerGetPolicyInstanceServiceUnavailable()
naman.gupta6579f782022-05-02 15:34:31 +053097 })
98
naman.gupta05a0f582022-05-05 15:59:51 +053099 api.A1MediatorA1ControllerGetAllInstancesForTypeHandler = a1_mediator.A1ControllerGetAllInstancesForTypeHandlerFunc(func(params a1_mediator.A1ControllerGetAllInstancesForTypeParams) middleware.Responder {
100 a1.Logger.Debug("handler for get all policy instance")
naman.gupta44f5c8a2022-05-25 23:31:47 +0530101 if resp, err := r.rh.GetAllPolicyInstance(models.PolicyTypeID(params.PolicyTypeID)); err == nil {
102 if resp != nil {
103 return a1_mediator.NewA1ControllerGetAllInstancesForTypeOK().WithPayload(resp)
104 }
105 }
106 if r.rh.IsPolicyInstanceNotFound(err) {
107 return a1_mediator.NewA1ControllerGetPolicyInstanceNotFound()
108 }
109 return a1_mediator.NewA1ControllerGetAllInstancesForTypeServiceUnavailable()
110
naman.gupta05a0f582022-05-05 15:59:51 +0530111 })
112
naman.guptab5136502022-10-18 00:25:14 +0530113 api.A1MediatorA1ControllerDeletePolicyTypeHandler = a1_mediator.A1ControllerDeletePolicyTypeHandlerFunc(func(params a1_mediator.A1ControllerDeletePolicyTypeParams) middleware.Responder {
114 a1.Logger.Debug("handler for delete policy type")
115 if err := r.rh.DeletePolicyType(models.PolicyTypeID(params.PolicyTypeID)); err != nil {
116 if r.rh.CanPolicyTypeBeDeleted(err) {
117 return a1_mediator.NewA1ControllerDeletePolicyTypeBadRequest()
118 }
119 return a1_mediator.NewA1ControllerDeletePolicyTypeServiceUnavailable()
120 }
121
122 return a1_mediator.NewA1ControllerDeletePolicyTypeNoContent()
123
124 })
125
naman.guptadd7944b2022-10-26 14:38:04 +0530126 api.A1MediatorA1ControllerGetPolicyInstanceStatusHandler = a1_mediator.A1ControllerGetPolicyInstanceStatusHandlerFunc(func(params a1_mediator.A1ControllerGetPolicyInstanceStatusParams) middleware.Responder {
127 a1.Logger.Debug("handler for get policy instance status")
128 if resp, err := r.rh.GetPolicyInstanceStatus(models.PolicyTypeID(params.PolicyTypeID), models.PolicyInstanceID(params.PolicyInstanceID)); err == nil {
129 return a1_mediator.NewA1ControllerGetPolicyInstanceStatusOK().WithPayload(resp)
130 }
131 if r.rh.IsPolicyInstanceNotFound(err) {
132 return a1_mediator.NewA1ControllerGetPolicyInstanceStatusNotFound()
133 }
134 return a1_mediator.NewA1ControllerGetPolicyInstanceStatusServiceUnavailable()
135 })
136
naman.gupta52ef1de2022-11-02 21:45:05 +0530137 api.A1MediatorA1ControllerDeletePolicyInstanceHandler = a1_mediator.A1ControllerDeletePolicyInstanceHandlerFunc(func(params a1_mediator.A1ControllerDeletePolicyInstanceParams) middleware.Responder {
138 a1.Logger.Debug("handler for delete policy instance")
139 if err := r.rh.DeletePolicyInstance(models.PolicyTypeID(params.PolicyTypeID), models.PolicyInstanceID(params.PolicyInstanceID)); err != nil {
140 if r.rh.CanPolicyInstanceBeDeleted(err) {
141 return a1_mediator.NewA1ControllerDeletePolicyInstanceNotFound()
142 }
143 return a1_mediator.NewA1ControllerDeletePolicyInstanceServiceUnavailable()
144 }
145
146 return a1_mediator.NewA1ControllerDeletePolicyInstanceAccepted()
147
148 })
naman.gupta159ddb72022-11-08 19:52:05 +0530149
150 api.A1eiDataDeliveryA1ControllerDataDeliveryHandler = a1_e_i_data_delivery.A1ControllerDataDeliveryHandlerFunc(func(params a1_e_i_data_delivery.A1ControllerDataDeliveryParams) middleware.Responder {
151 a1.Logger.Debug("handler for EI data delivery")
152 if err = r.rh.DataDelivery(params.Body); err != nil {
153 return a1_e_i_data_delivery.NewA1ControllerDataDeliveryNotFound()
154 }
155 return a1_e_i_data_delivery.NewA1ControllerDataDeliveryOK()
156 })
157
subhash kumar singh327d9db2021-09-30 19:07:18 +0000158 return api
159
160}
161
162func (r *Restful) Run() {
163
164 server := restapi.NewServer(r.api)
165 defer server.Shutdown()
naman.gupta1a41ce72022-10-18 00:14:49 +0530166 server.Port = 10000
subhash kumar singh327d9db2021-09-30 19:07:18 +0000167 server.Host = "0.0.0.0"
168 if err := server.Serve(); err != nil {
169 log.Fatal(err.Error())
170 }
171}