Https API by APPLCM
[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         "net/http"
20         "os"
21
22         "github.com/gorilla/mux"
23         "github.com/sirupsen/logrus"
24 )
25
26 // URLS
27 const (
28         CreateAppInstance           = "/ealtedge/mepm/app_lcm/v1/app_instances"
29         InstantiateAppInstance      = "/ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}/instantiate"
30         QueryAppInstanceInfo        = "/ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}"
31         QueryAppLcmOperationStatus  = "/ealtedge/mepm/app_lcm/v1/app_lcm_op_occs"
32         TerminateAppIns             = "/ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}/terminate"
33         DeleteAppInstanceIdentifier = "/ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}"
34         OnboardPackage              = "/ealtedge/mepm/app_pkgm/v1/app_packages"
35         QueryOnboardPackage         = "/ealtedge/mepm/app_pkgm/v1/app_packages/{appPkgId}"
36 )
37
38 var (
39         PackageFolderPath   = os.Getenv("PACKAGE_PATH")
40         PackageArtifactPath = os.Getenv("PACKAGE_ARTIFACT_PATH")
41 )
42
43 // Handler of REST APIs
44 type Handlers struct {
45         router *mux.Router
46         logger *logrus.Logger
47         impl   HandlerImpl
48 }
49
50 // Initialize initializes the handler
51 func (hdlr *Handlers) Initialize(logger *logrus.Logger) {
52         hdlr.router = mux.NewRouter()
53         hdlr.logger = logger
54         hdlr.setRouters()
55         hdlr.impl = newHandlerImpl(hdlr.logger)
56 }
57
58 // Run on it's router
59 func (hdlr *Handlers) Run(host string) {
60         hdlr.logger.Infof("Server is running on port %s", host)
61         err := http.ListenAndServeTLS(host, os.Getenv("CERTIFICATE_PATH"), os.Getenv("KEY_PATH"), hdlr.router)
62         //err := http.ListenAndServe(host, hdlr.router)
63         if err != nil {
64                 hdlr.logger.Fatalf("Server couldn't run on port %s", host)
65         }
66 }
67
68 // SetRouters sets the all required routers
69 func (hdlr *Handlers) setRouters() {
70         // Routing for handling the requests
71         hdlr.Post(OnboardPackage, hdlr.handleRequest(hdlr.impl.UploadPackage))
72         hdlr.Get(QueryOnboardPackage, hdlr.handleRequest(hdlr.impl.QueryAppPackageInfo))
73         hdlr.Post(CreateAppInstance, hdlr.handleRequest(hdlr.impl.CreateAppInstance))
74         hdlr.Delete(QueryOnboardPackage, hdlr.handleRequest(hdlr.impl.DeleteAppPackage))
75         hdlr.Post(InstantiateAppInstance, hdlr.handleRequest(hdlr.impl.InstantiateAppInstance))
76         hdlr.Get(QueryAppInstanceInfo, hdlr.handleRequest(hdlr.impl.QueryAppInstanceInfo))
77         hdlr.Get(QueryAppLcmOperationStatus, hdlr.handleRequest(hdlr.impl.QueryAppLcmOperationStatus))
78         hdlr.Post(TerminateAppIns, hdlr.handleRequest(hdlr.impl.TerminateAppInstance))
79         hdlr.Delete(DeleteAppInstanceIdentifier, hdlr.handleRequest(hdlr.impl.DeleteAppInstanceIdentifier))
80 }
81
82 // Get wraps the router for GET method
83 func (hdlr *Handlers) Get(path string, f func(w http.ResponseWriter, r *http.Request)) {
84         hdlr.router.HandleFunc(path, f).Methods("GET")
85 }
86
87 // Post wraps the router for POST method
88 func (hdlr *Handlers) Post(path string, f func(w http.ResponseWriter, r *http.Request)) {
89         hdlr.router.HandleFunc(path, f).Methods("POST")
90 }
91
92 // Put wraps the router for PUT method
93 func (hdlr *Handlers) Put(path string, f func(w http.ResponseWriter, r *http.Request)) {
94         hdlr.router.HandleFunc(path, f).Methods("PUT")
95 }
96
97 // Delete wraps the router for DELETE method
98 func (hdlr *Handlers) Delete(path string, f func(w http.ResponseWriter, r *http.Request)) {
99         hdlr.router.HandleFunc(path, f).Methods("DELETE")
100 }
101
102 type RequestHandlerFunction func(w http.ResponseWriter, r *http.Request)
103
104 func (hdlr *Handlers) handleRequest(handler RequestHandlerFunction) http.HandlerFunc {
105         return func(w http.ResponseWriter, r *http.Request) {
106                 handler(w, r)
107         }
108 }