blob: f3153ca028b78d67b50f9944d9ffffe5970da9df [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 */
16package core
17
18import (
19 "context"
Stephane Barbarie1e28f3e2019-02-08 15:45:20 -050020 "github.com/gogo/protobuf/proto"
Stephane Barbariea75791c2019-01-24 10:58:06 -050021 "github.com/opencord/voltha-go/common/log"
22 "github.com/opencord/voltha-go/db/model"
23 "github.com/opencord/voltha-go/protos/voltha"
24 "google.golang.org/grpc/codes"
25 "google.golang.org/grpc/status"
26 "sync"
27)
28
29type LogicalDeviceAgent struct {
30 logicalDeviceId string
31 lastData *voltha.LogicalDevice
32 rootDeviceId string
33 deviceMgr *DeviceManager
34 ldeviceMgr *LogicalDeviceManager
35 clusterDataProxy *model.Proxy
36 exitChannel chan int
37 lockLogicalDevice sync.RWMutex
38}
39
40func newLogicalDeviceAgent(id string, deviceId string, ldeviceMgr *LogicalDeviceManager, deviceMgr *DeviceManager, cdProxy *model.Proxy) *LogicalDeviceAgent {
41 var agent LogicalDeviceAgent
42 agent.exitChannel = make(chan int, 1)
43 agent.logicalDeviceId = id
44 agent.rootDeviceId = deviceId
45 agent.deviceMgr = deviceMgr
46 agent.clusterDataProxy = cdProxy
47 agent.ldeviceMgr = ldeviceMgr
48 agent.lockLogicalDevice = sync.RWMutex{}
49 return &agent
50}
51
52// start creates the logical device and add it to the data model
Stephane Barbarie1e28f3e2019-02-08 15:45:20 -050053func (agent *LogicalDeviceAgent) start(ctx context.Context, loadFromDb bool) error {
54 log.Infow("starting-logical_device-agent", log.Fields{"logicaldeviceId": agent.logicalDeviceId, "loadFromdB": loadFromDb})
Stephane Barbariea75791c2019-01-24 10:58:06 -050055 agent.lockLogicalDevice.Lock()
56 defer agent.lockLogicalDevice.Unlock()
Stephane Barbarie1e28f3e2019-02-08 15:45:20 -050057 if loadFromDb {
58 // load from dB - the logical may not exist at this time. On error, just return and the calling function
59 // will destroy this agent.
60 if logicalDevice := agent.clusterDataProxy.Get("/logical_devices/"+agent.logicalDeviceId, 0, false, ""); logicalDevice != nil {
61 if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok {
62 agent.lastData = proto.Clone(lDevice).(*voltha.LogicalDevice)
63 }
64 } else {
65 log.Errorw("failed-to-load-device", log.Fields{"logicaldeviceId": agent.logicalDeviceId})
66 return status.Errorf(codes.NotFound, "logicaldeviceId-%s", agent.logicalDeviceId)
67 }
68 }
Stephane Barbariea75791c2019-01-24 10:58:06 -050069 log.Info("logical_device-agent-started")
70 return nil
71}
72
73// stop terminates the logical device agent.
74func (agent *LogicalDeviceAgent) stop(ctx context.Context) {
75 log.Info("stopping-logical_device-agent")
76 agent.lockLogicalDevice.Lock()
77 defer agent.lockLogicalDevice.Unlock()
78 //Remove the logical device from the model
79 agent.exitChannel <- 1
80 log.Info("logical_device-agent-stopped")
81}
82
83// GetLogicalDevice locks the logical device model and then retrieves the latest logical device information
84func (agent *LogicalDeviceAgent) GetLogicalDevice() (*voltha.LogicalDevice, error) {
85 log.Debug("GetLogicalDevice")
86 agent.lockLogicalDevice.Lock()
87 defer agent.lockLogicalDevice.Unlock()
Stephane Barbarie1e28f3e2019-02-08 15:45:20 -050088 if logicalDevice := agent.clusterDataProxy.Get("/logical_devices/"+agent.logicalDeviceId, 0, false,
89 ""); logicalDevice != nil {
90 if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok {
91 return lDevice, nil
92 }
Stephane Barbariea75791c2019-01-24 10:58:06 -050093 }
94 return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId)
95}
96
97func (agent *LogicalDeviceAgent) ListLogicalDevicePorts() (*voltha.LogicalPorts, error) {
Stephane Barbarie1e28f3e2019-02-08 15:45:20 -050098 log.Debug("ListLogicalDevicePorts")
99 if logicalDevice, _ := agent.ldeviceMgr.getLogicalDevice(agent.logicalDeviceId); logicalDevice != nil {
Stephane Barbariea75791c2019-01-24 10:58:06 -0500100 lPorts := make([]*voltha.LogicalPort, 0)
Stephane Barbarie1e28f3e2019-02-08 15:45:20 -0500101 for _, port := range logicalDevice.Ports {
Stephane Barbariea75791c2019-01-24 10:58:06 -0500102 lPorts = append(lPorts, port)
103 }
104 return &voltha.LogicalPorts{Items: lPorts}, nil
105 }
106 return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId)
107}
108
109// listFlows locks the logical device model and then retrieves the latest flow information
110func (agent *LogicalDeviceAgent) ListLogicalDeviceFlows() (*voltha.Flows, error) {
Stephane Barbarie1e28f3e2019-02-08 15:45:20 -0500111 log.Debug("ListLogicalDeviceFlows")
112 if logicalDevice, _ := agent.ldeviceMgr.getLogicalDevice(agent.logicalDeviceId); logicalDevice != nil {
113 return logicalDevice.GetFlows(), nil
Stephane Barbariea75791c2019-01-24 10:58:06 -0500114 }
115 return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId)
116}
117
118// listFlowGroups locks the logical device model and then retrieves the latest flow groups information
119func (agent *LogicalDeviceAgent) ListLogicalDeviceFlowGroups() (*voltha.FlowGroups, error) {
Stephane Barbarie1e28f3e2019-02-08 15:45:20 -0500120 log.Debug("ListLogicalDeviceFlowGroups")
121 if logicalDevice, _ := agent.ldeviceMgr.getLogicalDevice(agent.logicalDeviceId); logicalDevice != nil {
122 return logicalDevice.GetFlowGroups(), nil
Stephane Barbariea75791c2019-01-24 10:58:06 -0500123 }
124 return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId)
125}