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