blob: 61096f43c3d5f22c314ee69b1fed7f88c5185978 [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"
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080028 log "github.com/sirupsen/logrus"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090029 "golang.org/x/net/context"
30 "google.golang.org/grpc"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090031)
32
33// gRPC Service
34func (s *Server) DisableOlt(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080035 logger.Debug("OLT receives DisableOLT()")
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090036 if s.EnableServer != nil {
37 if err := sendOltIndDown(*s.EnableServer); err != nil {
38 return new(openolt.Empty), err
39 }
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080040 logger.Debug("Successfuly sent OLT DOWN indication")
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090041 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090042 return new(openolt.Empty), nil
43}
44
45func (s *Server) ReenableOlt(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080046 logger.Debug("OLT receives Reenable()")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090047 return new(openolt.Empty), nil
48}
49
50func (s *Server) CollectStatistics(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080051 logger.Debug("OLT receives CollectStatistics()")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090052 return new(openolt.Empty), nil
53}
54
55func (s *Server) GetDeviceInfo(c context.Context, empty *openolt.Empty) (*openolt.DeviceInfo, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080056 logger.Debug("OLT receives GetDeviceInfo()")
57 devinfo := new(openolt.DeviceInfo)
58 devinfo.Vendor = "EdgeCore"
59 devinfo.Model = "asfvolt16"
60 devinfo.HardwareVersion = ""
61 devinfo.FirmwareVersion = ""
62 devinfo.Technology = "xgspon"
63 devinfo.PonPorts = 1
64 devinfo.OnuIdStart = 1
65 devinfo.OnuIdEnd = 255
66 devinfo.AllocIdStart = 1024
67 devinfo.AllocIdEnd = 16383
68 devinfo.GemportIdStart = 1024
69 devinfo.GemportIdEnd = 65535
70 devinfo.FlowIdStart = 1
71 devinfo.FlowIdEnd = 16383
Shad Ansari010c1942018-11-08 09:32:24 -080072
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080073 /*
74 for intf_id := 0; intf_id < 16; intf_id++ {
75 r := new(openolt.DeviceInfo_DeviceResourceRanges)
Shad Ansari010c1942018-11-08 09:32:24 -080076
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080077 r.IntfIds = append(r.IntfIds, uint32(intf_id))
Shad Ansari010c1942018-11-08 09:32:24 -080078
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080079 r.Technology = "xgspon"
Shad Ansari010c1942018-11-08 09:32:24 -080080
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080081 p := new(openolt.DeviceInfo_DeviceResourceRanges_Pool)
82 p.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
83 p.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
84 p.Start = 1
85 p.End = 255
86 r.Pools = append(r.Pools, p)
Shad Ansari010c1942018-11-08 09:32:24 -080087
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080088 p = new(openolt.DeviceInfo_DeviceResourceRanges_Pool)
89 p.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
90 p.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH
91 p.Start = 1024
92 p.End = 16383
93 r.Pools = append(r.Pools, p)
Shad Ansari010c1942018-11-08 09:32:24 -080094
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080095 p = new(openolt.DeviceInfo_DeviceResourceRanges_Pool)
96 p.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
97 p.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
98 p.Start = 1024
99 p.End = 65535
100 r.Pools = append(r.Pools, p)
Shad Ansari010c1942018-11-08 09:32:24 -0800101
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800102 p = new(openolt.DeviceInfo_DeviceResourceRanges_Pool)
103 p.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
104 p.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
105 p.Start = 1
106 p.End = 16383
107 r.Pools = append(r.Pools, p)
Shad Ansari010c1942018-11-08 09:32:24 -0800108
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800109 devinfo.Ranges = append(devinfo.Ranges, r)
110 }
111 */
Shad Ansari010c1942018-11-08 09:32:24 -0800112
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900113 return devinfo, nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900114}
115
116func (s *Server) ActivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800117 logger.Debug("OLT receives ActivateONU()")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900118 result := device.ValidateONU(*onu, s.Onumap)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900119 if result == true {
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900120 matched, error := getOnuBySN(s.Onumap, onu.SerialNumber)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900121 if error != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800122 logger.Fatal("%s", error)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900123 }
124 onuid := onu.OnuId
125 matched.OnuID = onuid
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900126 matched.UpdateIntStatus(device.ONU_ACTIVATED)
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800127 logger.Debug("ONU IntfID: %d OnuID: %d activated succesufully.", onu.IntfId, onu.OnuId)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900128 }
129 return new(openolt.Empty), nil
130}
131
132func (s *Server) DeactivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800133 logger.Debug("OLT receives DeactivateONU()")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900134 return new(openolt.Empty), nil
135}
136
137func (s *Server) DeleteOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800138 logger.Debug("OLT receives DeleteONU()")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900139 return new(openolt.Empty), nil
140}
141
142func (s *Server) OmciMsgOut(c context.Context, msg *openolt.OmciMsg) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800143 logger.Debug("OLT %d receives OmciMsgOut to IF %v (ONU-ID: %v) pkt:%x.", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900144 //s.olt.Queue = append(s.olt.Queue, *msg)
145 return new(openolt.Empty), nil
146}
147
148func (s *Server) OnuPacketOut(c context.Context, packet *openolt.OnuPacket) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800149 onu, _ := s.GetOnuByID(packet.OnuId)
150 utils.LoggerWithOnu(onu).Debug("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 +0900151 onuid := packet.OnuId
152 intfid := packet.IntfId
153 rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
154 if err := s.onuPacketOut(intfid, onuid, rawpkt); err != nil {
155 return new(openolt.Empty), err
156 }
157 return new(openolt.Empty), nil
158}
159
160func (s *Server) UplinkPacketOut(c context.Context, packet *openolt.UplinkPacket) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800161 logger.Debug("OLT %d receives UplinkPacketOut().", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900162 rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
163 if err := s.uplinkPacketOut(rawpkt); err != nil {
164 return new(openolt.Empty), err
165 }
166 return new(openolt.Empty), nil
167}
168
169func (s *Server) FlowAdd(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800170
171 onu, _ := s.GetOnuByID(uint32(flow.OnuId))
172
173 utils.LoggerWithOnu(onu).WithFields(log.Fields{
174 "olt": s.Olt.ID,
175 "c_tag": flow.Action.IVid,
176 }).Debug("OLT receives FlowAdd().")
177
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900178 return new(openolt.Empty), nil
179}
180
181func (s *Server) FlowRemove(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800182 onu, _ := s.GetOnuByID(uint32(flow.OnuId))
183
184 utils.LoggerWithOnu(onu).WithFields(log.Fields{
185 "olt": s.Olt.ID,
186 "c_tag": flow.Action.IVid,
187 }).Debug("OLT receives FlowRemove().")
188
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900189 return new(openolt.Empty), nil
190}
191
192func (s *Server) HeartbeatCheck(c context.Context, empty *openolt.Empty) (*openolt.Heartbeat, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800193 logger.Debug("OLT %d receives HeartbeatCheck().", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900194 signature := new(openolt.Heartbeat)
195 signature.HeartbeatSignature = s.Olt.HeartbeatSignature
196 return signature, nil
197}
198
199func (s *Server) EnablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800200 logger.Debug("OLT %d receives EnablePonIf().", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900201 return new(openolt.Empty), nil
202}
203
204func (s *Server) DisablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800205 logger.Debug("OLT %d receives DisablePonIf().", s.Olt.ID)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900206 return new(openolt.Empty), nil
207}
208
209func (s *Server) Reboot(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800210 logger.Debug("OLT %d receives Reboot ().", s.Olt.ID)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900211 // Initialize OLT & Env
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900212 logger.Debug("Initialized by Reboot")
213 s.Disable()
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900214 return new(openolt.Empty), nil
215}
216
217func (s *Server) EnableIndication(empty *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800218 logger.Debug("OLT receives EnableInd.")
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900219 defer func() {
220 logger.Debug("grpc EnableIndication Done")
221 }()
222 if err := s.Enable(&stream); err != nil {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800223 logger.Error("Failed to Enable Core: %v", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900224 return err
225 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900226 return nil
227}
228
Keita NISHIMOTO9708e042018-10-27 09:24:44 +0900229func NewGrpcServer(addrport string) (l net.Listener, g *grpc.Server, e error) {
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800230 logger.Debug("Listening %s ...", addrport)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900231 g = grpc.NewServer()
232 l, e = net.Listen("tcp", addrport)
233 return
234}