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 | 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 | } |
| 38 | log.Println("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) { |
| 44 | log.Printf("OLT receives Reenable()\n") |
| 45 | return new(openolt.Empty), nil |
| 46 | } |
| 47 | |
| 48 | func (s *Server) CollectStatistics(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) { |
| 49 | log.Printf("OLT receives CollectStatistics()\n") |
| 50 | return new(openolt.Empty), nil |
| 51 | } |
| 52 | |
| 53 | func (s *Server) GetDeviceInfo(c context.Context, empty *openolt.Empty) (*openolt.DeviceInfo, error) { |
| 54 | log.Printf("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) { |
| 64 | log.Printf("OLT receives ActivateONU()\n") |
| 65 | result := device.ValidateONU(*onu, s.Onumap) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 66 | if result == true { |
| 67 | matched, error := s.getOnuBySN(onu.SerialNumber) |
| 68 | if error != nil { |
| 69 | log.Fatalf("%s\n", error) |
| 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 | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 74 | log.Printf("ONU IntfID: %d OnuID: %d activated succesufully.\n", onu.IntfId, onu.OnuId) |
| 75 | } |
| 76 | return new(openolt.Empty), nil |
| 77 | } |
| 78 | |
| 79 | func (s *Server) DeactivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) { |
| 80 | log.Printf("OLT receives DeactivateONU()\n") |
| 81 | return new(openolt.Empty), nil |
| 82 | } |
| 83 | |
| 84 | func (s *Server) DeleteOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) { |
| 85 | log.Printf("OLT receives DeleteONU()\n") |
| 86 | return new(openolt.Empty), nil |
| 87 | } |
| 88 | |
| 89 | func (s *Server) OmciMsgOut(c context.Context, msg *openolt.OmciMsg) (*openolt.Empty, error) { |
| 90 | log.Printf("OLT %d receives OmciMsgOut to IF %v (ONU-ID: %v) pkt:%x.\n", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt) |
| 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) { |
| 96 | log.Printf("OLT %d receives OnuPacketOut () to IF-ID:%d ONU-ID %d.\n", s.Olt.ID, packet.IntfId, packet.OnuId) |
| 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) { |
| 107 | log.Printf("OLT %d receives UplinkPacketOut().\n", s.Olt.ID) |
| 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) { |
| 116 | log.Printf("OLT %d receives FlowAdd().\n", s.Olt.ID) |
Keita NISHIMOTO | b841749 | 2018-10-19 17:37:38 +0900 | [diff] [blame^] | 117 | log.Printf("Flow's ONU-ID: %d, CTAG: %d\n", flow.OnuId, flow.Action.IVid) |
| 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) { |
| 125 | log.Printf("OLT %d receives FlowRemove().\n", s.Olt.ID) |
| 126 | return new(openolt.Empty), nil |
| 127 | } |
| 128 | |
| 129 | func (s *Server) HeartbeatCheck(c context.Context, empty *openolt.Empty) (*openolt.Heartbeat, error) { |
| 130 | log.Printf("OLT %d receives HeartbeatCheck().\n", s.Olt.ID) |
| 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) { |
| 137 | log.Printf("OLT %d receives EnablePonIf().\n", s.Olt.ID) |
| 138 | return new(openolt.Empty), nil |
| 139 | } |
| 140 | |
| 141 | func (s *Server) DisablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) { |
| 142 | log.Printf("OLT %d receives DisablePonIf().\n", s.Olt.ID) |
| 143 | return new(openolt.Empty), nil |
| 144 | } |
| 145 | |
| 146 | func (s *Server) Reboot(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) { |
| 147 | log.Printf("OLT %d receives Reboot ().\n", s.Olt.ID) |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 148 | // Initialize OLT & Env |
| 149 | if s.TestFlag == true{ |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 150 | log.Println("Initialized by Reboot") |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 151 | cleanUpVeths(s.VethEnv) |
| 152 | close(s.Endchan) |
| 153 | processes := s.Processes |
| 154 | log.Println("processes:", processes) |
| 155 | killProcesses(processes) |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 156 | exec.Command("rm", "/var/run/dhcpd.pid").Run() |
| 157 | exec.Command("touch", "/var/run/dhcpd.pid").Run() |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 158 | s.Initialize() |
| 159 | } |
| 160 | olt := s.Olt |
| 161 | olt.InitializeStatus() |
| 162 | for intfid, _ := range s.Onumap{ |
| 163 | for _, onu := range s.Onumap[intfid] { |
| 164 | onu.InitializeStatus() |
| 165 | } |
| 166 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 167 | return new(openolt.Empty), nil |
| 168 | } |
| 169 | |
| 170 | func (s *Server) EnableIndication(empty *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error { |
| 171 | defer func() { |
| 172 | s.gRPCserver.Stop() |
| 173 | }() |
Keita NISHIMOTO | 9c6f6f1 | 2018-10-18 04:56:34 +0900 | [diff] [blame] | 174 | s.EnableServer = &stream |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 175 | log.Printf("OLT receives EnableInd.\n") |
| 176 | if err := s.activateOLT(stream); err != nil { |
| 177 | log.Printf("Failed to activate OLT: %v\n", err) |
| 178 | return err |
| 179 | } |
| 180 | log.Println("Core server down.") |
| 181 | return nil |
| 182 | } |
| 183 | |
| 184 | func CreateGrpcServer(oltid uint32, npon uint32, nonus uint32, addrport string) (l net.Listener, g *grpc.Server, e error) { |
| 185 | log.Printf("Listening %s ...", addrport) |
| 186 | g = grpc.NewServer() |
| 187 | l, e = net.Listen("tcp", addrport) |
| 188 | return |
| 189 | } |