Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [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 | */ |
| 16 | package core |
| 17 | |
| 18 | import ( |
| 19 | "context" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 20 | "github.com/opencord/voltha-go/db/model" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 21 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
| 22 | "github.com/opencord/voltha-lib-go/v2/pkg/probe" |
Scott Baker | 555307d | 2019-11-04 08:58:01 -0800 | [diff] [blame] | 23 | "github.com/opencord/voltha-protos/v2/go/voltha" |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 24 | "google.golang.org/grpc/codes" |
| 25 | "google.golang.org/grpc/status" |
| 26 | "sync" |
| 27 | ) |
| 28 | |
| 29 | type LogicalDeviceManager struct { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 30 | logicalDeviceAgents sync.Map |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 31 | deviceMgr *DeviceManager |
| 32 | grpcNbiHdlr *APIHandler |
| 33 | clusterDataProxy *model.Proxy |
| 34 | exitChannel chan int |
| 35 | lockLogicalDeviceAgentsMap sync.RWMutex |
| 36 | } |
| 37 | |
| 38 | func newLogicalDeviceManager(deviceMgr *DeviceManager, cdProxy *model.Proxy) *LogicalDeviceManager { |
| 39 | var logicalDeviceMgr LogicalDeviceManager |
| 40 | logicalDeviceMgr.exitChannel = make(chan int, 1) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 41 | logicalDeviceMgr.deviceMgr = deviceMgr |
| 42 | logicalDeviceMgr.clusterDataProxy = cdProxy |
| 43 | logicalDeviceMgr.lockLogicalDeviceAgentsMap = sync.RWMutex{} |
| 44 | return &logicalDeviceMgr |
| 45 | } |
| 46 | |
| 47 | func (ldMgr *LogicalDeviceManager) setGrpcNbiHandler(grpcNbiHandler *APIHandler) { |
| 48 | ldMgr.grpcNbiHdlr = grpcNbiHandler |
| 49 | } |
| 50 | |
| 51 | func (ldMgr *LogicalDeviceManager) start(ctx context.Context) { |
| 52 | log.Info("starting-logical-device-manager") |
Hardik Windlass | dc63dde | 2019-09-30 07:15:13 +0000 | [diff] [blame] | 53 | probe.UpdateStatusFromContext(ctx, "logical-device-manager", probe.ServiceStatusRunning) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 54 | log.Info("logical-device-manager-started") |
| 55 | } |
| 56 | |
| 57 | func (ldMgr *LogicalDeviceManager) stop(ctx context.Context) { |
| 58 | log.Info("stopping-logical-device-manager") |
| 59 | ldMgr.exitChannel <- 1 |
Hardik Windlass | dc63dde | 2019-09-30 07:15:13 +0000 | [diff] [blame] | 60 | probe.UpdateStatusFromContext(ctx, "logical-device-manager", probe.ServiceStatusStopped) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 61 | log.Info("logical-device-manager-stopped") |
| 62 | } |
| 63 | |
| 64 | func (ldMgr *LogicalDeviceManager) addLogicalDeviceAgentToMap(agent *LogicalDeviceAgent) { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 65 | if _, exist := ldMgr.logicalDeviceAgents.Load(agent.logicalDeviceId); !exist { |
| 66 | ldMgr.logicalDeviceAgents.Store(agent.logicalDeviceId, agent) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | func (ldMgr *LogicalDeviceManager) getLogicalDeviceAgent(logicalDeviceId string) *LogicalDeviceAgent { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 71 | if agent, ok := ldMgr.logicalDeviceAgents.Load(logicalDeviceId); ok { |
| 72 | //return agent |
| 73 | return agent.(*LogicalDeviceAgent) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 74 | } |
| 75 | return nil |
| 76 | } |
| 77 | |
| 78 | func (ldMgr *LogicalDeviceManager) deleteLogicalDeviceAgent(logicalDeviceId string) { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 79 | ldMgr.logicalDeviceAgents.Delete(logicalDeviceId) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // GetLogicalDevice provides a cloned most up to date logical device |
| 83 | func (ldMgr *LogicalDeviceManager) getLogicalDevice(id string) (*voltha.LogicalDevice, error) { |
| 84 | log.Debugw("getlogicalDevice", log.Fields{"logicaldeviceid": id}) |
| 85 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
| 86 | return agent.GetLogicalDevice() |
| 87 | } |
| 88 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 89 | } |
| 90 | |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 91 | func (ldMgr *LogicalDeviceManager) IsLogicalDeviceInCache(id string) bool { |
| 92 | ldMgr.lockLogicalDeviceAgentsMap.Lock() |
| 93 | defer ldMgr.lockLogicalDeviceAgentsMap.Unlock() |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 94 | _, exist := ldMgr.logicalDeviceAgents.Load(id) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 95 | return exist |
| 96 | } |
| 97 | |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 98 | func (ldMgr *LogicalDeviceManager) listLogicalDevices() (*voltha.LogicalDevices, error) { |
| 99 | log.Debug("ListAllLogicalDevices") |
| 100 | result := &voltha.LogicalDevices{} |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 101 | if logicalDevices := ldMgr.clusterDataProxy.List(context.Background(), "/logical_devices", 0, false, |
| 102 | ""); logicalDevices != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 103 | for _, logicalDevice := range logicalDevices.([]interface{}) { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 104 | // If device is not in memory then set it up |
| 105 | if !ldMgr.IsLogicalDeviceInCache(logicalDevice.(*voltha.LogicalDevice).Id) { |
| 106 | agent := newLogicalDeviceAgent( |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 107 | logicalDevice.(*voltha.LogicalDevice).Id, |
| 108 | logicalDevice.(*voltha.LogicalDevice).RootDeviceId, |
| 109 | ldMgr, |
| 110 | ldMgr.deviceMgr, |
| 111 | ldMgr.clusterDataProxy, |
| 112 | ) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 113 | if err := agent.start(nil, true); err != nil { |
| 114 | log.Warnw("failure-starting-agent", log.Fields{"logicalDeviceId": logicalDevice.(*voltha.LogicalDevice).Id}) |
| 115 | agent.stop(nil) |
| 116 | } else { |
| 117 | ldMgr.addLogicalDeviceAgentToMap(agent) |
| 118 | } |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 119 | } |
| 120 | result.Items = append(result.Items, logicalDevice.(*voltha.LogicalDevice)) |
| 121 | } |
| 122 | } |
| 123 | return result, nil |
| 124 | } |
| 125 | |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 126 | // load loads a logical device manager in memory |
| 127 | func (ldMgr *LogicalDeviceManager) load(lDeviceId string) error { |
| 128 | log.Debugw("loading-logical-device", log.Fields{"lDeviceId": lDeviceId}) |
| 129 | // To prevent a race condition, let's hold the logical device agent map lock. This will prevent a loading and |
| 130 | // a create logical device callback from occurring at the same time. |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 131 | if ldAgent, _ := ldMgr.logicalDeviceAgents.Load(lDeviceId); ldAgent == nil { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 132 | // Logical device not in memory - create a temp logical device Agent and let it load from memory |
| 133 | agent := newLogicalDeviceAgent(lDeviceId, "", ldMgr, ldMgr.deviceMgr, ldMgr.clusterDataProxy) |
| 134 | if err := agent.start(nil, true); err != nil { |
| 135 | agent.stop(nil) |
| 136 | return err |
| 137 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 138 | ldMgr.logicalDeviceAgents.Store(agent.logicalDeviceId, agent) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 139 | } |
| 140 | // TODO: load the child device |
| 141 | return nil |
| 142 | } |
| 143 | |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 144 | func (ldMgr *LogicalDeviceManager) getLogicalDeviceId(device *voltha.Device) (*string, error) { |
| 145 | // Device can either be a parent or a child device |
| 146 | if device.Root { |
| 147 | // Parent device. The ID of a parent device is the logical device ID |
| 148 | return &device.ParentId, nil |
| 149 | } |
| 150 | // Device is child device |
| 151 | // retrieve parent device using child device ID |
| 152 | if parentDevice := ldMgr.deviceMgr.getParentDevice(device); parentDevice != nil { |
| 153 | return &parentDevice.ParentId, nil |
| 154 | } |
| 155 | return nil, status.Errorf(codes.NotFound, "%s", device.Id) |
| 156 | } |
| 157 | |
| 158 | func (ldMgr *LogicalDeviceManager) getLogicalPortId(device *voltha.Device) (*voltha.LogicalPortId, error) { |
| 159 | // Get the logical device where this device is attached |
| 160 | var lDeviceId *string |
| 161 | var err error |
| 162 | if lDeviceId, err = ldMgr.getLogicalDeviceId(device); err != nil { |
| 163 | return nil, err |
| 164 | } |
| 165 | var lDevice *voltha.LogicalDevice |
| 166 | if lDevice, err = ldMgr.getLogicalDevice(*lDeviceId); err != nil { |
| 167 | return nil, err |
| 168 | } |
| 169 | // Go over list of ports |
| 170 | for _, port := range lDevice.Ports { |
| 171 | if port.DeviceId == device.Id { |
| 172 | return &voltha.LogicalPortId{Id: *lDeviceId, PortId: port.Id}, nil |
| 173 | } |
| 174 | } |
| 175 | return nil, status.Errorf(codes.NotFound, "%s", device.Id) |
| 176 | } |
| 177 | |
| 178 | func (ldMgr *LogicalDeviceManager) ListLogicalDevicePorts(ctx context.Context, id string) (*voltha.LogicalPorts, error) { |
| 179 | log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id}) |
| 180 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
| 181 | return agent.ListLogicalDevicePorts() |
| 182 | } |
| 183 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 184 | |
| 185 | } |
| 186 | |
| 187 | func (ldMgr *LogicalDeviceManager) ListLogicalDeviceFlows(ctx context.Context, id string) (*voltha.Flows, error) { |
| 188 | log.Debugw("ListLogicalDeviceFlows", log.Fields{"logicaldeviceid": id}) |
| 189 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
| 190 | return agent.ListLogicalDeviceFlows() |
| 191 | } |
| 192 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 193 | |
| 194 | } |
| 195 | |
| 196 | func (ldMgr *LogicalDeviceManager) ListLogicalDeviceFlowGroups(ctx context.Context, id string) (*voltha.FlowGroups, error) { |
| 197 | log.Debugw("ListLogicalDeviceFlowGroups", log.Fields{"logicaldeviceid": id}) |
| 198 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
| 199 | return agent.ListLogicalDeviceFlowGroups() |
| 200 | } |
| 201 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 202 | |
| 203 | } |
| 204 | |
| 205 | func (ldMgr *LogicalDeviceManager) getLogicalPort(lPortId *voltha.LogicalPortId) (*voltha.LogicalPort, error) { |
| 206 | // Get the logical device where this device is attached |
| 207 | var err error |
| 208 | var lDevice *voltha.LogicalDevice |
| 209 | if lDevice, err = ldMgr.getLogicalDevice(lPortId.Id); err != nil { |
| 210 | return nil, err |
| 211 | } |
| 212 | // Go over list of ports |
| 213 | for _, port := range lDevice.Ports { |
| 214 | if port.Id == lPortId.PortId { |
| 215 | return port, nil |
| 216 | } |
| 217 | } |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 218 | return nil, status.Errorf(codes.NotFound, "%s-%s", lPortId.Id, lPortId.PortId) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 219 | } |