Error handling & logging for broker
[ealt-edge.git] / mecm / mepm / applcm / broker / pkg / handlers / handlers.go
1 /*
2  * Copyright 2020 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package handlers
17
18 import (
19         "github.com/gorilla/mux"
20         "github.com/sirupsen/logrus"
21         "net/http"
22 )
23
24 // URLS
25 const (
26         CreateAppInstance = "/ealtedge/mepm/app_lcm/v1/app_instances"
27     InstantiateAppInstance = "/ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}/instantiate"
28     QueryAppInstanceInfo = "/ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}"
29     QueryAppLcmOperationStatus = "/ealtedge/mepm/app_lcm/v1/app_lcm_op_occs"
30     TerminateAppIns = "/ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}/terminate"
31     DeleteAppInstanceIdentifier = "/ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}"
32     OnboardPackage = "/ealtedge/mepm/app_pkgm/v1/app_packages"
33     QueryOnboardPackage = "/ealtedge/mepm/app_pkgm/v1/app_packages/{appPkgId}"
34 )
35
36 // Package paths, to be created in deployment file (docker-compose/k8s yaml/helm)
37 const (
38         PackageFolderPath = "/go/release/application/packages/"
39         PackageArtifactPath = "/Artifacts/Deployment/"
40 )
41
42 // Handler of REST APIs
43 type Handlers struct {
44         router *mux.Router
45         logger *logrus.Logger
46         impl   HandlerImpl
47 }
48
49 // Initialize initializes the handler
50 func (hdlr *Handlers) Initialize(logger *logrus.Logger) {
51         hdlr.router = mux.NewRouter()
52         hdlr.logger = logger
53         hdlr.setRouters()
54         hdlr.impl = newHandlerImpl(hdlr.logger)
55 }
56
57 // Run on it's router
58 func (hdlr *Handlers) Run(host string) {
59         hdlr.logger.Info("Server is running on port %s", host)
60         err := http.ListenAndServe(host, hdlr.router)
61         if err != nil {
62                 hdlr.logger.Fatalf("Server couldn't run on port %s", host)
63         }
64 }
65
66 // SetRouters sets the all required routers
67 func (hdlr *Handlers) setRouters() {
68         // Routing for handling the requests
69         hdlr.Post(OnboardPackage, hdlr.handleRequest(hdlr.impl.UploadPackage))
70         hdlr.Get(QueryOnboardPackage, hdlr.handleRequest(hdlr.impl.QueryAppPackageInfo))
71         hdlr.Post(CreateAppInstance, hdlr.handleRequest(hdlr.impl.CreateAppInstance))
72         hdlr.Delete(QueryOnboardPackage, hdlr.handleRequest(hdlr.impl.DeleteAppPackage))
73         hdlr.Post(InstantiateAppInstance, hdlr.handleRequest(hdlr.impl.InstantiateAppInstance))
74         hdlr.Get(QueryAppInstanceInfo, hdlr.handleRequest(hdlr.impl.QueryAppInstanceInfo))
75         hdlr.Get(QueryAppLcmOperationStatus, hdlr.handleRequest(hdlr.impl.QueryAppLcmOperationStatus))
76         hdlr.Post(TerminateAppIns, hdlr.handleRequest(hdlr.impl.TerminateAppInstance))
77         hdlr.Delete(DeleteAppInstanceIdentifier, hdlr.handleRequest(hdlr.impl.DeleteAppInstanceIdentifier))
78 }
79
80 // Get wraps the router for GET method
81 func (hdlr *Handlers) Get(path string, f func(w http.ResponseWriter, r *http.Request)) {
82         hdlr.router.HandleFunc(path, f).Methods("GET")
83 }
84
85 // Post wraps the router for POST method
86 func (hdlr *Handlers) Post(path string, f func(w http.ResponseWriter, r *http.Request)) {
87         hdlr.router.HandleFunc(path, f).Methods("POST")
88 }
89
90 // Put wraps the router for PUT method
91 func (hdlr *Handlers) Put(path string, f func(w http.ResponseWriter, r *http.Request)) {
92         hdlr.router.HandleFunc(path, f).Methods("PUT")
93 }
94
95 // Delete wraps the router for DELETE method
96 func (hdlr *Handlers) Delete(path string, f func(w http.ResponseWriter, r *http.Request)) {
97         hdlr.router.HandleFunc(path, f).Methods("DELETE")
98 }
99
100 type RequestHandlerFunction func(w http.ResponseWriter, r *http.Request)
101
102 func (hdlr *Handlers) handleRequest(handler RequestHandlerFunction) http.HandlerFunc {
103         return func(w http.ResponseWriter, r *http.Request) {
104                 handler(w, r)
105         }
106 }