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" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 23 | "gerrit.opencord.org/voltha-bbsim/device" |
| 24 | "gerrit.opencord.org/voltha-bbsim/protos" |
| 25 | "github.com/google/gopacket" |
| 26 | "github.com/google/gopacket/layers" |
| 27 | "golang.org/x/net/context" |
| 28 | "google.golang.org/grpc" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | // gRPC Service |
| 32 | func (s *Server) DisableOlt(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 33 | logger.Info("OLT receives DisableOLT()\n") |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 34 | if s.EnableServer != nil { |
| 35 | if err := sendOltIndDown(*s.EnableServer); err != nil { |
| 36 | return new(openolt.Empty), err |
| 37 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 38 | logger.Info("Successfuly sent OLT DOWN indication") |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 39 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 40 | return new(openolt.Empty), nil |
| 41 | } |
| 42 | |
| 43 | func (s *Server) ReenableOlt(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 44 | logger.Info("OLT receives Reenable()\n") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 45 | return new(openolt.Empty), nil |
| 46 | } |
| 47 | |
| 48 | func (s *Server) CollectStatistics(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 49 | logger.Info("OLT receives CollectStatistics()\n") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 50 | return new(openolt.Empty), nil |
| 51 | } |
| 52 | |
| 53 | func (s *Server) GetDeviceInfo(c context.Context, empty *openolt.Empty) (*openolt.DeviceInfo, error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 54 | logger.Info("OLT receives GetDeviceInfo()\n") |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 55 | devinfo := new(openolt.DeviceInfo) |
| 56 | devinfo.Vendor = "CORD" |
| 57 | devinfo.OnuIdStart = 0 |
| 58 | devinfo.OnuIdEnd = 3 |
| 59 | devinfo.PonPorts = 4 |
| 60 | return devinfo, nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | func (s *Server) ActivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 64 | logger.Info("OLT receives ActivateONU()\n") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 65 | result := device.ValidateONU(*onu, s.Onumap) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 66 | if result == true { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 67 | matched, error := getOnuBySN(s.Onumap, onu.SerialNumber) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 68 | if error != nil { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame^] | 69 | logger.Fatal("%s\n", error) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 70 | } |
| 71 | onuid := onu.OnuId |
| 72 | matched.OnuID = onuid |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 73 | matched.UpdateIntStatus(device.ONU_ACTIVATED) |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 74 | logger.Info("ONU IntfID: %d OnuID: %d activated succesufully.\n", onu.IntfId, onu.OnuId) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 75 | } |
| 76 | return new(openolt.Empty), nil |
| 77 | } |
| 78 | |
| 79 | func (s *Server) DeactivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 80 | logger.Info("OLT receives DeactivateONU()\n") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 81 | return new(openolt.Empty), nil |
| 82 | } |
| 83 | |
| 84 | func (s *Server) DeleteOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 85 | logger.Info("OLT receives DeleteONU()\n") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 86 | return new(openolt.Empty), nil |
| 87 | } |
| 88 | |
| 89 | func (s *Server) OmciMsgOut(c context.Context, msg *openolt.OmciMsg) (*openolt.Empty, error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 90 | logger.Info("OLT %d receives OmciMsgOut to IF %v (ONU-ID: %v) pkt:%x.\n", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 91 | //s.olt.Queue = append(s.olt.Queue, *msg) |
| 92 | return new(openolt.Empty), nil |
| 93 | } |
| 94 | |
| 95 | func (s *Server) OnuPacketOut(c context.Context, packet *openolt.OnuPacket) (*openolt.Empty, error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 96 | logger.Info("OLT %d receives OnuPacketOut () to IF-ID:%d ONU-ID %d.\n", s.Olt.ID, packet.IntfId, packet.OnuId) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 97 | onuid := packet.OnuId |
| 98 | intfid := packet.IntfId |
| 99 | rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default) |
| 100 | if err := s.onuPacketOut(intfid, onuid, rawpkt); err != nil { |
| 101 | return new(openolt.Empty), err |
| 102 | } |
| 103 | return new(openolt.Empty), nil |
| 104 | } |
| 105 | |
| 106 | func (s *Server) UplinkPacketOut(c context.Context, packet *openolt.UplinkPacket) (*openolt.Empty, error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 107 | logger.Info("OLT %d receives UplinkPacketOut().\n", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 108 | rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default) |
| 109 | if err := s.uplinkPacketOut(rawpkt); err != nil { |
| 110 | return new(openolt.Empty), err |
| 111 | } |
| 112 | return new(openolt.Empty), nil |
| 113 | } |
| 114 | |
| 115 | func (s *Server) FlowAdd(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 116 | logger.Info("OLT %d receives FlowAdd().\n", s.Olt.ID) |
| 117 | logger.Debug("Flow's ONU-ID: %d, CTAG: %d\n", flow.OnuId, flow.Action.IVid) |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 118 | //onuid := uint32(flow.OnuId) |
| 119 | //ctag := flow.Action.IVid |
| 120 | //s.CtagMap[onuid] = ctag |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 121 | return new(openolt.Empty), nil |
| 122 | } |
| 123 | |
| 124 | func (s *Server) FlowRemove(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 125 | logger.Info("OLT %d receives FlowRemove().\n", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 126 | return new(openolt.Empty), nil |
| 127 | } |
| 128 | |
| 129 | func (s *Server) HeartbeatCheck(c context.Context, empty *openolt.Empty) (*openolt.Heartbeat, error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 130 | logger.Info("OLT %d receives HeartbeatCheck().\n", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 131 | signature := new(openolt.Heartbeat) |
| 132 | signature.HeartbeatSignature = s.Olt.HeartbeatSignature |
| 133 | return signature, nil |
| 134 | } |
| 135 | |
| 136 | func (s *Server) EnablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 137 | logger.Info("OLT %d receives EnablePonIf().\n", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 138 | return new(openolt.Empty), nil |
| 139 | } |
| 140 | |
| 141 | func (s *Server) DisablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 142 | logger.Info("OLT %d receives DisablePonIf().\n", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 143 | return new(openolt.Empty), nil |
| 144 | } |
| 145 | |
| 146 | func (s *Server) Reboot(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 147 | logger.Info("OLT %d receives Reboot ().\n", s.Olt.ID) |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 148 | // Initialize OLT & Env |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 149 | logger.Debug("Initialized by Reboot") |
| 150 | s.Disable() |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 151 | return new(openolt.Empty), nil |
| 152 | } |
| 153 | |
| 154 | func (s *Server) EnableIndication(empty *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 155 | logger.Info("OLT receives EnableInd.\n") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 156 | defer func() { |
| 157 | logger.Debug("grpc EnableIndication Done") |
| 158 | }() |
| 159 | if err := s.Enable(&stream); err != nil { |
| 160 | logger.Error("Failed to Enable Core: %v\n", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 161 | return err |
| 162 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 163 | return nil |
| 164 | } |
| 165 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 166 | func NewGrpcServer(addrport string) (l net.Listener, g *grpc.Server, e error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame] | 167 | logger.Info("Listening %s ...", addrport) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 168 | g = grpc.NewServer() |
| 169 | l, e = net.Listen("tcp", addrport) |
| 170 | return |
| 171 | } |