blob: dce608ba5b2515873f9e4a3a20637026370ab91f [file] [log] [blame]
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +09001/*
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
17package core
18
19import (
20 "gerrit.opencord.org/voltha-bbsim/device"
21 "gerrit.opencord.org/voltha-bbsim/protos"
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090022 "gerrit.opencord.org/voltha-bbsim/common"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090023 "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 NISHIMOTO9c6f6f12018-10-18 04:56:34 +090029 "os/exec"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090030)
31
32// gRPC Service
33func (s *Server) DisableOlt(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090034 logger.Info("OLT receives DisableOLT()\n")
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090035 if s.EnableServer != nil {
36 if err := sendOltIndDown(*s.EnableServer); err != nil {
37 return new(openolt.Empty), err
38 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090039 logger.Info("Successfuly sent OLT DOWN indication")
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090040 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090041 return new(openolt.Empty), nil
42}
43
44func (s *Server) ReenableOlt(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090045 logger.Info("OLT receives Reenable()\n")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090046 return new(openolt.Empty), nil
47}
48
49func (s *Server) CollectStatistics(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090050 logger.Info("OLT receives CollectStatistics()\n")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090051 return new(openolt.Empty), nil
52}
53
54func (s *Server) GetDeviceInfo(c context.Context, empty *openolt.Empty) (*openolt.DeviceInfo, error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090055 logger.Info("OLT receives GetDeviceInfo()\n")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090056 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 NISHIMOTO3b8b9c02018-10-09 09:40:01 +090062}
63
64func (s *Server) ActivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090065 logger.Info("OLT receives ActivateONU()\n")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090066 result := device.ValidateONU(*onu, s.Onumap)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090067 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 NISHIMOTOb8417492018-10-19 17:37:38 +090074 matched.UpdateIntStatus(device.ONU_ACTIVATED)
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090075 logger.Info("ONU IntfID: %d OnuID: %d activated succesufully.\n", onu.IntfId, onu.OnuId)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090076 }
77 return new(openolt.Empty), nil
78}
79
80func (s *Server) DeactivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090081 logger.Info("OLT receives DeactivateONU()\n")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090082 return new(openolt.Empty), nil
83}
84
85func (s *Server) DeleteOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090086 logger.Info("OLT receives DeleteONU()\n")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090087 return new(openolt.Empty), nil
88}
89
90func (s *Server) OmciMsgOut(c context.Context, msg *openolt.OmciMsg) (*openolt.Empty, error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090091 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 NISHIMOTO3b8b9c02018-10-09 09:40:01 +090092 //s.olt.Queue = append(s.olt.Queue, *msg)
93 return new(openolt.Empty), nil
94}
95
96func (s *Server) OnuPacketOut(c context.Context, packet *openolt.OnuPacket) (*openolt.Empty, error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090097 logger.Info("OLT %d receives OnuPacketOut () to IF-ID:%d ONU-ID %d.\n", s.Olt.ID, packet.IntfId, packet.OnuId)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090098 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
107func (s *Server) UplinkPacketOut(c context.Context, packet *openolt.UplinkPacket) (*openolt.Empty, error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900108 logger.Info("OLT %d receives UplinkPacketOut().\n", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900109 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
116func (s *Server) FlowAdd(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900117 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 NISHIMOTOb8417492018-10-19 17:37:38 +0900119 //onuid := uint32(flow.OnuId)
120 //ctag := flow.Action.IVid
121 //s.CtagMap[onuid] = ctag
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900122 return new(openolt.Empty), nil
123}
124
125func (s *Server) FlowRemove(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900126 logger.Info("OLT %d receives FlowRemove().\n", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900127 return new(openolt.Empty), nil
128}
129
130func (s *Server) HeartbeatCheck(c context.Context, empty *openolt.Empty) (*openolt.Heartbeat, error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900131 logger.Info("OLT %d receives HeartbeatCheck().\n", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900132 signature := new(openolt.Heartbeat)
133 signature.HeartbeatSignature = s.Olt.HeartbeatSignature
134 return signature, nil
135}
136
137func (s *Server) EnablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900138 logger.Info("OLT %d receives EnablePonIf().\n", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900139 return new(openolt.Empty), nil
140}
141
142func (s *Server) DisablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900143 logger.Info("OLT %d receives DisablePonIf().\n", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900144 return new(openolt.Empty), nil
145}
146
147func (s *Server) Reboot(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900148 logger.Info("OLT %d receives Reboot ().\n", s.Olt.ID)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900149 // Initialize OLT & Env
150 if s.TestFlag == true{
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900151 logger.Debug("Initialized by Reboot")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900152 cleanUpVeths(s.VethEnv)
153 close(s.Endchan)
154 processes := s.Processes
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900155 logger.Debug("Runnig Processes:", processes)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900156 killProcesses(processes)
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900157 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 NISHIMOTOca4da5f2018-10-15 22:48:52 +0900159 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 NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900168 return new(openolt.Empty), nil
169}
170
171func (s *Server) EnableIndication(empty *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error {
172 defer func() {
173 s.gRPCserver.Stop()
174 }()
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900175 s.EnableServer = &stream
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900176 logger.Info("OLT receives EnableInd.\n")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900177 if err := s.activateOLT(stream); err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900178 logger.Error("Failed to activate OLT: %v\n", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900179 return err
180 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900181 logger.Debug("Core server down.")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900182 return nil
183}
184
185func CreateGrpcServer(oltid uint32, npon uint32, nonus uint32, addrport string) (l net.Listener, g *grpc.Server, e error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900186 logger.Info("Listening %s ...", addrport)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900187 g = grpc.NewServer()
188 l, e = net.Listen("tcp", addrport)
189 return
190}