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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
|
/*-
* ============LICENSE_START=======================================================
* ONAP : APPC
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Copyright (C) 2017 Amdocs
* =============================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ECOMP is a trademark and service mark of AT&T Intellectual Property.
* ============LICENSE_END=========================================================
*/
package org.onap.appc.provider.topology;
import static com.att.eelf.configuration.Configuration.MDC_ALERT_SEVERITY;
import static com.att.eelf.configuration.Configuration.MDC_INSTANCE_UUID;
import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID;
import static com.att.eelf.configuration.Configuration.MDC_REMOTE_HOST;
import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN;
import static com.att.eelf.configuration.Configuration.MDC_SERVER_IP_ADDRESS;
import static com.att.eelf.configuration.Configuration.MDC_SERVICE_INSTANCE_ID;
import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.att.eelf.i18n.EELFResourceManager;
import java.net.InetAddress;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.TimeZone;
import org.onap.appc.Constants;
import org.onap.appc.configuration.Configuration;
import org.onap.appc.configuration.ConfigurationFactory;
import org.onap.appc.i18n.Msg;
import org.onap.appc.provider.AppcProvider;
import org.onap.appc.provider.AppcProviderClient;
import org.onap.appc.provider.ResponseHeaderBuilder;
import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.MigrateOutput;
import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.MigrateOutputBuilder;
import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.ModifyConfigOutput;
import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.ModifyConfigOutputBuilder;
import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.RebuildOutput;
import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.RebuildOutputBuilder;
import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.RestartOutput;
import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.RestartOutputBuilder;
import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.SnapshotOutput;
import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.SnapshotOutputBuilder;
import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.UUID;
import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.VmstatuscheckOutput;
import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.VmstatuscheckOutputBuilder;
import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.common.request.header.CommonRequestHeader;
import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.config.payload.ConfigPayload;
import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.vnf.resource.VnfResource;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
import org.slf4j.MDC;
/**
* This class is used to implement the topology services API and invoke the appropriate directed graphs based on the
* service being requested.
*/
public class TopologyService {
/**
* The loggers we are using
*/
private final EELFLogger logger = EELFManager.getInstance().getApplicationLogger();
private final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
private final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
private final EELFLogger performanceLogger = EELFManager.getInstance().getPerformanceLogger();
private final static String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssX";
private final static String START_TIME_PARAM = "startTime";
private final static String RESTART_INITIATED_STR = "Metrics Logger: App-C Restart initiated. Start Time: [%s]. Request ID: [%s]";
private final static String TARGET_PARAM = "target";
private final static String SUCCESS_PARAM = "SUCCESS";
private final static String FAILURE_PARAM = "FAILURE";
private final static String END_TIME_PARAM = "endTime";
private final static String DURATION_PARAM = "duration";
private final static String ERROR_STR = "An error occurred";
/**
* The provider we are servicing
*/
private AppcProvider provider;
/**
* The reason associated with the last DG call
*/
private String reason;
/**
* The APPC configuration properties
*/
private Configuration configuration = ConfigurationFactory.getConfiguration();
/**
* Create the topology services implementation for the specific appc provider (api) implementation
*
* @param provider The provider we are servicing
*/
public TopologyService(AppcProvider provider) {
this.provider = provider;
}
/**
* Modify configuration
*
* @param hdr The common request header
* @param data The payload of the configuration
* @return The rpc result of the operation
*/
public RpcResult<ModifyConfigOutput> modifyConfig(CommonRequestHeader hdr, ConfigPayload data) {
long startTime = System.currentTimeMillis();
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat(DATE_FORMAT);
df.setTimeZone(tz);
String requestId = hdr.getServiceRequestId();
MDC.put(MDC_REMOTE_HOST, "");
MDC.put(MDC_KEY_REQUEST_ID, requestId);
MDC.put(MDC_SERVICE_NAME, "App-C Provider:Restart");
MDC.put(MDC_SERVICE_INSTANCE_ID, "");
try {
MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName());
MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());
} catch (Exception e) {
logger.error(ERROR_STR, e);
}
MDC.put(MDC_INSTANCE_UUID, java.util.UUID.randomUUID().toString());
MDC.put(MDC_ALERT_SEVERITY, "0");
MDC.put(START_TIME_PARAM, Long.toString(startTime));
MDC.put(TARGET_PARAM, "appc");
logger.info(String.format("Starting RESTART for request with id [%s]", requestId));
metricsLogger.info(String.format(RESTART_INITIATED_STR,
startTime, requestId));
/*
* Copy any needed inputs or other values into the properties to be passed to the DG model
*/
Properties properties = new Properties();
properties.put(Constants.CONTEXT_ACTION, "modifyConfig");
properties.put(Constants.CONTEXT_REQID, requestId);
String url = configuration.getProperty("appc.provider.vfodl.url");
try {
if (url.contains("NODE_NAME")) {
url = url.replace("NODE_NAME", data.getConfigUrl());
}
} catch (Exception e) {
logger.error("An error occurred when replacing node name", e);
url = configuration.getProperty("appc.provider.vfodl.url");
}
logger.trace("Final URL to VF ODL: " + url);
properties.put("org.onap.appc.configURL", url);
properties.put("org.onap.appc.configJson", data.getConfigJson());
/*
* Attempt to call the DG with the appropriate properties
*/
boolean success = callGraph(properties);
MDC.put(TARGET_PARAM, "appc");
String statusStr = success ? SUCCESS_PARAM : FAILURE_PARAM;
String infomsg =
String.format("APPC0119I ModifyConfig '%s' finished with status %s. Reason: %s", requestId, statusStr,
reason);
logger.info(infomsg);
ModifyConfigOutputBuilder rob = new ModifyConfigOutputBuilder();
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
String endTimeStr = String.valueOf(endTime);
String durationStr = String.valueOf(duration);
MDC.put(END_TIME_PARAM, endTimeStr);
MDC.put(DURATION_PARAM, durationStr);
rob.setCommonResponseHeader(ResponseHeaderBuilder.buildHeader(success, requestId, reason, duration));
auditLogger.info(String.format(
"Audit Logger: APPC0119I Restart '%s' finished with status %s. Start Time: [%s]. End Time: [%s]. Duration: [%s]. Request ID: [%s]. Reason:%s",
requestId, statusStr, startTime, endTime, duration, requestId, reason));
metricsLogger.info(String.format(
"Metrics Logger: APPC0119I Restart '%s' finished with status %s. Start Time: [%s]. End Time: [%s]. Duration: [%s]. Request ID: [%s]. Reason:%s",
requestId, statusStr, startTime, endTime, duration, requestId, reason));
// Status must be set to true to indicate that our return is expected
return RpcResultBuilder.<ModifyConfigOutput>status(true).withResult(rob.build()).build();
}
/**
* Restart a VM
*
* @param hdr The common request header
* @param vnf The identification of the VNF resource to be operated upon
* @return The rpc result of the restart operation
*/
public RpcResult<MigrateOutput> migrate(CommonRequestHeader hdr, VnfResource vnf) {
long startTime = System.currentTimeMillis();
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat(DATE_FORMAT);
df.setTimeZone(tz);
String startTimeStr = df.format(new Date());
String requestId = hdr.getServiceRequestId();
MDC.put(MDC_REMOTE_HOST, "");
MDC.put(MDC_KEY_REQUEST_ID, requestId);
MDC.put(MDC_SERVICE_NAME, "App-C Provider:Migrate");
MDC.put(MDC_SERVICE_INSTANCE_ID, "");
try {
MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName());
MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());
} catch (Exception e) {
logger.error(ERROR_STR, e);
}
MDC.put(MDC_INSTANCE_UUID, java.util.UUID.randomUUID().toString());
MDC.put(MDC_ALERT_SEVERITY, "0");
MDC.put(START_TIME_PARAM, startTimeStr);
MDC.put(TARGET_PARAM, "appc");
logger.info(String.format("Starting ANY for request with id [%s]", requestId));
metricsLogger.info(String.format(RESTART_INITIATED_STR,
startTime, requestId));
/*
* Copy any needed inputs or other values into the properties to be passed to the DG model
*/
UUID vmId = vnf.getVmId();
Properties properties = new Properties();
properties.put(Constants.CONTEXT_ACTION, "migrate");
properties.put(Constants.CONTEXT_REQID, requestId);
properties.put(Constants.CONTEXT_VMID, vmId.getValue());
UUID identityUrl = vnf.getIdentityUrl();
if (identityUrl != null) {
properties.put(Constants.CONTEXT_IDENTITY_URL, identityUrl.getValue());
}
/*
* Attempt to call the DG with the appropriate properties
*/
boolean success = callGraph(properties);
/*
* Generate the appropriate response
*/
MDC.put(TARGET_PARAM, "appc");
String statusStr = success ? SUCCESS_PARAM : FAILURE_PARAM;
String infomsg =
String.format("APPC0118I Migrate '%s' finished with status %s. Reason: %s", requestId, statusStr, reason);
logger.info(infomsg);
MigrateOutputBuilder mob = new MigrateOutputBuilder();
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
String endTimeStr = String.valueOf(endTime);
String durationStr = String.valueOf(duration);
MDC.put(END_TIME_PARAM, endTimeStr);
MDC.put(DURATION_PARAM, durationStr);
mob.setCommonResponseHeader(ResponseHeaderBuilder.buildHeader(success, requestId, reason, duration));
mob.setVmId(new UUID(vmId));
auditLogger.info(String.format(
"Audit Logger: APPC0118I Migrate '%s' finished with status %s. Start Time: [%s]. End Time: [%s]. Duration: [%s]. Request ID: [%s]. Reason:%s",
requestId, statusStr, startTime, endTime, duration, requestId, reason));
metricsLogger.info(String.format(
"Metrics Logger: APPC0118I Migrate '%s' finished with status %s. Start Time: [%s]. End Time: [%s]. Duration: [%s]. Request ID: [%s]. Reason:%s",
requestId, statusStr, startTime, endTime, duration, requestId, reason));
// Status must be set to true to indicate that our return is expected
return RpcResultBuilder.<MigrateOutput>status(true).withResult(mob.build()).build();
}
/**
* Restart a VM
*
* @param hdr The common request header
* @param vnf The identification of the VNF resource to be operated upon
* @return The rpc result of the restart operation
*/
public RpcResult<RestartOutput> restart(CommonRequestHeader hdr, VnfResource vnf) {
long startTime = System.currentTimeMillis();
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat(DATE_FORMAT);
df.setTimeZone(tz);
String requestId = hdr.getServiceRequestId();
MDC.put(MDC_REMOTE_HOST, "");
MDC.put(MDC_KEY_REQUEST_ID, requestId);
MDC.put(MDC_SERVICE_NAME, "App-C Provider:Restart");
MDC.put(MDC_SERVICE_INSTANCE_ID, "");
try {
MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName());
MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());
} catch (Exception e) {
logger.error(ERROR_STR, e);
}
MDC.put(MDC_INSTANCE_UUID, java.util.UUID.randomUUID().toString());
MDC.put(MDC_ALERT_SEVERITY, "0");
MDC.put(START_TIME_PARAM, Long.toString(startTime));
MDC.put(TARGET_PARAM, "appc");
logger.info(String.format("Starting RESTART for request with id [%s]", requestId));
metricsLogger.info(String.format(RESTART_INITIATED_STR,
startTime, requestId));
/*
* Copy any needed inputs or other values into the properties to be passed to the DG model
*/
UUID vmId = vnf.getVmId();
Properties properties = new Properties();
properties.put(Constants.CONTEXT_ACTION, "restart");
properties.put(Constants.CONTEXT_REQID, requestId);
properties.put(Constants.CONTEXT_VMID, vmId.getValue());
UUID identityUrl = vnf.getIdentityUrl();
if (identityUrl != null) {
properties.put(Constants.CONTEXT_IDENTITY_URL, identityUrl.getValue());
}
/*
* Attempt to call the DG with the appropriate properties
*/
boolean success = callGraph(properties);
/*
* Generate the appropriate response
*/
MDC.put(TARGET_PARAM, "appc");
String statusStr = success ? SUCCESS_PARAM : FAILURE_PARAM;
String infomsg =
String.format("APPC0119I Restart '%s' finished with status %s. Reason: %s", requestId, statusStr, reason);
logger.info(infomsg);
RestartOutputBuilder rob = new RestartOutputBuilder();
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
String endTimeStr = String.valueOf(endTime);
String durationStr = String.valueOf(duration);
MDC.put(END_TIME_PARAM, endTimeStr);
MDC.put(DURATION_PARAM, durationStr);
rob.setCommonResponseHeader(ResponseHeaderBuilder.buildHeader(success, requestId, reason, duration));
rob.setVmId(new UUID(vmId));
auditLogger.info(String.format(
"Audit Logger: APPC0119I Restart '%s' finished with status %s. Start Time: [%s]. End Time: [%s]. Duration: [%s]. Request ID: [%s]. Reason:%s",
requestId, statusStr, startTime, endTime, duration, requestId, reason));
metricsLogger.info(String.format(
"Metrics Logger: APPC0119I Restart '%s' finished with status %s. Start Time: [%s]. End Time: [%s]. Duration: [%s]. Request ID: [%s]. Reason:%s",
requestId, statusStr, startTime, endTime, duration, requestId, reason));
// Status must be set to true to indicate that our return is expected
return RpcResultBuilder.<RestartOutput>status(true).withResult(rob.build()).build();
}
/**
* Rebuild a VM
*
* @param hdr The common request header
* @param vnf The identification of the VNF resource to be operated upon
* @return The rpc result of the rebuild operation
*/
public RpcResult<RebuildOutput> rebuild(CommonRequestHeader hdr, VnfResource vnf) {
long startTime = System.currentTimeMillis();
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat(DATE_FORMAT);
df.setTimeZone(tz);
String startTimeStr = df.format(new Date());
String requestId = hdr.getServiceRequestId();
MDC.put(MDC_REMOTE_HOST, "");
MDC.put(MDC_KEY_REQUEST_ID, requestId);
MDC.put(MDC_SERVICE_NAME, "App-C Provider:Rebuild");
MDC.put(MDC_SERVICE_INSTANCE_ID, "");
try {
MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName());
MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());
} catch (Exception e) {
logger.error(ERROR_STR, e);
}
MDC.put(MDC_INSTANCE_UUID, java.util.UUID.randomUUID().toString());
MDC.put(MDC_ALERT_SEVERITY, "0");
MDC.put(START_TIME_PARAM, startTimeStr);
MDC.put(TARGET_PARAM, "appc");
logger.info(String.format("Starting REBUILD for request with id [%s]", requestId));
metricsLogger.info(String.format(RESTART_INITIATED_STR,
startTime, requestId));
/*
* Copy any needed inputs or other values into the properties to be passed to the DG model
*/
UUID vmId = vnf.getVmId();
Properties properties = new Properties();
properties.put(Constants.CONTEXT_ACTION, "rebuild");
properties.put(Constants.CONTEXT_REQID, requestId);
properties.put(Constants.CONTEXT_VMID, vmId.getValue());
UUID identityUrl = vnf.getIdentityUrl();
if (identityUrl != null) {
properties.put(Constants.CONTEXT_IDENTITY_URL, identityUrl.getValue());
}
/*
* Attempt to call the DG with the appropriate properties
*/
boolean success = callGraph(properties);
/*
* Generate the appropriate response
*/
MDC.put(TARGET_PARAM, "appc");
String statusStr = success ? SUCCESS_PARAM : FAILURE_PARAM;
String infomsg =
String.format("APPC0120I Rebuild '%s' finished with status %s. Reason: %s", requestId, statusStr, reason);
logger.info(infomsg);
RebuildOutputBuilder rob = new RebuildOutputBuilder();
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
String endTimeStr = String.valueOf(endTime);
String durationStr = String.valueOf(duration);
MDC.put(END_TIME_PARAM, endTimeStr);
MDC.put(DURATION_PARAM, durationStr);
rob.setCommonResponseHeader(ResponseHeaderBuilder.buildHeader(success, requestId, reason, duration));
rob.setOriginalVmId(new UUID(vmId));
rob.setNewVmId(new UUID(vmId));
auditLogger.info(String.format(
"Audit Logger: APPC0120I Rebuild '%s' finished with status %s. Start Time: [%s]. End Time: [%s]. Duration: [%s]. Request ID: [%s]. Reason:%s",
requestId, statusStr, startTime, endTime, duration, requestId, reason));
metricsLogger.info(String.format(
"Metrics Logger: APPC0120I Rebuild '%s' finished with status %s. Start Time: [%s]. End Time: [%s]. Duration: [%s]. Request ID: [%s]. Reason:%s",
requestId, statusStr, startTime, endTime, duration, requestId, reason));
// Status must be set to true to indicate that our return is expected
return RpcResultBuilder.<RebuildOutput>status(true).withResult(rob.build()).build();
}
/**
* Snapshot a VM
*
* @param hdr The common request header
* @param vnf The identification of the VNF resource to be operated upon
* @return The rpc result of the restart operation
*/
public RpcResult<SnapshotOutput> snapshot(CommonRequestHeader hdr, VnfResource vnf) {
long startTime = System.currentTimeMillis();
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat(DATE_FORMAT);
df.setTimeZone(tz);
String requestId = hdr.getServiceRequestId();
MDC.put(MDC_REMOTE_HOST, "");
MDC.put(MDC_KEY_REQUEST_ID, requestId);
MDC.put(MDC_SERVICE_NAME, "App-C Provider:Snapshot");
MDC.put(MDC_SERVICE_INSTANCE_ID, "");
try {
MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName());
MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());
} catch (Exception e) {
logger.error(ERROR_STR, e);
}
MDC.put(MDC_INSTANCE_UUID, java.util.UUID.randomUUID().toString());
MDC.put(MDC_ALERT_SEVERITY, "0");
MDC.put(START_TIME_PARAM, Long.toString(startTime));
MDC.put(TARGET_PARAM, "appc");
logger.info(String.format("Starting SNAPSHOT for request with id [%s]", requestId));
metricsLogger.info(String.format("Metrics Logger: App-C Snapshot initiated. Start Time: [%s]. Request ID: [%s]",
startTime, requestId));
/*
* Copy any needed inputs or other values into the properties to be passed to the DG model
*/
UUID vmId = vnf.getVmId();
Properties properties = new Properties();
properties.put(Constants.CONTEXT_ACTION, "snapshot");
properties.put(Constants.CONTEXT_REQID, requestId);
properties.put(Constants.CONTEXT_VMID, vmId.getValue());
UUID identityUrl = vnf.getIdentityUrl();
if (identityUrl != null) {
properties.put(Constants.CONTEXT_IDENTITY_URL, identityUrl.getValue());
}
/*
* Attempt to call the DG with the appropriate properties
*/
boolean success = callGraph(properties);
/*
* Generate the appropriate response
*/
MDC.put(TARGET_PARAM, "appc");
String statusStr = success ? SUCCESS_PARAM : FAILURE_PARAM;
String infomsg =
String.format("APPC0119I Snapshot '%s' finished with status %s. Reason: %s", requestId, statusStr, reason);
logger.info(infomsg);
SnapshotOutputBuilder sob = new SnapshotOutputBuilder();
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
String endTimeStr = String.valueOf(endTime);
String durationStr = String.valueOf(duration);
MDC.put(END_TIME_PARAM, endTimeStr);
MDC.put(DURATION_PARAM, durationStr);
sob.setCommonResponseHeader(ResponseHeaderBuilder.buildHeader(success, requestId, reason, duration));
sob.setVmId(new UUID(vmId));
auditLogger.info(String.format(
"Audit Logger: APPC0119I Snapshot '%s' finished with status %s. Start Time: [%s]. End Time: [%s]. Duration: [%s]. Request ID: [%s]. Reason:%s",
requestId, statusStr, startTime, endTime, duration, requestId, reason));
metricsLogger.info(String.format(
"Metrics Logger: APPC0119I Snapshot '%s' finished with status %s. Start Time: [%s]. End Time: [%s]. Duration: [%s]. Request ID: [%s]. Reason:%s",
requestId, statusStr, startTime, endTime, duration, requestId, reason));
// Status must be set to true to indicate that our return is expected
return RpcResultBuilder.<SnapshotOutput>status(true).withResult(sob.build()).build();
}
/**************************************************/
public RpcResult<VmstatuscheckOutput> vmstatuscheck(CommonRequestHeader hdr, VnfResource vnf) {
long startTime = System.currentTimeMillis();
String requestId = hdr.getServiceRequestId();
MDC.clear();
MDC.put(MDC_REMOTE_HOST, "");
MDC.put(MDC_KEY_REQUEST_ID, requestId);
MDC.put(MDC_SERVICE_NAME, "App-C Provider:vmstatuscheck");
MDC.put(MDC_SERVICE_INSTANCE_ID, "");
try {
MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName());
MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());
} catch (Exception e) {
logger.error(ERROR_STR, e);
}
MDC.put(MDC_INSTANCE_UUID, java.util.UUID.randomUUID().toString());
MDC.put(MDC_ALERT_SEVERITY, "0");
logger.info(String.format("Starting VMSTATUSCHECK for request with id [%s]", requestId));
performanceLogger.info(String
.format("Performance Logger: App-C vmstatuscheck initiated. Start Time: [%s]. Request ID: [%s]", startTime,
requestId));
auditLogger.info(String
.format("Audit Logger: App-C vmstatuscheck initiated. Start Time: [%s]. Request ID: [%s]", startTime,
requestId));
metricsLogger.info(String
.format("Metrics Logger: App-C vmstatuscheck initiated. Start Time: [%s]. Request ID: [%s]", startTime,
requestId));
/*
* Copy any needed inputs or other values into the properties to be passed to the DG model
*/
UUID vmId = vnf.getVmId();
Properties properties = new Properties();
properties.put(Constants.CONTEXT_ACTION, "vmstatuschecking");
properties.put(Constants.CONTEXT_REQID, requestId);
properties.put(Constants.CONTEXT_VMID, vmId.getValue());
properties.put(Constants.STATUS_GETTER, "checking");
UUID identityUrl = vnf.getIdentityUrl();
if (identityUrl != null) {
properties.put(Constants.CONTEXT_IDENTITY_URL, identityUrl.getValue());
}
/*
* Attempt to call the DG with the appropriate properties
*/
boolean success = callGraph(properties);
/*
* Generate the appropriate response
*/
String statusStr = success ? SUCCESS_PARAM : FAILURE_PARAM;
String infomsg =
String.format("VMSTATUSCHECK '%s' finished with status %s. Reason: %s", requestId, statusStr, reason);
logger.info(infomsg);
long endTime = System.currentTimeMillis();
auditLogger.info(String.format(
"Audit Logger: VMSTATUSCHECK '%s' finished with status %s. Start Time: [%s]. End Time: [%s]. Request ID: [%s]. Reason:%s",
requestId, statusStr, startTime, endTime, requestId, reason));
metricsLogger.info(String.format(
"Metrics Logger: VMSTATUSCHECK '%s' finished with status %s. Start Time: [%s]. End Time: [%s]. Request ID: [%s]. Reason:%s",
requestId, statusStr, startTime, endTime, requestId, reason));
String tempstring2 = properties.getProperty(Constants.STATUS_GETTER).trim();
VmstatuscheckOutputBuilder vob = new VmstatuscheckOutputBuilder();
long duration = System.currentTimeMillis() - startTime;
vob.setCommonResponseHeader(ResponseHeaderBuilder.buildHeader(success, requestId, reason, duration));
vob.setStatMsg(tempstring2);
// Status must be set to true to indicate that our return is expected
return RpcResultBuilder.<VmstatuscheckOutput>status(true).withResult(vob.build()).build();
}
/*************************************************/
private boolean callGraph(Properties props) {
String moduleName = configuration.getProperty(Constants.PROPERTY_MODULE_NAME);
String methodName = configuration.getProperty(Constants.PROPERTY_TOPOLOGY_METHOD);
String version = configuration.getProperty(Constants.PROPERTY_TOPOLOGY_VERSION);
String mode = Constants.SYNC_MODE;
return callGraph(moduleName, methodName, version, mode, props);
}
/**
* Calls a specified directed graph with the specified properties and returns the response
*
* @param module The module name to be used to locate the graph
* @param method The method name to be executed (rpc)
* @param version The version of the graph to be used, or null for the latest
* @param mode the execution mode of the graph, sync or async
* @param props A set of name-value properties to be passed to the graph for context variables.
*/
private boolean callGraph(String module, String method, String version, String mode, Properties props) {
String graphName = String.format(("%s:%s:%s"), module, method, version);
logger.debug(String.format("Calling Graph %s", graphName));
metricsLogger.info(String.format("Calling Graph %s", graphName));
boolean success;
String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
AppcProviderClient svcLogicClient = new AppcProviderClient();
try {
if (svcLogicClient.hasGraph(module, method, version, mode)) {
try {
Properties respProps = svcLogicClient.execute(module, method, version, mode, props);
reason = "Failed"; // Assume it failed unless proven otherwise
logger.debug(EELFResourceManager.format(Msg.DEBUG_GRAPH_RESPONSE_HEADER, appName, graphName,
Integer.toString(respProps.size())));
logKeys(graphName, appName, respProps);
success = resolveSuccess(graphName, appName, respProps);
} catch (Exception e) {
success = false;
reason = EELFResourceManager.format(Msg.EXCEPTION_CALLING_DG, e, appName,
e.getClass().getSimpleName(), graphName, e.getMessage());
logger.error(reason);
}
} else {
success = false;
reason = EELFResourceManager.format(Msg.GRAPH_NOT_FOUND, appName, graphName);
logger.error(reason);
}
} catch (Exception e) {
success = false;
reason = EELFResourceManager.format(Msg.EXCEPTION_CALLING_DG, e, appName, e.getClass().getSimpleName(),
graphName, e.getMessage());
logger.error(reason);
}
return success;
}
private boolean resolveSuccess(String graphName, String appName, Properties respProps) {
// TODO - Find docs and see if there is a better way to handle this
// Bad requests have errors
if (respProps.containsKey(Constants.ATTRIBUTE_ERROR_CODE)) {
String errorCodeProperty = respProps.getProperty(Constants.ATTRIBUTE_ERROR_CODE).trim();
return doResolveSuccess(graphName, appName, respProps, errorCodeProperty);
} else {
/*
* Added code that requires error code to now be defined in ALL cases. If not, it is an error
* and the response will be set to failed regardless if the DG worked or not.
*/
reason = EELFResourceManager.format(Msg.PARAMETER_IS_MISSING, appName, graphName,
Constants.ATTRIBUTE_ERROR_CODE);
logger.error(reason);
return false;
}
}
private boolean doResolveSuccess(String graphName, String appName, Properties respProps, String errorCodeProperty) {
try {
int errorCode = Integer.parseInt(errorCodeProperty);
if (errorCode >= 300) {
reason = EELFResourceManager.format(Msg.DG_FAILED_RESPONSE, appName, graphName,
errorCodeProperty, respProps.getProperty(Constants.ATTRIBUTE_ERROR_MESSAGE));
logger.error(reason);
return false;
} else {
reason = "Success";
return true;
}
} catch (NumberFormatException e) {
reason = EELFResourceManager.format(Msg.PARAMETER_NOT_NUMERIC, appName, graphName,
Constants.ATTRIBUTE_ERROR_CODE, errorCodeProperty);
logger.error(reason);
return false;
}
}
private void logKeys(String graphName, String appName, Properties respProps) {
for (String key : respProps.stringPropertyNames()) {
logger.debug(EELFResourceManager.format(
Msg.DEBUG_GRAPH_RESPONSE_DETAIL, appName, graphName, key, (String) respProps.get(key)));
}
}
}
|