[VOL-4638] Add communication with VOLTHA NBI and the Olt app REST APIs

Change-Id: I6ccf5725a108b91f47dfc4c20e9614b38c71419a
diff --git a/internal/clients/logger.go b/internal/clients/logger.go
new file mode 100644
index 0000000..00de5a0
--- /dev/null
+++ b/internal/clients/logger.go
@@ -0,0 +1,32 @@
+/*
+* Copyright 2022-present Open Networking Foundation
+
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+
+* http://www.apache.org/licenses/LICENSE-2.0
+
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+ */
+
+package clients
+
+import (
+	"github.com/opencord/voltha-lib-go/v7/pkg/log"
+)
+
+var logger log.CLogger
+
+func init() {
+	// Setup this package so that it's log level can be modified at run time
+	var err error
+	logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{})
+	if err != nil {
+		panic(err)
+	}
+}
diff --git a/internal/clients/nbi.go b/internal/clients/nbi.go
new file mode 100644
index 0000000..48df827
--- /dev/null
+++ b/internal/clients/nbi.go
@@ -0,0 +1,110 @@
+/*
+* Copyright 2022-present Open Networking Foundation
+
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+
+* http://www.apache.org/licenses/LICENSE-2.0
+
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+ */
+
+package clients
+
+import (
+	"context"
+	"crypto/tls"
+	"fmt"
+	"time"
+
+	"github.com/opencord/voltha-lib-go/v7/pkg/log"
+	"github.com/opencord/voltha-protos/v5/go/voltha"
+	"google.golang.org/grpc"
+	"google.golang.org/grpc/backoff"
+	"google.golang.org/grpc/connectivity"
+	"google.golang.org/grpc/credentials"
+)
+
+const (
+	nbiMaxBackoffInterval = time.Second * 10
+)
+
+//Used to keep track of a connection to a grpc endpoint of the northbound api
+type VolthaNbiClient struct {
+	conn     *grpc.ClientConn
+	Service  voltha.VolthaServiceClient
+	endpoint string
+}
+
+// Creates a new voltha northbound client
+func NewVolthaNbiClient(endpoint string) *VolthaNbiClient {
+	return &VolthaNbiClient{
+		endpoint: endpoint,
+	}
+}
+
+// Dials the grpc connection to the endpoint and sets the service as running
+func (c *VolthaNbiClient) Connect(ctx context.Context, useTls bool, verifyTls bool) error {
+	var opts []grpc.DialOption
+
+	backoffConfig := backoff.DefaultConfig
+	backoffConfig.MaxDelay = nbiMaxBackoffInterval
+
+	opts = append(opts,
+		grpc.WithConnectParams(
+			grpc.ConnectParams{
+				Backoff: backoffConfig,
+			},
+		),
+	)
+
+	if useTls {
+		//TODO: should this be expanded with the ability to provide certificates?
+		creds := credentials.NewTLS(&tls.Config{InsecureSkipVerify: !verifyTls})
+		opts = append(opts, grpc.WithTransportCredentials(creds))
+	} else {
+		opts = append(opts, grpc.WithInsecure())
+	}
+
+	logger.Debugw(ctx, "connecting-to-voltha-nbi-grpc", log.Fields{"endpoint": c.endpoint})
+
+	var err error
+	c.conn, err = grpc.DialContext(ctx, c.endpoint, opts...)
+
+	if err != nil {
+		return err
+	}
+
+	//Wait for the connection to be successful, with periodic updates on its status
+	for {
+		if state := c.conn.GetState(); state == connectivity.Ready {
+			break
+		} else {
+			logger.Warnw(ctx, "voltha-nbi-grpc-not-ready", log.Fields{"state": state})
+		}
+
+		select {
+		case <-ctx.Done():
+			return fmt.Errorf("voltha-nbi-connection-stopped-due-to-context-done")
+		case <-time.After(nbiMaxBackoffInterval):
+			continue
+		}
+	}
+
+	c.Service = voltha.NewVolthaServiceClient(c.conn)
+
+	logger.Debug(ctx, "voltha-nbi-grpc-connected")
+
+	return nil
+}
+
+// Closes the connection and cleans up
+func (c *VolthaNbiClient) Close() {
+	c.conn.Close()
+	c.Service = nil
+}
diff --git a/internal/clients/olt_app.go b/internal/clients/olt_app.go
new file mode 100644
index 0000000..6e171b9
--- /dev/null
+++ b/internal/clients/olt_app.go
@@ -0,0 +1,137 @@
+/*
+* Copyright 2022-present Open Networking Foundation
+
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+
+* http://www.apache.org/licenses/LICENSE-2.0
+
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+ */
+
+package clients
+
+import (
+	"context"
+	"fmt"
+	"io"
+	"net/http"
+	"time"
+
+	"github.com/opencord/voltha-lib-go/v7/pkg/log"
+)
+
+const (
+	oltAppHttpRequestTimeout = time.Second * 10
+	oltAppBackoffInterval    = time.Second * 10
+)
+
+type OltAppClient struct {
+	httpClient *http.Client
+	endpoint   string
+	username   string
+	password   string
+}
+
+type RestResponse struct {
+	Body string
+	Code int
+}
+
+// Creates a new olt app client
+func NewOltAppClient(endpoint string, user string, pass string) *OltAppClient {
+	return &OltAppClient{
+		httpClient: &http.Client{
+			Timeout: oltAppHttpRequestTimeout,
+		},
+		endpoint: endpoint,
+		username: user,
+		password: pass,
+	}
+}
+
+func (c *OltAppClient) CheckConnection(ctx context.Context) error {
+	logger.Debugw(ctx, "checking-connection-to-onos-olt-app-api", log.Fields{"endpoint": c.endpoint})
+
+	for {
+		if resp, err := c.GetStatus(); err == nil {
+			logger.Debug(ctx, "onos-olt-app-api-reachable")
+			break
+		} else {
+			logger.Warnw(ctx, "onos-olt-app-api-not-ready", log.Fields{
+				"err":      err,
+				"response": resp,
+			})
+		}
+
+		//Wait a bit before trying again
+		select {
+		case <-ctx.Done():
+			return fmt.Errorf("onos-olt-app-connection-stopped-due-to-context-done")
+		case <-time.After(oltAppBackoffInterval):
+			continue
+		}
+	}
+
+	return nil
+}
+
+func (c *OltAppClient) makeRequest(method string, url string) (RestResponse, error) {
+	result := RestResponse{Code: 0}
+
+	req, err := http.NewRequest(method, url, nil)
+	if err != nil {
+		return result, fmt.Errorf("cannot-create-request: %s", err)
+	}
+
+	req.SetBasicAuth(c.username, c.password)
+
+	resp, err := c.httpClient.Do(req)
+	if err != nil {
+		return result, fmt.Errorf("cannot-get-response: %s", err)
+	}
+	defer resp.Body.Close()
+
+	buffer, err := io.ReadAll(resp.Body)
+	if err != nil {
+		return result, fmt.Errorf("error-while-reading-response-body: %s", err)
+	}
+
+	result.Body = string(buffer)
+	result.Code = resp.StatusCode
+
+	if result.Code != http.StatusOK {
+		return result, fmt.Errorf("status-code-not-ok: %s %s %d", method, url, result.Code)
+	}
+
+	return result, nil
+}
+
+func (c *OltAppClient) GetStatus() (RestResponse, error) {
+	method := http.MethodGet
+	url := fmt.Sprintf("http://%s/onos/olt/oltapp/status", c.endpoint)
+
+	return c.makeRequest(method, url)
+}
+
+//NOTE: if methods are used to retrieve more complex information
+//it may be better to return an already deserialized structure
+//instead of the current RestResponse
+func (c *OltAppClient) ProvisionSubscriber(device string, port uint32) (RestResponse, error) {
+	method := http.MethodPost
+	url := fmt.Sprintf("http://%s/onos/olt/oltapp/%s/%d", c.endpoint, device, port)
+
+	return c.makeRequest(method, url)
+}
+
+func (c *OltAppClient) RemoveSubscriber(device string, port uint32) (RestResponse, error) {
+	method := http.MethodDelete
+	url := fmt.Sprintf("http://%s/onos/olt/oltapp/%s/%d", c.endpoint, device, port)
+
+	return c.makeRequest(method, url)
+}
diff --git a/internal/config/config.go b/internal/config/config.go
index a8ae760..d7e4916 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -29,6 +29,12 @@
 	TraceEnabled          bool
 	TraceAgentAddress     string
 	LogCorrelationEnabled bool
