blob: 26a229f51f82e2cd26f3498478a0f0f2c20541bd [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 {
71 a1.Logger.Error("handler for get policy type from policytypeID")
72 return a1_mediator.NewA1ControllerGetPolicyTypeOK().WithPayload(r.rh.GetPolicyType(models.PolicyTypeID(params.PolicyTypeID)))
73 })
74
subhash kumar singh327d9db2021-09-30 19:07:18 +000075 return api
76
77}
78
79func (r *Restful) Run() {
80
81 server := restapi.NewServer(r.api)
82 defer server.Shutdown()
83 server.Port = 8080
84 server.Host = "0.0.0.0"
85 if err := server.Serve(); err != nil {
86 log.Fatal(err.Error())
87 }
88}