khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [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" |
| 20 | "errors" |
| 21 | "github.com/opencord/voltha-go/common/log" |
| 22 | "github.com/opencord/voltha-go/db/model" |
| 23 | "github.com/opencord/voltha-go/kafka" |
| 24 | "github.com/opencord/voltha-go/protos/voltha" |
| 25 | "google.golang.org/grpc/codes" |
| 26 | "google.golang.org/grpc/status" |
| 27 | "reflect" |
| 28 | "strings" |
| 29 | "sync" |
| 30 | ) |
| 31 | |
| 32 | type LogicalDeviceManager struct { |
| 33 | logicalDeviceAgents map[string]*LogicalDeviceAgent |
| 34 | deviceMgr *DeviceManager |
| 35 | adapterProxy *AdapterProxy |
| 36 | kafkaProxy *kafka.KafkaMessagingProxy |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 37 | clusterDataProxy *model.Proxy |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 38 | exitChannel chan int |
| 39 | lockLogicalDeviceAgentsMap sync.RWMutex |
| 40 | } |
| 41 | |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 42 | func NewLogicalDeviceManager(deviceMgr *DeviceManager, kafkaProxy *kafka.KafkaMessagingProxy, cdProxy *model.Proxy) *LogicalDeviceManager { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 43 | var logicalDeviceMgr LogicalDeviceManager |
| 44 | logicalDeviceMgr.exitChannel = make(chan int, 1) |
| 45 | logicalDeviceMgr.logicalDeviceAgents = make(map[string]*LogicalDeviceAgent) |
| 46 | logicalDeviceMgr.deviceMgr = deviceMgr |
| 47 | logicalDeviceMgr.kafkaProxy = kafkaProxy |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 48 | logicalDeviceMgr.clusterDataProxy = cdProxy |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 49 | logicalDeviceMgr.lockLogicalDeviceAgentsMap = sync.RWMutex{} |
| 50 | return &logicalDeviceMgr |
| 51 | } |
| 52 | |
| 53 | func (ldMgr *LogicalDeviceManager) Start(ctx context.Context) { |
| 54 | log.Info("starting-logical-device-manager") |
| 55 | log.Info("logical-device-manager-started") |
| 56 | } |
| 57 | |
| 58 | func (ldMgr *LogicalDeviceManager) Stop(ctx context.Context) { |
| 59 | log.Info("stopping-logical-device-manager") |
| 60 | ldMgr.exitChannel <- 1 |
| 61 | log.Info("logical-device-manager-stopped") |
| 62 | } |
| 63 | |
| 64 | func (ldMgr *LogicalDeviceManager) addLogicalDeviceAgentToMap(agent *LogicalDeviceAgent) { |
| 65 | ldMgr.lockLogicalDeviceAgentsMap.Lock() |
| 66 | defer ldMgr.lockLogicalDeviceAgentsMap.Unlock() |
| 67 | if _, exist := ldMgr.logicalDeviceAgents[agent.logicalDeviceId]; !exist { |
| 68 | ldMgr.logicalDeviceAgents[agent.logicalDeviceId] = agent |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func (ldMgr *LogicalDeviceManager) getLogicalDeviceAgent(logicalDeviceId string) *LogicalDeviceAgent { |
| 73 | ldMgr.lockLogicalDeviceAgentsMap.Lock() |
| 74 | defer ldMgr.lockLogicalDeviceAgentsMap.Unlock() |
| 75 | if agent, ok := ldMgr.logicalDeviceAgents[logicalDeviceId]; ok { |
| 76 | return agent |
| 77 | } |
| 78 | return nil |
| 79 | } |
| 80 | |
| 81 | func (ldMgr *LogicalDeviceManager) getLogicalDevice(id string) (*voltha.LogicalDevice, error) { |
| 82 | log.Debugw("getlogicalDevice-start", log.Fields{"logicaldeviceid": id}) |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 83 | logicalDevice := ldMgr.clusterDataProxy.Get("/logical_devices/"+id, 1, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 84 | if logicalDevice != nil { |
| 85 | cloned := reflect.ValueOf(logicalDevice).Elem().Interface().(voltha.LogicalDevice) |
| 86 | return &cloned, nil |
| 87 | } |
| 88 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 89 | } |
| 90 | |
| 91 | func (ldMgr *LogicalDeviceManager) listLogicalDevices() (*voltha.LogicalDevices, error) { |
| 92 | log.Debug("listLogicalDevices-start") |
| 93 | result := &voltha.LogicalDevices{} |
| 94 | ldMgr.lockLogicalDeviceAgentsMap.Lock() |
| 95 | defer ldMgr.lockLogicalDeviceAgentsMap.Unlock() |
| 96 | for _, agent := range ldMgr.logicalDeviceAgents { |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 97 | logicalDevice := ldMgr.clusterDataProxy.Get("/logical_devices/"+agent.logicalDeviceId, 1, false, "") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 98 | if logicalDevice != nil { |
| 99 | cloned := reflect.ValueOf(logicalDevice).Elem().Interface().(voltha.LogicalDevice) |
| 100 | result.Items = append(result.Items, &cloned) |
| 101 | } |
| 102 | } |
| 103 | return result, nil |
| 104 | } |
| 105 | |
| 106 | func (ldMgr *LogicalDeviceManager) CreateLogicalDevice(ctx context.Context, device *voltha.Device) (*string, error) { |
| 107 | log.Infow("creating-logical-device-start", log.Fields{"deviceId": device.Id}) |
| 108 | // Sanity check |
| 109 | if !device.Root { |
| 110 | return nil, errors.New("Device-not-root") |
| 111 | } |
| 112 | |
| 113 | // Create a logical device agent - the logical device Id is based on the mac address of the device |
| 114 | // For now use the serial number - it may contain any combination of alphabetic characters and numbers, |
| 115 | // with length varying from eight characters to a maximum of 14 characters. Mac Address is part of oneof |
| 116 | // in the Device model. May need to be moved out. |
| 117 | macAddress := device.MacAddress |
| 118 | id := strings.Replace(macAddress, ":", "", -1) |
| 119 | log.Debugw("setting-logical-device-id", log.Fields{"logicaldeviceId": id}) |
| 120 | |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 121 | agent := NewLogicalDeviceAgent(id, device, ldMgr, ldMgr.deviceMgr, ldMgr.clusterDataProxy) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 122 | ldMgr.addLogicalDeviceAgentToMap(agent) |
| 123 | go agent.Start(ctx) |
| 124 | |
| 125 | log.Info("creating-logical-device-ends") |
| 126 | return &id, nil |
| 127 | } |
| 128 | |
| 129 | func (ldMgr *LogicalDeviceManager) AddUNILogicalPort(ctx context.Context, childDevice *voltha.Device) error { |
| 130 | log.Infow("AddUNILogicalPort-start", log.Fields{"deviceId": childDevice.Id}) |
| 131 | // Sanity check |
| 132 | if childDevice.Root { |
| 133 | return errors.New("Device-root") |
| 134 | } |
| 135 | |
| 136 | // Get the logical device id parent device |
| 137 | parentId := childDevice.ParentId |
| 138 | logDeviceId := ldMgr.deviceMgr.GetParentDeviceId(parentId) |
| 139 | |
| 140 | log.Infow("AddUNILogicalPort", log.Fields{"logDeviceId": logDeviceId, "parentId": parentId}) |
| 141 | |
| 142 | if agent := ldMgr.getLogicalDeviceAgent(*logDeviceId); agent != nil { |
| 143 | return agent.addUNILogicalPort(ctx, childDevice, childDevice.ProxyAddress.ChannelId) |
| 144 | } |
| 145 | return status.Errorf(codes.NotFound, "%s", childDevice.Id) |
| 146 | } |