Improve unit test coverage
[ric-plt/e2mgr.git] / E2Manager / httpserver / http_server.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 // Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved.
5 //
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
9 //
10 //      http://www.apache.org/licenses/LICENSE-2.0
11 //
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.
17
18 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 //  platform project (RICP).
20
21 package httpserver
22
23 import (
24         "e2mgr/controllers"
25         "e2mgr/logger"
26         "fmt"
27         "github.com/gorilla/mux"
28         "net/http"
29 )
30
31 func Run(log *logger.Logger, port int, rootController controllers.IRootController, nodebController controllers.INodebController, e2tController controllers.IE2TController) error {
32
33         router := mux.NewRouter()
34         initializeRoutes(router, rootController, nodebController, e2tController)
35
36         addr := fmt.Sprintf(":%d", port)
37
38         err := http.ListenAndServe(addr, router)
39
40         log.Errorf("#http_server.Run - Fail initiating HTTP server. Error: %v", err)
41         return err
42 }
43
44 func initializeRoutes(router *mux.Router, rootController controllers.IRootController, nodebController controllers.INodebController, e2tController controllers.IE2TController) {
45         r := router.PathPrefix("/v1").Subrouter()
46         r.HandleFunc("/health", rootController.HandleHealthCheckRequest).Methods(http.MethodGet)
47
48         rr := r.PathPrefix("/nodeb").Subrouter()
49         rr.HandleFunc("/states", nodebController.GetNodebIdList).Methods(http.MethodGet)
50         rr.HandleFunc("/states/{ranName}", nodebController.GetNodebId).Methods(http.MethodGet)
51         rr.HandleFunc("/{ranName}", nodebController.GetNodeb).Methods(http.MethodGet)
52         rr.HandleFunc("/enb", nodebController.AddEnb).Methods(http.MethodPost)
53         rr.HandleFunc("/enb/{ranName}", nodebController.DeleteEnb).Methods(http.MethodDelete)
54         rr.HandleFunc("/gnb/{ranName}", nodebController.UpdateGnb).Methods(http.MethodPut)
55         rr.HandleFunc("/enb/{ranName}", nodebController.UpdateEnb).Methods(http.MethodPut)
56         rr.HandleFunc("/shutdown", nodebController.Shutdown).Methods(http.MethodPut)
57         rr.HandleFunc("/parameters", nodebController.SetGeneralConfiguration).Methods(http.MethodPut)
58         rr.HandleFunc("/health", nodebController.HealthCheckRequest).Methods(http.MethodPut)
59         rrr := r.PathPrefix("/e2t").Subrouter()
60         rrr.HandleFunc("/list", e2tController.GetE2TInstances).Methods(http.MethodGet)
61 }