blob: dce2db712bde42c0ca3e21889e947463656dac77 [file] [log] [blame]
khenaidoob9203542018-09-17 22:56:37 -04001/*
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 */
16package core
17
18import (
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"
khenaidoo92e62c52018-10-03 14:02:54 -040028 "sync"
khenaidoob9203542018-09-17 22:56:37 -040029)
30
31type LogicalDeviceAgent struct {
khenaidoo92e62c52018-10-03 14:02:54 -040032 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
khenaidoob9203542018-09-17 22:56:37 -040040}
41
khenaidoo4d4802d2018-10-04 21:59:49 -040042func newLogicalDeviceAgent(id string, device *voltha.Device, ldeviceMgr *LogicalDeviceManager, deviceMgr *DeviceManager,
khenaidoo9a468962018-09-19 15:33:13 -040043 cdProxy *model.Proxy) *LogicalDeviceAgent {
khenaidoob9203542018-09-17 22:56:37 -040044 var agent LogicalDeviceAgent
45 agent.exitChannel = make(chan int, 1)
46 agent.logicalDeviceId = id
47 agent.rootDeviceId = device.Id
48 agent.deviceMgr = deviceMgr
khenaidoo9a468962018-09-19 15:33:13 -040049 agent.clusterDataProxy = cdProxy
khenaidoob9203542018-09-17 22:56:37 -040050 agent.ldeviceMgr = ldeviceMgr
khenaidoo92e62c52018-10-03 14:02:54 -040051 agent.lockLogicalDevice = sync.RWMutex{}
khenaidoob9203542018-09-17 22:56:37 -040052 return &agent
53}
54
khenaidoo4d4802d2018-10-04 21:59:49 -040055// start creates the logical device and add it to the data model
56func (agent *LogicalDeviceAgent) start(ctx context.Context) error {
khenaidoo92e62c52018-10-03 14:02:54 -040057 log.Infow("starting-logical_device-agent", log.Fields{"logicaldeviceId": agent.logicalDeviceId})
khenaidoob9203542018-09-17 22:56:37 -040058 //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
khenaidoo92e62c52018-10-03 14:02:54 -040074 if nniPorts, err = agent.deviceMgr.getPorts(ctx, agent.rootDeviceId, voltha.Port_ETHERNET_NNI); err != nil {
khenaidoob9203542018-09-17 22:56:37 -040075 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)
khenaidoo92e62c52018-10-03 14:02:54 -040086 lp.DeviceId = agent.rootDeviceId
khenaidoob9203542018-09-17 22:56:37 -040087 ld.Ports = append(ld.Ports, lp)
88 }
khenaidoo92e62c52018-10-03 14:02:54 -040089 agent.lockLogicalDevice.Lock()
90 defer agent.lockLogicalDevice.Unlock()
khenaidoob9203542018-09-17 22:56:37 -040091 // Save the logical device
khenaidoo9a468962018-09-19 15:33:13 -040092 if added := agent.clusterDataProxy.Add("/logical_devices", ld, ""); added == nil {
khenaidoob9203542018-09-17 22:56:37 -040093 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
khenaidoo4d4802d2018-10-04 21:59:49 -0400101// stop stops the logical devuce agent. This removes the logical device from the data model.
102func (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
khenaidoo92e62c52018-10-03 14:02:54 -0400117func (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
khenaidoo4d4802d2018-10-04 21:59:49 -0400129// 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
khenaidoo92e62c52018-10-03 14:02:54 -0400131func (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
khenaidoo4d4802d2018-10-04 21:59:49 -0400141// addUNILogicalPort creates a UNI port on the logical device that represents a child device
khenaidoob9203542018-09-17 22:56:37 -0400142func (agent *LogicalDeviceAgent) addUNILogicalPort(ctx context.Context, childDevice *voltha.Device, portNo uint32) error {
khenaidoo92e62c52018-10-03 14:02:54 -0400143 log.Infow("addUNILogicalPort-start", log.Fields{"logicalDeviceId": agent.logicalDeviceId})
khenaidoob9203542018-09-17 22:56:37 -0400144 // 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 }
khenaidoo92e62c52018-10-03 14:02:54 -0400151 agent.lockLogicalDevice.Lock()
152 defer agent.lockLogicalDevice.Unlock()
khenaidoob9203542018-09-17 22:56:37 -0400153 // Get stored logical device
khenaidoo92e62c52018-10-03 14:02:54 -0400154 if ldevice, err := agent.getLogicalDeviceWithoutLock(); err != nil {
khenaidoob9203542018-09-17 22:56:37 -0400155 return status.Error(codes.NotFound, agent.logicalDeviceId)
156 } else {
khenaidoo92e62c52018-10-03 14:02:54 -0400157 cloned := proto.Clone(ldevice).(*voltha.LogicalDevice)
158 lp := proto.Clone(portCap.Port).(*voltha.LogicalPort)
159 lp.DeviceId = childDevice.Id
khenaidoob9203542018-09-17 22:56:37 -0400160 cloned.Ports = append(cloned.Ports, lp)
khenaidoo92e62c52018-10-03 14:02:54 -0400161 return agent.updateLogicalDeviceWithoutLock(cloned)
162 }
163}
164
165//updateLogicalDeviceWithoutLock updates the model with the logical device. It clones the logicaldevice before saving it
166func (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
176func (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})
khenaidoob9203542018-09-17 22:56:37 -0400183 return nil
184 }
khenaidoo92e62c52018-10-03 14:02:54 -0400185 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
khenaidoob9203542018-09-17 22:56:37 -0400200}
201
khenaidoo4d4802d2018-10-04 21:59:49 -0400202