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 |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 44 | Mode Mode |
| 45 | KafkaBroker string |
Mahir Gunyel | 0918334 | 2019-01-29 14:26:50 -0800 | [diff] [blame] | 46 | Debuglvl string |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 47 | } |
| 48 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame^] | 49 | // GetOptions receives command line options and stores them in option structure |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 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 | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 59 | dhcpservip := flag.String("s", "182.21.0.128", "DHCP Server IP Address") |
Keita NISHIMOTO | d502e66 | 2019-03-01 12:02:51 +0900 | [diff] [blame] | 60 | intvl := flag.Int("v", 1000, "Interval each Indication (ms)") |
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 |
Mahir Gunyel | 0918334 | 2019-01-29 14:26:50 -0800 | [diff] [blame] | 63 | debg := flag.String("d", "DEBUG", "Debug Level(TRACE DEBUG INFO WARN ERROR)") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 64 | flag.Parse() |
| 65 | if *modeopt == "aaa" { |
| 66 | o.Mode = AAA |
| 67 | } else if *modeopt == "both" { |
| 68 | o.Mode = BOTH |
| 69 | } |
Mahir Gunyel | 0918334 | 2019-01-29 14:26:50 -0800 | [diff] [blame] | 70 | o.Debuglvl = *debg |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 71 | o.oltid = uint32(*oltid) |
| 72 | o.npon = uint32(*npon) |
| 73 | o.nonus = uint32(*nonus) |
| 74 | o.aaawait = *aaawait |
| 75 | o.dhcpwait = *dhcpwait |
| 76 | o.dhcpservip = *dhcpservip |
| 77 | o.intvl = *intvl |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 78 | o.KafkaBroker = *kafkaBroker |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 79 | o.address = (strings.Split(*addressport, ":")[0]) |
| 80 | tmp, _ := strconv.Atoi(strings.Split(*addressport, ":")[1]) |
| 81 | o.port = uint32(tmp) |
| 82 | return o |
| 83 | } |
| 84 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 85 | type mediator struct { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 86 | opt *option |
| 87 | server *Server |
| 88 | testmanager *TestManager |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 89 | } |
| 90 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame^] | 91 | // NewMediator returns a new mediator object |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 92 | func NewMediator(o *option) *mediator { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 93 | m := new(mediator) |
| 94 | m.opt = o |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 95 | logger.WithFields(log.Fields{ |
| 96 | "ip": o.address, |
| 97 | "baseport": o.port, |
| 98 | "pon_ports": o.npon, |
| 99 | "onus": o.nonus, |
| 100 | "mode": o.Mode, |
| 101 | }).Debug("New mediator") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 102 | return m |
| 103 | } |
| 104 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame^] | 105 | // Start mediator |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 106 | func (m *mediator) Start() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 107 | var wg sync.WaitGroup |
| 108 | opt := m.opt |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 109 | server := NewCore(opt) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 110 | wg.Add(1) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 111 | go func() { |
| 112 | if err := server.Start(); err != nil { //Blocking |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 113 | logger.Error("Start %s", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 114 | } |
| 115 | wg.Done() |
| 116 | return |
| 117 | }() |
| 118 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 119 | tm := NewTestManager(opt) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 120 | m.server = server |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 121 | m.testmanager = tm |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 122 | go func() { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 123 | m.Mediate() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 124 | }() |
| 125 | |
| 126 | c := make(chan os.Signal, 1) |
| 127 | signal.Notify(c, os.Interrupt) |
| 128 | go func() { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 129 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 130 | logger.Debug("SIGINT catcher Done") |
| 131 | wg.Done() |
| 132 | }() |
| 133 | for sig := range c { |
| 134 | wg.Add(1) |
| 135 | fmt.Println("SIGINT", sig) |
| 136 | close(c) |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 137 | server.Stop() //Non-blocking |
| 138 | tm.Stop() //Non-blocking |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 139 | return |
| 140 | } |
| 141 | }() |
| 142 | wg.Wait() |
| 143 | logger.Debug("Reach to the end line") |
| 144 | } |
| 145 | |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame^] | 146 | // Mediate method is invoked on OLT and ONU state change |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 147 | func (m *mediator) Mediate() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 148 | defer logger.Debug("Mediate Done") |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 149 | for sr := range m.server.stateRepCh { |
| 150 | next := sr.next |
| 151 | current := sr.current |
| 152 | dev := sr.device |
| 153 | if reflect.TypeOf(dev) == reflect.TypeOf(&device.Olt{}){ |
| 154 | logger.Debug("Received OLT Device %v Current: %d Next: %d", dev, current, next) |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 155 | if err := transitOlt(current, next, m.testmanager, m.opt); err != nil { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 156 | logger.Error("%v", err) |
| 157 | } |
| 158 | } else if reflect.TypeOf(dev) == reflect.TypeOf(&device.Onu{}) { |
| 159 | logger.Debug("Received ONU Device %v Current: %d Next: %d", dev, current, next) |
| 160 | key := dev.GetDevkey() |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 161 | if err := transitOnu(key, current, next, m.testmanager, m.opt); err != nil { |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 162 | logger.Error("%v", err) |
| 163 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 164 | } |
| 165 | } |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 166 | } |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 167 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 168 | func transitOlt (current device.DeviceState, next device.DeviceState, tm *TestManager, o *option) error { |
| 169 | logger.Debug("trnsitOlt called current:%d , next:%d", current, next) |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 170 | if current == device.OLT_PREACTIVE && next == device.OLT_ACTIVE { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 171 | tm.Start() |
Mahir Gunyel | 639c4f7 | 2019-04-26 12:05:32 -0700 | [diff] [blame] | 172 | nniup, _ := makeNniName(o.oltid) |
| 173 | activateDHCPServer(nniup, o.dhcpservip) |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 174 | } else if current == device.OLT_ACTIVE && next == device.OLT_PREACTIVE{ |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 175 | tm.Stop() |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 176 | } |
| 177 | return nil |
| 178 | } |
| 179 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 180 | func transitOnu (key device.Devkey, current device.DeviceState, next device.DeviceState, tm *TestManager, o *option) error { |
Zack Williams | b85f593 | 2019-05-10 16:21:35 -0700 | [diff] [blame] | 181 | logger.Debug("trnsitOnu called with key: %v, current: %d, next: %d", key, current, next) |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 182 | if current == device.ONU_ACTIVE && next == device.ONU_OMCIACTIVE { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 183 | t := tm.CreateTester(o, key) |
| 184 | if err := tm.StartTester(key, t); err != nil { |
| 185 | logger.Error("Cannot Start Executer error:%v", err) |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 186 | } |
Zdravko Bozakov | 8b9328c | 2019-05-15 05:13:34 +0200 | [diff] [blame^] | 187 | } else if current == device.ONU_OMCIACTIVE && next == device.ONU_INACTIVE { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 188 | if err := tm.StopTester(key); err != nil { |
| 189 | logger.Error("Cannot Start Executer error:%v", err) |
| 190 | } |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 191 | } |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 192 | return nil |
Mahir Gunyel | 0918334 | 2019-01-29 14:26:50 -0800 | [diff] [blame] | 193 | } |