+	VolthaNbiEndpoint     string
+	TlsEnabled            bool
+	TlsVerify             bool
+	OnosRestEndpoint      string
+	OnosUser              string
+	OnosPassword          string
 }
 
 // LoadConfig loads the BBF adapter configuration through
@@ -43,6 +49,12 @@
 	flag.BoolVar(&conf.TraceEnabled, "trace_enabled", conf.TraceEnabled, "Whether to send logs to tracing agent")
 	flag.StringVar(&conf.TraceAgentAddress, "trace_agent_address", conf.TraceAgentAddress, "The address of tracing agent to which span info should be sent")
 	flag.BoolVar(&conf.LogCorrelationEnabled, "log_correlation_enabled", conf.LogCorrelationEnabled, "Whether to enrich log statements with fields denoting operation being executed for achieving correlation")
+	flag.StringVar(&conf.VolthaNbiEndpoint, "voltha_nbi_endpoint", conf.VolthaNbiEndpoint, "Endpoint of the VOLTHA northbound interface")
+	flag.BoolVar(&conf.TlsEnabled, "tls_enabled", conf.TlsEnabled, "Whether to use TLS when connecting to VOLTHA's northbound grpc server")
+	flag.BoolVar(&conf.TlsVerify, "tls_verify", conf.TlsVerify, "Whether to verify the server's certificate when connecting to VOLTHA's northbound grpc server. To be used with 'tls_enabled'.")
+	flag.StringVar(&conf.OnosRestEndpoint, "onos_rest_endpoint", conf.OnosRestEndpoint, "Endpoint of ONOS REST APIs")
+	flag.StringVar(&conf.OnosUser, "onos_user", conf.OnosUser, "Username for ONOS REST APIs")
+	flag.StringVar(&conf.OnosPassword, "onos_pass", conf.OnosPassword, "Password for ONOS REST APIs")
 
 	flag.Parse()
 
@@ -59,5 +71,11 @@
 		TraceEnabled:          false,
 		TraceAgentAddress:     "127.0.0.1:6831",
 		LogCorrelationEnabled: true,
+		VolthaNbiEndpoint:     "voltha-voltha-api.voltha.svc:55555",
+		TlsEnabled:            false,
+		TlsVerify:             false,
+		OnosRestEndpoint:      "voltha-infra-onos-classic-hs.infra.svc:8181",
+		OnosUser:              "onos",
+		OnosPassword:          "rocks",
 	}
 }