blob: 9cb6655f57c04e195d936dced9393364a54eb6be [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"
20 "github.com/opencord/voltha-go/common/log"
21 "github.com/opencord/voltha-go/db/model"
22 "github.com/opencord/voltha-go/protos/voltha"
23 "google.golang.org/grpc/codes"
24 "google.golang.org/grpc/status"
25 "sync"
26)
27
28type LogicalDeviceAgent struct {
29 logicalDeviceId string
30 lastData *voltha.LogicalDevice
31 rootDeviceId string
32 deviceMgr *DeviceManager
33 ldeviceMgr *LogicalDeviceManager
34 clusterDataProxy *model.Proxy
35 exitChannel chan int
36 lockLogicalDevice sync.RWMutex
37}
38
39func newLogicalDeviceAgent(id string, deviceId string, ldeviceMgr *LogicalDeviceManager, deviceMgr *DeviceManager, cdProxy *model.Proxy) *LogicalDeviceAgent {
40 var agent LogicalDeviceAgent
41 agent.exitChannel = make(chan int, 1)
42 agent.logicalDeviceId = id
43 agent.rootDeviceId = deviceId
44 agent.deviceMgr = deviceMgr
45 agent.clusterDataProxy = cdProxy
46 agent.ldeviceMgr = ldeviceMgr
47 agent.lockLogicalDevice = sync.RWMutex{}
48 return &agent
49}
50
51// start creates the logical device and add it to the data model
52func (agent *LogicalDeviceAgent) start(ctx context.Context) error {
53 log.Infow("starting-logical_device-agent", log.Fields{"logicaldeviceId": agent.logicalDeviceId})
54 agent.lockLogicalDevice.Lock()
55 defer agent.lockLogicalDevice.Unlock()
56 log.Info("logical_device-agent-started")
57 return nil
58}
59
60// stop terminates the logical device agent.
61func (agent *LogicalDeviceAgent) stop(ctx context.Context) {
62 log.Info("stopping-logical_device-agent")
63 agent.lockLogicalDevice.Lock()
64 defer agent.lockLogicalDevice.Unlock()
65 //Remove the logical device from the model
66 agent.exitChannel <- 1
67 log.Info("logical_device-agent-stopped")
68}
69
70// GetLogicalDevice locks the logical device model and then retrieves the latest logical device information
71func (agent *LogicalDeviceAgent) GetLogicalDevice() (*voltha.LogicalDevice, error) {
72 log.Debug("GetLogicalDevice")
73 agent.lockLogicalDevice.Lock()
74 defer agent.lockLogicalDevice.Unlock()
75 logicalDevice := agent.clusterDataProxy.Get("/logical_devices/"+agent.logicalDeviceId, 1, false, "")
76 if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok {
77 return lDevice, nil
78 }
79 return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId)
80}
81
82func (agent *LogicalDeviceAgent) ListLogicalDevicePorts() (*voltha.LogicalPorts, error) {
83 log.Debug("!!!!!ListLogicalDevicePorts")
84 agent.lockLogicalDevice.Lock()
85 defer agent.lockLogicalDevice.Unlock()
86 logicalDevice := agent.clusterDataProxy.Get("/logical_devices/"+agent.logicalDeviceId, 1, false, "")
87 if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok {
88 lPorts := make([]*voltha.LogicalPort, 0)
89 for _, port := range lDevice.Ports {
90 lPorts = append(lPorts, port)
91 }
92 return &voltha.LogicalPorts{Items: lPorts}, nil
93 }
94 return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId)
95}
96
97// listFlows locks the logical device model and then retrieves the latest flow information
98func (agent *LogicalDeviceAgent) ListLogicalDeviceFlows() (*voltha.Flows, error) {
99 log.Debug("listFlows")
100 agent.lockLogicalDevice.Lock()
101 defer agent.lockLogicalDevice.Unlock()
102 logicalDevice := agent.clusterDataProxy.Get("/logical_devices/"+agent.logicalDeviceId+"/flows", 1, false, "")
103 if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok {
104 return lDevice.Flows, nil
105 }
106 return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId)
107}
108
109// listFlowGroups locks the logical device model and then retrieves the latest flow groups information
110func (agent *LogicalDeviceAgent) ListLogicalDeviceFlowGroups() (*voltha.FlowGroups, error) {
111 log.Debug("listFlowGroups")
112 agent.lockLogicalDevice.Lock()
113 defer agent.lockLogicalDevice.Unlock()
114 logicalDevice := agent.clusterDataProxy.Get("/logical_devices/"+agent.logicalDeviceId, 1, false, "")
115 if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok {
116 return lDevice.FlowGroups, nil
117 }
118 return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId)
119}