David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2020 the original author or authors. |
| 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 | |
| 17 | package ofagent |
| 18 | |
| 19 | import ( |
| 20 | "context" |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 21 | "sync" |
| 22 | "time" |
| 23 | |
| 24 | "github.com/opencord/ofagent-go/internal/pkg/holder" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 25 | "github.com/opencord/ofagent-go/internal/pkg/openflow" |
Maninder | 12b909f | 2020-10-23 14:23:36 +0530 | [diff] [blame^] | 26 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 27 | "github.com/opencord/voltha-lib-go/v4/pkg/probe" |
| 28 | "github.com/opencord/voltha-protos/v4/go/openflow_13" |
| 29 | "github.com/opencord/voltha-protos/v4/go/voltha" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 30 | "google.golang.org/grpc" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 31 | ) |
| 32 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 33 | type ofaEvent byte |
| 34 | type ofaState byte |
| 35 | |
| 36 | const ( |
| 37 | ofaEventStart = ofaEvent(iota) |
| 38 | ofaEventVolthaConnected |
| 39 | ofaEventVolthaDisconnected |
| 40 | ofaEventError |
| 41 | |
| 42 | ofaStateConnected = ofaState(iota) |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 43 | ofaStateConnecting |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 44 | ofaStateDisconnected |
| 45 | ) |
| 46 | |
| 47 | type OFAgent struct { |
| 48 | VolthaApiEndPoint string |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 49 | OFControllerEndPoints []string |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 50 | DeviceListRefreshInterval time.Duration |
| 51 | ConnectionMaxRetries int |
| 52 | ConnectionRetryDelay time.Duration |
| 53 | |
| 54 | volthaConnection *grpc.ClientConn |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 55 | volthaClient *holder.VolthaServiceClientHolder |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 56 | mapLock sync.Mutex |
| 57 | clientMap map[string]*openflow.OFClient |
| 58 | events chan ofaEvent |
| 59 | |
| 60 | packetInChannel chan *voltha.PacketIn |
| 61 | packetOutChannel chan *voltha.PacketOut |
Maninder | 12b909f | 2020-10-23 14:23:36 +0530 | [diff] [blame^] | 62 | changeEventChannel chan *openflow_13.ChangeEvent |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 65 | func NewOFAgent(ctx context.Context, config *OFAgent) (*OFAgent, error) { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 66 | ofa := OFAgent{ |
| 67 | VolthaApiEndPoint: config.VolthaApiEndPoint, |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 68 | OFControllerEndPoints: config.OFControllerEndPoints, |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 69 | DeviceListRefreshInterval: config.DeviceListRefreshInterval, |
| 70 | ConnectionMaxRetries: config.ConnectionMaxRetries, |
| 71 | ConnectionRetryDelay: config.ConnectionRetryDelay, |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 72 | volthaClient: &holder.VolthaServiceClientHolder{}, |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 73 | packetInChannel: make(chan *voltha.PacketIn), |
| 74 | packetOutChannel: make(chan *voltha.PacketOut), |
Maninder | 12b909f | 2020-10-23 14:23:36 +0530 | [diff] [blame^] | 75 | changeEventChannel: make(chan *openflow_13.ChangeEvent), |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 76 | clientMap: make(map[string]*openflow.OFClient), |
| 77 | events: make(chan ofaEvent, 100), |
| 78 | } |
| 79 | |
| 80 | if ofa.DeviceListRefreshInterval <= 0 { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 81 | logger.Warnw(ctx, "device list refresh internal not valid, setting to default", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 82 | log.Fields{ |
| 83 | "value": ofa.DeviceListRefreshInterval.String(), |
| 84 | "default": (1 * time.Minute).String()}) |
| 85 | ofa.DeviceListRefreshInterval = 1 * time.Minute |
| 86 | } |
| 87 | |
| 88 | if ofa.ConnectionRetryDelay <= 0 { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 89 | logger.Warnw(ctx, "connection retry delay not value, setting to default", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 90 | log.Fields{ |
| 91 | "value": ofa.ConnectionRetryDelay.String(), |
| 92 | "default": (3 * time.Second).String()}) |
| 93 | ofa.ConnectionRetryDelay = 3 * time.Second |
| 94 | } |
| 95 | |
| 96 | return &ofa, nil |
| 97 | } |
| 98 | |
| 99 | // Run - make the inital connection to voltha and kicks off io streams |
| 100 | func (ofa *OFAgent) Run(ctx context.Context) { |
| 101 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 102 | logger.Debugw(ctx, "Starting GRPC - VOLTHA client", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 103 | log.Fields{ |
| 104 | "voltha-endpoint": ofa.VolthaApiEndPoint, |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 105 | "controller-endpoint": ofa.OFControllerEndPoints}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 106 | |
| 107 | // If the context contains a k8s probe then register services |
| 108 | p := probe.GetProbeFromContext(ctx) |
| 109 | if p != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 110 | p.RegisterService(ctx, "voltha") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 111 | } |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 112 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 113 | ofa.events <- ofaEventStart |
| 114 | |
| 115 | /* |
| 116 | * Two sub-contexts are created here for different purposes so we can |
| 117 | * control the lifecyle of processing loops differently. |
| 118 | * |
| 119 | * volthaCtx - controls those processes that rely on the GRPC |
| 120 | * GRPCconnection to voltha and will be restarted when the |
| 121 | * GRPC connection is interrupted. |
| 122 | * hdlCtx - controls those processes that listen to channels and |
| 123 | * process each message. these will likely never be |
| 124 | * stopped until the ofagent is stopped. |
| 125 | */ |
| 126 | var volthaCtx, hdlCtx context.Context |
| 127 | var volthaDone, hdlDone func() |
| 128 | state := ofaStateDisconnected |
| 129 | |
| 130 | for { |
| 131 | select { |
| 132 | case <-ctx.Done(): |
| 133 | if volthaDone != nil { |
| 134 | volthaDone() |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 135 | } |
| 136 | if hdlDone != nil { |
| 137 | hdlDone() |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 138 | } |
| 139 | return |
| 140 | case event := <-ofa.events: |
| 141 | switch event { |
| 142 | case ofaEventStart: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 143 | logger.Debug(ctx, "ofagent-voltha-start-event") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 144 | |
| 145 | // Start the loops that process messages |
| 146 | hdlCtx, hdlDone = context.WithCancel(context.Background()) |
| 147 | go ofa.handlePacketsIn(hdlCtx) |
| 148 | go ofa.handleChangeEvents(hdlCtx) |
| 149 | |
| 150 | // Kick off process to attempt to establish |
| 151 | // connection to voltha |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 152 | state = ofaStateConnecting |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 153 | go func() { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 154 | if err := ofa.establishConnectionToVoltha(ctx, p); err != nil { |
| 155 | logger.Errorw(ctx, "voltha-connection-failed", log.Fields{"error": err}) |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 156 | panic(err) |
| 157 | } |
| 158 | }() |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 159 | |
| 160 | case ofaEventVolthaConnected: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 161 | logger.Debug(ctx, "ofagent-voltha-connect-event") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 162 | |
| 163 | // Start the loops that poll from voltha |
| 164 | if state != ofaStateConnected { |
| 165 | state = ofaStateConnected |
| 166 | volthaCtx, volthaDone = context.WithCancel(context.Background()) |
| 167 | go ofa.receiveChangeEvents(volthaCtx) |
| 168 | go ofa.receivePacketsIn(volthaCtx) |
| 169 | go ofa.streamPacketOut(volthaCtx) |
| 170 | go ofa.synchronizeDeviceList(volthaCtx) |
| 171 | } |
| 172 | |
| 173 | case ofaEventVolthaDisconnected: |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 174 | if p != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 175 | p.UpdateStatus(ctx, "voltha", probe.ServiceStatusNotReady) |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 176 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 177 | logger.Debug(ctx, "ofagent-voltha-disconnect-event") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 178 | if state == ofaStateConnected { |
| 179 | state = ofaStateDisconnected |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 180 | ofa.volthaClient.Clear() |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 181 | volthaDone() |
| 182 | volthaDone = nil |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 183 | } |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 184 | if state != ofaStateConnecting { |
| 185 | state = ofaStateConnecting |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 186 | go func() { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 187 | if err := ofa.establishConnectionToVoltha(ctx, p); err != nil { |
| 188 | logger.Errorw(ctx, "voltha-connection-failed", log.Fields{"error": err}) |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 189 | panic(err) |
| 190 | } |
| 191 | }() |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 192 | } |
| 193 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 194 | case ofaEventError: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 195 | logger.Debug(ctx, "ofagent-error-event") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 196 | default: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 197 | logger.Fatalw(ctx, "ofagent-unknown-event", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 198 | log.Fields{"event": event}) |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |