Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [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 | |
| 17 | package core |
| 18 | |
| 19 | import ( |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 20 | "flag" |
| 21 | "fmt" |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 22 | "os" |
| 23 | "os/signal" |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 24 | "strconv" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 25 | "strings" |
| 26 | "sync" |
| 27 | |
| 28 | "gerrit.opencord.org/voltha-bbsim/common/logger" |
| 29 | log "github.com/sirupsen/logrus" |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 30 | "gerrit.opencord.org/voltha-bbsim/device" |
| 31 | "reflect" |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 32 | ) |
| 33 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 34 | type option struct { |
| 35 | address string |
| 36 | port uint32 |
| 37 | oltid uint32 |
| 38 | npon uint32 |
| 39 | nonus uint32 |
| 40 | aaawait int |
| 41 | dhcpwait int |
| 42 | dhcpservip string |
| 43 | intvl int |
| 44 | intvl_test int |
| 45 | Mode Mode |
| 46 | KafkaBroker string |
Mahir Gunyel | 0918334 | 2019-01-29 14:26:50 -0800 | [diff] [blame^] | 47 | Debuglvl string |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | func GetOptions() *option { |
| 51 | o := new(option) |
| 52 | addressport := flag.String("H", ":50060", "IP address:port") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 53 | oltid := flag.Int("id", 0, "OLT-ID") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 54 | npon := flag.Int("i", 1, "Number of PON-IF ports") |
| 55 | nonus := flag.Int("n", 1, "Number of ONUs per PON-IF port") |
| 56 | modeopt := flag.String("m", "default", "Emulation mode (default, aaa, both (aaa & dhcp))") |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 57 | aaawait := flag.Int("aw", 10, "Wait time (sec) for activation WPA supplicants") |
| 58 | dhcpwait := flag.Int("dw", 20, "Wait time (sec) for activation DHCP clients") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 59 | dhcpservip := flag.String("s", "182.21.0.1", "DHCP Server IP Address") |
| 60 | intvl := flag.Int("v", 1, "Interval each Indication") |
| 61 | intvl_test := flag.Int("V", 1, "Interval each Indication") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 62 | kafkaBroker := flag.String("k", "", "Kafka broker") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 63 | o.Mode = DEFAULT |
Mahir Gunyel | 0918334 | 2019-01-29 14:26:50 -0800 | [diff] [blame^] | 64 | debg := flag.String("d", "DEBUG", "Debug Level(TRACE DEBUG INFO WARN ERROR)") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 65 | flag.Parse() |
| 66 | if *modeopt == "aaa" { |
| 67 | o.Mode = AAA |
| 68 | } else if *modeopt == "both" { |
| 69 | o.Mode = BOTH |
| 70 | } |
Mahir Gunyel | 0918334 | 2019-01-29 14:26:50 -0800 | [diff] [blame^] | 71 | o.Debuglvl = *debg |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 72 | o.oltid = uint32(*oltid) |
| 73 | o.npon = uint32(*npon) |
| 74 | o.nonus = uint32(*nonus) |
| 75 | o.aaawait = *aaawait |
| 76 | o.dhcpwait = *dhcpwait |
| 77 | o.dhcpservip = *dhcpservip |
| 78 | o.intvl = *intvl |
| 79 | o.intvl_test = *intvl_test |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 80 | o.KafkaBroker = *kafkaBroker |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 81 | o.address = (strings.Split(*addressport, ":")[0]) |
| 82 | tmp, _ := strconv.Atoi(strings.Split(*addressport, ":")[1]) |
| 83 | o.port = uint32(tmp) |
| 84 | return o |
| 85 | } |
| 86 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 87 | type handler struct { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 88 | dst device.DeviceState |
| 89 | src device.DeviceState |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 90 | method func(s *Server) error |
| 91 | } |
| 92 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 93 | type mediator struct { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 94 | opt *option |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 95 | server *Server |
| 96 | tester *Tester |
| 97 | } |
| 98 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 99 | func NewMediator(o *option) *mediator { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 100 | m := new(mediator) |
| 101 | m.opt = o |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 102 | logger.WithFields(log.Fields{ |
| 103 | "ip": o.address, |
| 104 | "baseport": o.port, |
| 105 | "pon_ports": o.npon, |
| 106 | "onus": o.nonus, |
| 107 | "mode": o.Mode, |
| 108 | }).Debug("New mediator") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 109 | return m |
| 110 | } |
| 111 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 112 | func (m *mediator) Start() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 113 | var wg sync.WaitGroup |
| 114 | opt := m.opt |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 115 | server := NewCore(opt) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 116 | wg.Add(1) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 117 | go func() { |
| 118 | if err := server.Start(); err != nil { //Blocking |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 119 | logger.Error("Start %s", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 120 | } |
| 121 | wg.Done() |
| 122 | return |
| 123 | }() |
| 124 | |
| 125 | tester := NewTester(opt) |
| 126 | m.server = server |
| 127 | m.tester = tester |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 128 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 129 | go func() { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 130 | m.Mediate(server) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 131 | }() |
| 132 | |
| 133 | c := make(chan os.Signal, 1) |
| 134 | signal.Notify(c, os.Interrupt) |
| 135 | go func() { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 136 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 137 | logger.Debug("SIGINT catcher Done") |
| 138 | wg.Done() |
| 139 | }() |
| 140 | for sig := range c { |
| 141 | wg.Add(1) |
| 142 | fmt.Println("SIGINT", sig) |
| 143 | close(c) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 144 | server.Stop() //Non-blocking |
| 145 | tester.Stop(server) //Non-blocking |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 146 | return |
| 147 | } |
| 148 | }() |
| 149 | wg.Wait() |
| 150 | logger.Debug("Reach to the end line") |
| 151 | } |
| 152 | |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 153 | func (m *mediator) Mediate(s *Server) { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 154 | defer logger.Debug("Mediate Done") |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 155 | for sr := range m.server.stateRepCh { |
| 156 | next := sr.next |
| 157 | current := sr.current |
| 158 | dev := sr.device |
| 159 | if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}){ |
| 160 | logger.Debug("Received OLT Device %v Current: %d Next: %d", dev, current, next) |
| 161 | if err := transitOlt(s, current, next, m.tester, m.opt); err != nil { |
| 162 | logger.Error("%v", err) |
| 163 | } |
| 164 | } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) { |
| 165 | logger.Debug("Received ONU Device %v Current: %d Next: %d", dev, current, next) |
| 166 | key := dev.GetDevkey() |
| 167 | if err := transitOnu(s, key, current, next, m.tester, m.opt); err != nil { |
| 168 | logger.Error("%v", err) |
| 169 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 170 | } |
| 171 | } |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 172 | } |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 173 | |
| 174 | func transitOlt (s *Server, current device.DeviceState, next device.DeviceState, tester *Tester, o *option) error { |
| 175 | if current == device.OLT_PREACTIVE && next == device.OLT_ACTIVE { |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 176 | |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 177 | } else if current == device.OLT_ACTIVE && next == device.OLT_PREACTIVE{ |
| 178 | tester.Stop(s) |
| 179 | } |
| 180 | return nil |
| 181 | } |
| 182 | |
| 183 | func transitOnu (s *Server, key device.Devkey, current device.DeviceState, next device.DeviceState, tester *Tester, o *option) error { |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 184 | if current == device.ONU_ACTIVE && next == device.ONU_OMCIACTIVE { |
| 185 | if s.isAllOnuOmciActive(){ //TODO: This should be per-ONU control, not by cheking All ONU's status |
| 186 | tester.Start(s) |
| 187 | } |
| 188 | } else if (current == device.ONU_OMCIACTIVE || current == device.ONU_ACTIVE) && |
| 189 | next == device.ONU_INACTIVE { |
| 190 | } |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 191 | return nil |
Mahir Gunyel | 0918334 | 2019-01-29 14:26:50 -0800 | [diff] [blame^] | 192 | } |