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