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