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 ( |
| 20 | "gerrit.opencord.org/voltha-bbsim/device" |
| 21 | "gerrit.opencord.org/voltha-bbsim/protos" |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 22 | "gerrit.opencord.org/voltha-bbsim/common" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 23 | "github.com/google/gopacket" |
| 24 | "github.com/google/gopacket/layers" |
| 25 | "golang.org/x/net/context" |
| 26 | "google.golang.org/grpc" |
| 27 | "log" |
| 28 | "net" |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 29 | "os/exec" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | // gRPC Service |
| 33 | 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^] | 34 | logger.Info("OLT receives DisableOLT()\n") |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 35 | if s.EnableServer != nil { |
| 36 | if err := sendOltIndDown(*s.EnableServer); err != nil { |
| 37 | return new(openolt.Empty), err |
| 38 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 39 | logger.Info("Successfuly sent OLT DOWN indication") |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 40 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 41 | return new(openolt.Empty), nil |
| 42 | } |
| 43 | |
| 44 | 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^] | 45 | logger.Info("OLT receives Reenable()\n") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 46 | return new(openolt.Empty), nil |
| 47 | } |
| 48 | |
| 49 | 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^] | 50 | logger.Info("OLT receives CollectStatistics()\n") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 51 | return new(openolt.Empty), nil |
| 52 | } |
| 53 | |
| 54 | 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^] | 55 | logger.Info("OLT receives GetDeviceInfo()\n") |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 56 | devinfo := new(openolt.DeviceInfo) |
| 57 | devinfo.Vendor = "CORD" |
| 58 | devinfo.OnuIdStart = 0 |
| 59 | devinfo.OnuIdEnd = 3 |
| 60 | devinfo.PonPorts = 4 |
| 61 | return devinfo, nil |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | 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^] | 65 | logger.Info("OLT receives ActivateONU()\n") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 66 | result := device.ValidateONU(*onu, s.Onumap) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 67 | if result == true { |
| 68 | matched, error := s.getOnuBySN(onu.SerialNumber) |
| 69 | if error != nil { |
| 70 | log.Fatalf("%s\n", error) |
| 71 | } |
| 72 | onuid := onu.OnuId |
| 73 | matched.OnuID = onuid |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame] | 74 | matched.UpdateIntStatus(device.ONU_ACTIVATED) |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 75 | 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] | 76 | } |
| 77 | return new(openolt.Empty), nil |
| 78 | } |
| 79 | |
| 80 | 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^] | 81 | logger.Info("OLT receives DeactivateONU()\n") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 82 | return new(openolt.Empty), nil |
| 83 | } |
| 84 | |
| 85 | 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^] | 86 | logger.Info("OLT receives DeleteONU()\n") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 87 | return new(openolt.Empty), nil |
| 88 | } |
| 89 | |
| 90 | 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^] | 91 | 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] | 92 | //s.olt.Queue = append(s.olt.Queue, *msg) |
| 93 | return new(openolt.Empty), nil |
| 94 | } |
| 95 | |
| 96 | 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^] | 97 | 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] | 98 | onuid := packet.OnuId |
| 99 | intfid := packet.IntfId |
| 100 | rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default) |
| 101 | if err := s.onuPacketOut(intfid, onuid, rawpkt); err != nil { |
| 102 | return new(openolt.Empty), err |
| 103 | } |
| 104 | return new(openolt.Empty), nil |
| 105 | } |
| 106 | |
| 107 | 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^] | 108 | logger.Info("OLT %d receives UplinkPacketOut().\n", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 109 | rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default) |
| 110 | if err := s.uplinkPacketOut(rawpkt); err != nil { |
| 111 | return new(openolt.Empty), err |
| 112 | } |
| 113 | return new(openolt.Empty), nil |
| 114 | } |
| 115 | |
| 116 | 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^] | 117 | logger.Info("OLT %d receives FlowAdd().\n", s.Olt.ID) |
| 118 | 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] | 119 | //onuid := uint32(flow.OnuId) |
| 120 | //ctag := flow.Action.IVid |
| 121 | //s.CtagMap[onuid] = ctag |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 122 | return new(openolt.Empty), nil |
| 123 | } |
| 124 | |
| 125 | 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^] | 126 | logger.Info("OLT %d receives FlowRemove().\n", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 127 | return new(openolt.Empty), nil |
| 128 | } |
| 129 | |
| 130 | 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^] | 131 | logger.Info("OLT %d receives HeartbeatCheck().\n", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 132 | signature := new(openolt.Heartbeat) |
| 133 | signature.HeartbeatSignature = s.Olt.HeartbeatSignature |
| 134 | return signature, nil |
| 135 | } |
| 136 | |
| 137 | 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^] | 138 | logger.Info("OLT %d receives EnablePonIf().\n", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 139 | return new(openolt.Empty), nil |
| 140 | } |
| 141 | |
| 142 | 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^] | 143 | logger.Info("OLT %d receives DisablePonIf().\n", s.Olt.ID) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 144 | return new(openolt.Empty), nil |
| 145 | } |
| 146 | |
| 147 | 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^] | 148 | logger.Info("OLT %d receives Reboot ().\n", s.Olt.ID) |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 149 | // Initialize OLT & Env |
| 150 | if s.TestFlag == true{ |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 151 | logger.Debug("Initialized by Reboot") |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 152 | cleanUpVeths(s.VethEnv) |
| 153 | close(s.Endchan) |
| 154 | processes := s.Processes |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 155 | logger.Debug("Runnig Processes:", processes) |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 156 | killProcesses(processes) |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 157 | exec.Command("rm", "/var/run/dhcpd.pid").Run() //This is for DHCP server activation |
| 158 | exec.Command("touch", "/var/run/dhcpd.pid").Run() //This is for DHCP server activation |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 159 | s.Initialize() |
| 160 | } |
| 161 | olt := s.Olt |
| 162 | olt.InitializeStatus() |
| 163 | for intfid, _ := range s.Onumap{ |
| 164 | for _, onu := range s.Onumap[intfid] { |
| 165 | onu.InitializeStatus() |
| 166 | } |
| 167 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 168 | return new(openolt.Empty), nil |
| 169 | } |
| 170 | |
| 171 | func (s *Server) EnableIndication(empty *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error { |
| 172 | defer func() { |
| 173 | s.gRPCserver.Stop() |
| 174 | }() |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 175 | s.EnableServer = &stream |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 176 | logger.Info("OLT receives EnableInd.\n") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 177 | if err := s.activateOLT(stream); err != nil { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 178 | logger.Error("Failed to activate OLT: %v\n", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 179 | return err |
| 180 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 181 | logger.Debug("Core server down.") |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 182 | return nil |
| 183 | } |
| 184 | |
| 185 | func CreateGrpcServer(oltid uint32, npon uint32, nonus uint32, addrport string) (l net.Listener, g *grpc.Server, e error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 186 | logger.Info("Listening %s ...", addrport) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 187 | g = grpc.NewServer() |
| 188 | l, e = net.Listen("tcp", addrport) |
| 189 | return |
| 190 | } |