Test and debug interface improvements
[ric-plt/submgr.git] / pkg / control / debug_rest_if.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 package control
21
22 import (
23         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
24         "github.com/gorilla/mux"
25         "net/http"
26         "os"
27         "strconv"
28         "strings"
29 )
30
31 func (c *Control) TestRestHandler(w http.ResponseWriter, r *http.Request) {
32         xapp.Logger.Debug("RESTTestRestHandler() called")
33
34         pathParams := mux.Vars(r)
35         s := pathParams["testId"]
36
37         // This can be used to delete single subscription from db
38         if contains := strings.Contains(s, "deletesubid="); contains == true {
39                 var splits = strings.Split(s, "=")
40                 if subId, err := strconv.ParseInt(splits[1], 10, 64); err == nil {
41                         xapp.Logger.Debug("RemoveSubscriptionFromSdl() called. subId = %v", subId)
42                         c.RemoveSubscriptionFromSdl(uint32(subId))
43                         return
44                 }
45         }
46
47         // This can be used to remove all subscriptions db from
48         if s == "emptydb" {
49                 xapp.Logger.Debug("RemoveAllSubscriptionsFromSdl() called")
50                 c.RemoveAllSubscriptionsFromSdl()
51                 c.RemoveAllRESTSubscriptionsFromSdl()
52                 return
53         }
54
55         // This is meant to cause submgr's restart in testing
56         if s == "restart" {
57                 xapp.Logger.Debug("os.Exit(1) called")
58                 os.Exit(1)
59         }
60
61         xapp.Logger.Debug("Unsupported rest command received %s", s)
62 }
63
64 //-------------------------------------------------------------------
65 //
66 //-------------------------------------------------------------------
67 func (c *Control) GetAllRestSubscriptions(w http.ResponseWriter, r *http.Request) {
68
69         // Get all REST Subscriptions in subscription manager
70         xapp.Logger.Debug("GetAllRestSubscriptions() called")
71         w.Write(c.registry.GetAllRestSubscriptionsJson())
72 }
73
74 func (c *Control) GetAllE2Nodes(w http.ResponseWriter, r *http.Request) {
75
76         // Get all E2Nodes in subscription manager
77         xapp.Logger.Debug("GetAllE2Nodes() called")
78         w.Write(c.e2IfState.GetE2NodesJson())
79 }
80
81 func (c *Control) GetAllE2NodeRestSubscriptions(w http.ResponseWriter, r *http.Request) {
82         xapp.Logger.Debug("GetAllE2NodeRestSubscriptions() called: Req= %v", r.URL.Path)
83
84         // Get all REST Subscriptions of a E2Node
85         pathParams := mux.Vars(r)
86         ranName := pathParams["ranName"]
87         xapp.Logger.Debug("GetAllE2NodeRestSubscriptions() ranName=%s", ranName)
88         if ranName != "" {
89                 w.Write(c.registry.GetAllE2NodeRestSubscriptionsJson(ranName))
90         } else {
91                 xapp.Logger.Debug("GetAllE2NodeRestSubscriptions() Invalid path %s", ranName)
92                 w.WriteHeader(400) // Bad request
93         }
94 }
95
96 func (c *Control) GetAllXapps(w http.ResponseWriter, r *http.Request) {
97
98         // Get all xApps in subscription manager
99         xapp.Logger.Debug("GetAllXapps() called: Req= %v", r.URL.Path)
100         w.Write(c.registry.GetAllXappsJson())
101 }
102
103 func (c *Control) GetAllXappRestSubscriptions(w http.ResponseWriter, r *http.Request) {
104         xapp.Logger.Debug("GetAllXappRestSubscriptions() called")
105
106         // Get all REST Subscriptions of a xApp
107         pathParams := mux.Vars(r)
108         xappServiceName := pathParams["xappServiceName"]
109         xapp.Logger.Debug("GetAllXappRestSubscriptions() xappServiceName=%s", xappServiceName)
110         if xappServiceName != "" {
111                 w.Write(c.registry.GetAllXappRestSubscriptionsJson(xappServiceName))
112         } else {
113                 xapp.Logger.Debug("GetAllXappRestSubscriptions() Invalid path %s", xappServiceName)
114                 w.WriteHeader(400) // Bad request
115         }
116 }
117
118 func (c *Control) DeleteAllE2nodeSubscriptions(w http.ResponseWriter, r *http.Request) {
119         xapp.Logger.Debug("DeleteAllE2nodeSubscriptions() called: Req= %v", r.URL.Path)
120
121         // Delete all REST Subscriptions of a E2Node
122         pathParams := mux.Vars(r)
123         ranName := pathParams["ranName"]
124         xapp.Logger.Debug("DeleteE2nodeSubscriptions() ranName=%s", ranName)
125         if ranName == "" {
126                 w.WriteHeader(400) // Bad request
127         }
128         nbIds := c.e2IfState.GetAllE2Nodes()
129         ranName, ok := nbIds[ranName]
130         if ok {
131                 restSubscriptions := c.registry.GetAllE2NodeRestSubscriptions(ranName)
132                 for restSubsId, _ := range restSubscriptions {
133                         c.RESTSubscriptionDeleteHandler(restSubsId)
134                 }
135                 return
136         } else {
137                 w.WriteHeader(404) // Not found
138         }
139 }
140
141 func (c *Control) DeleteAllXappSubscriptions(w http.ResponseWriter, r *http.Request) {
142         xapp.Logger.Debug("DeleteAllXappSubscriptions() called: Req= %v", r.URL.Path)
143
144         // Delete all REST Subscriptions of a xApp
145         pathParams := mux.Vars(r)
146         xappServiceName := pathParams["xappServiceName"]
147         xapp.Logger.Debug("DeleteAllXappSubscriptions() ranName=%s", xappServiceName)
148         if xappServiceName == "" {
149                 w.WriteHeader(400) // Bad request
150         }
151         xapps := c.registry.GetAllXapps()
152         _, ok := xapps[xappServiceName]
153         if ok {
154                 XappRestSubscriptions := c.registry.GetAllXappRestSubscriptions(xappServiceName)
155                 for restSubsId, _ := range XappRestSubscriptions {
156                         c.RESTSubscriptionDeleteHandler(restSubsId)
157                 }
158                 return
159         } else {
160                 w.WriteHeader(404) // Not found
161         }
162         w.WriteHeader(200) // OK
163 }
164
165 func (c *Control) GetE2Subscriptions(w http.ResponseWriter, r *http.Request) {
166         xapp.Logger.Debug("GetE2Subscriptions() called: Req= %v", r.URL.Path)
167
168         // Get all E2 subscriptions of a REST Subscription
169         pathParams := mux.Vars(r)
170         restId := pathParams["restId"]
171         xapp.Logger.Debug("GetE2Subscriptions(): restId=%s", restId)
172         if restId == "" {
173                 w.WriteHeader(400) // Bad request
174         }
175
176         e2Subscriptions, err := c.registry.GetE2SubscriptionsJson(restId)
177         if err != nil {
178                 w.WriteHeader(404) // Not found
179         } else {
180                 w.Write(e2Subscriptions)
181         }
182 }