8
8
import hlf .java .rest .client .exception .ErrorConstants ;
9
9
import hlf .java .rest .client .exception .FabricTransactionException ;
10
10
import hlf .java .rest .client .exception .ServiceException ;
11
+ import hlf .java .rest .client .model .AnchorPeerDTO ;
11
12
import hlf .java .rest .client .model .ChannelUpdateParamsDTO ;
12
13
import hlf .java .rest .client .model .ClientResponseModel ;
13
14
import hlf .java .rest .client .model .CommitChannelParamsDTO ;
14
15
import hlf .java .rest .client .service .ChannelConfigDeserialization ;
15
16
import hlf .java .rest .client .service .NetworkStatus ;
16
17
import hlf .java .rest .client .service .UpdateChannel ;
17
18
import java .io .IOException ;
19
+ import java .util .ArrayList ;
18
20
import java .util .Base64 ;
21
+ import java .util .List ;
19
22
import lombok .extern .slf4j .Slf4j ;
20
23
import org .hyperledger .fabric .gateway .Gateway ;
21
24
import org .hyperledger .fabric .gateway .Network ;
@@ -79,7 +82,7 @@ public ResponseEntity<ClientResponseModel> getChannelFromNetwork(String channelN
79
82
"One or more arguments included in the config update are invalid" ,
80
83
e );
81
84
} catch (TransactionException e ) {
82
- log .warn ("Error retrieving channel config: " + e .getMessage ());
85
+ log .warn ("Error retrieving channel config: {} " , e .getMessage ());
83
86
throw new FabricTransactionException (
84
87
ErrorCode .HYPERLEDGER_FABRIC_TRANSACTION_ERROR ,
85
88
ErrorCode .HYPERLEDGER_FABRIC_TRANSACTION_ERROR .name (),
@@ -91,7 +94,7 @@ public ResponseEntity<ClientResponseModel> getChannelFromNetwork(String channelN
91
94
"Error while establishing connection to the gateway" ,
92
95
e );
93
96
}
94
- log .warn ("Error getting channel config: Network cannot be NULL: " + " Network = " + network );
97
+ log .warn ("Error getting channel config: Network cannot be NULL: Network: {}" , network );
95
98
return new ResponseEntity <>(
96
99
new ClientResponseModel (ErrorCode .NOT_FOUND .getValue (), ErrorCode .NOT_FOUND .name ()),
97
100
HttpStatus .OK );
@@ -110,9 +113,8 @@ public ResponseEntity<ClientResponseModel> generateConfigUpdate(
110
113
HttpStatus .OK );
111
114
} else {
112
115
log .warn (
113
- "Error generating the Config Update: Network and User cannot be NULL: "
114
- + "Network = "
115
- + network );
116
+ "Error generating the Config Update: Network and User cannot be NULL: Network: {}" ,
117
+ network );
116
118
return new ResponseEntity <>(
117
119
new ClientResponseModel (
118
120
ErrorCode .NOT_FOUND .getValue (),
@@ -124,7 +126,7 @@ public ResponseEntity<ClientResponseModel> generateConfigUpdate(
124
126
private ConfigUpdate createConfigUpdate (
125
127
String channelName , ChannelUpdateParamsDTO organizationDetails ) {
126
128
Network network = gateway .getNetwork (channelName );
127
- if (network != null ) {
129
+ if (network != null && organizationDetails . getMspDTO () != null ) {
128
130
try {
129
131
Channel selectedChannel = network .getChannel ();
130
132
byte [] channelConfigBytes = selectedChannel .getChannelConfigurationBytes ();
@@ -161,7 +163,7 @@ private ConfigUpdate createConfigUpdate(
161
163
"Channel has no peer or orderers defined. Can not get configuration block" ,
162
164
e );
163
165
} catch (TransactionException e ) {
164
- log .warn ("Error while fetching channel config: " + e .getMessage ());
166
+ log .warn ("Error while fetching channel config: {} " , e .getMessage ());
165
167
throw new FabricTransactionException (
166
168
ErrorCode .HYPERLEDGER_FABRIC_TRANSACTION_ERROR ,
167
169
ErrorCode .HYPERLEDGER_FABRIC_TRANSACTION_ERROR .name (),
@@ -175,7 +177,9 @@ private ConfigUpdate createConfigUpdate(
175
177
}
176
178
} else {
177
179
log .warn (
178
- "Error fetching the channel config: Network cannot be NULL: " + "Network = " + network );
180
+ "Error fetching the channel config: Network and MSP details cannot be NULL: Network: {} and MSP details: {} " ,
181
+ network ,
182
+ organizationDetails .getMspDTO ());
179
183
return ConfigUpdate .newBuilder ().build ();
180
184
}
181
185
}
@@ -227,11 +231,9 @@ public ResponseEntity<ClientResponseModel> signChannelConfigTransaction(
227
231
}
228
232
} else {
229
233
log .warn (
230
- "Error while signing channel config: Network and User cannot be NULL: "
231
- + "Network = "
232
- + network
233
- + "and User = "
234
- + user );
234
+ "Error while signing channel config: Network and User cannot be NULL: Network: {} and User: {}" ,
235
+ network ,
236
+ user );
235
237
return new ResponseEntity <>(
236
238
new ClientResponseModel (
237
239
ErrorCode .NOT_FOUND .getValue (),
@@ -287,7 +289,7 @@ public ResponseEntity<ClientResponseModel> commitChannelConfigTransaction(
287
289
"One or more arguments included in the config update are invalid" ,
288
290
e );
289
291
} catch (TransactionException e ) {
290
- log .warn ("Error while committing channel config: " + e .getMessage ());
292
+ log .warn ("Error while committing channel config: {} " , e .getMessage ());
291
293
throw new FabricTransactionException (
292
294
ErrorCode .HYPERLEDGER_FABRIC_TRANSACTION_ERROR ,
293
295
ErrorCode .HYPERLEDGER_FABRIC_TRANSACTION_ERROR .name (),
@@ -301,11 +303,9 @@ public ResponseEntity<ClientResponseModel> commitChannelConfigTransaction(
301
303
}
302
304
} else {
303
305
log .warn (
304
- "Error while committing channel config: Network and User cannot be NULL: "
305
- + "Network = "
306
- + network
307
- + "and User = "
308
- + user );
306
+ "Error while committing channel config: Network and User cannot be NULL: Network: {} and User:{}" ,
307
+ network ,
308
+ user );
309
309
return new ResponseEntity <>(
310
310
new ClientResponseModel (
311
311
ErrorCode .NOT_FOUND .getValue (),
@@ -318,7 +318,7 @@ public ResponseEntity<ClientResponseModel> commitChannelConfigTransaction(
318
318
public ResponseEntity <ClientResponseModel > addOrgToChannel (
319
319
String channelName , ChannelUpdateParamsDTO organizationDetails ) {
320
320
Network network = gateway .getNetwork (channelName );
321
- if (network != null && user != null ) {
321
+ if (network != null && user != null && organizationDetails . getMspDTO () != null ) {
322
322
try {
323
323
Channel selectedChannel = network .getChannel ();
324
324
ConfigUpdate configUpdate = createConfigUpdate (channelName , organizationDetails );
@@ -339,7 +339,7 @@ public ResponseEntity<ClientResponseModel> addOrgToChannel(
339
339
"One or more arguments included in the config update are invalid" ,
340
340
e );
341
341
} catch (TransactionException e ) {
342
- log .warn ("Error while committing channel config: " + e .getMessage ());
342
+ log .warn ("Error while committing channel config: {} " , e .getMessage ());
343
343
throw new FabricTransactionException (
344
344
ErrorCode .HYPERLEDGER_FABRIC_TRANSACTION_ERROR ,
345
345
ErrorCode .HYPERLEDGER_FABRIC_TRANSACTION_ERROR .name (),
@@ -355,32 +355,51 @@ public ResponseEntity<ClientResponseModel> addOrgToChannel(
355
355
new ClientResponseModel (ErrorConstants .NO_ERROR , ErrorCode .SUCCESS .getReason ()),
356
356
HttpStatus .OK );
357
357
} else {
358
- log .warn ("Network and User cannot be NULL: " + "Network = " + network + "and User = " + user );
358
+ log .warn (
359
+ "Network, User and MSP details cannot be NULL: Network: {}, User: {} and MSP details: {}" ,
360
+ network ,
361
+ user ,
362
+ organizationDetails .getMspDTO ());
359
363
return new ResponseEntity <>(
360
364
new ClientResponseModel (
361
365
ErrorCode .NOT_FOUND .getValue (),
362
- "Network and User cannot be NULL: " + "Network = " + network + "and User = " + user ),
366
+ "Network, User and MSP details cannot be NULL: "
367
+ + "Network = "
368
+ + network
369
+ + " User = "
370
+ + user
371
+ + " and MSP details = "
372
+ + organizationDetails .getMspDTO ()),
363
373
HttpStatus .OK );
364
374
}
365
375
}
366
376
377
+ private List <String > getAnchorPeersToAdd (ChannelUpdateParamsDTO channelUpdateParamsDTO ) {
378
+ // Anchor peers are required as Host:Port
379
+ List <String > anchorPeerList = new ArrayList <>();
380
+ for (AnchorPeerDTO anchorPeerDTO : channelUpdateParamsDTO .getAnchorPeerDTOs ()) {
381
+ anchorPeerList .add (anchorPeerDTO .getHostname () + ":" + anchorPeerDTO .getPort ());
382
+ }
383
+ return anchorPeerList ;
384
+ }
385
+
367
386
@ Override
368
387
public ResponseEntity <ClientResponseModel > addAnchorPeersToChannel (
369
388
String channelName , ChannelUpdateParamsDTO channelUpdateParamsDTO ) {
370
389
Network network = gateway .getNetwork (channelName );
371
390
if (network != null && user != null ) {
372
391
try {
373
392
Channel selectedChannel = network .getChannel ();
374
- ConfigUpdate configUpdate = createConfigUpdate ( channelName , channelUpdateParamsDTO );
375
- String channelConfigString = JsonFormat . printer (). print ( configUpdate );
376
- log . info ( channelConfigDeserialization . deserializeValueFields ( channelConfigString ));
377
- UpdateChannelConfiguration updateChannelConfiguration = new UpdateChannelConfiguration ();
378
- updateChannelConfiguration . setUpdateChannelConfiguration (
379
- configUpdate . toByteString (). toByteArray () );
393
+ Channel . AnchorPeersConfigUpdateResult configUpdateAnchorPeers =
394
+ selectedChannel . getConfigUpdateAnchorPeers (
395
+ selectedChannel . getPeers (). iterator (). next (),
396
+ user ,
397
+ getAnchorPeersToAdd ( channelUpdateParamsDTO ),
398
+ null );
380
399
selectedChannel .updateChannelConfiguration (
381
- updateChannelConfiguration ,
400
+ configUpdateAnchorPeers . getUpdateChannelConfiguration () ,
382
401
selectedChannel .getUpdateChannelConfigurationSignature (
383
- updateChannelConfiguration , user ));
402
+ configUpdateAnchorPeers . getUpdateChannelConfiguration () , user ));
384
403
} catch (InvalidArgumentException e ) {
385
404
log .warn (
386
405
"Error while committing channel config: One or more arguments included in the config update are invalid" );
@@ -389,23 +408,23 @@ public ResponseEntity<ClientResponseModel> addAnchorPeersToChannel(
389
408
"One or more arguments included in the config update are invalid" ,
390
409
e );
391
410
} catch (TransactionException e ) {
392
- log .warn ("Error while committing channel config: " + e .getMessage ());
411
+ log .warn ("Error while committing channel config: {}" , e .getMessage ());
393
412
throw new FabricTransactionException (
394
413
ErrorCode .HYPERLEDGER_FABRIC_TRANSACTION_ERROR ,
395
414
ErrorCode .HYPERLEDGER_FABRIC_TRANSACTION_ERROR .name (),
396
415
e );
397
- } catch (IOException e ) {
398
- log .warn ("Error while establishing connection to the gateway" );
399
- throw new ServiceException (
400
- ErrorCode .HYPERLEDGER_FABRIC_CONNECTION_ERROR ,
401
- "Error while establishing connection to the gateway" ,
416
+ } catch (Exception e ) {
417
+ log .warn ("Error while channel configuration update: {}" , e . getMessage () );
418
+ throw new FabricTransactionException (
419
+ ErrorCode .HYPERLEDGER_FABRIC_TRANSACTION_ERROR ,
420
+ ErrorCode . HYPERLEDGER_FABRIC_TRANSACTION_ERROR . name () ,
402
421
e );
403
422
}
404
423
return new ResponseEntity <>(
405
424
new ClientResponseModel (ErrorConstants .NO_ERROR , ErrorCode .SUCCESS .getReason ()),
406
425
HttpStatus .OK );
407
426
} else {
408
- log .warn ("Network and User cannot be NULL: " + " Network = " + network + " and User = " + user );
427
+ log .warn ("Network and User cannot be NULL: Network: {} and User: {}" , network , user );
409
428
return new ResponseEntity <>(
410
429
new ClientResponseModel (
411
430
ErrorCode .NOT_FOUND .getValue (),
0 commit comments