khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | */ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 16 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "errors" |
David Bainbridge | fd27f4b | 2020-03-26 18:27:41 -0700 | [diff] [blame^] | 22 | "strings" |
| 23 | "sync" |
| 24 | "time" |
| 25 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 26 | "github.com/opencord/voltha-go/db/model" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 27 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 28 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 29 | "github.com/opencord/voltha-lib-go/v3/pkg/probe" |
| 30 | "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 31 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 32 | "google.golang.org/grpc/codes" |
| 33 | "google.golang.org/grpc/status" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 34 | ) |
| 35 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 36 | // LogicalDeviceManager represent logical device manager attributes |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 37 | type LogicalDeviceManager struct { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 38 | logicalDeviceAgents sync.Map |
| 39 | core *Core |
| 40 | deviceMgr *DeviceManager |
| 41 | grpcNbiHdlr *APIHandler |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 42 | kafkaICProxy kafka.InterContainerProxy |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 43 | clusterDataProxy *model.Proxy |
| 44 | exitChannel chan int |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 45 | defaultTimeout time.Duration |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 46 | logicalDevicesLoadingLock sync.RWMutex |
| 47 | logicalDeviceLoadingInProgress map[string][]chan int |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 48 | } |
| 49 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 50 | func newLogicalDeviceManager(core *Core, deviceMgr *DeviceManager, kafkaICProxy kafka.InterContainerProxy, cdProxy *model.Proxy, timeout time.Duration) *LogicalDeviceManager { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 51 | var logicalDeviceMgr LogicalDeviceManager |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 52 | logicalDeviceMgr.core = core |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 53 | logicalDeviceMgr.exitChannel = make(chan int, 1) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 54 | logicalDeviceMgr.deviceMgr = deviceMgr |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 55 | logicalDeviceMgr.kafkaICProxy = kafkaICProxy |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 56 | logicalDeviceMgr.clusterDataProxy = cdProxy |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 57 | logicalDeviceMgr.defaultTimeout = timeout |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 58 | logicalDeviceMgr.logicalDevicesLoadingLock = sync.RWMutex{} |
| 59 | logicalDeviceMgr.logicalDeviceLoadingInProgress = make(map[string][]chan int) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 60 | return &logicalDeviceMgr |
| 61 | } |
| 62 | |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 63 | func (ldMgr *LogicalDeviceManager) setGrpcNbiHandler(grpcNbiHandler *APIHandler) { |
| 64 | ldMgr.grpcNbiHdlr = grpcNbiHandler |
| 65 | } |
| 66 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 67 | func (ldMgr *LogicalDeviceManager) start(ctx context.Context) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 68 | logger.Info("starting-logical-device-manager") |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 69 | probe.UpdateStatusFromContext(ctx, "logical-device-manager", probe.ServiceStatusRunning) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 70 | logger.Info("logical-device-manager-started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 71 | } |
| 72 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 73 | func (ldMgr *LogicalDeviceManager) stop(ctx context.Context) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 74 | logger.Info("stopping-logical-device-manager") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 75 | ldMgr.exitChannel <- 1 |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 76 | probe.UpdateStatusFromContext(ctx, "logical-device-manager", probe.ServiceStatusStopped) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 77 | logger.Info("logical-device-manager-stopped") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 78 | } |
| 79 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 80 | func sendAPIResponse(ctx context.Context, ch chan interface{}, result interface{}) { |
| 81 | if ctx.Err() == nil { |
| 82 | // Returned response only of the ctx has not been cancelled/timeout/etc |
| 83 | // Channel is automatically closed when a context is Done |
| 84 | ch <- result |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 85 | logger.Debugw("sendResponse", log.Fields{"result": result}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 86 | } else { |
| 87 | // Should the transaction be reverted back? |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 88 | logger.Debugw("sendResponse-context-error", log.Fields{"context-error": ctx.Err()}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 92 | func (ldMgr *LogicalDeviceManager) addLogicalDeviceAgentToMap(agent *LogicalDeviceAgent) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 93 | if _, exist := ldMgr.logicalDeviceAgents.Load(agent.logicalDeviceID); !exist { |
| 94 | ldMgr.logicalDeviceAgents.Store(agent.logicalDeviceID, agent) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 98 | // getLogicalDeviceAgent returns the logical device agent. If the device is not in memory then the device will |
| 99 | // be loaded from dB and a logical device agent created to managed it. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 100 | func (ldMgr *LogicalDeviceManager) getLogicalDeviceAgent(ctx context.Context, logicalDeviceID string) *LogicalDeviceAgent { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 101 | agent, ok := ldMgr.logicalDeviceAgents.Load(logicalDeviceID) |
| 102 | if ok { |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 103 | lda := agent.(*LogicalDeviceAgent) |
| 104 | if lda.logicalDevice == nil { |
| 105 | // This can happen when an agent for the logical device has been created but the logical device |
| 106 | // itself is not ready for action as it is waiting for switch and port capabilities from the |
| 107 | // relevant adapter. In such a case prevent any request aimed at that logical device. |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 108 | logger.Debugf("Logical device %s is not ready to serve requests", logicalDeviceID) |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 109 | return nil |
| 110 | } |
| 111 | return lda |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 112 | } |
| 113 | // Try to load into memory - loading will also create the logical device agent |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 114 | if err := ldMgr.load(ctx, logicalDeviceID); err == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 115 | if agent, ok = ldMgr.logicalDeviceAgents.Load(logicalDeviceID); ok { |
| 116 | return agent.(*LogicalDeviceAgent) |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 117 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 118 | } |
| 119 | return nil |
| 120 | } |
| 121 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 122 | func (ldMgr *LogicalDeviceManager) deleteLogicalDeviceAgent(logicalDeviceID string) { |
| 123 | ldMgr.logicalDeviceAgents.Delete(logicalDeviceID) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 124 | } |
| 125 | |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 126 | // GetLogicalDevice provides a cloned most up to date logical device. If device is not in memory |
| 127 | // it will be fetched from the dB |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 128 | func (ldMgr *LogicalDeviceManager) getLogicalDevice(ctx context.Context, id string) (*voltha.LogicalDevice, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 129 | logger.Debugw("getlogicalDevice", log.Fields{"logicaldeviceid": id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 130 | if agent := ldMgr.getLogicalDeviceAgent(ctx, id); agent != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 131 | return agent.GetLogicalDevice(ctx) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 132 | } |
| 133 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 134 | } |
| 135 | |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 136 | //listLogicalDevices returns the list of all logical devices |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 137 | func (ldMgr *LogicalDeviceManager) listLogicalDevices(ctx context.Context) (*voltha.LogicalDevices, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 138 | logger.Debug("ListAllLogicalDevices") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 139 | result := &voltha.LogicalDevices{} |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 140 | logicalDevices, err := ldMgr.clusterDataProxy.List(ctx, "/logical_devices", 0, true, "") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 141 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 142 | logger.Errorw("failed-to-list-logical-devices-from-cluster-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 143 | return nil, err |
| 144 | } |
| 145 | if logicalDevices != nil { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 146 | for _, logicalDevice := range logicalDevices.([]interface{}) { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 147 | result.Items = append(result.Items, logicalDevice.(*voltha.LogicalDevice)) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | return result, nil |
| 151 | } |
| 152 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 153 | func (ldMgr *LogicalDeviceManager) createLogicalDevice(ctx context.Context, device *voltha.Device) (*string, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 154 | logger.Debugw("creating-logical-device", log.Fields{"deviceId": device.Id}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 155 | // Sanity check |
| 156 | if !device.Root { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 157 | return nil, errors.New("device-not-root") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // Create a logical device agent - the logical device Id is based on the mac address of the device |
| 161 | // For now use the serial number - it may contain any combination of alphabetic characters and numbers, |
| 162 | // with length varying from eight characters to a maximum of 14 characters. Mac Address is part of oneof |
| 163 | // in the Device model. May need to be moved out. |
David Bainbridge | fd27f4b | 2020-03-26 18:27:41 -0700 | [diff] [blame^] | 164 | id := CreateLogicalDeviceID() |
| 165 | sn := strings.Replace(device.MacAddress, ":", "", -1) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 166 | if id == "" { |
David Bainbridge | fd27f4b | 2020-03-26 18:27:41 -0700 | [diff] [blame^] | 167 | logger.Errorw("mac-address-not-set", log.Fields{"deviceId": device.Id, "serial-number": sn}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 168 | return nil, errors.New("mac-address-not-set") |
| 169 | } |
David Bainbridge | fd27f4b | 2020-03-26 18:27:41 -0700 | [diff] [blame^] | 170 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 171 | logger.Debugw("logical-device-id", log.Fields{"logicaldeviceId": id}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 172 | |
David Bainbridge | fd27f4b | 2020-03-26 18:27:41 -0700 | [diff] [blame^] | 173 | agent := newLogicalDeviceAgent(id, sn, device.Id, ldMgr, ldMgr.deviceMgr, ldMgr.clusterDataProxy, ldMgr.defaultTimeout) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 174 | ldMgr.addLogicalDeviceAgentToMap(agent) |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 175 | |
| 176 | // Update the root device with the logical device Id reference |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 177 | if err := ldMgr.deviceMgr.setParentID(ctx, device, id); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 178 | logger.Errorw("failed-setting-parent-id", log.Fields{"logicalDeviceId": id, "deviceId": device.Id}) |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 179 | return nil, err |
| 180 | } |
| 181 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 182 | go func() { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 183 | //agent := newLogicalDeviceAgent(id, device.Id, ldMgr, ldMgr.deviceMgr, ldMgr.clusterDataProxy, ldMgr.defaultTimeout) |
| 184 | err := agent.start(context.Background(), false) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 185 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 186 | logger.Errorw("unable-to-create-the-logical-device", log.Fields{"error": err}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 187 | ldMgr.deleteLogicalDeviceAgent(id) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 188 | } |
| 189 | }() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 190 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 191 | logger.Debug("creating-logical-device-ends") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 192 | return &id, nil |
| 193 | } |
| 194 | |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 195 | // stopManagingLogicalDeviceWithDeviceId stops the management of the logical device. This implies removal of any |
| 196 | // reference of this logical device in cache. The device Id is passed as param because the logical device may already |
| 197 | // have been removed from the model. This function returns the logical device Id if found |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 198 | func (ldMgr *LogicalDeviceManager) stopManagingLogicalDeviceWithDeviceID(ctx context.Context, id string) string { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 199 | logger.Infow("stop-managing-logical-device", log.Fields{"deviceId": id}) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 200 | // Go over the list of logical device agents to find the one which has rootDeviceId as id |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 201 | var ldID = "" |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 202 | ldMgr.logicalDeviceAgents.Range(func(key, value interface{}) bool { |
| 203 | ldAgent := value.(*LogicalDeviceAgent) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 204 | if ldAgent.rootDeviceID == id { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 205 | logger.Infow("stopping-logical-device-agent", log.Fields{"lDeviceId": key}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 206 | if err := ldAgent.stop(ctx); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 207 | logger.Errorw("failed-to-stop-LDAgent", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 208 | return false |
| 209 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 210 | ldID = key.(string) |
| 211 | ldMgr.logicalDeviceAgents.Delete(ldID) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 212 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 213 | return true |
| 214 | }) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 215 | return ldID |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | //getLogicalDeviceFromModel retrieves the logical device data from the model. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 219 | func (ldMgr *LogicalDeviceManager) getLogicalDeviceFromModel(ctx context.Context, lDeviceID string) (*voltha.LogicalDevice, error) { |
| 220 | logicalDevice, err := ldMgr.clusterDataProxy.Get(ctx, "/logical_devices/"+lDeviceID, 0, false, "") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 221 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 222 | logger.Errorw("failed-to-get-logical-devices-from-cluster-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 223 | return nil, err |
| 224 | } |
| 225 | if logicalDevice != nil { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 226 | if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok { |
| 227 | return lDevice, nil |
| 228 | } |
| 229 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 230 | return nil, status.Error(codes.NotFound, lDeviceID) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 231 | } |
| 232 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 233 | // load loads a logical device manager in memory |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 234 | func (ldMgr *LogicalDeviceManager) load(ctx context.Context, lDeviceID string) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 235 | if lDeviceID == "" { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 236 | return nil |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 237 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 238 | // Add a lock to prevent two concurrent calls from loading the same device twice |
| 239 | ldMgr.logicalDevicesLoadingLock.Lock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 240 | if _, exist := ldMgr.logicalDeviceLoadingInProgress[lDeviceID]; !exist { |
| 241 | if ldAgent, _ := ldMgr.logicalDeviceAgents.Load(lDeviceID); ldAgent == nil { |
| 242 | ldMgr.logicalDeviceLoadingInProgress[lDeviceID] = []chan int{make(chan int, 1)} |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 243 | ldMgr.logicalDevicesLoadingLock.Unlock() |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 244 | if _, err := ldMgr.getLogicalDeviceFromModel(ctx, lDeviceID); err == nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 245 | logger.Debugw("loading-logical-device", log.Fields{"lDeviceId": lDeviceID}) |
David Bainbridge | fd27f4b | 2020-03-26 18:27:41 -0700 | [diff] [blame^] | 246 | agent := newLogicalDeviceAgent(lDeviceID, "", "", ldMgr, ldMgr.deviceMgr, ldMgr.clusterDataProxy, ldMgr.defaultTimeout) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 247 | if err := agent.start(ctx, true); err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 248 | return err |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 249 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 250 | ldMgr.logicalDeviceAgents.Store(agent.logicalDeviceID, agent) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 251 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 252 | logger.Debugw("logicalDevice not in model", log.Fields{"lDeviceId": lDeviceID}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 253 | } |
| 254 | // announce completion of task to any number of waiting channels |
| 255 | ldMgr.logicalDevicesLoadingLock.Lock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 256 | if v, ok := ldMgr.logicalDeviceLoadingInProgress[lDeviceID]; ok { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 257 | for _, ch := range v { |
| 258 | close(ch) |
| 259 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 260 | delete(ldMgr.logicalDeviceLoadingInProgress, lDeviceID) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 261 | } |
| 262 | ldMgr.logicalDevicesLoadingLock.Unlock() |
| 263 | } else { |
| 264 | ldMgr.logicalDevicesLoadingLock.Unlock() |
| 265 | } |
| 266 | } else { |
| 267 | ch := make(chan int, 1) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 268 | ldMgr.logicalDeviceLoadingInProgress[lDeviceID] = append(ldMgr.logicalDeviceLoadingInProgress[lDeviceID], ch) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 269 | ldMgr.logicalDevicesLoadingLock.Unlock() |
| 270 | // Wait for the channel to be closed, implying the process loading this device is done. |
| 271 | <-ch |
| 272 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 273 | if _, exist := ldMgr.logicalDeviceAgents.Load(lDeviceID); exist { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 274 | return nil |
| 275 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 276 | return status.Errorf(codes.Aborted, "Error loading logical device %s", lDeviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 277 | } |
| 278 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 279 | func (ldMgr *LogicalDeviceManager) deleteLogicalDevice(ctx context.Context, device *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 280 | logger.Debugw("deleting-logical-device", log.Fields{"deviceId": device.Id}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 281 | // Sanity check |
| 282 | if !device.Root { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 283 | return errors.New("device-not-root") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 284 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 285 | logDeviceID := device.ParentId |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 286 | if agent := ldMgr.getLogicalDeviceAgent(ctx, logDeviceID); agent != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 287 | // Stop the logical device agent |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 288 | if err := agent.stop(ctx); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 289 | logger.Errorw("failed-to-stop-agent", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 290 | return err |
| 291 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 292 | //Remove the logical device agent from the Map |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 293 | ldMgr.deleteLogicalDeviceAgent(logDeviceID) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 294 | } |
| 295 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 296 | logger.Debug("deleting-logical-device-ends") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 297 | return nil |
| 298 | } |
| 299 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 300 | func (ldMgr *LogicalDeviceManager) getLogicalDeviceID(ctx context.Context, device *voltha.Device) (*string, error) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 301 | // Device can either be a parent or a child device |
| 302 | if device.Root { |
| 303 | // Parent device. The ID of a parent device is the logical device ID |
| 304 | return &device.ParentId, nil |
| 305 | } |
| 306 | // Device is child device |
| 307 | // retrieve parent device using child device ID |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 308 | if parentDevice := ldMgr.deviceMgr.getParentDevice(ctx, device); parentDevice != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 309 | return &parentDevice.ParentId, nil |
| 310 | } |
| 311 | return nil, status.Errorf(codes.NotFound, "%s", device.Id) |
| 312 | } |
| 313 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 314 | func (ldMgr *LogicalDeviceManager) getLogicalDeviceIDFromDeviceID(ctx context.Context, deviceID string) (*string, error) { |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 315 | // Get the device |
| 316 | var device *voltha.Device |
| 317 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 318 | if device, err = ldMgr.deviceMgr.GetDevice(ctx, deviceID); err != nil { |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 319 | return nil, err |
| 320 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 321 | return ldMgr.getLogicalDeviceID(ctx, device) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 322 | } |
| 323 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 324 | func (ldMgr *LogicalDeviceManager) getLogicalPortID(ctx context.Context, device *voltha.Device) (*voltha.LogicalPortId, error) { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 325 | // Get the logical device where this device is attached |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 326 | var lDeviceID *string |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 327 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 328 | if lDeviceID, err = ldMgr.getLogicalDeviceID(ctx, device); err != nil { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 329 | return nil, err |
| 330 | } |
| 331 | var lDevice *voltha.LogicalDevice |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 332 | if lDevice, err = ldMgr.getLogicalDevice(ctx, *lDeviceID); err != nil { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 333 | return nil, err |
| 334 | } |
| 335 | // Go over list of ports |
| 336 | for _, port := range lDevice.Ports { |
| 337 | if port.DeviceId == device.Id { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 338 | return &voltha.LogicalPortId{Id: *lDeviceID, PortId: port.Id}, nil |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | return nil, status.Errorf(codes.NotFound, "%s", device.Id) |
| 342 | } |
| 343 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 344 | // ListLogicalDeviceFlows returns the flows of logical device |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 345 | func (ldMgr *LogicalDeviceManager) ListLogicalDeviceFlows(ctx context.Context, id string) (*openflow_13.Flows, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 346 | logger.Debugw("ListLogicalDeviceFlows", log.Fields{"logicaldeviceid": id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 347 | if agent := ldMgr.getLogicalDeviceAgent(ctx, id); agent != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 348 | return agent.ListLogicalDeviceFlows(ctx) |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 349 | } |
| 350 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 351 | } |
| 352 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 353 | // ListLogicalDeviceFlowGroups returns logical device flow groups |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 354 | func (ldMgr *LogicalDeviceManager) ListLogicalDeviceFlowGroups(ctx context.Context, id string) (*openflow_13.FlowGroups, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 355 | logger.Debugw("ListLogicalDeviceFlowGroups", log.Fields{"logicaldeviceid": id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 356 | if agent := ldMgr.getLogicalDeviceAgent(ctx, id); agent != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 357 | return agent.ListLogicalDeviceFlowGroups(ctx) |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 358 | } |
| 359 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 360 | } |
| 361 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 362 | // ListLogicalDevicePorts returns logical device ports |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 363 | func (ldMgr *LogicalDeviceManager) ListLogicalDevicePorts(ctx context.Context, id string) (*voltha.LogicalPorts, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 364 | logger.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 365 | if agent := ldMgr.getLogicalDeviceAgent(ctx, id); agent != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 366 | return agent.ListLogicalDevicePorts(ctx) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 367 | } |
| 368 | return nil, status.Errorf(codes.NotFound, "%s", id) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 369 | } |
| 370 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 371 | func (ldMgr *LogicalDeviceManager) getLogicalPort(ctx context.Context, lPortID *voltha.LogicalPortId) (*voltha.LogicalPort, error) { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 372 | // Get the logical device where this device is attached |
| 373 | var err error |
| 374 | var lDevice *voltha.LogicalDevice |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 375 | if lDevice, err = ldMgr.getLogicalDevice(ctx, lPortID.Id); err != nil { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 376 | return nil, err |
| 377 | } |
| 378 | // Go over list of ports |
| 379 | for _, port := range lDevice.Ports { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 380 | if port.Id == lPortID.PortId { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 381 | return port, nil |
| 382 | } |
| 383 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 384 | return nil, status.Errorf(codes.NotFound, "%s-%s", lPortID.Id, lPortID.PortId) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 385 | } |
| 386 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 387 | // updateLogicalPort sets up a logical port on the logical device based on the device port |
| 388 | // information, if needed |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 389 | func (ldMgr *LogicalDeviceManager) updateLogicalPort(ctx context.Context, device *voltha.Device, port *voltha.Port) error { |
| 390 | ldID, err := ldMgr.getLogicalDeviceID(ctx, device) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 391 | if err != nil || *ldID == "" { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 392 | // This is not an error as the logical device may not have been created at this time. In such a case, |
| 393 | // the ports will be created when the logical device is ready. |
| 394 | return nil |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 395 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 396 | if agent := ldMgr.getLogicalDeviceAgent(ctx, *ldID); agent != nil { |
| 397 | if err := agent.updateLogicalPort(ctx, device, port); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 398 | return err |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | return nil |
| 402 | } |
| 403 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 404 | // deleteLogicalPort removes the logical port associated with a device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 405 | func (ldMgr *LogicalDeviceManager) deleteLogicalPort(ctx context.Context, lPortID *voltha.LogicalPortId) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 406 | logger.Debugw("deleting-logical-port", log.Fields{"LDeviceId": lPortID.Id}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 407 | // Get logical port |
| 408 | var logicalPort *voltha.LogicalPort |
| 409 | var err error |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 410 | if logicalPort, err = ldMgr.getLogicalPort(ctx, lPortID); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 411 | logger.Debugw("no-logical-device-port-present", log.Fields{"logicalPortId": lPortID.PortId}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 412 | return err |
| 413 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 414 | // Sanity check |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 415 | if logicalPort.RootPort { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 416 | return errors.New("device-root") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 417 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 418 | if agent := ldMgr.getLogicalDeviceAgent(ctx, lPortID.Id); agent != nil { |
| 419 | if err := agent.deleteLogicalPort(ctx, logicalPort); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 420 | logger.Warnw("deleting-logicalport-failed", log.Fields{"LDeviceId": lPortID.Id, "error": err}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 421 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 422 | } |
| 423 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 424 | logger.Debug("deleting-logical-port-ends") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 425 | return nil |
| 426 | } |
| 427 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 428 | // deleteLogicalPort removes the logical port associated with a child device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 429 | func (ldMgr *LogicalDeviceManager) deleteLogicalPorts(ctx context.Context, deviceID string) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 430 | logger.Debugw("deleting-logical-ports", log.Fields{"deviceId": deviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 431 | // Get logical port |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 432 | ldID, err := ldMgr.getLogicalDeviceIDFromDeviceID(ctx, deviceID) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 433 | if err != nil { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 434 | return err |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 435 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 436 | if agent := ldMgr.getLogicalDeviceAgent(ctx, *ldID); agent != nil { |
| 437 | if err = agent.deleteLogicalPorts(ctx, deviceID); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 438 | logger.Warnw("deleteLogicalPorts-failed", log.Fields{"ldeviceId": *ldID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 439 | return err |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 440 | } |
| 441 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 442 | logger.Debug("deleting-logical-port-ends") |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 443 | return nil |
| 444 | } |
| 445 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 446 | func (ldMgr *LogicalDeviceManager) setupUNILogicalPorts(ctx context.Context, childDevice *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 447 | logger.Debugw("setupUNILogicalPorts", log.Fields{"childDeviceId": childDevice.Id, "parentDeviceId": childDevice.ParentId, "current-data": childDevice}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 448 | // Sanity check |
| 449 | if childDevice.Root { |
| 450 | return errors.New("Device-root") |
| 451 | } |
| 452 | |
| 453 | // Get the logical device id parent device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 454 | parentID := childDevice.ParentId |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 455 | logDeviceID := ldMgr.deviceMgr.GetParentDeviceID(ctx, parentID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 456 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 457 | logger.Debugw("setupUNILogicalPorts", log.Fields{"logDeviceId": logDeviceID, "parentId": parentID}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 458 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 459 | if parentID == "" || logDeviceID == "" { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 460 | return errors.New("device-in-invalid-state") |
khenaidoo | 5e677ae | 2019-02-28 17:26:29 -0500 | [diff] [blame] | 461 | } |
| 462 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 463 | if agent := ldMgr.getLogicalDeviceAgent(ctx, logDeviceID); agent != nil { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 464 | if err := agent.setupUNILogicalPorts(ctx, childDevice); err != nil { |
khenaidoo | bcf205b | 2019-01-25 22:21:14 -0500 | [diff] [blame] | 465 | return err |
| 466 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 467 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 468 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 469 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 470 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 471 | func (ldMgr *LogicalDeviceManager) deleteAllLogicalPorts(ctx context.Context, device *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 472 | logger.Debugw("deleteAllLogicalPorts", log.Fields{"deviceId": device.Id}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 473 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 474 | var ldID *string |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 475 | var err error |
| 476 | //Get the logical device Id for this device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 477 | if ldID, err = ldMgr.getLogicalDeviceID(ctx, device); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 478 | logger.Warnw("no-logical-device-found", log.Fields{"deviceId": device.Id, "error": err}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 479 | return err |
| 480 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 481 | if agent := ldMgr.getLogicalDeviceAgent(ctx, *ldID); agent != nil { |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 482 | if err := agent.deleteAllLogicalPorts(ctx); err != nil { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 483 | return err |
| 484 | } |
| 485 | } |
| 486 | return nil |
| 487 | } |
Hardik Windlass | c704def | 2020-02-26 18:23:19 +0000 | [diff] [blame] | 488 | |
| 489 | func (ldMgr *LogicalDeviceManager) deleteAllUNILogicalPorts(ctx context.Context, parentDevice *voltha.Device) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 490 | logger.Debugw("delete-all-uni-logical-ports", log.Fields{"parent-device-id": parentDevice.Id}) |
Hardik Windlass | c704def | 2020-02-26 18:23:19 +0000 | [diff] [blame] | 491 | |
| 492 | var ldID *string |
| 493 | var err error |
| 494 | //Get the logical device Id for this device |
| 495 | if ldID, err = ldMgr.getLogicalDeviceID(ctx, parentDevice); err != nil { |
| 496 | return err |
| 497 | } |
| 498 | if agent := ldMgr.getLogicalDeviceAgent(ctx, *ldID); agent != nil { |
| 499 | if err := agent.deleteAllUNILogicalPorts(ctx, parentDevice); err != nil { |
| 500 | return err |
| 501 | } |
| 502 | } |
| 503 | return nil |
| 504 | } |
| 505 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 506 | func (ldMgr *LogicalDeviceManager) updatePortState(ctx context.Context, deviceID string, portNo uint32, state voltha.OperStatus_Types) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 507 | logger.Debugw("updatePortState", log.Fields{"deviceId": deviceID, "state": state, "portNo": portNo}) |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 508 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 509 | var ldID *string |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 510 | var err error |
| 511 | //Get the logical device Id for this device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 512 | if ldID, err = ldMgr.getLogicalDeviceIDFromDeviceID(ctx, deviceID); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 513 | logger.Warnw("no-logical-device-found", log.Fields{"deviceId": deviceID, "error": err}) |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 514 | return err |
| 515 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 516 | if agent := ldMgr.getLogicalDeviceAgent(ctx, *ldID); agent != nil { |
| 517 | if err := agent.updatePortState(ctx, deviceID, portNo, state); err != nil { |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 518 | return err |
| 519 | } |
| 520 | } |
| 521 | return nil |
| 522 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 523 | |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 524 | func (ldMgr *LogicalDeviceManager) updatePortsState(ctx context.Context, device *voltha.Device, state voltha.OperStatus_Types) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 525 | logger.Debugw("updatePortsState", log.Fields{"deviceId": device.Id, "state": state, "current-data": device}) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 526 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 527 | var ldID *string |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 528 | var err error |
| 529 | //Get the logical device Id for this device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 530 | if ldID, err = ldMgr.getLogicalDeviceID(ctx, device); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 531 | logger.Warnw("no-logical-device-found", log.Fields{"deviceId": device.Id, "error": err}) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 532 | return err |
| 533 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 534 | if agent := ldMgr.getLogicalDeviceAgent(ctx, *ldID); agent != nil { |
| 535 | if err := agent.updatePortsState(ctx, device, state); err != nil { |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 536 | return err |
| 537 | } |
| 538 | } |
| 539 | return nil |
| 540 | } |
| 541 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 542 | func (ldMgr *LogicalDeviceManager) updateFlowTable(ctx context.Context, id string, flow *openflow_13.OfpFlowMod, ch chan interface{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 543 | logger.Debugw("updateFlowTable", log.Fields{"logicalDeviceId": id}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 544 | var res interface{} |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 545 | if agent := ldMgr.getLogicalDeviceAgent(ctx, id); agent != nil { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 546 | res = agent.updateFlowTable(ctx, flow) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 547 | logger.Debugw("updateFlowTable-result", log.Fields{"result": res}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 548 | } else { |
| 549 | res = status.Errorf(codes.NotFound, "%s", id) |
| 550 | } |
| 551 | sendAPIResponse(ctx, ch, res) |
| 552 | } |
| 553 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 554 | func (ldMgr *LogicalDeviceManager) updateMeterTable(ctx context.Context, id string, meter *openflow_13.OfpMeterMod, ch chan interface{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 555 | logger.Debugw("updateMeterTable", log.Fields{"logicalDeviceId": id}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 556 | var res interface{} |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 557 | if agent := ldMgr.getLogicalDeviceAgent(ctx, id); agent != nil { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 558 | res = agent.updateMeterTable(ctx, meter) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 559 | logger.Debugw("updateMeterTable-result", log.Fields{"result": res}) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 560 | } else { |
| 561 | res = status.Errorf(codes.NotFound, "%s", id) |
| 562 | } |
| 563 | sendAPIResponse(ctx, ch, res) |
| 564 | } |
| 565 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 566 | // ListLogicalDeviceMeters returns logical device meters |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 567 | func (ldMgr *LogicalDeviceManager) ListLogicalDeviceMeters(ctx context.Context, id string) (*openflow_13.Meters, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 568 | logger.Debugw("ListLogicalDeviceMeters", log.Fields{"logicalDeviceId": id}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 569 | if agent := ldMgr.getLogicalDeviceAgent(ctx, id); agent != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 570 | return agent.ListLogicalDeviceMeters(ctx) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 571 | } |
| 572 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 573 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 574 | func (ldMgr *LogicalDeviceManager) updateGroupTable(ctx context.Context, id string, groupMod *openflow_13.OfpGroupMod, ch chan interface{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 575 | logger.Debugw("updateGroupTable", log.Fields{"logicalDeviceId": id}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 576 | var res interface{} |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 577 | if agent := ldMgr.getLogicalDeviceAgent(ctx, id); agent != nil { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 578 | res = agent.updateGroupTable(ctx, groupMod) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 579 | logger.Debugw("updateGroupTable-result", log.Fields{"result": res}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 580 | } else { |
| 581 | res = status.Errorf(codes.NotFound, "%s", id) |
| 582 | } |
| 583 | sendAPIResponse(ctx, ch, res) |
| 584 | } |
| 585 | |
| 586 | func (ldMgr *LogicalDeviceManager) enableLogicalPort(ctx context.Context, id *voltha.LogicalPortId, ch chan interface{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 587 | logger.Debugw("enableLogicalPort", log.Fields{"logicalDeviceId": id}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 588 | var res interface{} |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 589 | if agent := ldMgr.getLogicalDeviceAgent(ctx, id.Id); agent != nil { |
| 590 | res = agent.enableLogicalPort(ctx, id.PortId) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 591 | logger.Debugw("enableLogicalPort-result", log.Fields{"result": res}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 592 | } else { |
| 593 | res = status.Errorf(codes.NotFound, "%s", id.Id) |
| 594 | } |
| 595 | sendAPIResponse(ctx, ch, res) |
| 596 | } |
| 597 | |
| 598 | func (ldMgr *LogicalDeviceManager) disableLogicalPort(ctx context.Context, id *voltha.LogicalPortId, ch chan interface{}) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 599 | logger.Debugw("disableLogicalPort", log.Fields{"logicalDeviceId": id}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 600 | var res interface{} |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 601 | if agent := ldMgr.getLogicalDeviceAgent(ctx, id.Id); agent != nil { |
| 602 | res = agent.disableLogicalPort(ctx, id.PortId) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 603 | logger.Debugw("disableLogicalPort-result", log.Fields{"result": res}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 604 | } else { |
| 605 | res = status.Errorf(codes.NotFound, "%s", id.Id) |
| 606 | } |
| 607 | sendAPIResponse(ctx, ch, res) |
| 608 | } |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 609 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 610 | func (ldMgr *LogicalDeviceManager) packetIn(ctx context.Context, logicalDeviceID string, port uint32, transactionID string, packet []byte) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 611 | logger.Debugw("packetIn", log.Fields{"logicalDeviceId": logicalDeviceID, "port": port}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 612 | if agent := ldMgr.getLogicalDeviceAgent(ctx, logicalDeviceID); agent != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 613 | agent.packetIn(port, transactionID, packet) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 614 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 615 | logger.Error("logical-device-not-exist", log.Fields{"logicalDeviceId": logicalDeviceID}) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 616 | } |
| 617 | return nil |
| 618 | } |