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 | 1e28f3e | 2019-02-08 15:45:20 -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" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Scott Baker | 555307d | 2019-11-04 08:58:01 -0800 | [diff] [blame] | 26 | "github.com/opencord/voltha-protos/v2/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 | // LogicalDeviceAgent represents logical device agent related information |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 32 | type LogicalDeviceAgent struct { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 33 | logicalDeviceID string |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 34 | lastData *voltha.LogicalDevice |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 35 | rootDeviceID string |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 36 | deviceMgr *DeviceManager |
| 37 | ldeviceMgr *LogicalDeviceManager |
| 38 | clusterDataProxy *model.Proxy |
| 39 | exitChannel chan int |
| 40 | lockLogicalDevice sync.RWMutex |
| 41 | } |
| 42 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 43 | func newLogicalDeviceAgent(id string, deviceID string, ldeviceMgr *LogicalDeviceManager, deviceMgr *DeviceManager, cdProxy *model.Proxy) *LogicalDeviceAgent { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 44 | var agent LogicalDeviceAgent |
| 45 | agent.exitChannel = make(chan int, 1) |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 46 | agent.logicalDeviceID = id |
| 47 | agent.rootDeviceID = deviceID |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 48 | agent.deviceMgr = deviceMgr |
| 49 | agent.clusterDataProxy = cdProxy |
| 50 | agent.ldeviceMgr = ldeviceMgr |
| 51 | agent.lockLogicalDevice = sync.RWMutex{} |
| 52 | return &agent |
| 53 | } |
| 54 | |
| 55 | // start creates the logical device and add it to the data model |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 56 | func (agent *LogicalDeviceAgent) start(ctx context.Context, loadFromDb bool) error { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 57 | 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] | 58 | agent.lockLogicalDevice.Lock() |
| 59 | defer agent.lockLogicalDevice.Unlock() |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 60 | if loadFromDb { |
| 61 | // load from dB - the logical may not exist at this time. On error, just return and the calling function |
| 62 | // will destroy this agent. |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 63 | 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] | 64 | if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok { |
| 65 | agent.lastData = proto.Clone(lDevice).(*voltha.LogicalDevice) |
| 66 | } |
| 67 | } else { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 68 | log.Errorw("failed-to-load-device", log.Fields{"logicaldeviceID": agent.logicalDeviceID}) |
| 69 | return status.Errorf(codes.NotFound, "logicaldeviceID-%s", agent.logicalDeviceID) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 70 | } |
| 71 | } |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 72 | log.Info("logical_device-agent-started") |
| 73 | return nil |
| 74 | } |
| 75 | |
| 76 | // stop terminates the logical device agent. |
| 77 | func (agent *LogicalDeviceAgent) stop(ctx context.Context) { |
| 78 | log.Info("stopping-logical_device-agent") |
| 79 | agent.lockLogicalDevice.Lock() |
| 80 | defer agent.lockLogicalDevice.Unlock() |
| 81 | //Remove the logical device from the model |
| 82 | agent.exitChannel <- 1 |
| 83 | log.Info("logical_device-agent-stopped") |
| 84 | } |
| 85 | |
| 86 | // GetLogicalDevice locks the logical device model and then retrieves the latest logical device information |
| 87 | func (agent *LogicalDeviceAgent) GetLogicalDevice() (*voltha.LogicalDevice, error) { |
| 88 | log.Debug("GetLogicalDevice") |
| 89 | agent.lockLogicalDevice.Lock() |
| 90 | defer agent.lockLogicalDevice.Unlock() |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 91 | 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] | 92 | if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok { |
| 93 | return lDevice, nil |
| 94 | } |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 95 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 96 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 97 | } |
| 98 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 99 | // ListLogicalDevicePorts returns all logical device ports details |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 100 | func (agent *LogicalDeviceAgent) ListLogicalDevicePorts() (*voltha.LogicalPorts, error) { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 101 | log.Debug("ListLogicalDevicePorts") |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 102 | if logicalDevice, _ := agent.ldeviceMgr.getLogicalDevice(agent.logicalDeviceID); logicalDevice != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 103 | lPorts := make([]*voltha.LogicalPort, 0) |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 104 | lPorts = append(lPorts, logicalDevice.Ports...) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 105 | return &voltha.LogicalPorts{Items: lPorts}, nil |
| 106 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 107 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 108 | } |
| 109 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 110 | // ListLogicalDeviceFlows - listFlows locks the logical device model and then retrieves the latest flow information |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 111 | func (agent *LogicalDeviceAgent) ListLogicalDeviceFlows() (*voltha.Flows, error) { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 112 | log.Debug("ListLogicalDeviceFlows") |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 113 | if logicalDevice, _ := agent.ldeviceMgr.getLogicalDevice(agent.logicalDeviceID); logicalDevice != nil { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 114 | return logicalDevice.GetFlows(), nil |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 115 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 116 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 117 | } |
| 118 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 119 | // ListLogicalDeviceFlowGroups - listFlowGroups locks the logical device model and then retrieves the latest flow groups information |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 120 | func (agent *LogicalDeviceAgent) ListLogicalDeviceFlowGroups() (*voltha.FlowGroups, error) { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 121 | log.Debug("ListLogicalDeviceFlowGroups") |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 122 | if logicalDevice, _ := agent.ldeviceMgr.getLogicalDevice(agent.logicalDeviceID); logicalDevice != nil { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 123 | return logicalDevice.GetFlowGroups(), nil |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 124 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 125 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 126 | } |