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" |
| 21 | |
| 22 | "gerrit.opencord.org/voltha-bbsim/common/logger" |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 23 | "gerrit.opencord.org/voltha-bbsim/common/utils" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 24 | "gerrit.opencord.org/voltha-bbsim/device" |
| 25 | "gerrit.opencord.org/voltha-bbsim/protos" |
| 26 | "github.com/google/gopacket" |
| 27 | "github.com/google/gopacket/layers" |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 28 | log "github.com/sirupsen/logrus" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 29 | "golang.org/x/net/context" |
| 30 | "google.golang.org/grpc" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | // gRPC Service |
| 34 | 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] | 35 | logger.Debug("OLT receives DisableOLT()") |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 36 | if s.EnableServer != nil { |
| 37 | if err := sendOltIndDown(*s.EnableServer); err != nil { |
| 38 | return new(openolt.Empty), err |
| 39 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 40 | logger.Debug("Successfuly sent OLT DOWN indication") |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 41 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 42 | return new(openolt.Empty), nil |
| 43 | } |
| 44 | |
| 45 | 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] | 46 | logger.Debug("OLT receives Reenable()") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 47 | return new(openolt.Empty), nil |
| 48 | } |
| 49 | |
| 50 | 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] | 51 | logger.Debug("OLT receives CollectStatistics()") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 52 | return new(openolt.Empty), nil |
| 53 | } |
| 54 | |
| 55 | 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] | 56 | logger.Debug("OLT receives GetDeviceInfo()") |
| 57 | devinfo := new(openolt.DeviceInfo) |
| 58 | devinfo.Vendor = "EdgeCore" |
| 59 | devinfo.Model = "asfvolt16" |
| 60 | devinfo.HardwareVersion = "" |
| 61 | devinfo.FirmwareVersion = "" |
| 62 | devinfo.Technology = "xgspon" |
Shad Ansari | 704c6f2 | 2018-11-13 14:57:53 -0800 | [diff] [blame] | 63 | devinfo.PonPorts = 16 |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 64 | devinfo.OnuIdStart = 1 |
| 65 | devinfo.OnuIdEnd = 255 |
| 66 | devinfo.AllocIdStart = 1024 |
| 67 | devinfo.AllocIdEnd = 16383 |
| 68 | devinfo.GemportIdStart = 1024 |
| 69 | devinfo.GemportIdEnd = 65535 |
| 70 | devinfo.FlowIdStart = 1 |
| 71 | devinfo.FlowIdEnd = 16383 |
Shad Ansari | 010c194 | 2018-11-08 09:32:24 -0800 | [diff] [blame] | 72 | |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 73 | return devinfo, nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | 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] | 77 | logger.Debug("OLT receives ActivateONU()") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 78 | result := device.ValidateONU(*onu, s.Onumap) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 79 | if result == true { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 80 | matched, error := getOnuBySN(s.Onumap, onu.SerialNumber) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 81 | if error != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 82 | logger.Fatal("%s", error) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 83 | } |
| 84 | onuid := onu.OnuId |
| 85 | matched.OnuID = onuid |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 86 | matched.UpdateIntStatus(device.ONU_ACTIVATED) |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 87 | 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] | 88 | } |
| 89 | return new(openolt.Empty), nil |
| 90 | } |
| 91 | |
| 92 | 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] | 93 | logger.Debug("OLT receives DeactivateONU()") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 94 | return new(openolt.Empty), nil |
| 95 | } |
| 96 | |
| 97 | 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] | 98 | logger.Debug("OLT receives DeleteONU()") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 99 | return new(openolt.Empty), nil |
| 100 | } |
| 101 | |
| 102 | func (s *Server) OmciMsgOut(c context.Context, msg *openolt.OmciMsg) (*openolt.Empty, error) { |
Shad Ansari | 70aafb0 | 2018-11-14 22:28:17 -0800 | [diff] [blame^] | 103 | var resp OmciMsg |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 104 | 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 | ef43627 | 2018-11-14 15:58:20 -0800 | [diff] [blame] | 105 | resp.IntfId = msg.IntfId |
| 106 | resp.OnuId = msg.OnuId |
| 107 | resp.Pkt = make([]byte, len(msg.Pkt)) |
Shad Ansari | 70aafb0 | 2018-11-14 22:28:17 -0800 | [diff] [blame^] | 108 | s.omciOut <- resp |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 109 | return new(openolt.Empty), nil |
| 110 | } |
| 111 | |
| 112 | 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] | 113 | onu, _ := s.GetOnuByID(packet.OnuId) |
| 114 | utils.LoggerWithOnu(onu).Debug("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] | 115 | onuid := packet.OnuId |
| 116 | intfid := packet.IntfId |
| 117 | rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default) |
| 118 | if err := s.onuPacketOut(intfid, onuid, rawpkt); err != nil { |
| 119 | return new(openolt.Empty), err |
| 120 | } |
| 121 | return new(openolt.Empty), nil |
| 122 | } |
| 123 | |
| 124 | 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] | 125 | logger.Debug("OLT %d receives UplinkPacketOut().", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 126 | rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default) |
| 127 | if err := s.uplinkPacketOut(rawpkt); err != nil { |
| 128 | return new(openolt.Empty), err |
| 129 | } |
| 130 | return new(openolt.Empty), nil |
| 131 | } |
| 132 | |
| 133 | func (s *Server) FlowAdd(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 134 | |
| 135 | onu, _ := s.GetOnuByID(uint32(flow.OnuId)) |
| 136 | |
| 137 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 138 | "olt": s.Olt.ID, |
| 139 | "c_tag": flow.Action.IVid, |
| 140 | }).Debug("OLT receives FlowAdd().") |
| 141 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 142 | return new(openolt.Empty), nil |
| 143 | } |
| 144 | |
| 145 | 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] | 146 | onu, _ := s.GetOnuByID(uint32(flow.OnuId)) |
| 147 | |
| 148 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 149 | "olt": s.Olt.ID, |
| 150 | "c_tag": flow.Action.IVid, |
| 151 | }).Debug("OLT receives FlowRemove().") |
| 152 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 153 | return new(openolt.Empty), nil |
| 154 | } |
| 155 | |
| 156 | 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] | 157 | logger.Debug("OLT %d receives HeartbeatCheck().", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 158 | signature := new(openolt.Heartbeat) |
| 159 | signature.HeartbeatSignature = s.Olt.HeartbeatSignature |
| 160 | return signature, nil |
| 161 | } |
| 162 | |
| 163 | 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] | 164 | logger.Debug("OLT %d receives EnablePonIf().", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 165 | return new(openolt.Empty), nil |
| 166 | } |
| 167 | |
| 168 | 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] | 169 | logger.Debug("OLT %d receives DisablePonIf().", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 170 | return new(openolt.Empty), nil |
| 171 | } |
| 172 | |
| 173 | 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] | 174 | logger.Debug("OLT %d receives Reboot ().", s.Olt.ID) |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 175 | // Initialize OLT & Env |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 176 | logger.Debug("Initialized by Reboot") |
| 177 | s.Disable() |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 178 | return new(openolt.Empty), nil |
| 179 | } |
| 180 | |
| 181 | func (s *Server) EnableIndication(empty *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 182 | logger.Debug("OLT receives EnableInd.") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 183 | defer func() { |
| 184 | logger.Debug("grpc EnableIndication Done") |
| 185 | }() |
| 186 | if err := s.Enable(&stream); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 187 | logger.Error("Failed to Enable Core: %v", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 188 | return err |
| 189 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 190 | return nil |
| 191 | } |
| 192 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 193 | func NewGrpcServer(addrport string) (l net.Listener, g *grpc.Server, e error) { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 194 | logger.Debug("Listening %s ...", addrport) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 195 | g = grpc.NewServer() |
| 196 | l, e = net.Listen("tcp", addrport) |
| 197 | return |
| 198 | } |