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