b8f80be3f2a2f1e25257e70cb8d2686bad60993f
[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         //vars := mux.Vars(r)
113         fmt.Println("::updateRRMPolicyDedicatedRatio::")
114         var prMessage messages.DistributedUnitFunction
115         decoder := json.NewDecoder(r.Body)
116
117         if err := decoder.Decode(&prMessage); err != nil {
118                 respondWithError(w, http.StatusBadRequest, "Invalid request payload")
119                 return
120         }
121         defer r.Body.Close()
122
123         fmt.Println("prMessage: ", prMessage)
124         //prMessage.Id = vars["POLICY-ID"]
125
126         respondWithJSON(w, http.StatusOK, map[string]string{"status": "200"})
127 }
128
129 func respondWithError(w http.ResponseWriter, code int, message string) {
130         fmt.Println("-----------------------------------------------------------------------------")
131         fmt.Println("Sending error message: ", message)
132         fmt.Println("-----------------------------------------------------------------------------")
133         respondWithJSON(w, code, map[string]string{"error": message})
134 }
135
136 func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
137         fmt.Println("-----------------------------------------------------------------------------")
138         fmt.Println("Sending message: ", payload)
139         fmt.Println("-----------------------------------------------------------------------------")
140         response, _ := json.Marshal(payload)
141
142         w.Header().Set("Content-Type", "application/json")
143         w.WriteHeader(code)
144         w.Write(response)
145 }