WIP - Suggesting changes (take2)
This is not yet completed, still working on things. Eventually the plan
is to provide the following changes
- restructure repo to be more aligned with https://github.com/golang-standards/project-layout
- add k8s probes
- modifications (golang range loops, etc) to follow some golang
practices
Change-Id: I6922cbc00b5ef17ceab183aba00a7fc59ab46480
diff --git a/internal/pkg/ofagent/connection.go b/internal/pkg/ofagent/connection.go
new file mode 100644
index 0000000..5fd8a3f
--- /dev/null
+++ b/internal/pkg/ofagent/connection.go
@@ -0,0 +1,77 @@
+/*
+ Copyright 2020 the original author or authors.
+
+ 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 ofagent
+
+import (
+ "context"
+ "errors"
+ "github.com/golang/protobuf/ptypes/empty"
+ "github.com/opencord/voltha-lib-go/v2/pkg/log"
+ "github.com/opencord/voltha-lib-go/v2/pkg/probe"
+ "github.com/opencord/voltha-protos/v2/go/voltha"
+ "google.golang.org/grpc"
+ "time"
+)
+
+func (ofa *OFAgent) establishConnectionToVoltha(p *probe.Probe) error {
+ if p != nil {
+ p.UpdateStatus("voltha", probe.ServiceStatusPreparing)
+ }
+
+ if ofa.volthaConnection != nil {
+ ofa.volthaConnection.Close()
+ }
+
+ ofa.volthaConnection = nil
+ ofa.volthaClient = nil
+ try := 1
+ for ofa.ConnectionMaxRetries == 0 || try < ofa.ConnectionMaxRetries {
+ conn, err := grpc.Dial(ofa.VolthaApiEndPoint, grpc.WithInsecure())
+ if err == nil {
+ svc := voltha.NewVolthaServiceClient(conn)
+ if svc != nil {
+ if _, err = svc.GetVoltha(context.Background(), &empty.Empty{}); err == nil {
+ logger.Debugw("Established connection to Voltha",
+ log.Fields{
+ "VolthaApiEndPoint": ofa.VolthaApiEndPoint,
+ })
+ ofa.volthaConnection = conn
+ ofa.volthaClient = svc
+ if p != nil {
+ p.UpdateStatus("voltha", probe.ServiceStatusRunning)
+ }
+ ofa.events <- ofaEventVolthaConnected
+ return nil
+ }
+ }
+ }
+ logger.Warnw("Failed to connect to voltha",
+ log.Fields{
+ "VolthaApiEndPoint": ofa.VolthaApiEndPoint,
+ "error": err.Error(),
+ })
+ if ofa.ConnectionMaxRetries == 0 || try < ofa.ConnectionMaxRetries {
+ if ofa.ConnectionMaxRetries != 0 {
+ try += 1
+ }
+ time.Sleep(ofa.ConnectionRetryDelay)
+ }
+ }
+ if p != nil {
+ p.UpdateStatus("voltha", probe.ServiceStatusFailed)
+ }
+ return errors.New("failed-to-connect-to-voltha")
+}