blob: b0663dd8586edb7dec024dea2cf2fe710e383949 [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"
31 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/restapi/operations/a1_mediator"
32 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/resthooks"
subhash kumar singh6017d6a2021-10-26 12:14:26 +000033 "github.com/go-openapi/loads"
34 "github.com/go-openapi/runtime/middleware"
subhash kumar singh327d9db2021-09-30 19:07:18 +000035)
36
37func NewRestful() *Restful {
38 r := &Restful{
39 rh: resthooks.NewResthook(),
40 }
41 r.api = r.setupHandler()
42 return r
43}
44
45func (r *Restful) setupHandler() *operations.A1API {
46 swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
47 if err != nil {
48 os.Exit(1)
49 }
50
51 api := operations.NewA1API(swaggerSpec)
52 api.A1MediatorA1ControllerGetAllPolicyTypesHandler = a1_mediator.A1ControllerGetAllPolicyTypesHandlerFunc(func(param a1_mediator.A1ControllerGetAllPolicyTypesParams) middleware.Responder {
naman.guptaab3e9ee2022-02-18 18:03:36 +053053 a1.Logger.Debug("handler for get all policy type")
subhash kumar singh327d9db2021-09-30 19:07:18 +000054 return a1_mediator.NewA1ControllerGetAllPolicyTypesOK().WithPayload(r.rh.GetAllPolicyType())
55 })
naman.guptaab3e9ee2022-02-18 18:03:36 +053056
57 api.A1MediatorA1ControllerCreatePolicyTypeHandler = a1_mediator.A1ControllerCreatePolicyTypeHandlerFunc(func(params a1_mediator.A1ControllerCreatePolicyTypeParams) middleware.Responder {
naman.gupta68644732022-02-24 14:41:55 +053058 a1.Logger.Debug("handler for Create policy type ")
naman.guptaab3e9ee2022-02-18 18:03:36 +053059 if err = r.rh.CreatePolicyType(models.PolicyTypeID(params.PolicyTypeID), *params.Body); err == nil {
60 //Increase prometheus counter
61 return a1_mediator.NewA1ControllerCreatePolicyTypeCreated()
62 }
63 if r.rh.IsTypeAlready(err) || r.rh.IsTypeMismatch(err) {
64 return a1_mediator.NewA1ControllerCreatePolicyTypeBadRequest()
65 }
66 return a1_mediator.NewA1ControllerCreatePolicyTypeServiceUnavailable()
67
68 })
69
naman.gupta68644732022-02-24 14:41:55 +053070 api.A1MediatorA1ControllerGetPolicyTypeHandler = a1_mediator.A1ControllerGetPolicyTypeHandlerFunc(func(params a1_mediator.A1ControllerGetPolicyTypeParams) middleware.Responder {
naman.gupta3ad6de42022-04-26 21:59:05 +053071 a1.Logger.Debug("handler for get policy type from policytypeID")
naman.gupta68644732022-02-24 14:41:55 +053072 return a1_mediator.NewA1ControllerGetPolicyTypeOK().WithPayload(r.rh.GetPolicyType(models.PolicyTypeID(params.PolicyTypeID)))
73 })
74
naman.gupta3ad6de42022-04-26 21:59:05 +053075 api.A1MediatorA1ControllerCreateOrReplacePolicyInstanceHandler = a1_mediator.A1ControllerCreateOrReplacePolicyInstanceHandlerFunc(func(params a1_mediator.A1ControllerCreateOrReplacePolicyInstanceParams) middleware.Responder {
76 a1.Logger.Debug("handler for create policy type instance ")
77 if err = r.rh.CreatePolicyInstance(models.PolicyTypeID(params.PolicyTypeID), models.PolicyInstanceID(params.PolicyInstanceID), params.Body); err == nil {
78 return a1_mediator.NewA1ControllerCreateOrReplacePolicyInstanceAccepted()
79 }
80 if r.rh.IsValidJson(err) {
81 return a1_mediator.NewA1ControllerCreateOrReplacePolicyInstanceBadRequest()
82 }
83 return a1_mediator.NewA1ControllerCreateOrReplacePolicyInstanceServiceUnavailable()
84
85 })
86
naman.gupta6579f782022-05-02 15:34:31 +053087 api.A1MediatorA1ControllerGetPolicyInstanceHandler = a1_mediator.A1ControllerGetPolicyInstanceHandlerFunc(func(params a1_mediator.A1ControllerGetPolicyInstanceParams) middleware.Responder {
88 a1.Logger.Debug("handler for get policy instance from policytypeID")
89 if resp, err := r.rh.GetPolicyInstance(models.PolicyTypeID(params.PolicyTypeID), models.PolicyInstanceID(params.PolicyInstanceID)); err == nil {
90 return a1_mediator.NewA1ControllerGetPolicyInstanceOK().WithPayload(resp)
91 }
92 if r.rh.IsPolicyInstanceNotFound(err) {
Naman Gupta/Single vRAN Call Control /SRI-Bangalore/Engineer/Samsung Electronics584d7552022-09-27 10:32:15 +053093 return a1_mediator.NewA1ControllerGetPolicyInstanceNotFound()
naman.gupta6579f782022-05-02 15:34:31 +053094 }
Naman Gupta/Single vRAN Call Control /SRI-Bangalore/Engineer/Samsung Electronics584d7552022-09-27 10:32:15 +053095 return a1_mediator.NewA1ControllerGetPolicyInstanceServiceUnavailable()
naman.gupta6579f782022-05-02 15:34:31 +053096 })
97
naman.gupta05a0f582022-05-05 15:59:51 +053098 api.A1MediatorA1ControllerGetAllInstancesForTypeHandler = a1_mediator.A1ControllerGetAllInstancesForTypeHandlerFunc(func(params a1_mediator.A1ControllerGetAllInstancesForTypeParams) middleware.Responder {
99 a1.Logger.Debug("handler for get all policy instance")
naman.gupta44f5c8a2022-05-25 23:31:47 +0530100 if resp, err := r.rh.GetAllPolicyInstance(models.PolicyTypeID(params.PolicyTypeID)); err == nil {
101 if resp != nil {
102 return a1_mediator.NewA1ControllerGetAllInstancesForTypeOK().WithPayload(resp)
103 }
104 }
105 if r.rh.IsPolicyInstanceNotFound(err) {
106 return a1_mediator.NewA1ControllerGetPolicyInstanceNotFound()
107 }
108 return a1_mediator.NewA1ControllerGetAllInstancesForTypeServiceUnavailable()
109
naman.gupta05a0f582022-05-05 15:59:51 +0530110 })
111
naman.guptab5136502022-10-18 00:25:14 +0530112 api.A1MediatorA1ControllerDeletePolicyTypeHandler = a1_mediator.A1ControllerDeletePolicyTypeHandlerFunc(func(params a1_mediator.A1ControllerDeletePolicyTypeParams) middleware.Responder {
113 a1.Logger.Debug("handler for delete policy type")
114 if err := r.rh.DeletePolicyType(models.PolicyTypeID(params.PolicyTypeID)); err != nil {
115 if r.rh.CanPolicyTypeBeDeleted(err) {
116 return a1_mediator.NewA1ControllerDeletePolicyTypeBadRequest()
117 }
118 return a1_mediator.NewA1ControllerDeletePolicyTypeServiceUnavailable()
119 }
120
121 return a1_mediator.NewA1ControllerDeletePolicyTypeNoContent()
122
123 })
124
naman.guptadd7944b2022-10-26 14:38:04 +0530125 api.A1MediatorA1ControllerGetPolicyInstanceStatusHandler = a1_mediator.A1ControllerGetPolicyInstanceStatusHandlerFunc(func(params a1_mediator.A1ControllerGetPolicyInstanceStatusParams) middleware.Responder {
126 a1.Logger.Debug("handler for get policy instance status")
127 if resp, err := r.rh.GetPolicyInstanceStatus(models.PolicyTypeID(params.PolicyTypeID), models.PolicyInstanceID(params.PolicyInstanceID)); err == nil {
128 return a1_mediator.NewA1ControllerGetPolicyInstanceStatusOK().WithPayload(resp)
129 }
130 if r.rh.IsPolicyInstanceNotFound(err) {
131 return a1_mediator.NewA1ControllerGetPolicyInstanceStatusNotFound()
132 }
133 return a1_mediator.NewA1ControllerGetPolicyInstanceStatusServiceUnavailable()
134 })
135
naman.gupta52ef1de2022-11-02 21:45:05 +0530136 api.A1MediatorA1ControllerDeletePolicyInstanceHandler = a1_mediator.A1ControllerDeletePolicyInstanceHandlerFunc(func(params a1_mediator.A1ControllerDeletePolicyInstanceParams) middleware.Responder {
137 a1.Logger.Debug("handler for delete policy instance")
138 if err := r.rh.DeletePolicyInstance(models.PolicyTypeID(params.PolicyTypeID), models.PolicyInstanceID(params.PolicyInstanceID)); err != nil {
139 if r.rh.CanPolicyInstanceBeDeleted(err) {
140 return a1_mediator.NewA1ControllerDeletePolicyInstanceNotFound()
141 }
142 return a1_mediator.NewA1ControllerDeletePolicyInstanceServiceUnavailable()
143 }
144
145 return a1_mediator.NewA1ControllerDeletePolicyInstanceAccepted()
146
147 })
148
subhash kumar singh327d9db2021-09-30 19:07:18 +0000149 return api
150
151}
152
153func (r *Restful) Run() {
154
155 server := restapi.NewServer(r.api)
156 defer server.Shutdown()
naman.gupta1a41ce72022-10-18 00:14:49 +0530157 server.Port = 10000
subhash kumar singh327d9db2021-09-30 19:07:18 +0000158 server.Host = "0.0.0.0"
159 if err := server.Serve(); err != nil {
160 log.Fatal(err.Error())
161 }
162}