blob: d088e057534d19ebb241145e4414a4dcf819578e [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"
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020032 "google.golang.org/grpc/codes"
33 "google.golang.org/grpc/status"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090034)
35
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020036// DisableOlt method sends OLT down indication
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090037func (s *Server) DisableOlt(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080038 logger.Debug("OLT receives DisableOLT()")
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090039 if s.EnableServer != nil {
40 if err := sendOltIndDown(*s.EnableServer); err != nil {
41 return new(openolt.Empty), err
42 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080043 logger.Debug("Successfuly sent OLT DOWN indication")
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090044 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090045 return new(openolt.Empty), nil
46}
47
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020048// ReenableOlt method sends OLT up indication for re-enabling OLT
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090049func (s *Server) ReenableOlt(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080050 logger.Debug("OLT receives Reenable()")
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020051 if s.EnableServer != nil {
52 if err := sendOltIndUp(*s.EnableServer, s.Olt); err != nil {
53 logger.Error("Failed to send OLT UP indication for reenable OLT: %v", err)
54 return new(openolt.Empty), err
55 }
56 logger.Debug("Successfuly sent OLT UP indication")
57 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090058 return new(openolt.Empty), nil
59}
60
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020061// CollectStatistics method invoked by VOLTHA to get OLT statistics
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090062func (s *Server) CollectStatistics(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080063 logger.Debug("OLT receives CollectStatistics()")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090064 return new(openolt.Empty), nil
65}
66
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020067// GetDeviceInfo returns OLT info
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090068func (s *Server) GetDeviceInfo(c context.Context, empty *openolt.Empty) (*openolt.DeviceInfo, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080069 logger.Debug("OLT receives GetDeviceInfo()")
70 devinfo := new(openolt.DeviceInfo)
71 devinfo.Vendor = "EdgeCore"
72 devinfo.Model = "asfvolt16"
73 devinfo.HardwareVersion = ""
74 devinfo.FirmwareVersion = ""
75 devinfo.Technology = "xgspon"
Shad Ansari704c6f22018-11-13 14:57:53 -080076 devinfo.PonPorts = 16
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080077 devinfo.OnuIdStart = 1
78 devinfo.OnuIdEnd = 255
79 devinfo.AllocIdStart = 1024
80 devinfo.AllocIdEnd = 16383
81 devinfo.GemportIdStart = 1024
82 devinfo.GemportIdEnd = 65535
83 devinfo.FlowIdStart = 1
84 devinfo.FlowIdEnd = 16383
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020085 devinfo.DeviceSerialNumber = s.Olt.SerialNumber
Shad Ansari010c1942018-11-08 09:32:24 -080086
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090087 return devinfo, nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090088}
89
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020090// ActivateOnu method handles ONU activation request from VOLTHA
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090091func (s *Server) ActivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080092 logger.Debug("OLT receives ActivateONU()")
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020093
94 matched, err := getOnuBySN(s.Onumap, onu.SerialNumber)
95 if err != nil {
96 logger.Fatal("%s", err)
97 return new(openolt.Empty), status.Errorf(codes.NotFound, "ONU not found with serial number %v", onu.SerialNumber)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090098 }
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020099 onuid := onu.OnuId
100 matched.OnuID = onuid
101 s.updateDevIntState(matched, device.ONU_ACTIVE)
102 logger.Debug("ONU IntfID: %d OnuID: %d activated succesufully.", onu.IntfId, onu.OnuId)
103
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900104 return new(openolt.Empty), nil
105}
106
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200107// CreateTconts method should handle Tcont creation
Shad Ansarifd298442019-01-08 16:19:35 -0800108func (s *Server) CreateTconts(c context.Context, tconts *openolt.Tconts) (*openolt.Empty, error) {
109 logger.Debug("OLT receives CreateTconts()")
110 return new(openolt.Empty), nil
111}
112
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200113// RemoveTconts method should handle t-cont removal
Shad Ansarifd298442019-01-08 16:19:35 -0800114func (s *Server) RemoveTconts(c context.Context, tconts *openolt.Tconts) (*openolt.Empty, error) {
115 logger.Debug("OLT receives RemoveTconts()")
116 return new(openolt.Empty), nil
117}
118
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200119// DeactivateOnu method should handle ONU deactivation
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900120func (s *Server) DeactivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800121 logger.Debug("OLT receives DeactivateONU()")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900122 return new(openolt.Empty), nil
123}
124
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200125// DeleteOnu handles ONU deletion request from VOLTHA
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900126func (s *Server) DeleteOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800127 logger.Debug("OLT receives DeleteONU()")
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200128 Onu, err := s.GetOnuByID(onu.OnuId, onu.IntfId)
129 if err != nil {
130 return new(openolt.Empty), err
131 }
132
133 // Mark ONU internal state as ONU_FREE and reset onuID
134 Onu.InternalState = device.ONU_FREE
135 Onu.OnuID = 0
136
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900137 return new(openolt.Empty), nil
138}
139
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200140// OmciMsgOut receives OMCI messages from voltha
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900141func (s *Server) OmciMsgOut(c context.Context, msg *openolt.OmciMsg) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800142 logger.Debug("OLT %d receives OmciMsgOut to IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200143 // Get ONU state
144 onu, err := s.GetOnuByID(msg.OnuId, msg.IntfId)
145 if err != nil {
146 logger.Error("ONU not found intfID %d, onuID %d", msg.IntfId, msg.OnuId)
147 return new(openolt.Empty), err
148 }
149 state := onu.GetIntState()
150 logger.Debug("ONU-ID: %v, ONU state: %d", msg.OnuId, state)
151
152 // If ONU is ONU_INACTIVE or ONU_FREE do not send omci response
153 if state == device.ONU_INACTIVE || state == device.ONU_FREE {
154 logger.Info("ONU (IF %v ONU-ID: %v) is not ACTIVE, so not processing OmciMsg", msg.IntfId, msg.OnuId)
155 return new(openolt.Empty), nil
156 }
Shad Ansaria5c79892018-11-29 16:32:59 -0800157 s.omciOut <- *msg
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900158 return new(openolt.Empty), nil
159}
160
161func (s *Server) OnuPacketOut(c context.Context, packet *openolt.OnuPacket) (*openolt.Empty, error) {
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200162 onu, err := s.GetOnuByID(packet.OnuId, packet.IntfId)
163 if err != nil {
164 logger.Error("Failed in OnuPacketOut, %v", err)
165 return new(openolt.Empty), err
166 }
Matteo Scandolo2a659142018-11-15 11:23:45 -0800167 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 +0900168 onuid := packet.OnuId
169 intfid := packet.IntfId
170 rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
171 if err := s.onuPacketOut(intfid, onuid, rawpkt); err != nil {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800172 utils.LoggerWithOnu(onu).WithField("error", err).Errorf("OnuPacketOut Error ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900173 return new(openolt.Empty), err
174 }
175 return new(openolt.Empty), nil
176}
177
178func (s *Server) UplinkPacketOut(c context.Context, packet *openolt.UplinkPacket) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800179 logger.Debug("OLT %d receives UplinkPacketOut().", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900180 rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
181 if err := s.uplinkPacketOut(rawpkt); err != nil {
182 return new(openolt.Empty), err
183 }
184 return new(openolt.Empty), nil
185}
186
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200187// FlowAdd method should handle flows addition to datapath for OLT and ONU
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900188func (s *Server) FlowAdd(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900189 logger.Debug("OLT %d receives FlowAdd() IntfID:%d OnuID:%d EType:%x GemPortID:%d", s.Olt.ID, flow.AccessIntfId, flow.OnuId, flow.Classifier.EthType, flow.GemportId)
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200190 onu, err := s.GetOnuByID(uint32(flow.OnuId), uint32(flow.AccessIntfId))
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800191
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900192 if err == nil {
193 intfid := onu.IntfID
194 onuid := onu.OnuID
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900195 onu.GemportID = uint16(flow.GemportId)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800196
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900197 utils.LoggerWithOnu(onu).WithFields(log.Fields{
198 "olt": s.Olt.ID,
199 "c_tag": flow.Action.IVid,
200 }).Debug("OLT receives FlowAdd().")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800201
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900202 if flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) {
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900203 omcistate := omci.GetOnuOmciState(onu.IntfID, onu.OnuID)
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900204 if omcistate != omci.DONE {
205 logger.Warn("FlowAdd() OMCI state %d is not \"DONE\"", omci.GetOnuOmciState(onu.OnuID, onu.IntfID))
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900206 }
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900207 s.updateOnuIntState(intfid, onuid, device.ONU_OMCIACTIVE)
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900208 }
209 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900210 return new(openolt.Empty), nil
211}
212
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200213// FlowRemove should handle flow deletion from datapath
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900214func (s *Server) FlowRemove(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Zdravko Bozakovedb65322019-04-11 14:49:32 +0200215 onu, _ := s.GetOnuByID(uint32(flow.OnuId), uint32(flow.AccessIntfId))
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800216
217 utils.LoggerWithOnu(onu).WithFields(log.Fields{
218 "olt": s.Olt.ID,
219 "c_tag": flow.Action.IVid,
220 }).Debug("OLT receives FlowRemove().")
221
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900222 return new(openolt.Empty), nil
223}
224
225func (s *Server) HeartbeatCheck(c context.Context, empty *openolt.Empty) (*openolt.Heartbeat, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800226 logger.Debug("OLT %d receives HeartbeatCheck().", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900227 signature := new(openolt.Heartbeat)
228 signature.HeartbeatSignature = s.Olt.HeartbeatSignature
229 return signature, nil
230}
231
232func (s *Server) EnablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800233 logger.Debug("OLT %d receives EnablePonIf().", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900234 return new(openolt.Empty), nil
235}
236
237func (s *Server) DisablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800238 logger.Debug("OLT %d receives DisablePonIf().", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900239 return new(openolt.Empty), nil
240}
241
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200242// Reboot method handles reboot of OLT
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900243func (s *Server) Reboot(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800244 logger.Debug("OLT %d receives Reboot ().", s.Olt.ID)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900245 // Initialize OLT & Env
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900246 logger.Debug("Initialized by Reboot")
247 s.Disable()
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900248 return new(openolt.Empty), nil
249}
250
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200251// EnableIndication starts sending indications for OLT and ONU
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900252func (s *Server) EnableIndication(empty *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800253 logger.Debug("OLT receives EnableInd.")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900254 defer func() {
255 logger.Debug("grpc EnableIndication Done")
256 }()
257 if err := s.Enable(&stream); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800258 logger.Error("Failed to Enable Core: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900259 return err
260 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900261 return nil
262}
263
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200264// NewGrpcServer starts openolt gRPC server
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900265func NewGrpcServer(addrport string) (l net.Listener, g *grpc.Server, e error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800266 logger.Debug("Listening %s ...", addrport)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900267 g = grpc.NewServer()
268 l, e = net.Listen("tcp", addrport)
269 return
270}