Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 AT&T Intellectual Property. |
| 3 | * Copyright (c) 2018-2019 Nokia. |
| 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. |
Roni Riska | 6ffba08 | 2019-11-27 10:59:54 +0200 | [diff] [blame^] | 16 | * |
| 17 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 18 | * platform project (RICP). |
| 19 | * |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | package main |
| 23 | |
| 24 | import ( |
| 25 | "fmt" |
| 26 | "io/ioutil" |
| 27 | "net" |
| 28 | "net/http" |
| 29 | ) |
| 30 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 31 | // SupervisionURL is the url where kubernetes posts alive queries |
| 32 | const SupervisionURL = "/supervision/" |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 33 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 34 | // HTTPServer is the VesMgr HTTP server struct |
| 35 | type HTTPServer struct { |
| 36 | listener net.Listener |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 37 | } |
| 38 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 39 | func (s *HTTPServer) init(address string) *HTTPServer { |
| 40 | var err error |
| 41 | s.listener, err = net.Listen("tcp", address) |
| 42 | if err != nil { |
| 43 | panic("Cannot listen:" + err.Error()) |
| 44 | } |
| 45 | return s |
| 46 | } |
| 47 | |
| 48 | func (s *HTTPServer) start(notifPath string, notifCh chan []byte, supCh chan chan string) { |
| 49 | go runHTTPServer(s.listener, notifPath, notifCh, supCh) |
| 50 | } |
| 51 | |
| 52 | func (s *HTTPServer) addr() net.Addr { |
| 53 | return s.listener.Addr() |
| 54 | } |
| 55 | |
| 56 | func runHTTPServer(listener net.Listener, xappNotifURL string, notifCh chan []byte, supervisionCh chan chan string) { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 57 | |
| 58 | logger.Info("vesmgr http server serving at %s", listener.Addr()) |
| 59 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 60 | http.HandleFunc(xappNotifURL, func(w http.ResponseWriter, r *http.Request) { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 61 | |
| 62 | switch r.Method { |
| 63 | case "POST": |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 64 | logger.Info("httpServer: POST in %s", xappNotifURL) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 65 | body, err := ioutil.ReadAll(r.Body) |
| 66 | defer r.Body.Close() |
| 67 | if err != nil { |
| 68 | logger.Error("httpServer: Invalid body in POST request") |
| 69 | return |
| 70 | } |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 71 | notifCh <- body |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 72 | return |
| 73 | default: |
| 74 | logger.Error("httpServer: Invalid method %s to %s", r.Method, r.URL.Path) |
| 75 | http.Error(w, "405 method not allowed", http.StatusMethodNotAllowed) |
| 76 | return |
| 77 | } |
| 78 | }) |
| 79 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 80 | http.HandleFunc(SupervisionURL, func(w http.ResponseWriter, r *http.Request) { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 81 | |
| 82 | switch r.Method { |
| 83 | case "GET": |
| 84 | logger.Info("httpServer: GET supervision") |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 85 | supervisionAckCh := make(chan string) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 86 | // send supervision to the main loop |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 87 | supervisionCh <- supervisionAckCh |
| 88 | reply := <-supervisionAckCh |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 89 | logger.Info("httpServer: supervision ack from the main loop: %s", reply) |
| 90 | fmt.Fprintf(w, reply) |
| 91 | return |
| 92 | default: |
| 93 | logger.Error("httpServer: invalid method %s to %s", r.Method, r.URL.Path) |
| 94 | http.Error(w, "405 method not allowed", http.StatusMethodNotAllowed) |
| 95 | return |
| 96 | } |
| 97 | |
| 98 | }) |
| 99 | |
| 100 | http.Serve(listener, nil) |
| 101 | } |