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" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 26 | "github.com/opencord/voltha-protos/v3/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 { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 62 | if device, err := agent.clusterDataProxy.Get(ctx, "/devices/"+agent.deviceID, 0, false, ""); err != nil { |
| 63 | log.Errorw("failed-to-get-device", log.Fields{"error": err}) |
| 64 | return err |
| 65 | } else if device != nil { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 66 | if d, ok := device.(*voltha.Device); ok { |
| 67 | agent.lastData = proto.Clone(d).(*voltha.Device) |
| 68 | } |
| 69 | } else { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 70 | log.Errorw("failed-to-load-device", log.Fields{"deviceID": agent.deviceID}) |
| 71 | return status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 72 | } |
| 73 | log.Debugw("device-loaded-from-dB", log.Fields{"device": agent.lastData}) |
| 74 | } |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 75 | log.Debug("device-agent-started") |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 76 | return nil |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // stop stops the device agent. Not much to do for now |
| 80 | func (agent *DeviceAgent) stop(ctx context.Context) { |
| 81 | agent.lockDevice.Lock() |
| 82 | defer agent.lockDevice.Unlock() |
| 83 | log.Debug("stopping-device-agent") |
| 84 | agent.exitChannel <- 1 |
| 85 | log.Debug("device-agent-stopped") |
| 86 | } |
| 87 | |
| 88 | // GetDevice retrieves the latest device information from the data model |
| 89 | func (agent *DeviceAgent) getDevice() (*voltha.Device, error) { |
| 90 | agent.lockDevice.Lock() |
| 91 | defer agent.lockDevice.Unlock() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 92 | if device, err := agent.clusterDataProxy.Get(context.Background(), "/devices/"+agent.deviceID, 0, false, ""); err != nil { |
| 93 | log.Errorw("failed-to-get-device", log.Fields{"error": err}) |
| 94 | return nil, err |
| 95 | } else if device != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 96 | if d, ok := device.(*voltha.Device); ok { |
| 97 | cloned := proto.Clone(d).(*voltha.Device) |
| 98 | return cloned, nil |
| 99 | } |
| 100 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 101 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // ListDevicePorts retrieves the ports information for a particular device. |
| 105 | func (agent *DeviceAgent) ListDevicePorts(ctx context.Context) (*voltha.Ports, error) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 106 | log.Debugw("ListDevicePorts", log.Fields{"id": agent.deviceID}) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 107 | ports := &voltha.Ports{} |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 108 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil { |
| 109 | ports.Items = append(ports.Items, device.GetPorts()...) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 110 | return ports, nil |
| 111 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 112 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // ListDevicePmConfigs retrieves the ports information for a particular device. |
| 116 | func (agent *DeviceAgent) ListDevicePmConfigs(ctx context.Context) (*voltha.PmConfigs, error) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 117 | log.Debugw("ListDevicePmConfigs", log.Fields{"id": agent.deviceID}) |
| 118 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 119 | return device.GetPmConfigs(), nil |
| 120 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 121 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // ListDeviceFlows retrieves the ports information for a particular device. |
| 125 | func (agent *DeviceAgent) ListDeviceFlows(ctx context.Context) (*voltha.Flows, error) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 126 | log.Debugw("ListDeviceFlows", log.Fields{"id": agent.deviceID}) |
| 127 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 128 | return device.GetFlows(), nil |
| 129 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 130 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 131 | } |
| 132 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 133 | // ListDeviceFlowGroups retrieves the ports information for a particular device. |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 134 | func (agent *DeviceAgent) ListDeviceFlowGroups(ctx context.Context) (*voltha.FlowGroups, error) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 135 | log.Debugw("ListDeviceFlowGroups", log.Fields{"id": agent.deviceID}) |
| 136 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 137 | return device.GetFlowGroups(), nil |
| 138 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 139 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // GetImageDownloadStatus retrieves the download status of an image of a particular device. |
| 143 | func (agent *DeviceAgent) GetImageDownloadStatus(ctx context.Context, imageName string) (*voltha.ImageDownload, error) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 144 | log.Debugw("GetImageDownloadStatus", log.Fields{"id": agent.deviceID}) |
| 145 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 146 | for _, img := range device.GetImageDownloads() { |
| 147 | if img.GetName() == imageName { |
| 148 | return img, nil |
| 149 | } |
| 150 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 151 | 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] | 152 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 153 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // GetImageDownload retrieves the image download for a particular device. |
| 157 | func (agent *DeviceAgent) GetImageDownload(ctx context.Context, imageName string) (*voltha.ImageDownload, error) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 158 | log.Debugw("GetImageDownload", log.Fields{"id": agent.deviceID}) |
| 159 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 160 | for _, img := range device.GetImageDownloads() { |
| 161 | if img.GetName() == imageName { |
| 162 | return img, nil |
| 163 | } |
| 164 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 165 | 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] | 166 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 167 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // ListImageDownloads retrieves the image downloads for a particular device. |
| 171 | func (agent *DeviceAgent) ListImageDownloads(ctx context.Context) (*voltha.ImageDownloads, error) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 172 | log.Debugw("ListImageDownloads", log.Fields{"id": agent.deviceID}) |
| 173 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 174 | return &voltha.ImageDownloads{Items: device.GetImageDownloads()}, nil |
| 175 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 176 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // GetImages retrieves the list of images for a particular device. |
| 180 | func (agent *DeviceAgent) GetImages(ctx context.Context) (*voltha.Images, error) { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 181 | log.Debugw("GetImages", log.Fields{"id": agent.deviceID}) |
| 182 | if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 183 | return device.GetImages(), nil |
| 184 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 185 | return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 186 | } |