Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +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 | "net" |
Mahir Gunyel | 0fc2b74 | 2019-02-20 16:45:30 -0800 | [diff] [blame] | 21 | "strconv" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 22 | |
| 23 | "gerrit.opencord.org/voltha-bbsim/common/logger" |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 24 | "gerrit.opencord.org/voltha-bbsim/common/utils" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 25 | "gerrit.opencord.org/voltha-bbsim/device" |
| 26 | "gerrit.opencord.org/voltha-bbsim/protos" |
| 27 | "github.com/google/gopacket" |
| 28 | "github.com/google/gopacket/layers" |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 29 | omci "github.com/opencord/omci-sim" |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 30 | log "github.com/sirupsen/logrus" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 31 | "golang.org/x/net/context" |
| 32 | "google.golang.org/grpc" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | // gRPC Service |
| 36 | func (s *Server) DisableOlt(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 37 | logger.Debug("OLT receives DisableOLT()") |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 38 | if s.EnableServer != nil { |
| 39 | if err := sendOltIndDown(*s.EnableServer); err != nil { |
| 40 | return new(openolt.Empty), err |
| 41 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 42 | logger.Debug("Successfuly sent OLT DOWN indication") |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 43 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 44 | return new(openolt.Empty), nil |
| 45 | } |
| 46 | |
| 47 | func (s *Server) ReenableOlt(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 48 | logger.Debug("OLT receives Reenable()") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 49 | return new(openolt.Empty), nil |
| 50 | } |
| 51 | |
| 52 | func (s *Server) CollectStatistics(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 53 | logger.Debug("OLT receives CollectStatistics()") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 54 | return new(openolt.Empty), nil |
| 55 | } |
| 56 | |
| 57 | func (s *Server) GetDeviceInfo(c context.Context, empty *openolt.Empty) (*openolt.DeviceInfo, error) { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 58 | logger.Debug("OLT receives GetDeviceInfo()") |
| 59 | devinfo := new(openolt.DeviceInfo) |
| 60 | devinfo.Vendor = "EdgeCore" |
| 61 | devinfo.Model = "asfvolt16" |
| 62 | devinfo.HardwareVersion = "" |
| 63 | devinfo.FirmwareVersion = "" |
| 64 | devinfo.Technology = "xgspon" |
Shad Ansari | 704c6f2 | 2018-11-13 14:57:53 -0800 | [diff] [blame] | 65 | devinfo.PonPorts = 16 |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 66 | devinfo.OnuIdStart = 1 |
| 67 | devinfo.OnuIdEnd = 255 |
| 68 | devinfo.AllocIdStart = 1024 |
| 69 | devinfo.AllocIdEnd = 16383 |
| 70 | devinfo.GemportIdStart = 1024 |
| 71 | devinfo.GemportIdEnd = 65535 |
| 72 | devinfo.FlowIdStart = 1 |
| 73 | devinfo.FlowIdEnd = 16383 |
Mahir Gunyel | 0fc2b74 | 2019-02-20 16:45:30 -0800 | [diff] [blame] | 74 | devinfo.DeviceSerialNumber = "BBSIMOLT00"+strconv.FormatInt(int64(s.Olt.ID), 10) |
| 75 | |
Shad Ansari | 010c194 | 2018-11-08 09:32:24 -0800 | [diff] [blame] | 76 | |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 77 | return devinfo, nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | func (s *Server) ActivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 81 | logger.Debug("OLT receives ActivateONU()") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 82 | result := device.ValidateONU(*onu, s.Onumap) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 83 | if result == true { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 84 | matched, error := getOnuBySN(s.Onumap, onu.SerialNumber) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 85 | if error != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 86 | logger.Fatal("%s", error) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 87 | } |
| 88 | onuid := onu.OnuId |
| 89 | matched.OnuID = onuid |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame] | 90 | s.updateDevIntState(matched, device.ONU_ACTIVE) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 91 | logger.Debug("ONU IntfID: %d OnuID: %d activated succesufully.", onu.IntfId, onu.OnuId) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 92 | } |
| 93 | return new(openolt.Empty), nil |
| 94 | } |
| 95 | |
Shad Ansari | fd29844 | 2019-01-08 16:19:35 -0800 | [diff] [blame] | 96 | func (s *Server) CreateTconts(c context.Context, tconts *openolt.Tconts) (*openolt.Empty, error) { |
| 97 | logger.Debug("OLT receives CreateTconts()") |
| 98 | return new(openolt.Empty), nil |
| 99 | } |
| 100 | |
| 101 | func (s *Server) RemoveTconts(c context.Context, tconts *openolt.Tconts) (*openolt.Empty, error) { |
| 102 | logger.Debug("OLT receives RemoveTconts()") |
| 103 | return new(openolt.Empty), nil |
| 104 | } |
| 105 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 106 | func (s *Server) DeactivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 107 | logger.Debug("OLT receives DeactivateONU()") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 108 | return new(openolt.Empty), nil |
| 109 | } |
| 110 | |
| 111 | func (s *Server) DeleteOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 112 | logger.Debug("OLT receives DeleteONU()") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 113 | return new(openolt.Empty), nil |
| 114 | } |
| 115 | |
| 116 | func (s *Server) OmciMsgOut(c context.Context, msg *openolt.OmciMsg) (*openolt.Empty, error) { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 117 | logger.Debug("OLT %d receives OmciMsgOut to IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt) |
Shad Ansari | a5c7989 | 2018-11-29 16:32:59 -0800 | [diff] [blame] | 118 | s.omciOut <- *msg |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 119 | return new(openolt.Empty), nil |
| 120 | } |
| 121 | |
| 122 | func (s *Server) OnuPacketOut(c context.Context, packet *openolt.OnuPacket) (*openolt.Empty, error) { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 123 | onu, _ := s.GetOnuByID(packet.OnuId) |
Matteo Scandolo | 2a65914 | 2018-11-15 11:23:45 -0800 | [diff] [blame] | 124 | utils.LoggerWithOnu(onu).Debugf("OLT %d receives OnuPacketOut () to IF-ID:%d ONU-ID %d.", s.Olt.ID, packet.IntfId, packet.OnuId) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 125 | onuid := packet.OnuId |
| 126 | intfid := packet.IntfId |
| 127 | rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default) |
| 128 | if err := s.onuPacketOut(intfid, onuid, rawpkt); err != nil { |
Matteo Scandolo | a286c74 | 2018-11-20 08:10:04 -0800 | [diff] [blame] | 129 | utils.LoggerWithOnu(onu).WithField("error", err).Errorf("OnuPacketOut Error ") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 130 | return new(openolt.Empty), err |
| 131 | } |
| 132 | return new(openolt.Empty), nil |
| 133 | } |
| 134 | |
| 135 | func (s *Server) UplinkPacketOut(c context.Context, packet *openolt.UplinkPacket) (*openolt.Empty, error) { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 136 | logger.Debug("OLT %d receives UplinkPacketOut().", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 137 | rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default) |
| 138 | if err := s.uplinkPacketOut(rawpkt); err != nil { |
| 139 | return new(openolt.Empty), err |
| 140 | } |
| 141 | return new(openolt.Empty), nil |
| 142 | } |
| 143 | |
| 144 | func (s *Server) FlowAdd(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) { |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame^] | 145 | logger.Debug("OLT %d receives FlowAdd() IntfID:%d OnuID:%d EType:%x GemPortID:%d", s.Olt.ID, flow.AccessIntfId, flow.OnuId, flow.Classifier.EthType, flow.GemportId) |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 146 | onu, err := s.GetOnuByID(uint32(flow.OnuId)) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 147 | |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 148 | if err == nil { |
| 149 | intfid := onu.IntfID |
| 150 | onuid := onu.OnuID |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame^] | 151 | onu.GemportID = uint16(flow.GemportId) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 152 | |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 153 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 154 | "olt": s.Olt.ID, |
| 155 | "c_tag": flow.Action.IVid, |
| 156 | }).Debug("OLT receives FlowAdd().") |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 157 | |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 158 | if flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) { |
Keita NISHIMOTO | 42db49e | 2019-01-29 07:49:41 +0900 | [diff] [blame] | 159 | omcistate := omci.GetOnuOmciState(onu.IntfID, onu.OnuID) |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame^] | 160 | if omcistate != omci.DONE { |
| 161 | logger.Warn("FlowAdd() OMCI state %d is not \"DONE\"", omci.GetOnuOmciState(onu.OnuID, onu.IntfID)) |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 162 | } |
Keita NISHIMOTO | 26ebaa8 | 2019-03-07 10:00:35 +0900 | [diff] [blame^] | 163 | s.updateOnuIntState(intfid, onuid, device.ONU_OMCIACTIVE) |
Keita NISHIMOTO | 7bce769 | 2019-01-19 09:31:09 +0900 | [diff] [blame] | 164 | } |
| 165 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 166 | return new(openolt.Empty), nil |
| 167 | } |
| 168 | |
| 169 | func (s *Server) FlowRemove(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 170 | onu, _ := s.GetOnuByID(uint32(flow.OnuId)) |
| 171 | |
| 172 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 173 | "olt": s.Olt.ID, |
| 174 | "c_tag": flow.Action.IVid, |
| 175 | }).Debug("OLT receives FlowRemove().") |
| 176 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 177 | return new(openolt.Empty), nil |
| 178 | } |
| 179 | |
| 180 | func (s *Server) HeartbeatCheck(c context.Context, empty *openolt.Empty) (*openolt.Heartbeat, error) { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 181 | logger.Debug("OLT %d receives HeartbeatCheck().", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 182 | signature := new(openolt.Heartbeat) |
| 183 | signature.HeartbeatSignature = s.Olt.HeartbeatSignature |
| 184 | return signature, nil |
| 185 | } |
| 186 | |
| 187 | func (s *Server) EnablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 188 | logger.Debug("OLT %d receives EnablePonIf().", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 189 | return new(openolt.Empty), nil |
| 190 | } |
| 191 | |
| 192 | func (s *Server) DisablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 193 | logger.Debug("OLT %d receives DisablePonIf().", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 194 | return new(openolt.Empty), nil |
| 195 | } |
| 196 | |
| 197 | func (s *Server) Reboot(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 198 | logger.Debug("OLT %d receives Reboot ().", s.Olt.ID) |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 199 | // Initialize OLT & Env |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 200 | logger.Debug("Initialized by Reboot") |
| 201 | s.Disable() |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 202 | return new(openolt.Empty), nil |
| 203 | } |
| 204 | |
| 205 | func (s *Server) EnableIndication(empty *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 206 | logger.Debug("OLT receives EnableInd.") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 207 | defer func() { |
| 208 | logger.Debug("grpc EnableIndication Done") |
| 209 | }() |
| 210 | if err := s.Enable(&stream); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 211 | logger.Error("Failed to Enable Core: %v", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 212 | return err |
| 213 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 214 | return nil |
| 215 | } |
| 216 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 217 | func NewGrpcServer(addrport string) (l net.Listener, g *grpc.Server, e error) { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 218 | logger.Debug("Listening %s ...", addrport) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 219 | g = grpc.NewServer() |
| 220 | l, e = net.Listen("tcp", addrport) |
| 221 | return |
| 222 | } |