0d249d923b24fcb172dea184a213fe5ec54ea1bd
[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                 err := c.RemoveAllSubscriptionsFromSdl()
51                 if err != nil {
52                         xapp.Logger.Error("RemoveAllSubscriptionsFromSdl() RemoveAllSubscriptionsFromSdl() failure: %s", err.Error())
53                 }
54                 err = c.RemoveAllRESTSubscriptionsFromSdl()
55                 if err != nil {
56                         xapp.Logger.Error("RemoveAllRESTSubscriptionsFromSdl() RemoveAllSubscriptionsFromSdl() failure: %s", err.Error())
57                 }
58                 return
59         }
60
61         // This is meant to cause submgr's restart in testing
62         if s == "restart" {
63                 xapp.Logger.Debug("os.Exit(1) called")
64                 os.Exit(1)
65         }
66
67         xapp.Logger.Debug("Unsupported rest command received %s", s)
68 }
69
70 //-------------------------------------------------------------------
71 //
72 //-------------------------------------------------------------------
73 func (c *Control) GetAllRestSubscriptions(w http.ResponseWriter, r *http.Request) {
74
75         // Get all REST Subscriptions in subscription manager
76         xapp.Logger.Debug("GetAllRestSubscriptions() called")
77         _, err := w.Write(c.registry.GetAllRestSubscriptionsJson())
78         if err != nil {
79                 xapp.Logger.Error("GetAllRestSubscriptions() w.Write failure: %s", err.Error())
80         }
81 }
82
83 func (c *Control) GetAllE2Nodes(w http.ResponseWriter, r *http.Request) {
84
85         // Get all E2Nodes in subscription manager
86         xapp.Logger.Debug("GetAllE2Nodes() called")
87         w.Write(c.e2IfState.GetE2NodesJson())
88 }
89
90 func (c *Control) GetAllE2NodeRestSubscriptions(w http.ResponseWriter, r *http.Request) {
91         xapp.Logger.Debug("GetAllE2NodeRestSubscriptions() called: Req= %v", r.URL.Path)
92
93         // Get all REST Subscriptions of a E2Node
94         pathParams := mux.Vars(r)
95         ranName := pathParams["ranName"]
96         xapp.Logger.Debug("GetAllE2NodeRestSubscriptions() ranName=%s", ranName)
97         if ranName != "" {
98                 _, err := w.Write(c.registry.GetAllE2NodeRestSubscriptionsJson(ranName))
99                 if err != nil {
100                         xapp.Logger.Error("GetAllE2NodeRestSubscriptions() w.Write failure: %s", err.Error())
101                 }
102         } else {
103                 xapp.Logger.Debug("GetAllE2NodeRestSubscriptions() Invalid path %s", ranName)
104                 w.WriteHeader(400) // Bad request
105         }
106 }
107
108 func (c *Control) GetAllXapps(w http.ResponseWriter, r *http.Request) {
109
110         // Get all xApps in subscription manager
111         xapp.Logger.Debug("GetAllXapps() called: Req= %v", r.URL.Path)
112         _, err := w.Write(c.registry.GetAllXappsJson())
113         if err != nil {
114                 xapp.Logger.Error("GetAllXapps() w.Write failure: %s", err.Error())
115         }
116 }
117
118 func (c *Control) GetAllXappRestSubscriptions(w http.ResponseWriter, r *http.Request) {
119         xapp.Logger.Debug("GetAllXappRestSubscriptions() called")
120
121         // Get all REST Subscriptions of a xApp
122         pathParams := mux.Vars(r)
123         xappServiceName := pathParams["xappServiceName"]
124         xapp.Logger.Debug("GetAllXappRestSubscriptions() xappServiceName=%s", xappServiceName)
125         if xappServiceName != "" {
126                 _, err := w.Write(c.registry.GetAllXappRestSubscriptionsJson(xappServiceName))
127                 if err != nil {
128                         xapp.Logger.Error("GetAllXappRestSubscriptions() w.Write failure: %s", err.Error())
129                 }
130         } else {
131                 xapp.Logger.Debug("GetAllXappRestSubscriptions() Invalid path %s", xappServiceName)
132                 w.WriteHeader(400) // Bad request
133         }
134 }
135
136 func (c *Control) DeleteAllE2nodeSubscriptions(w http.ResponseWriter, r *http.Request) {
137         xapp.Logger.Debug("DeleteAllE2nodeSubscriptions() called: Req= %v", r.URL.Path)
138
139         // Delete all REST Subscriptions of a E2Node
140         pathParams := mux.Vars(r)
141         ranName := pathParams["ranName"]
142         xapp.Logger.Debug("DeleteE2nodeSubscriptions() ranName=%s", ranName)
143         if ranName == "" {
144                 w.WriteHeader(400) // Bad request
145         }
146         nbIds := c.e2IfState.GetAllE2Nodes()
147         ranName, ok := nbIds[ranName]
148         if ok {
149                 restSubscriptions := c.registry.GetAllE2NodeRestSubscriptions(ranName)
150                 for restSubsId, _ := range restSubscriptions {
151                         c.RESTSubscriptionDeleteHandler(restSubsId)
152                 }
153                 return
154         } else {
155                 w.WriteHeader(404) // Not found
156         }
157 }
158
159 func (c *Control) DeleteAllXappSubscriptions(w http.ResponseWriter, r *http.Request) {
160         xapp.Logger.Debug("DeleteAllXappSubscriptions() called: Req= %v", r.URL.Path)
161
162         // Delete all REST Subscriptions of a xApp
163         pathParams := mux.Vars(r)
164         xappServiceName := pathParams["xappServiceName"]
165         xapp.Logger.Debug("DeleteAllXappSubscriptions() ranName=%s", xappServiceName)
166         if xappServiceName == "" {
167                 w.WriteHeader(400) // Bad request
168         }
169         xapps := c.registry.GetAllXapps()
170         _, ok := xapps[xappServiceName]
171         if ok {
172                 XappRestSubscriptions := c.registry.GetAllXappRestSubscriptions(xappServiceName)
173                 for restSubsId, _ := range XappRestSubscriptions {
174                         c.RESTSubscriptionDeleteHandler(restSubsId)
175                 }
176                 return
177         } else {
178                 w.WriteHeader(404) // Not found
179         }
180         w.WriteHeader(200) // OK
181 }
182
183 func (c *Control) GetE2Subscriptions(w http.ResponseWriter, r *http.Request) {
184         xapp.Logger.Debug("GetE2Subscriptions() called: Req= %v", r.URL.Path)
185
186         // Get all E2 subscriptions of a REST Subscription
187         pathParams := mux.Vars(r)
188         restId := pathParams["restId"]
189         xapp.Logger.Debug("GetE2Subscriptions(): restId=%s", restId)
190         if restId == "" {
191                 w.WriteHeader(400) // Bad request
192         }
193
194         e2Subscriptions, err := c.registry.GetE2SubscriptionsJson(restId)
195         if err != nil {
196                 w.WriteHeader(404) // Not found
197         } else {
198                 _, err := w.Write(e2Subscriptions)
199                 if err != nil {
200                         xapp.Logger.Error("GetE2Subscriptions() w.Write failure: %s", err.Error())
201                 }
202         }
203 }