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 | */ |
| 16 | package core |
| 17 | |
| 18 | import ( |
| 19 | "context" |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 20 | "github.com/gogo/protobuf/proto" |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 21 | "github.com/opencord/voltha-go/common/log" |
| 22 | "github.com/opencord/voltha-go/db/model" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 23 | "github.com/opencord/voltha-protos/go/voltha" |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 24 | "google.golang.org/grpc/codes" |
| 25 | "google.golang.org/grpc/status" |
| 26 | "sync" |
| 27 | ) |
| 28 | |
| 29 | type 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 | |
| 40 | func 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 Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 53 | func (agent *LogicalDeviceAgent) start(ctx context.Context, loadFromDb bool) error { |
| 54 | log.Infow("starting-logical_device-agent", log.Fields{"logicaldeviceId": agent.logicalDeviceId, "loadFromdB": loadFromDb}) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 55 | agent.lockLogicalDevice.Lock() |
| 56 | defer agent.lockLogicalDevice.Unlock() |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 57 | 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. |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 60 | if logicalDevice := agent.clusterDataProxy.Get(ctx, "/logical_devices/"+agent.logicalDeviceId, 0, false, ""); logicalDevice != nil { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 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 Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 69 | log.Info("logical_device-agent-started") |
| 70 | return nil |
| 71 | } |
| 72 | |
| 73 | // stop terminates the logical device agent. |
| 74 | func (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 |
| 84 | func (agent *LogicalDeviceAgent) GetLogicalDevice() (*voltha.LogicalDevice, error) { |
| 85 | log.Debug("GetLogicalDevice") |
| 86 | agent.lockLogicalDevice.Lock() |
| 87 | defer agent.lockLogicalDevice.Unlock() |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 88 | if logicalDevice := agent.clusterDataProxy.Get(context.Background(), "/logical_devices/"+agent.logicalDeviceId, 0, false, ""); logicalDevice != nil { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 89 | if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok { |
| 90 | return lDevice, nil |
| 91 | } |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 92 | } |
| 93 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId) |
| 94 | } |
| 95 | |
| 96 | func (agent *LogicalDeviceAgent) ListLogicalDevicePorts() (*voltha.LogicalPorts, error) { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 97 | log.Debug("ListLogicalDevicePorts") |
| 98 | if logicalDevice, _ := agent.ldeviceMgr.getLogicalDevice(agent.logicalDeviceId); logicalDevice != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 99 | lPorts := make([]*voltha.LogicalPort, 0) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 100 | for _, port := range logicalDevice.Ports { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 101 | lPorts = append(lPorts, port) |
| 102 | } |
| 103 | return &voltha.LogicalPorts{Items: lPorts}, nil |
| 104 | } |
| 105 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId) |
| 106 | } |
| 107 | |
| 108 | // listFlows locks the logical device model and then retrieves the latest flow information |
| 109 | func (agent *LogicalDeviceAgent) ListLogicalDeviceFlows() (*voltha.Flows, error) { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 110 | log.Debug("ListLogicalDeviceFlows") |
| 111 | if logicalDevice, _ := agent.ldeviceMgr.getLogicalDevice(agent.logicalDeviceId); logicalDevice != nil { |
| 112 | return logicalDevice.GetFlows(), nil |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 113 | } |
| 114 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId) |
| 115 | } |
| 116 | |
| 117 | // listFlowGroups locks the logical device model and then retrieves the latest flow groups information |
| 118 | func (agent *LogicalDeviceAgent) ListLogicalDeviceFlowGroups() (*voltha.FlowGroups, error) { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 119 | log.Debug("ListLogicalDeviceFlowGroups") |
| 120 | if logicalDevice, _ := agent.ldeviceMgr.getLogicalDevice(agent.logicalDeviceId); logicalDevice != nil { |
| 121 | return logicalDevice.GetFlowGroups(), nil |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 122 | } |
| 123 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceId) |
| 124 | } |