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" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 26 | "github.com/opencord/voltha-protos/v3/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. |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 63 | if logicalDevice, err := agent.clusterDataProxy.Get(ctx, "/logical_devices/"+agent.logicalDeviceID, 0, false, ""); err != nil { |
| 64 | log.Errorw("failed-to-get-logical-device", log.Fields{"error": err}) |
| 65 | return err |
| 66 | } else if logicalDevice != nil { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 67 | if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok { |
| 68 | agent.lastData = proto.Clone(lDevice).(*voltha.LogicalDevice) |
| 69 | } |
| 70 | } else { |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 71 | log.Errorw("failed-to-load-device", log.Fields{"logicaldeviceID": agent.logicalDeviceID}) |
| 72 | return status.Errorf(codes.NotFound, "logicaldeviceID-%s", agent.logicalDeviceID) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 73 | } |
| 74 | } |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 75 | log.Info("logical_device-agent-started") |
| 76 | return nil |
| 77 | } |
| 78 | |
| 79 | // stop terminates the logical device agent. |
| 80 | func (agent *LogicalDeviceAgent) stop(ctx context.Context) { |
| 81 | log.Info("stopping-logical_device-agent") |
| 82 | agent.lockLogicalDevice.Lock() |
| 83 | defer agent.lockLogicalDevice.Unlock() |
| 84 | //Remove the logical device from the model |
| 85 | agent.exitChannel <- 1 |
| 86 | log.Info("logical_device-agent-stopped") |
| 87 | } |
| 88 | |
| 89 | // GetLogicalDevice locks the logical device model and then retrieves the latest logical device information |
| 90 | func (agent *LogicalDeviceAgent) GetLogicalDevice() (*voltha.LogicalDevice, error) { |
| 91 | log.Debug("GetLogicalDevice") |
| 92 | agent.lockLogicalDevice.Lock() |
| 93 | defer agent.lockLogicalDevice.Unlock() |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 94 | if logicalDevice, err := agent.clusterDataProxy.Get(context.Background(), "/logical_devices/"+agent.logicalDeviceID, 0, false, ""); err != nil { |
| 95 | log.Errorw("failed-to-get-logical-device", log.Fields{"error": err}) |
| 96 | return nil, err |
| 97 | } else if logicalDevice != nil { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 98 | if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok { |
| 99 | return lDevice, nil |
| 100 | } |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 101 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 102 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 103 | } |
| 104 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 105 | // ListLogicalDevicePorts returns all logical device ports details |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 106 | func (agent *LogicalDeviceAgent) ListLogicalDevicePorts() (*voltha.LogicalPorts, error) { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 107 | log.Debug("ListLogicalDevicePorts") |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 108 | if logicalDevice, _ := agent.ldeviceMgr.getLogicalDevice(agent.logicalDeviceID); logicalDevice != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 109 | lPorts := make([]*voltha.LogicalPort, 0) |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 110 | lPorts = append(lPorts, logicalDevice.Ports...) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 111 | return &voltha.LogicalPorts{Items: lPorts}, nil |
| 112 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 113 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 114 | } |
| 115 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 116 | // 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] | 117 | func (agent *LogicalDeviceAgent) ListLogicalDeviceFlows() (*voltha.Flows, error) { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 118 | log.Debug("ListLogicalDeviceFlows") |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 119 | if logicalDevice, _ := agent.ldeviceMgr.getLogicalDevice(agent.logicalDeviceID); logicalDevice != nil { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 120 | return logicalDevice.GetFlows(), nil |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 121 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 122 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 123 | } |
| 124 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 125 | // 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] | 126 | func (agent *LogicalDeviceAgent) ListLogicalDeviceFlowGroups() (*voltha.FlowGroups, error) { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 127 | log.Debug("ListLogicalDeviceFlowGroups") |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 128 | if logicalDevice, _ := agent.ldeviceMgr.getLogicalDevice(agent.logicalDeviceID); logicalDevice != nil { |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 129 | return logicalDevice.GetFlowGroups(), nil |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 130 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 131 | return nil, status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceID) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 132 | } |