blob: 329292df029804fc2afc7f3cfb7e23f8f0640032 [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 NISHIMOTOb8417492018-10-19 17:37:38 +090034 if s.EnableServer != nil {
35 if err := sendOltIndDown(*s.EnableServer); err != nil {
36 return new(openolt.Empty), err
37 }
38 log.Println("Successfuly sent OLT DOWN indication !")
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +090039 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090040 return new(openolt.Empty), nil
41}
42
43func (s *Server) ReenableOlt(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
44 log.Printf("OLT receives Reenable()\n")
45 return new(openolt.Empty), nil
46}
47
48func (s *Server) CollectStatistics(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
49 log.Printf("OLT receives CollectStatistics()\n")
50 return new(openolt.Empty), nil
51}
52
53func (s *Server) GetDeviceInfo(c context.Context, empty *openolt.Empty) (*openolt.DeviceInfo, error) {
54 log.Printf("OLT receives GetDeviceInfo()\n")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090055 devinfo := new(openolt.DeviceInfo)
56 devinfo.Vendor = "CORD"
57 devinfo.OnuIdStart = 0
58 devinfo.OnuIdEnd = 3
59 devinfo.PonPorts = 4
60 return devinfo, nil
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090061}
62
63func (s *Server) ActivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
64 log.Printf("OLT receives ActivateONU()\n")
65 result := device.ValidateONU(*onu, s.Onumap)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090066 if result == true {
67 matched, error := s.getOnuBySN(onu.SerialNumber)
68 if error != nil {
69 log.Fatalf("%s\n", error)
70 }
71 onuid := onu.OnuId
72 matched.OnuID = onuid
Keita NISHIMOTOb8417492018-10-19 17:37:38 +090073 matched.UpdateIntStatus(device.ONU_ACTIVATED)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090074 log.Printf("ONU IntfID: %d OnuID: %d activated succesufully.\n", onu.IntfId, onu.OnuId)
75 }
76 return new(openolt.Empty), nil
77}
78
79func (s *Server) DeactivateOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
80 log.Printf("OLT receives DeactivateONU()\n")
81 return new(openolt.Empty), nil
82}
83
84func (s *Server) DeleteOnu(c context.Context, onu *openolt.Onu) (*openolt.Empty, error) {
85 log.Printf("OLT receives DeleteONU()\n")
86 return new(openolt.Empty), nil
87}
88
89func (s *Server) OmciMsgOut(c context.Context, msg *openolt.OmciMsg) (*openolt.Empty, error) {
90 log.Printf("OLT %d receives OmciMsgOut to IF %v (ONU-ID: %v) pkt:%x.\n", s.Olt.ID, msg.IntfId, msg.OnuId, msg.Pkt)
91 //s.olt.Queue = append(s.olt.Queue, *msg)
92 return new(openolt.Empty), nil
93}
94
95func (s *Server) OnuPacketOut(c context.Context, packet *openolt.OnuPacket) (*openolt.Empty, error) {
96 log.Printf("OLT %d receives OnuPacketOut () to IF-ID:%d ONU-ID %d.\n", s.Olt.ID, packet.IntfId, packet.OnuId)
97 onuid := packet.OnuId
98 intfid := packet.IntfId
99 rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
100 if err := s.onuPacketOut(intfid, onuid, rawpkt); err != nil {
101 return new(openolt.Empty), err
102 }
103 return new(openolt.Empty), nil
104}
105
106func (s *Server) UplinkPacketOut(c context.Context, packet *openolt.UplinkPacket) (*openolt.Empty, error) {
107 log.Printf("OLT %d receives UplinkPacketOut().\n", s.Olt.ID)
108 rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
109 if err := s.uplinkPacketOut(rawpkt); err != nil {
110 return new(openolt.Empty), err
111 }
112 return new(openolt.Empty), nil
113}
114
115func (s *Server) FlowAdd(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
116 log.Printf("OLT %d receives FlowAdd().\n", s.Olt.ID)
Keita NISHIMOTOb8417492018-10-19 17:37:38 +0900117 log.Printf("Flow's ONU-ID: %d, CTAG: %d\n", flow.OnuId, flow.Action.IVid)
118 //onuid := uint32(flow.OnuId)
119 //ctag := flow.Action.IVid
120 //s.CtagMap[onuid] = ctag
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900121 return new(openolt.Empty), nil
122}
123
124func (s *Server) FlowRemove(c context.Context, flow *openolt.Flow) (*openolt.Empty, error) {
125 log.Printf("OLT %d receives FlowRemove().\n", s.Olt.ID)
126 return new(openolt.Empty), nil
127}
128
129func (s *Server) HeartbeatCheck(c context.Context, empty *openolt.Empty) (*openolt.Heartbeat, error) {
130 log.Printf("OLT %d receives HeartbeatCheck().\n", s.Olt.ID)
131 signature := new(openolt.Heartbeat)
132 signature.HeartbeatSignature = s.Olt.HeartbeatSignature
133 return signature, nil
134}
135
136func (s *Server) EnablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
137 log.Printf("OLT %d receives EnablePonIf().\n", s.Olt.ID)
138 return new(openolt.Empty), nil
139}
140
141func (s *Server) DisablePonIf(c context.Context, intf *openolt.Interface) (*openolt.Empty, error) {
142 log.Printf("OLT %d receives DisablePonIf().\n", s.Olt.ID)
143 return new(openolt.Empty), nil
144}
145
146func (s *Server) Reboot(c context.Context, empty *openolt.Empty) (*openolt.Empty, error) {
147 log.Printf("OLT %d receives Reboot ().\n", s.Olt.ID)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900148 // Initialize OLT & Env
149 if s.TestFlag == true{
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900150 log.Println("Initialized by Reboot")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900151 cleanUpVeths(s.VethEnv)
152 close(s.Endchan)
153 processes := s.Processes
154 log.Println("processes:", processes)
155 killProcesses(processes)
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900156 exec.Command("rm", "/var/run/dhcpd.pid").Run()
157 exec.Command("touch", "/var/run/dhcpd.pid").Run()
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900158 s.Initialize()
159 }
160 olt := s.Olt
161 olt.InitializeStatus()
162 for intfid, _ := range s.Onumap{
163 for _, onu := range s.Onumap[intfid] {
164 onu.InitializeStatus()
165 }
166 }
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900167 return new(openolt.Empty), nil
168}
169
170func (s *Server) EnableIndication(empty *openolt.Empty, stream openolt.Openolt_EnableIndicationServer) error {
171 defer func() {
172 s.gRPCserver.Stop()
173 }()
Keita NISHIMOTO9c6f6f12018-10-18 04:56:34 +0900174 s.EnableServer = &stream
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900175 log.Printf("OLT receives EnableInd.\n")
176 if err := s.activateOLT(stream); err != nil {
177 log.Printf("Failed to activate OLT: %v\n", err)
178 return err
179 }
180 log.Println("Core server down.")
181 return nil
182}
183
184func CreateGrpcServer(oltid uint32, npon uint32, nonus uint32, addrport string) (l net.Listener, g *grpc.Server, e error) {
185 log.Printf("Listening %s ...", addrport)
186 g = grpc.NewServer()
187 l, e = net.Listen("tcp", addrport)
188 return
189}