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 | */ |
| 16 | package core |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "github.com/gogo/protobuf/proto" |
| 21 | "github.com/opencord/voltha-go/common/log" |
| 22 | "github.com/opencord/voltha-go/db/model" |
| 23 | ca "github.com/opencord/voltha-go/protos/core_adapter" |
| 24 | "github.com/opencord/voltha-go/protos/openflow_13" |
| 25 | "github.com/opencord/voltha-go/protos/voltha" |
| 26 | "google.golang.org/grpc/codes" |
| 27 | "google.golang.org/grpc/status" |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 28 | "sync" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | type LogicalDeviceAgent struct { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 32 | logicalDeviceId string |
| 33 | lastData *voltha.LogicalDevice |
| 34 | rootDeviceId string |
| 35 | deviceMgr *DeviceManager |
| 36 | ldeviceMgr *LogicalDeviceManager |
| 37 | clusterDataProxy *model.Proxy |
| 38 | exitChannel chan int |
| 39 | lockLogicalDevice sync.RWMutex |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 40 | } |
| 41 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame^] | 42 | func newLogicalDeviceAgent(id string, device *voltha.Device, ldeviceMgr *LogicalDeviceManager, deviceMgr *DeviceManager, |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 43 | cdProxy *model.Proxy) *LogicalDeviceAgent { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 44 | var agent LogicalDeviceAgent |
| 45 | agent.exitChannel = make(chan int, 1) |
| 46 | agent.logicalDeviceId = id |
| 47 | agent.rootDeviceId = device.Id |
| 48 | agent.deviceMgr = deviceMgr |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 49 | agent.clusterDataProxy = cdProxy |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 50 | agent.ldeviceMgr = ldeviceMgr |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 51 | agent.lockLogicalDevice = sync.RWMutex{} |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 52 | return &agent |
| 53 | } |
| 54 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame^] | 55 | // start creates the logical device and add it to the data model |
| 56 | func (agent *LogicalDeviceAgent) start(ctx context.Context) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 57 | log.Infow("starting-logical_device-agent", log.Fields{"logicaldeviceId": agent.logicalDeviceId}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 58 | //Build the logical device based on information retrieved from the device adapter |
| 59 | var switchCap *ca.SwitchCapability |
| 60 | var err error |
| 61 | if switchCap, err = agent.deviceMgr.getSwitchCapability(ctx, agent.rootDeviceId); err != nil { |
| 62 | log.Errorw("error-creating-logical-device", log.Fields{"error": err}) |
| 63 | return err |
| 64 | } |
| 65 | ld := &voltha.LogicalDevice{Id: agent.logicalDeviceId, RootDeviceId: agent.rootDeviceId} |
| 66 | ld.Desc = (proto.Clone(switchCap.Desc)).(*openflow_13.OfpDesc) |
| 67 | ld.SwitchFeatures = (proto.Clone(switchCap.SwitchFeatures)).(*openflow_13.OfpSwitchFeatures) |
| 68 | |
| 69 | //Add logical ports to the logical device based on the number of NNI ports discovered |
| 70 | //First get the default port capability - TODO: each NNI port may have different capabilities, |
| 71 | //hence. may need to extract the port by the NNI port id defined by the adapter during device |
| 72 | //creation |
| 73 | var nniPorts *voltha.Ports |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 74 | if nniPorts, err = agent.deviceMgr.getPorts(ctx, agent.rootDeviceId, voltha.Port_ETHERNET_NNI); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 75 | log.Errorw("error-creating-logical-port", log.Fields{"error": err}) |
| 76 | } |
| 77 | var portCap *ca.PortCapability |
| 78 | for _, port := range nniPorts.Items { |
| 79 | log.Infow("NNI PORTS", log.Fields{"NNI": port}) |
| 80 | if portCap, err = agent.deviceMgr.getPortCapability(ctx, agent.rootDeviceId, port.PortNo); err != nil { |
| 81 | log.Errorw("error-creating-logical-device", log.Fields{"error": err}) |
| 82 | return err |
| 83 | } |
| 84 | |
| 85 | lp := (proto.Clone(portCap.Port)).(*voltha.LogicalPort) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 86 | lp.DeviceId = agent.rootDeviceId |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 87 | ld.Ports = append(ld.Ports, lp) |
| 88 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 89 | agent.lockLogicalDevice.Lock() |
| 90 | defer agent.lockLogicalDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 91 | // Save the logical device |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 92 | if added := agent.clusterDataProxy.Add("/logical_devices", ld, ""); added == nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 93 | log.Errorw("failed-to-add-logical-device", log.Fields{"logicaldeviceId": agent.logicalDeviceId}) |
| 94 | } else { |
| 95 | log.Debugw("logicaldevice-created", log.Fields{"logicaldeviceId": agent.logicalDeviceId}) |
| 96 | } |
| 97 | |
| 98 | return nil |
| 99 | } |
| 100 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame^] | 101 | // stop stops the logical devuce agent. This removes the logical device from the data model. |
| 102 | func (agent *LogicalDeviceAgent) stop(ctx context.Context) { |
| 103 | log.Info("stopping-logical_device-agent") |
| 104 | agent.lockLogicalDevice.Lock() |
| 105 | defer agent.lockLogicalDevice.Unlock() |
| 106 | //Remove the logical device from the model |
| 107 | if removed := agent.clusterDataProxy.Remove("/logical_devices/"+agent.logicalDeviceId, ""); removed == nil { |
| 108 | log.Errorw("failed-to-remove-logical-device", log.Fields{"logicaldeviceId": agent.logicalDeviceId}) |
| 109 | } else { |
| 110 | log.Debugw("logicaldevice-removed", log.Fields{"logicaldeviceId": agent.logicalDeviceId}) |
| 111 | } |
| 112 | agent.exitChannel <- 1 |
| 113 | log.Info("logical_device-agent-stopped") |
| 114 | } |
| 115 | |
| 116 | // getLogicalDevice locks the logical device model and then retrieves the latest logical device information |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 117 | func (agent *LogicalDeviceAgent) getLogicalDevice() (*voltha.LogicalDevice, error) { |
| 118 | log.Debug("getLogicalDevice") |
| 119 | agent.lockLogicalDevice.Lock() |
| 120 | defer agent.lockLogicalDevice.Unlock() |
| 121 | logicalDevice := agent.clusterDataProxy.Get("/logical_devices/"+agent.logicalDeviceId, 1, false, "") |
| 122 | if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok { |
| 123 | cloned := proto.Clone(lDevice).(*voltha.LogicalDevice) |
| 124 | return cloned, nil |
| 125 | } |
| 126 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId) |
| 127 | } |
| 128 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame^] | 129 | // getLogicalDeviceWithoutLock retrieves a logical device from the model without locking it. This is used only by |
| 130 | // functions that have already acquired the logical device lock to the model |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 131 | func (agent *LogicalDeviceAgent) getLogicalDeviceWithoutLock() (*voltha.LogicalDevice, error) { |
| 132 | log.Debug("getLogicalDeviceWithoutLock") |
| 133 | logicalDevice := agent.clusterDataProxy.Get("/logical_devices/"+agent.logicalDeviceId, 1, false, "") |
| 134 | if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok { |
| 135 | cloned := proto.Clone(lDevice).(*voltha.LogicalDevice) |
| 136 | return cloned, nil |
| 137 | } |
| 138 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId) |
| 139 | } |
| 140 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame^] | 141 | // addUNILogicalPort creates a UNI port on the logical device that represents a child device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 142 | func (agent *LogicalDeviceAgent) addUNILogicalPort(ctx context.Context, childDevice *voltha.Device, portNo uint32) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 143 | log.Infow("addUNILogicalPort-start", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 144 | // Build the logical device based on information retrieved from the device adapter |
| 145 | var portCap *ca.PortCapability |
| 146 | var err error |
| 147 | if portCap, err = agent.deviceMgr.getPortCapability(ctx, childDevice.Id, portNo); err != nil { |
| 148 | log.Errorw("error-creating-logical-port", log.Fields{"error": err}) |
| 149 | return err |
| 150 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 151 | agent.lockLogicalDevice.Lock() |
| 152 | defer agent.lockLogicalDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 153 | // Get stored logical device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 154 | if ldevice, err := agent.getLogicalDeviceWithoutLock(); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 155 | return status.Error(codes.NotFound, agent.logicalDeviceId) |
| 156 | } else { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 157 | cloned := proto.Clone(ldevice).(*voltha.LogicalDevice) |
| 158 | lp := proto.Clone(portCap.Port).(*voltha.LogicalPort) |
| 159 | lp.DeviceId = childDevice.Id |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 160 | cloned.Ports = append(cloned.Ports, lp) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 161 | return agent.updateLogicalDeviceWithoutLock(cloned) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | //updateLogicalDeviceWithoutLock updates the model with the logical device. It clones the logicaldevice before saving it |
| 166 | func (agent *LogicalDeviceAgent) updateLogicalDeviceWithoutLock(logicalDevice *voltha.LogicalDevice) error { |
| 167 | cloned := proto.Clone(logicalDevice).(*voltha.LogicalDevice) |
| 168 | afterUpdate := agent.clusterDataProxy.Update("/logical_devices/"+agent.logicalDeviceId, cloned, false, "") |
| 169 | if afterUpdate == nil { |
| 170 | return status.Errorf(codes.Internal, "failed-updating-logical-device:%s", agent.logicalDeviceId) |
| 171 | } |
| 172 | return nil |
| 173 | } |
| 174 | |
| 175 | // deleteLogicalPort removes the logical port associated with a child device |
| 176 | func (agent *LogicalDeviceAgent) deleteLogicalPort(device *voltha.Device) error { |
| 177 | agent.lockLogicalDevice.Lock() |
| 178 | defer agent.lockLogicalDevice.Unlock() |
| 179 | // Get the most up to date logical device |
| 180 | var logicaldevice *voltha.LogicalDevice |
| 181 | if logicaldevice, _ = agent.getLogicalDeviceWithoutLock(); logicaldevice == nil { |
| 182 | log.Debugw("no-logical-device", log.Fields{"logicalDeviceId": agent.logicalDeviceId, "deviceId": device.Id}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 183 | return nil |
| 184 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 185 | index := -1 |
| 186 | for i, logicalPort := range logicaldevice.Ports { |
| 187 | if logicalPort.DeviceId == device.Id { |
| 188 | index = i |
| 189 | break |
| 190 | } |
| 191 | } |
| 192 | if index >= 0 { |
| 193 | copy(logicaldevice.Ports[index:], logicaldevice.Ports[index+1:]) |
| 194 | logicaldevice.Ports[len(logicaldevice.Ports)-1] = nil |
| 195 | logicaldevice.Ports = logicaldevice.Ports[:len(logicaldevice.Ports)-1] |
| 196 | log.Debugw("logical-port-deleted", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 197 | return agent.updateLogicalDeviceWithoutLock(logicaldevice) |
| 198 | } |
| 199 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 200 | } |
| 201 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame^] | 202 | |