Merge "Add docker-compose file for odu-app"
[nonrtric.git] / test / usecases / odusliceassurance / goversion / stub / sdnr / sdnrstub.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2021: 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         "encoding/json"
25         "flag"
26         "fmt"
27         "net/http"
28
29         "github.com/gorilla/mux"
30         "oransc.org/usecase/oduclosedloop/messages"
31 )
32
33 func main() {
34         port := flag.Int("port", 3904, "The port this SDNR stub will listen on")
35         flag.Parse()
36
37         r := mux.NewRouter()
38         r.HandleFunc("/rests/data/network-topology:network-topology/topology=topology-netconf/node={NODE-ID}/yang-ext:mount/o-ran-sc-du-hello-world:network-function/distributed-unit-functions={O-DU-ID}", getDistributedUnitFunctions).Methods(http.MethodGet)
39         r.HandleFunc("/rests/data/network-topology:network-topology/topology=topology-netconf/node={NODE-ID}/yang-ext:mount/o-ran-sc-du-hello-world:network-function/distributed-unit-functions={O-DU-ID}/radio-resource-management-policy-ratio={POLICY-ID}", updateRRMPolicyDedicatedRatio).Methods(http.MethodPost)
40
41         fmt.Println("Starting SDNR on port: ", *port)
42         http.ListenAndServe(fmt.Sprintf(":%v", *port), r)
43
44 }
45
46 func getDistributedUnitFunctions(w http.ResponseWriter, r *http.Request) {
47         vars := mux.Vars(r)
48
49         message := messages.ORanDuRestConf{
50                 DistributedUnitFunction: messages.DistributedUnitFunction{
51                         Id: vars["O-DU-ID"],
52                         RRMPolicyRatio: []messages.RRMPolicyRatio{
53                                 {
54                                         Id:                      "rrm-pol-1",
55                                         AdmState:                "locked",
56                                         UserLabel:               "rrm-pol-1",
57                                         RRMPolicyMaxRatio:       100,
58                                         RRMPolicyMinRatio:       0,
59                                         RRMPolicyDedicatedRatio: 0,
60                                         ResourceType:            "prb",
61                                         RRMPolicyMembers: []messages.RRMPolicyMember{
62                                                 {
63                                                         MobileCountryCode:   "046",
64                                                         MobileNetworkCode:   "651",
65                                                         SliceDifferentiator: 1,
66                                                         SliceServiceType:    0,
67                                                 },
68                                         },
69                                 },
70                                 {
71                                         Id:                      "rrm-pol-2",
72                                         AdmState:                "unlocked",
73                                         UserLabel:               "rrm-pol-2",
74                                         RRMPolicyMaxRatio:       20,
75                                         RRMPolicyMinRatio:       10,
76                                         RRMPolicyDedicatedRatio: 15,
77                                         ResourceType:            "prb",
78                                         RRMPolicyMembers: []messages.RRMPolicyMember{
79                                                 {
80                                                         MobileCountryCode:   "046",
81                                                         MobileNetworkCode:   "651",
82                                                         SliceDifferentiator: 2,
83                                                         SliceServiceType:    1,
84                                                 },
85                                         },
86                                 },
87                                 {
88                                         Id:                      "rrm-pol-3",
89                                         AdmState:                "unlocked",
90                                         UserLabel:               "rrm-pol-3",
91                                         RRMPolicyMaxRatio:       30,
92                                         RRMPolicyMinRatio:       10,
93                                         RRMPolicyDedicatedRatio: 5,
94                                         ResourceType:            "prb",
95                                         RRMPolicyMembers: []messages.RRMPolicyMember{
96                                                 {
97                                                         MobileCountryCode:   "310",
98                                                         MobileNetworkCode:   "150",
99                                                         SliceDifferentiator: 2,
100                                                         SliceServiceType:    2,
101                                                 },
102                                         },
103                                 },
104                         },
105                 },
106         }
107
108         respondWithJSON(w, http.StatusOK, message)
109 }
110
111 func updateRRMPolicyDedicatedRatio(w http.ResponseWriter, r *http.Request) {
112         var prMessage messages.DistributedUnitFunction
113         decoder := json.NewDecoder(r.Body)
114
115         if err := decoder.Decode(&prMessage); err != nil {
116                 respondWithError(w, http.StatusBadRequest, "Invalid request payload")
117                 return
118         }
119         defer r.Body.Close()
120
121         fmt.Println("prMessage: ", prMessage)
122
123         respondWithJSON(w, http.StatusOK, map[string]string{"status": "200"})
124 }
125
126 func respondWithError(w http.ResponseWriter, code int, message string) {
127         fmt.Println("-----------------------------------------------------------------------------")
128         fmt.Println("Sending error message: ", message)
129         fmt.Println("-----------------------------------------------------------------------------")
130         respondWithJSON(w, code, map[string]string{"error": message})
131 }
132
133 func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
134         fmt.Println("-----------------------------------------------------------------------------")
135         fmt.Println("Sending message: ", payload)
136         fmt.Println("-----------------------------------------------------------------------------")
137         response, _ := json.Marshal(payload)
138
139         w.Header().Set("Content-Type", "application/json")
140         w.WriteHeader(code)
141         w.Write(response)
142 }