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