Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [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 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 17 | package devices |
| 18 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 19 | import ( |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 20 | "strconv" |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame^] | 21 | "time" |
| 22 | |
| 23 | "github.com/looplab/fsm" |
| 24 | "github.com/opencord/bbsim/internal/common" |
| 25 | "github.com/opencord/voltha-protos/v2/go/openolt" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 26 | ) |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 27 | |
Pragya Arya | 2225f20 | 2020-01-29 18:05:01 +0530 | [diff] [blame] | 28 | type mode int |
| 29 | |
| 30 | // Constants for Controlled Activation modes |
| 31 | const ( |
| 32 | Default mode = iota |
| 33 | OnlyONU |
| 34 | OnlyPON |
| 35 | Both |
| 36 | ) |
| 37 | |
| 38 | // ControlledActivationModes maps string to int value of mode |
| 39 | var ControlledActivationModes = map[string]mode{ |
| 40 | "default": Default, |
| 41 | "only-onu": OnlyONU, |
| 42 | "only-pon": OnlyPON, |
| 43 | "both": Both, |
| 44 | } |
| 45 | |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 46 | var newFSM = fsm.NewFSM |
| 47 | |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 48 | func getOperStateFSM(cb fsm.Callback) *fsm.FSM { |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 49 | return newFSM( |
Matteo Scandolo | 9a3518c | 2019-08-13 14:36:01 -0700 | [diff] [blame] | 50 | "down", |
| 51 | fsm.Events{ |
| 52 | {Name: "enable", Src: []string{"down"}, Dst: "up"}, |
| 53 | {Name: "disable", Src: []string{"up"}, Dst: "down"}, |
| 54 | }, |
| 55 | fsm.Callbacks{ |
| 56 | "enter_state": func(e *fsm.Event) { |
| 57 | cb(e) |
| 58 | }, |
| 59 | }, |
| 60 | ) |
| 61 | } |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 62 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 63 | // deprecated |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 64 | func onuSnToString(sn *openolt.SerialNumber) string { |
| 65 | s := string(sn.VendorId) |
| 66 | for _, i := range sn.VendorSpecific { |
| 67 | s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16) |
| 68 | } |
| 69 | return s |
| 70 | } |
Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame^] | 71 | |
| 72 | func publishEvent(eventType string, intfID int32, onuID int32, onuSerial string) { |
| 73 | if olt.PublishEvents { |
| 74 | currentTime := time.Now() |
| 75 | |
| 76 | event := common.Event{ |
| 77 | EventType: eventType, |
| 78 | OltID: olt.ID, |
| 79 | IntfID: intfID, |
| 80 | OnuID: onuID, |
| 81 | OnuSerial: onuSerial, |
| 82 | Timestamp: currentTime.Format("2006-01-02 15:04:05.000000000"), |
| 83 | EpochTime: currentTime.UnixNano() / 1000000, |
| 84 | } |
| 85 | olt.EventChannel <- event |
| 86 | } |
| 87 | } |