blob: 90bdf3504c3997db929fe6e872b13d2714de4683 [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"
22 "github.com/google/gopacket"
23 "github.com/google/gopacket/layers"
24 "golang.org/x/net/context"
25 "google.golang.org/grpc"
26 "log"
27 "net"
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090028 "os/exec"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090029)
30
31// gRPC Service
32func (s *Server) DisableOlt(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
33 log.Printf("OLT receives DisableOLT()\n")
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090034 if err := sendOltIndDown(*s.EnableServer); err != nil {
35 return new(openolt.Empty), err
36 }
37 log.Println("Successfuly sent OLT DOWN indication !")
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090038 return new(openolt.Empty), nil
39}
40
41func (s *Server) ReenableOlt(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
42 log.Printf("OLT receives Reenable()\n")
43 return new(openolt.Empty), nil
44}
45
46func (s *Server) CollectStatistics(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
47 log.Printf("OLT receives CollectStatistics()\n")
48 return new(openolt.Empty), nil
49}
50
51func (s *Server) GetDeviceInfo(c context.Context, empty *openolt.Empty) (*openolt.DeviceInfo, error) {
52 log.Printf("OLT receives GetDeviceInfo()\n")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090053 devinfo := new(openolt.DeviceInfo)
54 devinfo.Vendor = "CORD"
55 devinfo.OnuIdStart = 0
56 devinfo.OnuIdEnd = 3
57 devinfo.PonPorts = 4
58 return devinfo, nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090059}
60
61func (s *Server) ActivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
62 log.Printf("OLT receives ActivateONU()\n")
63 result := device.ValidateONU(*onu, s.Onumap)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090064 if result == true {
65 matched, error := s.getOnuBySN(onu.SerialNumber)
66 if error != nil {
67 log.Fatalf("%s\n", error)
68 }
69 onuid := onu.OnuId
70 matched.OnuID = onuid
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090071 *matched.InternalState = device.ONU_ACTIVATED
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090072 log.Printf("ONU IntfID: %d OnuID: %d activated succesufully.\n", onu.IntfId, onu.OnuId)
73 }
74 return new(openolt.Empty), nil
75}
76
77func (s *Server) DeactivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
78 log.Printf("OLT receives DeactivateONU()\n")
79 return new(openolt.Empty), nil
80}
81
82func (s *Server) DeleteOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
83 log.Printf("OLT receives DeleteONU()\n")
84 return new(openolt.Empty), nil
85}
86
87func (s *Server) OmciMsgOut(c context.Context, msg *openolt.OmciMsg) (*openolt.Empty, error) {
88 log.Printf("OLT %d receives OmciMsgOut to IF %v (ONU-ID: %v) pkt:%x.\n", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
89 //s.olt.Queue = append(s.olt.Queue, *msg)
90 return new(openolt.Empty), nil
91}
92
93func (s *Server) OnuPacketOut(c context.Context, packet *openolt.OnuPacket) (*openolt.Empty, error) {
94 log.Printf("OLT %d receives OnuPacketOut () to IF-ID:%d ONU-ID %d.\n", s.Olt.ID, packet.IntfId, packet.OnuId)
95 onuid := packet.OnuId
96 intfid := packet.IntfId
97 rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
98 if err := s.onuPacketOut(intfid, onuid, rawpkt); err != nil {
99 return new(openolt.Empty), err
100 }
101 return new(openolt.Empty), nil
102}
103
104func (s *Server) UplinkPacketOut(c context.Context, packet *openolt.UplinkPacket) (*openolt.Empty, error) {
105 log.Printf("OLT %d receives UplinkPacketOut().\n", s.Olt.ID)
106 rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
107 if err := s.uplinkPacketOut(rawpkt); err != nil {
108 return new(openolt.Empty), err
109 }
110 return new(openolt.Empty), nil
111}
112
113func (s *Server) FlowAdd(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
114 log.Printf("OLT %d receives FlowAdd().\n", s.Olt.ID)
115 return new(openolt.Empty), nil
116}
117
118func (s *Server) FlowRemove(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
119 log.Printf("OLT %d receives FlowRemove().\n", s.Olt.ID)
120 return new(openolt.Empty), nil
121}
122
123func (s *Server) HeartbeatCheck(c context.Context, empty *openolt.Empty) (*openolt.Heartbeat, error) {
124 log.Printf("OLT %d receives HeartbeatCheck().\n", s.Olt.ID)
125 signature := new(openolt.Heartbeat)
126 signature.HeartbeatSignature = s.Olt.HeartbeatSignature
127 return signature, nil
128}
129
130func (s *Server) EnablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
131 log.Printf("OLT %d receives EnablePonIf().\n", s.Olt.ID)
132 return new(openolt.Empty), nil
133}
134
135func (s *Server) DisablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
136 log.Printf("OLT %d receives DisablePonIf().\n", s.Olt.ID)
137 return new(openolt.Empty), nil
138}
139
140func (s *Server) Reboot(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
141 log.Printf("OLT %d receives Reboot ().\n", s.Olt.ID)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900142 // Initialize OLT & Env
143 if s.TestFlag == true{
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900144 log.Println("Initialized by Reboot")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900145 cleanUpVeths(s.VethEnv)
146 close(s.Endchan)
147 processes := s.Processes
148 log.Println("processes:", processes)
149 killProcesses(processes)
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900150 exec.Command("rm", "/var/run/dhcpd.pid").Run()
151 exec.Command("touch", "/var/run/dhcpd.pid").Run()
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900152 s.Initialize()
153 }
154 olt := s.Olt
155 olt.InitializeStatus()
156 for intfid, _ := range s.Onumap{
157 for _, onu := range s.Onumap[intfid] {
158 onu.InitializeStatus()
159 }
160 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900161 return new(openolt.Empty), nil
162}
163
164func (s *Server) EnableIndication(empty *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error {
165 defer func() {
166 s.gRPCserver.Stop()
167 }()
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900168 s.EnableServer = &stream
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900169 log.Printf("OLT receives EnableInd.\n")
170 if err := s.activateOLT(stream); err != nil {
171 log.Printf("Failed to activate OLT: %v\n", err)
172 return err
173 }
174 log.Println("Core server down.")
175 return nil
176}
177
178func CreateGrpcServer(oltid uint32, npon uint32, nonus uint32, addrport string) (l net.Listener, g *grpc.Server, e error) {
179 log.Printf("Listening %s ...", addrport)
180 g = grpc.NewServer()
181 l, e = net.Listen("tcp", addrport)
182 return
183}