blob: 6e171b95c8195bb32620a94bcd8c0d1ae8939137 [file] [log] [blame]
Elia Battistonbe9edc12022-03-09 11:35:58 +01001/*
2* Copyright 2022-present Open Networking Foundation
3
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7
8* http://www.apache.org/licenses/LICENSE-2.0
9
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15 */
16
17package clients
18
19import (
20 "context"
21 "fmt"
22 "io"
23 "net/http"
24 "time"
25
26 "github.com/opencord/voltha-lib-go/v7/pkg/log"
27)
28
29const (
30 oltAppHttpRequestTimeout = time.Second * 10
31 oltAppBackoffInterval = time.Second * 10
32)
33
34type OltAppClient struct {
35 httpClient *http.Client
36 endpoint string
37 username string
38 password string
39}
40
41type RestResponse struct {
42 Body string
43 Code int
44}
45
46// Creates a new olt app client
47func NewOltAppClient(endpoint string, user string, pass string) *OltAppClient {
48 return &OltAppClient{
49 httpClient: &http.Client{
50 Timeout: oltAppHttpRequestTimeout,
51 },
52 endpoint: endpoint,
53 username: user,
54 password: pass,
55 }
56}
57
58func (c *OltAppClient) CheckConnection(ctx context.Context) error {
59 logger.Debugw(ctx, "checking-connection-to-onos-olt-app-api", log.Fields{"endpoint": c.endpoint})
60
61 for {
62 if resp, err := c.GetStatus(); err == nil {
63 logger.Debug(ctx, "onos-olt-app-api-reachable")
64 break
65 } else {
66 logger.Warnw(ctx, "onos-olt-app-api-not-ready", log.Fields{
67 "err": err,
68 "response": resp,
69 })
70 }
71
72 //Wait a bit before trying again
73 select {
74 case <-ctx.Done():
75 return fmt.Errorf("onos-olt-app-connection-stopped-due-to-context-done")
76 case <-time.After(oltAppBackoffInterval):
77 continue
78 }
79 }
80
81 return nil
82}
83
84func (c *OltAppClient) makeRequest(method string, url string) (RestResponse, error) {
85 result := RestResponse{Code: 0}
86
87 req, err := http.NewRequest(method, url, nil)
88 if err != nil {
89 return result, fmt.Errorf("cannot-create-request: %s", err)
90 }
91
92 req.SetBasicAuth(c.username, c.password)
93
94 resp, err := c.httpClient.Do(req)
95 if err != nil {
96 return result, fmt.Errorf("cannot-get-response: %s", err)
97 }
98 defer resp.Body.Close()
99
100 buffer, err := io.ReadAll(resp.Body)
101 if err != nil {
102 return result, fmt.Errorf("error-while-reading-response-body: %s", err)
103 }
104
105 result.Body = string(buffer)
106 result.Code = resp.StatusCode
107
108 if result.Code != http.StatusOK {
109 return result, fmt.Errorf("status-code-not-ok: %s %s %d", method, url, result.Code)
110 }
111
112 return result, nil
113}
114
115func (c *OltAppClient) GetStatus() (RestResponse, error) {
116 method := http.MethodGet
117 url := fmt.Sprintf("http://%s/onos/olt/oltapp/status", c.endpoint)
118
119 return c.makeRequest(method, url)
120}
121
122//NOTE: if methods are used to retrieve more complex information
123//it may be better to return an already deserialized structure
124//instead of the current RestResponse
125func (c *OltAppClient) ProvisionSubscriber(device string, port uint32) (RestResponse, error) {
126 method := http.MethodPost
127 url := fmt.Sprintf("http://%s/onos/olt/oltapp/%s/%d", c.endpoint, device, port)
128
129 return c.makeRequest(method, url)
130}
131
132func (c *OltAppClient) RemoveSubscriber(device string, port uint32) (RestResponse, error) {
133 method := http.MethodDelete
134 url := fmt.Sprintf("http://%s/onos/olt/oltapp/%s/%d", c.endpoint, device, port)
135
136 return c.makeRequest(method, url)
137}