blob: 345e424fae8f39a6f7107ff503bf2b117332d3d8 [file] [log] [blame]
Stephane Barbariea75791c2019-01-24 10:58:06 -05001/*
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 */
npujar03b018e2019-11-13 15:29:36 +053016
Stephane Barbariea75791c2019-01-24 10:58:06 -050017package core
18
19import (
20 "context"
npujar03b018e2019-11-13 15:29:36 +053021 "sync"
22
Stephane Barbariea75791c2019-01-24 10:58:06 -050023 "github.com/gogo/protobuf/proto"
sbarbari17d7e222019-11-05 10:02:29 -050024 "github.com/opencord/voltha-go/db/model"
Scott Baker807addd2019-10-24 15:16:21 -070025 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Scott Baker555307d2019-11-04 08:58:01 -080026 "github.com/opencord/voltha-protos/v2/go/voltha"
Stephane Barbariea75791c2019-01-24 10:58:06 -050027 "google.golang.org/grpc/codes"
28 "google.golang.org/grpc/status"
Stephane Barbariea75791c2019-01-24 10:58:06 -050029)
30
npujar03b018e2019-11-13 15:29:36 +053031// DeviceAgent holds device specific information
Stephane Barbariea75791c2019-01-24 10:58:06 -050032type DeviceAgent struct {
npujar03b018e2019-11-13 15:29:36 +053033 deviceID string
Stephane Barbariea75791c2019-01-24 10:58:06 -050034 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
44func newDeviceAgent(device *voltha.Device, deviceMgr *DeviceManager, cdProxy *model.Proxy) *DeviceAgent {
45 var agent DeviceAgent
npujar03b018e2019-11-13 15:29:36 +053046 agent.deviceID = device.Id
Stephane Barbariea75791c2019-01-24 10:58:06 -050047 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 Barbarie1e28f3e2019-02-08 15:45:20 -050057func (agent *DeviceAgent) start(ctx context.Context, loadFromDb bool) error {
Stephane Barbariea75791c2019-01-24 10:58:06 -050058 agent.lockDevice.Lock()
59 defer agent.lockDevice.Unlock()
Stephane Barbarie1e28f3e2019-02-08 15:45:20 -050060 log.Debugw("starting-device-agent", log.Fields{"device": agent.lastData})
61 if loadFromDb {
npujar03b018e2019-11-13 15:29:36 +053062 if device := agent.clusterDataProxy.Get(ctx, "/devices/"+agent.deviceID, 0, false, ""); device != nil {
Stephane Barbarie1e28f3e2019-02-08 15:45:20 -050063 if d, ok := device.(*voltha.Device); ok {
64 agent.lastData = proto.Clone(d).(*voltha.Device)
65 }
66 } else {
npujar03b018e2019-11-13 15:29:36 +053067 log.Errorw("failed-to-load-device", log.Fields{"deviceID": agent.deviceID})
68 return status.Errorf(codes.NotFound, "device-%s", agent.deviceID)
Stephane Barbarie1e28f3e2019-02-08 15:45:20 -050069 }
70 log.Debugw("device-loaded-from-dB", log.Fields{"device": agent.lastData})
71 }
Stephane Barbariea75791c2019-01-24 10:58:06 -050072 log.Debug("device-agent-started")
Stephane Barbarie1e28f3e2019-02-08 15:45:20 -050073 return nil
Stephane Barbariea75791c2019-01-24 10:58:06 -050074}
75
76// stop stops the device agent. Not much to do for now
77func (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
86func (agent *DeviceAgent) getDevice() (*voltha.Device, error) {
87 agent.lockDevice.Lock()
88 defer agent.lockDevice.Unlock()
npujar03b018e2019-11-13 15:29:36 +053089 if device := agent.clusterDataProxy.Get(context.Background(), "/devices/"+agent.deviceID, 0, false, ""); device != nil {
Stephane Barbariea75791c2019-01-24 10:58:06 -050090 if d, ok := device.(*voltha.Device); ok {
91 cloned := proto.Clone(d).(*voltha.Device)
92 return cloned, nil
93 }
94 }
npujar03b018e2019-11-13 15:29:36 +053095 return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID)
Stephane Barbariea75791c2019-01-24 10:58:06 -050096}
97
98// ListDevicePorts retrieves the ports information for a particular device.
99func (agent *DeviceAgent) ListDevicePorts(ctx context.Context) (*voltha.Ports, error) {
npujar03b018e2019-11-13 15:29:36 +0530100 log.Debugw("ListDevicePorts", log.Fields{"id": agent.deviceID})
Stephane Barbariea75791c2019-01-24 10:58:06 -0500101 ports := &voltha.Ports{}
npujar03b018e2019-11-13 15:29:36 +0530102 if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil {
103 ports.Items = append(ports.Items, device.GetPorts()...)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500104 return ports, nil
105 }
npujar03b018e2019-11-13 15:29:36 +0530106 return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500107}
108
109// ListDevicePmConfigs retrieves the ports information for a particular device.
110func (agent *DeviceAgent) ListDevicePmConfigs(ctx context.Context) (*voltha.PmConfigs, error) {
npujar03b018e2019-11-13 15:29:36 +0530111 log.Debugw("ListDevicePmConfigs", log.Fields{"id": agent.deviceID})
112 if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil {
Stephane Barbariea75791c2019-01-24 10:58:06 -0500113 return device.GetPmConfigs(), nil
114 }
npujar03b018e2019-11-13 15:29:36 +0530115 return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500116}
117
118// ListDeviceFlows retrieves the ports information for a particular device.
119func (agent *DeviceAgent) ListDeviceFlows(ctx context.Context) (*voltha.Flows, error) {
npujar03b018e2019-11-13 15:29:36 +0530120 log.Debugw("ListDeviceFlows", log.Fields{"id": agent.deviceID})
121 if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil {
Stephane Barbariea75791c2019-01-24 10:58:06 -0500122 return device.GetFlows(), nil
123 }
npujar03b018e2019-11-13 15:29:36 +0530124 return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500125}
126
npujar03b018e2019-11-13 15:29:36 +0530127// ListDeviceFlowGroups retrieves the ports information for a particular device.
Stephane Barbariea75791c2019-01-24 10:58:06 -0500128func (agent *DeviceAgent) ListDeviceFlowGroups(ctx context.Context) (*voltha.FlowGroups, error) {
npujar03b018e2019-11-13 15:29:36 +0530129 log.Debugw("ListDeviceFlowGroups", log.Fields{"id": agent.deviceID})
130 if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil {
Stephane Barbariea75791c2019-01-24 10:58:06 -0500131 return device.GetFlowGroups(), nil
132 }
npujar03b018e2019-11-13 15:29:36 +0530133 return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500134}
135
136// GetImageDownloadStatus retrieves the download status of an image of a particular device.
137func (agent *DeviceAgent) GetImageDownloadStatus(ctx context.Context, imageName string) (*voltha.ImageDownload, error) {
npujar03b018e2019-11-13 15:29:36 +0530138 log.Debugw("GetImageDownloadStatus", log.Fields{"id": agent.deviceID})
139 if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil {
Stephane Barbariea75791c2019-01-24 10:58:06 -0500140 for _, img := range device.GetImageDownloads() {
141 if img.GetName() == imageName {
142 return img, nil
143 }
144 }
npujar03b018e2019-11-13 15:29:36 +0530145 return nil, status.Errorf(codes.NotFound, "device-%s, image-%s", agent.deviceID, imageName)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500146 }
npujar03b018e2019-11-13 15:29:36 +0530147 return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500148}
149
150// GetImageDownload retrieves the image download for a particular device.
151func (agent *DeviceAgent) GetImageDownload(ctx context.Context, imageName string) (*voltha.ImageDownload, error) {
npujar03b018e2019-11-13 15:29:36 +0530152 log.Debugw("GetImageDownload", log.Fields{"id": agent.deviceID})
153 if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil {
Stephane Barbariea75791c2019-01-24 10:58:06 -0500154 for _, img := range device.GetImageDownloads() {
155 if img.GetName() == imageName {
156 return img, nil
157 }
158 }
npujar03b018e2019-11-13 15:29:36 +0530159 return nil, status.Errorf(codes.NotFound, "device-%s, image-%s", agent.deviceID, imageName)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500160 }
npujar03b018e2019-11-13 15:29:36 +0530161 return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500162}
163
164// ListImageDownloads retrieves the image downloads for a particular device.
165func (agent *DeviceAgent) ListImageDownloads(ctx context.Context) (*voltha.ImageDownloads, error) {
npujar03b018e2019-11-13 15:29:36 +0530166 log.Debugw("ListImageDownloads", log.Fields{"id": agent.deviceID})
167 if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil {
Stephane Barbariea75791c2019-01-24 10:58:06 -0500168 return &voltha.ImageDownloads{Items: device.GetImageDownloads()}, nil
169 }
npujar03b018e2019-11-13 15:29:36 +0530170 return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500171}
172
173// GetImages retrieves the list of images for a particular device.
174func (agent *DeviceAgent) GetImages(ctx context.Context) (*voltha.Images, error) {
npujar03b018e2019-11-13 15:29:36 +0530175 log.Debugw("GetImages", log.Fields{"id": agent.deviceID})
176 if device, _ := agent.deviceMgr.GetDevice(agent.deviceID); device != nil {
Stephane Barbariea75791c2019-01-24 10:58:06 -0500177 return device.GetImages(), nil
178 }
npujar03b018e2019-11-13 15:29:36 +0530179 return nil, status.Errorf(codes.NotFound, "device-%s", agent.deviceID)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400180}