/
sim.go
164 lines (139 loc) · 5.11 KB
/
sim.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2023-present, Ukama Inc.
*/
package server
import (
"context"
"github.com/sirupsen/logrus"
"github.com/ukama/ukama/systems/common/grpc"
mb "github.com/ukama/ukama/systems/common/msgBusServiceClient"
"github.com/ukama/ukama/systems/common/msgbus"
pb "github.com/ukama/ukama/systems/subscriber/sim-pool/pb/gen"
"github.com/ukama/ukama/systems/subscriber/sim-pool/pkg"
"github.com/ukama/ukama/systems/subscriber/sim-pool/pkg/db"
"github.com/ukama/ukama/systems/subscriber/sim-pool/pkg/utils"
)
type SimPoolServer struct {
orgName string
simRepo db.SimRepo
msgbus mb.MsgBusServiceClient
baseRoutingKey msgbus.RoutingKeyBuilder
pb.UnimplementedSimServiceServer
}
func NewSimPoolServer(orgName string, simRepo db.SimRepo, msgBus mb.MsgBusServiceClient) *SimPoolServer {
return &SimPoolServer{
orgName: orgName,
simRepo: simRepo,
msgbus: msgBus,
baseRoutingKey: msgbus.NewRoutingKeyBuilder().SetCloudSource().SetSystem(pkg.SystemName).SetOrgName(orgName).SetService(pkg.ServiceName),
}
}
func (p *SimPoolServer) Get(ctx context.Context, req *pb.GetRequest) (*pb.GetResponse, error) {
logrus.Infof("GetSim isPhysical: %v, simType: %v", req.GetIsPhysicalSim(), req.GetSimType())
sim, err := p.simRepo.Get(req.GetIsPhysicalSim(), db.ParseType(req.GetSimType()))
if err != nil {
logrus.Error("error fetching a sim " + err.Error())
return nil, grpc.SqlErrorToGrpc(err, "sim-pool")
}
return &pb.GetResponse{Sim: dbSimToPbSim(sim)}, nil
}
func (p *SimPoolServer) GetByIccid(ctx context.Context, req *pb.GetByIccidRequest) (*pb.GetByIccidResponse, error) {
logrus.Infof("GetSimByIccid : %v", req.GetIccid())
sim, err := p.simRepo.GetByIccid(req.GetIccid())
if err != nil {
logrus.Error("error fetching a sim " + err.Error())
return nil, grpc.SqlErrorToGrpc(err, "sim-pool")
}
return &pb.GetByIccidResponse{Sim: dbSimToPbSim(sim)}, nil
}
func (p *SimPoolServer) GetStats(ctx context.Context, req *pb.GetStatsRequest) (*pb.GetStatsResponse, error) {
logrus.Infof("GetSimStats : %v ", req.GetSimType())
sim, err := p.simRepo.GetSimsByType(db.ParseType(req.SimType))
if err != nil {
logrus.Error("error getting a sim pool stats" + err.Error())
return nil, grpc.SqlErrorToGrpc(err, "sim-pool")
}
resp := utils.PoolStats(sim)
return resp, nil
}
func (p *SimPoolServer) GetSims(ctx context.Context, req *pb.GetSimsRequest) (*pb.GetSimsResponse, error) {
logrus.Infof("GetSims : %v ", req.GetSimType())
sims, err := p.simRepo.GetSims(db.ParseType(req.SimType))
if err != nil {
logrus.Error("error getting a sim pool stats" + err.Error())
return nil, grpc.SqlErrorToGrpc(err, "sim-pool")
}
resp := dbSimsToPbSim(sims)
return &pb.GetSimsResponse{
Sims: resp,
}, nil
}
func (p *SimPoolServer) Add(ctx context.Context, req *pb.AddRequest) (*pb.AddResponse, error) {
logrus.Infof("Add Sims : %v ", req.Sim)
result := utils.PbParseToModel(req.Sim)
err := p.simRepo.Add(result)
if err != nil {
logrus.Error("error adding a sims" + err.Error())
return nil, grpc.SqlErrorToGrpc(err, "sim-pool")
}
resp := &pb.AddResponse{Sim: dbSimsToPbSim(result)}
return resp, nil
}
func (p *SimPoolServer) Upload(ctx context.Context, req *pb.UploadRequest) (*pb.UploadResponse, error) {
logrus.Infof("Upload Sims to pool")
a, _ := utils.ParseBytesToRawSim(req.SimData)
s := utils.RawSimToPb(a, req.GetSimType())
err := p.simRepo.Add(s)
logrus.Info("ADDING SIMS: ", s, err)
if err != nil {
logrus.Error("error while Upload sims data" + err.Error())
return nil, grpc.SqlErrorToGrpc(err, "sim-pool")
}
route := p.baseRoutingKey.SetAction("upload").SetObject("sim").MustBuild()
err = p.msgbus.PublishRequest(route, req)
if err != nil {
logrus.Errorf("Failed to publish message %+v with key %+v. Errors %s", req, route, err.Error())
}
iccids := []string{}
for _, u := range s {
iccids = append(iccids, u.Iccid)
}
return &pb.UploadResponse{Iccid: iccids}, nil
}
func (p *SimPoolServer) Delete(ctx context.Context, req *pb.DeleteRequest) (*pb.DeleteResponse, error) {
logrus.Infof("Delete Sims: %v", req.GetId())
err := p.simRepo.Delete(req.GetId())
if err != nil {
logrus.Error("error while delete sims data" + err.Error())
return nil, grpc.SqlErrorToGrpc(err, "sim-pool")
}
return &pb.DeleteResponse{Id: req.GetId()}, nil
}
func dbSimsToPbSim(packages []db.Sim) []*pb.Sim {
res := []*pb.Sim{}
for _, u := range packages {
res = append(res, dbSimToPbSim(&u))
}
return res
}
func dbSimToPbSim(p *db.Sim) *pb.Sim {
return &pb.Sim{
Id: uint64(p.ID),
Iccid: p.Iccid,
Msisdn: p.Msisdn,
IsAllocated: p.IsAllocated,
SimType: p.SimType.String(),
SmDpAddress: p.SmDpAddress,
ActivationCode: p.ActivationCode,
CreatedAt: p.CreatedAt.String(),
DeletedAt: p.DeletedAt.Time.String(),
UpdatedAt: p.UpdatedAt.String(),
IsPhysical: p.IsPhysical,
QrCode: p.QrCode,
IsFailed: p.IsFailed,
}
}