blob: 7a66fc03bc7cea52891b0d4c7dc90efce8a9c096 [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"
Mahir Gunyel0fc2b742019-02-20 16:45:30 -080021 "strconv"
Matteo Scandolo88e91892018-11-06 16:29:19 -080022
23 "gerrit.opencord.org/voltha-bbsim/common/logger"
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080024 "gerrit.opencord.org/voltha-bbsim/common/utils"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090025 "gerrit.opencord.org/voltha-bbsim/device"
26 "gerrit.opencord.org/voltha-bbsim/protos"
27 "github.com/google/gopacket"
28 "github.com/google/gopacket/layers"
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +090029 omci "github.com/opencord/omci-sim"
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080030 log "github.com/sirupsen/logrus"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090031 "golang.org/x/net/context"
32 "google.golang.org/grpc"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090033)
34
35// gRPC Service
36func (s *Server) DisableOlt(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080037 logger.Debug("OLT receives DisableOLT()")
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090038 if s.EnableServer != nil {
39 if err := sendOltIndDown(*s.EnableServer); err != nil {
40 return new(openolt.Empty), err
41 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080042 logger.Debug("Successfuly sent OLT DOWN indication")
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090043 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090044 return new(openolt.Empty), nil
45}
46
47func (s *Server) ReenableOlt(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080048 logger.Debug("OLT receives Reenable()")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090049 return new(openolt.Empty), nil
50}
51
52func (s *Server) CollectStatistics(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080053 logger.Debug("OLT receives CollectStatistics()")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090054 return new(openolt.Empty), nil
55}
56
57func (s *Server) GetDeviceInfo(c context.Context, empty *openolt.Empty) (*openolt.DeviceInfo, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080058 logger.Debug("OLT receives GetDeviceInfo()")
59 devinfo := new(openolt.DeviceInfo)
60 devinfo.Vendor = "EdgeCore"
61 devinfo.Model = "asfvolt16"
62 devinfo.HardwareVersion = ""
63 devinfo.FirmwareVersion = ""
64 devinfo.Technology = "xgspon"
Shad Ansari704c6f22018-11-13 14:57:53 -080065 devinfo.PonPorts = 16
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080066 devinfo.OnuIdStart = 1
67 devinfo.OnuIdEnd = 255
68 devinfo.AllocIdStart = 1024
69 devinfo.AllocIdEnd = 16383
70 devinfo.GemportIdStart = 1024
71 devinfo.GemportIdEnd = 65535
72 devinfo.FlowIdStart = 1
73 devinfo.FlowIdEnd = 16383
Mahir Gunyel0fc2b742019-02-20 16:45:30 -080074 devinfo.DeviceSerialNumber = "BBSIMOLT00"+strconv.FormatInt(int64(s.Olt.ID), 10)
75
Shad Ansari010c1942018-11-08 09:32:24 -080076
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090077 return devinfo, nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090078}
79
80func (s *Server) ActivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080081 logger.Debug("OLT receives ActivateONU()")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090082 result := device.ValidateONU(*onu, s.Onumap)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090083 if result == true {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090084 matched, error := getOnuBySN(s.Onumap, onu.SerialNumber)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090085 if error != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080086 logger.Fatal("%s", error)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090087 }
88 onuid := onu.OnuId
89 matched.OnuID = onuid
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090090 s.updateDevIntState(matched, device.ONU_ACTIVE)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080091 logger.Debug("ONU IntfID: %d OnuID: %d activated succesufully.", onu.IntfId, onu.OnuId)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090092 }
93 return new(openolt.Empty), nil
94}
95
Shad Ansarifd298442019-01-08 16:19:35 -080096func (s *Server) CreateTconts(c context.Context, tconts *openolt.Tconts) (*openolt.Empty, error) {
97 logger.Debug("OLT receives CreateTconts()")
98 return new(openolt.Empty), nil
99}
100
101func (s *Server) RemoveTconts(c context.Context, tconts *openolt.Tconts) (*openolt.Empty, error) {
102 logger.Debug("OLT receives RemoveTconts()")
103 return new(openolt.Empty), nil
104}
105
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900106func (s *Server) DeactivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800107 logger.Debug("OLT receives DeactivateONU()")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900108 return new(openolt.Empty), nil
109}
110
111func (s *Server) DeleteOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800112 logger.Debug("OLT receives DeleteONU()")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900113 return new(openolt.Empty), nil
114}
115
116func (s *Server) OmciMsgOut(c context.Context, msg *openolt.OmciMsg) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800117 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 -0800118 s.omciOut <- *msg
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900119 return new(openolt.Empty), nil
120}
121
122func (s *Server) OnuPacketOut(c context.Context, packet *openolt.OnuPacket) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800123 onu, _ := s.GetOnuByID(packet.OnuId)
Matteo Scandolo2a659142018-11-15 11:23:45 -0800124 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 +0900125 onuid := packet.OnuId
126 intfid := packet.IntfId
127 rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
128 if err := s.onuPacketOut(intfid, onuid, rawpkt); err != nil {
Matteo Scandoloa286c742018-11-20 08:10:04 -0800129 utils.LoggerWithOnu(onu).WithField("error", err).Errorf("OnuPacketOut Error ")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900130 return new(openolt.Empty), err
131 }
132 return new(openolt.Empty), nil
133}
134
135func (s *Server) UplinkPacketOut(c context.Context, packet *openolt.UplinkPacket) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800136 logger.Debug("OLT %d receives UplinkPacketOut().", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900137 rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
138 if err := s.uplinkPacketOut(rawpkt); err != nil {
139 return new(openolt.Empty), err
140 }
141 return new(openolt.Empty), nil
142}
143
144func (s *Server) FlowAdd(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900145 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)
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900146 onu, err := s.GetOnuByID(uint32(flow.OnuId))
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800147
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900148 if err == nil {
149 intfid := onu.IntfID
150 onuid := onu.OnuID
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900151 onu.GemportID = uint16(flow.GemportId)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800152
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900153 utils.LoggerWithOnu(onu).WithFields(log.Fields{
154 "olt": s.Olt.ID,
155 "c_tag": flow.Action.IVid,
156 }).Debug("OLT receives FlowAdd().")
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800157
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900158 if flow.Classifier.EthType == uint32(layers.EthernetTypeEAPOL) {
Keita NISHIMOTO42db49e2019-01-29 07:49:41 +0900159 omcistate := omci.GetOnuOmciState(onu.IntfID, onu.OnuID)
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900160 if omcistate != omci.DONE {
161 logger.Warn("FlowAdd() OMCI state %d is not \"DONE\"", omci.GetOnuOmciState(onu.OnuID, onu.IntfID))
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900162 }
Keita NISHIMOTO26ebaa82019-03-07 10:00:35 +0900163 s.updateOnuIntState(intfid, onuid, device.ONU_OMCIACTIVE)
Keita NISHIMOTO7bce7692019-01-19 09:31:09 +0900164 }
165 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900166 return new(openolt.Empty), nil
167}
168
169func (s *Server) FlowRemove(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800170 onu, _ := s.GetOnuByID(uint32(flow.OnuId))
171
172 utils.LoggerWithOnu(onu).WithFields(log.Fields{
173 "olt": s.Olt.ID,
174 "c_tag": flow.Action.IVid,
175 }).Debug("OLT receives FlowRemove().")
176
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900177 return new(openolt.Empty), nil
178}
179
180func (s *Server) HeartbeatCheck(c context.Context, empty *openolt.Empty) (*openolt.Heartbeat, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800181 logger.Debug("OLT %d receives HeartbeatCheck().", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900182 signature := new(openolt.Heartbeat)
183 signature.HeartbeatSignature = s.Olt.HeartbeatSignature
184 return signature, nil
185}
186
187func (s *Server) EnablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800188 logger.Debug("OLT %d receives EnablePonIf().", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900189 return new(openolt.Empty), nil
190}
191
192func (s *Server) DisablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800193 logger.Debug("OLT %d receives DisablePonIf().", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900194 return new(openolt.Empty), nil
195}
196
197func (s *Server) Reboot(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800198 logger.Debug("OLT %d receives Reboot ().", s.Olt.ID)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900199 // Initialize OLT & Env
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900200 logger.Debug("Initialized by Reboot")
201 s.Disable()
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900202 return new(openolt.Empty), nil
203}
204
205func (s *Server) EnableIndication(empty *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800206 logger.Debug("OLT receives EnableInd.")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900207 defer func() {
208 logger.Debug("grpc EnableIndication Done")
209 }()
210 if err := s.Enable(&stream); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800211 logger.Error("Failed to Enable Core: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900212 return err
213 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900214 return nil
215}
216
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900217func NewGrpcServer(addrport string) (l net.Listener, g *grpc.Server, e error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800218 logger.Debug("Listening %s ...", addrport)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900219 g = grpc.NewServer()
220 l, e = net.Listen("tcp", addrport)
221 return
222}