-
Notifications
You must be signed in to change notification settings - Fork 23
/
server.go
443 lines (373 loc) · 16.3 KB
/
server.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// SPDX-License-Identifier: Apache-2.0
package ocpi
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/go-chi/render"
"github.com/thoughtworks/maeve-csms/manager/handlers"
"github.com/thoughtworks/maeve-csms/manager/ocpp/ocpp16"
"golang.org/x/exp/slog"
"k8s.io/utils/clock"
"net/http"
"regexp"
"strconv"
"time"
)
type Server struct {
ocpi Api
clock clock.PassiveClock
v16CallMaker *handlers.OcppCallMaker
}
func NewServer(ocpi Api, clock clock.PassiveClock, v16CallMaker *handlers.OcppCallMaker) (*Server, error) {
return &Server{
ocpi: ocpi,
clock: clock,
v16CallMaker: v16CallMaker,
}, nil
}
// VERSIONS
func (s *Server) GetVersions(w http.ResponseWriter, r *http.Request, params GetVersionsParams) {
versions, err := s.ocpi.GetVersions(r.Context())
if err != nil {
_ = render.Render(w, r, OcpiResponseListVersion{
StatusCode: StatusGenericServerFailure,
Timestamp: s.clock.Now().Format(time.RFC3339),
})
return
}
_ = render.Render(w, r, OcpiResponseListVersion{
Data: &versions,
StatusCode: StatusSuccess,
StatusMessage: &StatusSuccessMessage,
Timestamp: s.clock.Now().Format(time.RFC3339),
})
}
func (s *Server) GetVersion(w http.ResponseWriter, r *http.Request, params GetVersionParams) {
version, err := s.ocpi.GetVersion(r.Context())
if err != nil {
if err != nil {
_ = render.Render(w, r, OcpiResponseListVersion{
StatusCode: StatusGenericServerFailure,
Timestamp: s.clock.Now().Format(time.RFC3339),
})
return
}
}
_ = render.Render(w, r, OcpiResponseVersionDetail{
StatusCode: StatusSuccess,
Data: &version,
Timestamp: s.clock.Now().Format(time.RFC3339),
StatusMessage: &StatusSuccessMessage,
})
}
// CREDENTIALS
func (s *Server) PostCredentials(w http.ResponseWriter, r *http.Request, params PostCredentialsParams) {
creds := new(Credentials)
if err := render.Bind(r, creds); err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
matches := authzHeaderRegexp.FindStringSubmatch(params.Authorization)
if len(matches) != 2 {
_ = render.Render(w, r, ErrInvalidRequest(fmt.Errorf("invalid authorization header")))
return
}
err := s.ocpi.SetCredentials(r.Context(), matches[1], *creds)
if err != nil {
slog.Error("Error setting credentials", "err", err)
_ = render.Render(w, r, ErrInternalError(err))
return
}
w.WriteHeader(http.StatusCreated)
}
// TOKEN RECEIVER
func (s *Server) GetClientOwnedToken(w http.ResponseWriter, r *http.Request, countryCode string, partyID string, tokenUID string, params GetClientOwnedTokenParams) {
token, err := s.ocpi.GetToken(r.Context(), countryCode, partyID, tokenUID)
if err != nil {
_ = render.Render(w, r, ErrInternalError(err))
return
}
_ = render.Render(w, r, OcpiResponseToken{
StatusCode: StatusSuccess,
StatusMessage: &StatusSuccessMessage,
Timestamp: s.clock.Now().Format(time.RFC3339),
Data: token,
})
}
func (s *Server) PutClientOwnedToken(w http.ResponseWriter, r *http.Request, countryCode string, partyID string, tokenUID string, params PutClientOwnedTokenParams) {
tok := new(Token)
if err := render.Bind(r, tok); err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
if tok.CountryCode != countryCode {
_ = render.Render(w, r, ErrInvalidRequest(fmt.Errorf("token country code mismatch")))
return
}
if tok.PartyId != partyID {
_ = render.Render(w, r, ErrInvalidRequest(fmt.Errorf("token party id mismatch")))
return
}
if tok.Uid != tokenUID {
_ = render.Render(w, r, ErrInvalidRequest(fmt.Errorf("token uid mismatch")))
return
}
err := s.ocpi.SetToken(r.Context(), *tok)
if err != nil {
_ = render.Render(w, r, OcpiResponseListVersion{
StatusCode: StatusGenericServerFailure,
Timestamp: s.clock.Now().Format(time.RFC3339),
})
}
}
func (s *Server) PatchClientOwnedToken(w http.ResponseWriter, r *http.Request, countryCode string, partyID string, tokenUID string, params PatchClientOwnedTokenParams) {
var patch map[string]any
err := json.NewDecoder(r.Body).Decode(&patch)
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
tok, err := s.ocpi.GetToken(r.Context(), countryCode, partyID, tokenUID)
if err != nil {
_ = render.Render(w, r, ErrInternalError(err))
return
}
if tok == nil {
_ = render.Render(w, r, ErrNotFound)
return
}
for k, v := range patch {
switch k {
case "contract_id":
contractID := v.(string)
tok.ContractId = contractID
case "group_id":
groupID := v.(string)
tok.GroupId = &groupID
case "issuer":
issuer := v.(string)
tok.Issuer = issuer
case "language":
language := v.(string)
tok.Language = &language
case "type":
typ := v.(string)
tok.Type = TokenType(typ)
case "valid":
valid := v.(bool)
tok.Valid = valid
case "visual_number":
visualNumber := v.(string)
tok.VisualNumber = &visualNumber
case "whitelist":
whitelist := v.(string)
tok.Whitelist = TokenWhitelist(whitelist)
default:
_ = render.Render(w, r, ErrInvalidRequest(fmt.Errorf("unknown field %s", k)))
}
}
err = s.ocpi.SetToken(r.Context(), *tok)
if err != nil {
_ = render.Render(w, r, ErrInternalError(err))
return
}
}
func (s *Server) DeleteCredentials(w http.ResponseWriter, r *http.Request, params DeleteCredentialsParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetCredentials(w http.ResponseWriter, r *http.Request, params GetCredentialsParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PutCredentials(w http.ResponseWriter, r *http.Request, params PutCredentialsParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) DeleteReceiverChargingProfile(w http.ResponseWriter, r *http.Request, sessionId string, params DeleteReceiverChargingProfileParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetReceiverChargingProfile(w http.ResponseWriter, r *http.Request, sessionId string, params GetReceiverChargingProfileParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PutReceiverChargingProfile(w http.ResponseWriter, r *http.Request, sessionId string, params PutReceiverChargingProfileParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PostGenericChargingProfileResult(w http.ResponseWriter, r *http.Request, uid string, params PostGenericChargingProfileResultParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PutSenderChargingProfile(w http.ResponseWriter, r *http.Request, sessionId string, params PutSenderChargingProfileParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PostClientOwnedCdr(w http.ResponseWriter, r *http.Request, params PostClientOwnedCdrParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetClientOwnedCdr(w http.ResponseWriter, r *http.Request, cdrID string, params GetClientOwnedCdrParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PostCancelReservation(w http.ResponseWriter, r *http.Request, params PostCancelReservationParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PostReserveNow(w http.ResponseWriter, r *http.Request, params PostReserveNowParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PostStartSession(w http.ResponseWriter, r *http.Request, params PostStartSessionParams) {
// TODO: the following code supports OCPP 1.6 only, but we need to support 2.0 as well
// need to store OCPP protocol version for charge points on first connection
// need to create two handlers, one for 1.6 and one for 2.0, and call the right one based on the protocol version
startSession := new(StartSession)
if err := render.Bind(r, startSession); err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
var chargeStationId string
if startSession.EvseUid == nil || startSession.ConnectorId == nil {
_ = render.Render(w, r, ErrInvalidRequest(errors.New("CSMS does not support start session commands without evse_uid and connector_id")))
return
} else {
// TODO: this needs to become a service with a configurable pattern
extractedChargeStationId, err := extractChargeStationId(*startSession.EvseUid)
if err != nil {
slog.Error("error extracting charge station id", "err", err)
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
chargeStationId = extractedChargeStationId
}
// We need to store the token because the StartSession handler currently expects the idTag it receives in the store
err := s.ocpi.SetToken(context.Background(), startSession.Token)
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
connectorId, err := strconv.Atoi(*startSession.ConnectorId)
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
remoteStartTransactionReq := ocpp16.RemoteStartTransactionJson{
ConnectorId: &connectorId,
IdTag: startSession.Token.Uid,
}
commandResponse := CommandResponse{Result: CommandResponseResultACCEPTED}
err = s.v16CallMaker.Send(context.Background(), chargeStationId, &remoteStartTransactionReq)
if err != nil {
slog.Error("error sending mqtt message", "err", err)
commandResponse = CommandResponse{Result: CommandResponseResultREJECTED}
}
_ = render.Render(w, r, OcpiResponseCommandResponse{
StatusCode: StatusSuccess,
StatusMessage: &StatusSuccessMessage,
Timestamp: s.clock.Now().Format(time.RFC3339),
Data: &commandResponse,
})
}
func (s *Server) PostStopSession(w http.ResponseWriter, r *http.Request, params PostStopSessionParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PostUnlockConnector(w http.ResponseWriter, r *http.Request, params PostUnlockConnectorParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetClientOwnedLocation(w http.ResponseWriter, r *http.Request, countryCode string, partyID string, locationID string, params GetClientOwnedLocationParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PatchClientOwnedLocation(w http.ResponseWriter, r *http.Request, countryCode string, partyID string, locationID string, params PatchClientOwnedLocationParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PutClientOwnedLocation(w http.ResponseWriter, r *http.Request, countryCode string, partyID string, locationID string, params PutClientOwnedLocationParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetClientOwnedEvse(w http.ResponseWriter, r *http.Request, countryCode string, partyID string, locationID string, evseUID string, params GetClientOwnedEvseParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PatchClientOwnedEvse(w http.ResponseWriter, r *http.Request, countryCode string, partyID string, locationID string, evseUID string, params PatchClientOwnedEvseParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PutClientOwnedEvse(w http.ResponseWriter, r *http.Request, countryCode string, partyID string, locationID string, evseUID string, params PutClientOwnedEvseParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetClientOwnedConnector(w http.ResponseWriter, r *http.Request, countryCode string, partyID string, locationID string, evseUID string, connectorID string, params GetClientOwnedConnectorParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PatchClientOwnedConnector(w http.ResponseWriter, r *http.Request, countryCode string, partyID string, locationID string, evseUID string, connectorID string, params PatchClientOwnedConnectorParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PutClientOwnedConnector(w http.ResponseWriter, r *http.Request, countryCode string, partyID string, locationID string, evseUID string, connectorID string, params PutClientOwnedConnectorParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetClientOwnedSession(w http.ResponseWriter, r *http.Request, countryCode string, partyID string, sessionID string, params GetClientOwnedSessionParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PatchClientOwnedSession(w http.ResponseWriter, r *http.Request, countryCode string, partyID string, sessionID string, params PatchClientOwnedSessionParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PutClientOwnedSession(w http.ResponseWriter, r *http.Request, countryCode string, partyID string, sessionID string, params PutClientOwnedSessionParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) DeleteClientOwnedTariff(w http.ResponseWriter, r *http.Request, countryCode string, partyID string, tariffID string, params DeleteClientOwnedTariffParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetClientOwnedTariff(w http.ResponseWriter, r *http.Request, countryCode string, partyID string, tariffID string, params GetClientOwnedTariffParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PutClientOwnedTariff(w http.ResponseWriter, r *http.Request, countryCode string, partyID string, tariffID string, params PutClientOwnedTariffParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetCdrsFromDataOwner(w http.ResponseWriter, r *http.Request, params GetCdrsFromDataOwnerParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetCdrPageFromDataOwner(w http.ResponseWriter, r *http.Request, uid string, params GetCdrPageFromDataOwnerParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PostAsyncResponse(w http.ResponseWriter, r *http.Request, command PostAsyncResponseParamsCommand, uid string, params PostAsyncResponseParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetLocationListFromDataOwner(w http.ResponseWriter, r *http.Request, params GetLocationListFromDataOwnerParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetLocationPageFromDataOwner(w http.ResponseWriter, r *http.Request, uid string, params GetLocationPageFromDataOwnerParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetLocationObjectFromDataOwner(w http.ResponseWriter, r *http.Request, locationID string, params GetLocationObjectFromDataOwnerParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetEvseObjectFromDataOwner(w http.ResponseWriter, r *http.Request, locationID string, evseUID string, params GetEvseObjectFromDataOwnerParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetConnectorObjectFromDataOwner(w http.ResponseWriter, r *http.Request, locationID string, evseUID string, connectorID string, params GetConnectorObjectFromDataOwnerParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetSessionsFromDataOwner(w http.ResponseWriter, r *http.Request, params GetSessionsFromDataOwnerParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetSessionsPageFromDataOwner(w http.ResponseWriter, r *http.Request, uid string, params GetSessionsPageFromDataOwnerParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PutChargingPreferences(w http.ResponseWriter, r *http.Request, sessionID string, params PutChargingPreferencesParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetTariffsFromDataOwner(w http.ResponseWriter, r *http.Request, params GetTariffsFromDataOwnerParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetTariffsPageFromDataOwner(w http.ResponseWriter, r *http.Request, uid string, params GetTariffsPageFromDataOwnerParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetTokensFromDataOwner(w http.ResponseWriter, r *http.Request, params GetTokensFromDataOwnerParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) GetTokensPageFromDataOwner(w http.ResponseWriter, r *http.Request, uid string, params GetTokensPageFromDataOwnerParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func (s *Server) PostRealTimeTokenAuthorization(w http.ResponseWriter, r *http.Request, tokenUID string, params PostRealTimeTokenAuthorizationParams) {
w.WriteHeader(http.StatusNotImplemented)
}
func extractChargeStationId(evseId string) (string, error) {
pattern := `^[a-zA-Z]{5}E([a-zA-Z0-9]+)?$`
regex := regexp.MustCompile(pattern)
match := regex.FindStringSubmatch(evseId)
if len(match) >= 2 {
chargePointID := match[1]
return chargePointID, nil
} else {
return "", fmt.Errorf("invalid EVSE ID format, could not extract charge point ID: %s", evseId)
}
}