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 | */ |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 16 | |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 21 | "sync" |
| 22 | |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 23 | "github.com/gogo/protobuf/proto" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 24 | "github.com/opencord/voltha-go/db/model" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Scott Baker | 555307d | 2019-11-04 08:58:01 -0800 | [diff] [blame] | 26 | "github.com/opencord/voltha-protos/v2/go/voltha" |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 27 | "google.golang.org/grpc/codes" |
| 28 | "google.golang.org/grpc/status" |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 29 | ) |
| 30 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 31 | // DeviceAgent holds device specific information |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 32 | type DeviceAgent struct { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 33 | deviceID string |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 34 | deviceType string |
| 35 | lastData *voltha.Device |
| 36 | deviceMgr *DeviceManager |
| 37 | clusterDataProxy *model.Proxy |
| 38 | exitChannel chan int |
| 39 | lockDevice sync.RWMutex |
| 40 | } |
| 41 | |
| 42 | //newDeviceAgent creates a new device agent along as creating a unique ID for the device and set the device state to |
| 43 | //preprovisioning |
| 44 | func newDeviceAgent(device *voltha.Device, deviceMgr *DeviceManager, cdProxy *model.Proxy) *DeviceAgent { |
| 45 | var agent DeviceAgent |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 46 | agent.deviceID = device.Id |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 47 | agent.deviceType = device.Type |
| 48 | agent.lastData = device |
| 49 | agent.deviceMgr = deviceMgr |
| 50 | agent.exitChannel = make(chan int, 1) |
| 51 | agent.clusterDataProxy = cdProxy |
| 52 | agent.lockDevice = sync.RWMutex{} |
| 53 | return &agent |
| 54 | } |
| 55 | |
| 56 | // start save the device to the data model and registers for callbacks on that device |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 57 | func (agent *DeviceAgent) start(ctx context.Context, loadFromDb bool) error { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 58 | agent.lockDevice.Lock() |
| 59 | defer agent.lockDevice.Unlock() |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 60 | log.Debugw("starting-device-agent", log.Fields{"device": agent.lastData}) |
| 61 | if loadFromDb { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 62 | if device := agent.clusterDataProxy.Get(ctx, "/devices/"+agent.deviceID, 0, false, ""); device != nil { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 63 | if d, ok := device.(*voltha.Device); ok { |
| 64 | agent.lastData = proto.Clone(d).(*voltha.Device) |
| 65 | } |
| 66 | } else { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 67 | log.Errorw("failed-to-load-device", log.Fields{"deviceID": agent.deviceID}) |
| 68 | return status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 69 | } |
| 70 | log.Debugw("device-loaded-from-dB", log.Fields{"device": agent.lastData}) |
| 71 | } |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 72 | log.Debug("device-agent-started") |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 73 | return nil |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // stop stops the device agent. Not much to do for now |
| 77 | func (agent *DeviceAgent) stop(ctx context.Context) { |
| 78 | agent.lockDevice.Lock() |
| 79 | defer agent.lockDevice.Unlock() |
| 80 | log.Debug("stopping-device-agent") |
| 81 | agent.exitChannel <- 1 |
| 82 | log.Debug("device-agent-stopped") |
| 83 | } |
| 84 | |
| 85 | // GetDevice retrieves the latest device information from the data model |
| 86 | func (agent *DeviceAgent) getDevice() (*voltha.Device, error) { |
| 87 | agent.lockDevice.Lock() |
| 88 | defer agent.lockDevice.Unlock() |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 89 | if device := agent.clusterDataProxy.Get(context.Background(), "/devices/"+agent.deviceID, 0, false, ""); device != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 90 | if d, ok := device.(*voltha.Device); ok { |
| 91 | cloned := proto.Clone(d).(*voltha.Device) |
| 92 | return cloned, nil |
| 93 | } |
| 94 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 95 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // ListDevicePorts retrieves the ports information for a particular device. |
| 99 | func (agent *DeviceAgent) ListDevicePorts(ctx context.Context) (*voltha.Ports, error) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 100 | log.Debugw("ListDevicePorts", log.Fields{"id": agent.deviceID}) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 101 | ports := &voltha.Ports{} |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 102 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil { |
| 103 | ports.Items = append(ports.Items, device.GetPorts()...) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 104 | return ports, nil |
| 105 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 106 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // ListDevicePmConfigs retrieves the ports information for a particular device. |
| 110 | func (agent *DeviceAgent) ListDevicePmConfigs(ctx context.Context) (*voltha.PmConfigs, error) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 111 | log.Debugw("ListDevicePmConfigs", log.Fields{"id": agent.deviceID}) |
| 112 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 113 | return device.GetPmConfigs(), nil |
| 114 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 115 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // ListDeviceFlows retrieves the ports information for a particular device. |
| 119 | func (agent *DeviceAgent) ListDeviceFlows(ctx context.Context) (*voltha.Flows, error) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 120 | log.Debugw("ListDeviceFlows", log.Fields{"id": agent.deviceID}) |
| 121 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 122 | return device.GetFlows(), nil |
| 123 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 124 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 125 | } |
| 126 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 127 | // ListDeviceFlowGroups retrieves the ports information for a particular device. |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 128 | func (agent *DeviceAgent) ListDeviceFlowGroups(ctx context.Context) (*voltha.FlowGroups, error) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 129 | log.Debugw("ListDeviceFlowGroups", log.Fields{"id": agent.deviceID}) |
| 130 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 131 | return device.GetFlowGroups(), nil |
| 132 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 133 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // GetImageDownloadStatus retrieves the download status of an image of a particular device. |
| 137 | func (agent *DeviceAgent) GetImageDownloadStatus(ctx context.Context, imageName string) (*voltha.ImageDownload, error) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 138 | log.Debugw("GetImageDownloadStatus", log.Fields{"id": agent.deviceID}) |
| 139 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 140 | for _, img := range device.GetImageDownloads() { |
| 141 | if img.GetName() == imageName { |
| 142 | return img, nil |
| 143 | } |
| 144 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 145 | return nil, status.Errorf(codes.NotFound, "device-%s, image-%s", agent.deviceID, imageName) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 146 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 147 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // GetImageDownload retrieves the image download for a particular device. |
| 151 | func (agent *DeviceAgent) GetImageDownload(ctx context.Context, imageName string) (*voltha.ImageDownload, error) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 152 | log.Debugw("GetImageDownload", log.Fields{"id": agent.deviceID}) |
| 153 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 154 | for _, img := range device.GetImageDownloads() { |
| 155 | if img.GetName() == imageName { |
| 156 | return img, nil |
| 157 | } |
| 158 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 159 | return nil, status.Errorf(codes.NotFound, "device-%s, image-%s", agent.deviceID, imageName) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 160 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 161 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // ListImageDownloads retrieves the image downloads for a particular device. |
| 165 | func (agent *DeviceAgent) ListImageDownloads(ctx context.Context) (*voltha.ImageDownloads, error) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 166 | log.Debugw("ListImageDownloads", log.Fields{"id": agent.deviceID}) |
| 167 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 168 | return &voltha.ImageDownloads{Items: device.GetImageDownloads()}, nil |
| 169 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 170 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // GetImages retrieves the list of images for a particular device. |
| 174 | func (agent *DeviceAgent) GetImages(ctx context.Context) (*voltha.Images, error) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 175 | log.Debugw("GetImages", log.Fields{"id": agent.deviceID}) |
| 176 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 177 | return device.GetImages(), nil |
| 178 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 179 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 180 | } |