blob: fe88a5a59bc709f041e7574b1643c4e81661f2b3 [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 (
Matteo Scandolo88e91892018-11-06 16:29:19 -080020 "net"
21
22 "gerrit.opencord.org/voltha-bbsim/common/logger"
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080023 "gerrit.opencord.org/voltha-bbsim/common/utils"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090024 "gerrit.opencord.org/voltha-bbsim/device"
25 "gerrit.opencord.org/voltha-bbsim/protos"
26 "github.com/google/gopacket"
27 "github.com/google/gopacket/layers"
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +090028 omci "github.com/opencord/omci-sim"
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080029 log "github.com/sirupsen/logrus"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090030 "golang.org/x/net/context"
31 "google.golang.org/grpc"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090032)
33
34// gRPC Service
35func (s *Server) DisableOlt(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080036 logger.Debug("OLT receives DisableOLT()")
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090037 if s.EnableServer != nil {
38 if err := sendOltIndDown(*s.EnableServer); err != nil {
39 return new(openolt.Empty), err
40 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080041 logger.Debug("Successfuly sent OLT DOWN indication")
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090042 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090043 return new(openolt.Empty), nil
44}
45
46func (s *Server) ReenableOlt(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080047 logger.Debug("OLT receives Reenable()")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090048 return new(openolt.Empty), nil
49}
50
51func (s *Server) CollectStatistics(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080052 logger.Debug("OLT receives CollectStatistics()")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090053 return new(openolt.Empty), nil
54}
55
56func (s *Server) GetDeviceInfo(c context.Context, empty *openolt.Empty) (*openolt.DeviceInfo, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080057 logger.Debug("OLT receives GetDeviceInfo()")
58 devinfo := new(openolt.DeviceInfo)
59 devinfo.Vendor = "EdgeCore"
60 devinfo.Model = "asfvolt16"
61 devinfo.HardwareVersion = ""
62 devinfo.FirmwareVersion = ""
63 devinfo.Technology = "xgspon"
Shad Ansari704c6f22018-11-13 14:57:53 -080064 devinfo.PonPorts = 16
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080065 devinfo.OnuIdStart = 1
66 devinfo.OnuIdEnd = 255
67 devinfo.AllocIdStart = 1024
68 devinfo.AllocIdEnd = 16383
69 devinfo.GemportIdStart = 1024
70 devinfo.GemportIdEnd = 65535
71 devinfo.FlowIdStart = 1
72 devinfo.FlowIdEnd = 16383
Shad Ansari010c1942018-11-08 09:32:24 -080073
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090074 return devinfo, nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090075}
76
77func (s *Server) ActivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080078 logger.Debug("OLT receives ActivateONU()")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090079 result := device.ValidateONU(*onu, s.Onumap)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090080 if result == true {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090081 matched, error := getOnuBySN(s.Onumap, onu.SerialNumber)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090082 if error != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080083 logger.Fatal("%s", error)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090084 }
85 onuid := onu.OnuId
86 matched.OnuID = onuid
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090087 s.updateDevIntState(matched, device.ONU_ACTIVE)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080088 logger.Debug("ONU IntfID: %d OnuID: %d activated succesufully.", onu.IntfId, onu.OnuId)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090089 }
90 return new(openolt.Empty), nil
91}
92
Shad Ansarifd298442019-01-08 16:19:35 -080093func (s *Server) CreateTconts(c context.Context, tconts *openolt.Tconts) (*openolt.Empty, error) {
94 logger.Debug("OLT receives CreateTconts()")
95 return new(openolt.Empty), nil
96}
97
98func (s *Server) RemoveTconts(c context.Context, tconts *openolt.Tconts) (*openolt.Empty, error) {
99 logger.Debug("OLT receives RemoveTconts()")
100 return new(openolt.Empty), nil
101}
102
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900103func (s *Server) DeactivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800104 logger.Debug("OLT receives DeactivateONU()")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900105 return new(openolt.Empty), nil
106}
107
108func (s *Server) DeleteOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800109 logger.Debug("OLT receives DeleteONU()")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900110 return new(openolt.Empty), nil
111}
112
113func (s *Server) OmciMsgOut(c context.Context, msg *openolt.OmciMsg) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800114 logger.Debug("OLT %d receives OmciMsgOut to IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
Shad Ansaria5c79892018-11-29 16:32:59 -0800115 s.omciOut <- *msg
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900116 return new(openolt.Empty), nil
117}
118
119func (s *Server) OnuPacketOut(c context.Context, packet *openolt.OnuPacket) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800120 onu, _ := s.GetOnuByID(packet.OnuId)
Matteo Scandolo2a659142018-11-15 11:23:45 -0800121 utils.LoggerWithOnu(onu).Debugf("OLT %d receives OnuPacketOut () to IF-ID:%d ONU-ID %d.", s.Olt.ID, packet.IntfId, packet.OnuId)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900122 onuid := packet.OnuId
123 intfid := packet.IntfId
124 rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
125 if err := s.onuPacketOut(intfid, onuid, rawpkt); err != nil {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800126 utils.LoggerWithOnu(onu).WithField("error", err).Errorf("OnuPacketOut Error ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900127 return new(openolt.Empty), err
128 }
129 return new(openolt.Empty), nil
130}
131
132func (s *Server) UplinkPacketOut(c context.Context, packet *openolt.UplinkPacket) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800133 logger.Debug("OLT %d receives UplinkPacketOut().", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900134 rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
135 if err := s.uplinkPacketOut(rawpkt); err != nil {
136 return new(openolt.Empty), err
137 }
138 return new(openolt.Empty), nil
139}
140
141func (s *Server) FlowAdd(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900142 logger.Debug("OLT %d receives FlowAdd() IntfID:%d OnuID:%d EType:%x:.", s.Olt.ID, flow.AccessIntfId, flow.OnuId, flow.Classifier.EthType)
143 onu, err := s.GetOnuByID(uint32(flow.OnuId))
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800144
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900145 if err == nil {
146 intfid := onu.IntfID
147 onuid := onu.OnuID
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800148
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900149 utils.LoggerWithOnu(onu).WithFields(log.Fields{
150 "olt": s.Olt.ID,
151 "c_tag": flow.Action.IVid,
152 }).Debug("OLT receives FlowAdd().")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800153
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900154 if flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) {
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900155 omcistate := omci.GetOnuOmciState(onu.IntfID, onu.OnuID)
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900156 if omcistate == omci.DONE {
157 s.updateOnuIntState(intfid, onuid, device.ONU_OMCIACTIVE)
158 } else {
159 logger.Error("FlowAdd() OMCI state %d is not \"DONE\"", omci.GetOnuOmciState(onu.OnuID, onu.IntfID))
160 }
161 }
162 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900163 return new(openolt.Empty), nil
164}
165
166func (s *Server) FlowRemove(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800167 onu, _ := s.GetOnuByID(uint32(flow.OnuId))
168
169 utils.LoggerWithOnu(onu).WithFields(log.Fields{
170 "olt": s.Olt.ID,
171 "c_tag": flow.Action.IVid,
172 }).Debug("OLT receives FlowRemove().")
173
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900174 return new(openolt.Empty), nil
175}
176
177func (s *Server) HeartbeatCheck(c context.Context, empty *openolt.Empty) (*openolt.Heartbeat, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800178 logger.Debug("OLT %d receives HeartbeatCheck().", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900179 signature := new(openolt.Heartbeat)
180 signature.HeartbeatSignature = s.Olt.HeartbeatSignature
181 return signature, nil
182}
183
184func (s *Server) EnablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800185 logger.Debug("OLT %d receives EnablePonIf().", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900186 return new(openolt.Empty), nil
187}
188
189func (s *Server) DisablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800190 logger.Debug("OLT %d receives DisablePonIf().", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900191 return new(openolt.Empty), nil
192}
193
194func (s *Server) Reboot(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800195 logger.Debug("OLT %d receives Reboot ().", s.Olt.ID)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900196 // Initialize OLT & Env
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900197 logger.Debug("Initialized by Reboot")
198 s.Disable()
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900199 return new(openolt.Empty), nil
200}
201
202func (s *Server) EnableIndication(empty *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800203 logger.Debug("OLT receives EnableInd.")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900204 defer func() {
205 logger.Debug("grpc EnableIndication Done")
206 }()
207 if err := s.Enable(&stream); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800208 logger.Error("Failed to Enable Core: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900209 return err
210 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900211 return nil
212}
213
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900214func NewGrpcServer(addrport string) (l net.Listener, g *grpc.Server, e error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800215 logger.Debug("Listening %s ...", addrport)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900216 g = grpc.NewServer()
217 l, e = net.Listen("tcp", addrport)
218 return
219}