Takahiro Suzuki | 241c10e | 2020-12-17 20:17:57 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020-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 adaptercoreonu |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "fmt" |
| 21 | "time" |
| 22 | |
| 23 | "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif" |
| 24 | "github.com/opencord/voltha-protos/v3/go/common" |
| 25 | ) |
| 26 | |
| 27 | var startWatchStatus bool = false |
| 28 | |
| 29 | //WatchStatus ... |
| 30 | func WatchStatus(ctx context.Context, cp adapterif.CoreProxy) { |
| 31 | if startWatchStatus { |
| 32 | return |
| 33 | } |
| 34 | startWatchStatus = true |
| 35 | logger.Debug(ctx, fmt.Sprintf("Start WatchStatus()")) |
| 36 | ticker := time.NewTicker(1000 * time.Millisecond) |
| 37 | prevOnuList := make(map[string]OnuStatus) |
| 38 | for range ticker.C { |
| 39 | onuList, err := ReadOnuStatusList() |
| 40 | if err == nil { |
| 41 | for _, onu := range onuList { |
| 42 | current, ok := prevOnuList[onu.MacAddress] |
| 43 | if !ok || onu.OpeState != current.OpeState || onu.ConnectState != current.ConnectState { |
| 44 | con := common.ConnectStatus_Types(common.ConnectStatus_Types_value[onu.ConnectState]) |
| 45 | ope := common.OperStatus_Types(common.OperStatus_Types_value[onu.OpeState]) |
| 46 | err2 := cp.DeviceStateUpdate(ctx, onu.ID, con, ope) |
| 47 | logger.Debug(ctx, fmt.Sprintf("WatchStatus() update: %s, OpeState: %v, ConnectState: %v, error: %v", |
| 48 | onu.ID, onu.OpeState, onu.ConnectState, err2)) |
| 49 | prevOnuList[onu.MacAddress] = onu |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |