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 | |
| 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 | |
| 55 | func (agent *LogicalDeviceAgent) Start(ctx context.Context) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame^] | 56 | log.Infow("starting-logical_device-agent", log.Fields{"logicaldeviceId": agent.logicalDeviceId}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 57 | //Build the logical device based on information retrieved from the device adapter |
| 58 | var switchCap *ca.SwitchCapability |
| 59 | var err error |
| 60 | if switchCap, err = agent.deviceMgr.getSwitchCapability(ctx, agent.rootDeviceId); err != nil { |
| 61 | log.Errorw("error-creating-logical-device", log.Fields{"error": err}) |
| 62 | return err |
| 63 | } |
| 64 | ld := &voltha.LogicalDevice{Id: agent.logicalDeviceId, RootDeviceId: agent.rootDeviceId} |
| 65 | ld.Desc = (proto.Clone(switchCap.Desc)).(*openflow_13.OfpDesc) |
| 66 | ld.SwitchFeatures = (proto.Clone(switchCap.SwitchFeatures)).(*openflow_13.OfpSwitchFeatures) |
| 67 | |
| 68 | //Add logical ports to the logical device based on the number of NNI ports discovered |
| 69 | //First get the default port capability - TODO: each NNI port may have different capabilities, |
| 70 | //hence. may need to extract the port by the NNI port id defined by the adapter during device |
| 71 | //creation |
| 72 | var nniPorts *voltha.Ports |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame^] | 73 | 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] | 74 | log.Errorw("error-creating-logical-port", log.Fields{"error": err}) |
| 75 | } |
| 76 | var portCap *ca.PortCapability |
| 77 | for _, port := range nniPorts.Items { |
| 78 | log.Infow("NNI PORTS", log.Fields{"NNI": port}) |
| 79 | if portCap, err = agent.deviceMgr.getPortCapability(ctx, agent.rootDeviceId, port.PortNo); err != nil { |
| 80 | log.Errorw("error-creating-logical-device", log.Fields{"error": err}) |
| 81 | return err |
| 82 | } |
| 83 | |
| 84 | lp := (proto.Clone(portCap.Port)).(*voltha.LogicalPort) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame^] | 85 | lp.DeviceId = agent.rootDeviceId |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 86 | ld.Ports = append(ld.Ports, lp) |
| 87 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame^] | 88 | agent.lockLogicalDevice.Lock() |
| 89 | defer agent.lockLogicalDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 90 | // Save the logical device |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 91 | if added := agent.clusterDataProxy.Add("/logical_devices", ld, ""); added == nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 92 | log.Errorw("failed-to-add-logical-device", log.Fields{"logicaldeviceId": agent.logicalDeviceId}) |
| 93 | } else { |
| 94 | log.Debugw("logicaldevice-created", log.Fields{"logicaldeviceId": agent.logicalDeviceId}) |
| 95 | } |
| 96 | |
| 97 | return nil |
| 98 | } |
| 99 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame^] | 100 | func (agent *LogicalDeviceAgent) getLogicalDevice() (*voltha.LogicalDevice, error) { |
| 101 | log.Debug("getLogicalDevice") |
| 102 | agent.lockLogicalDevice.Lock() |
| 103 | defer agent.lockLogicalDevice.Unlock() |
| 104 | logicalDevice := agent.clusterDataProxy.Get("/logical_devices/"+agent.logicalDeviceId, 1, false, "") |
| 105 | if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok { |
| 106 | cloned := proto.Clone(lDevice).(*voltha.LogicalDevice) |
| 107 | return cloned, nil |
| 108 | } |
| 109 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId) |
| 110 | } |
| 111 | |
| 112 | func (agent *LogicalDeviceAgent) getLogicalDeviceWithoutLock() (*voltha.LogicalDevice, error) { |
| 113 | log.Debug("getLogicalDeviceWithoutLock") |
| 114 | logicalDevice := agent.clusterDataProxy.Get("/logical_devices/"+agent.logicalDeviceId, 1, false, "") |
| 115 | if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok { |
| 116 | cloned := proto.Clone(lDevice).(*voltha.LogicalDevice) |
| 117 | return cloned, nil |
| 118 | } |
| 119 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId) |
| 120 | } |
| 121 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 122 | func (agent *LogicalDeviceAgent) addUNILogicalPort(ctx context.Context, childDevice *voltha.Device, portNo uint32) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame^] | 123 | log.Infow("addUNILogicalPort-start", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 124 | // Build the logical device based on information retrieved from the device adapter |
| 125 | var portCap *ca.PortCapability |
| 126 | var err error |
| 127 | if portCap, err = agent.deviceMgr.getPortCapability(ctx, childDevice.Id, portNo); err != nil { |
| 128 | log.Errorw("error-creating-logical-port", log.Fields{"error": err}) |
| 129 | return err |
| 130 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame^] | 131 | agent.lockLogicalDevice.Lock() |
| 132 | defer agent.lockLogicalDevice.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 133 | // Get stored logical device |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame^] | 134 | if ldevice, err := agent.getLogicalDeviceWithoutLock(); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 135 | return status.Error(codes.NotFound, agent.logicalDeviceId) |
| 136 | } else { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame^] | 137 | cloned := proto.Clone(ldevice).(*voltha.LogicalDevice) |
| 138 | lp := proto.Clone(portCap.Port).(*voltha.LogicalPort) |
| 139 | lp.DeviceId = childDevice.Id |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 140 | cloned.Ports = append(cloned.Ports, lp) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame^] | 141 | return agent.updateLogicalDeviceWithoutLock(cloned) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | //updateLogicalDeviceWithoutLock updates the model with the logical device. It clones the logicaldevice before saving it |
| 146 | func (agent *LogicalDeviceAgent) updateLogicalDeviceWithoutLock(logicalDevice *voltha.LogicalDevice) error { |
| 147 | cloned := proto.Clone(logicalDevice).(*voltha.LogicalDevice) |
| 148 | afterUpdate := agent.clusterDataProxy.Update("/logical_devices/"+agent.logicalDeviceId, cloned, false, "") |
| 149 | if afterUpdate == nil { |
| 150 | return status.Errorf(codes.Internal, "failed-updating-logical-device:%s", agent.logicalDeviceId) |
| 151 | } |
| 152 | return nil |
| 153 | } |
| 154 | |
| 155 | // deleteLogicalPort removes the logical port associated with a child device |
| 156 | func (agent *LogicalDeviceAgent) deleteLogicalPort(device *voltha.Device) error { |
| 157 | agent.lockLogicalDevice.Lock() |
| 158 | defer agent.lockLogicalDevice.Unlock() |
| 159 | // Get the most up to date logical device |
| 160 | var logicaldevice *voltha.LogicalDevice |
| 161 | if logicaldevice, _ = agent.getLogicalDeviceWithoutLock(); logicaldevice == nil { |
| 162 | log.Debugw("no-logical-device", log.Fields{"logicalDeviceId": agent.logicalDeviceId, "deviceId": device.Id}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 163 | return nil |
| 164 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame^] | 165 | index := -1 |
| 166 | for i, logicalPort := range logicaldevice.Ports { |
| 167 | if logicalPort.DeviceId == device.Id { |
| 168 | index = i |
| 169 | break |
| 170 | } |
| 171 | } |
| 172 | if index >= 0 { |
| 173 | copy(logicaldevice.Ports[index:], logicaldevice.Ports[index+1:]) |
| 174 | logicaldevice.Ports[len(logicaldevice.Ports)-1] = nil |
| 175 | logicaldevice.Ports = logicaldevice.Ports[:len(logicaldevice.Ports)-1] |
| 176 | log.Debugw("logical-port-deleted", log.Fields{"logicalDeviceId": agent.logicalDeviceId}) |
| 177 | return agent.updateLogicalDeviceWithoutLock(logicaldevice) |
| 178 | } |
| 179 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | func (agent *LogicalDeviceAgent) Stop(ctx context.Context) { |
| 183 | log.Info("stopping-logical_device-agent") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame^] | 184 | agent.lockLogicalDevice.Lock() |
| 185 | defer agent.lockLogicalDevice.Unlock() |
| 186 | //Remove the logical device from the model |
| 187 | if removed := agent.clusterDataProxy.Remove("/logical_devices/"+agent.logicalDeviceId, ""); removed == nil { |
| 188 | log.Errorw("failed-to-remove-logical-device", log.Fields{"logicaldeviceId": agent.logicalDeviceId}) |
| 189 | } else { |
| 190 | log.Debugw("logicaldevice-removed", log.Fields{"logicaldeviceId": agent.logicalDeviceId}) |
| 191 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 192 | agent.exitChannel <- 1 |
| 193 | log.Info("logical_device-agent-stopped") |
| 194 | } |