Adding needed docker files
[nonrtric/rapp/ransliceassurance.git] / icsversion / stub / ics / icsStub.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2022: Nordix Foundation
6 //   %%
7 //   Licensed under the Apache License, Version 2.0 (the "License");
8 //   you may not use this file except in compliance with the License.
9 //   You may obtain a copy of the License at
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //
20
21 package main
22
23 import (
24         "flag"
25         "fmt"
26         "net/http"
27         "os"
28         "time"
29
30         "github.com/gorilla/mux"
31 )
32
33 var client = &http.Client{
34         Timeout: 5 * time.Second,
35 }
36
37 func main() {
38         port := flag.Int("port", 8083, "The port this consumer will listen on")
39         flag.Parse()
40         fmt.Println("Starting ICS stub on port ", *port)
41
42         r := mux.NewRouter()
43         r.HandleFunc("/data-consumer/v1/info-jobs/{jobId}", handleCalls).Methods(http.MethodPut, http.MethodDelete)
44         fmt.Println(http.ListenAndServe(fmt.Sprintf(":%v", *port), r))
45 }
46
47 func getEnv(key string, defaultVal string) string {
48         if value, exists := os.LookupEnv(key); exists {
49                 return value
50         }
51
52         return defaultVal
53 }
54
55 func handleCalls(w http.ResponseWriter, r *http.Request) {
56         producer_addr := getEnv("PRODUCER_ADDR", "http://prod-sdnr-sim:3905/")
57         vars := mux.Vars(r)
58         id, ok := vars["jobId"]
59         if ok {
60                 fmt.Println(r.Method, " of job ", id)
61                 if r.Method == http.MethodPut {
62                         req, _ := http.NewRequest(http.MethodPut, producer_addr+"create/"+id, nil)
63                         r, err := client.Do(req)
64                         if err != nil {
65                                 fmt.Println("Failed to create job in producer ", err)
66                                 return
67                         }
68                         fmt.Println("Created job in producer ", r.Status)
69                 } else {
70                         req, _ := http.NewRequest(http.MethodDelete, producer_addr+"delete/"+id, nil)
71                         r, err := client.Do(req)
72                         if err != nil {
73                                 fmt.Println("Failed to delete job in producer ", err)
74                                 return
75                         }
76                         fmt.Println("Deleted job in producer ", r.Status)
77                 }
78         }
79
80 }