aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/services/AsyncInstantiationBusinessLogicImpl.java
blob: bf0344c6bd7b1ad16ba110af2d60b23ec4fbcde7 (plain)
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
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.vid.services;

import static com.google.common.collect.Streams.concat;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Stream.empty;
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.onap.vid.controller.MsoController.SVC_INSTANCE_ID;
import static org.onap.vid.controller.MsoController.VNF_INSTANCE_ID;
import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;

import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.hibernate.SessionFactory;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.vid.aai.AaiClientInterface;
import org.onap.vid.aai.model.ResourceType;
import org.onap.vid.dal.AsyncInstantiationRepository;
import org.onap.vid.exceptions.DbFailureUncheckedException;
import org.onap.vid.exceptions.GenericUncheckedException;
import org.onap.vid.exceptions.MaxRetriesException;
import org.onap.vid.exceptions.OperationNotAllowedException;
import org.onap.vid.job.Job;
import org.onap.vid.job.Job.JobStatus;
import org.onap.vid.job.JobAdapter;
import org.onap.vid.job.JobType;
import org.onap.vid.job.JobsBrokerService;
import org.onap.vid.job.impl.JobSharedData;
import org.onap.vid.model.Action;
import org.onap.vid.model.NameCounter;
import org.onap.vid.model.ResourceInfo;
import org.onap.vid.model.ServiceInfo;
import org.onap.vid.model.ServiceInfo.ServiceAction;
import org.onap.vid.model.serviceInstantiation.BaseResource;
import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
import org.onap.vid.model.serviceInstantiation.VfModule;
import org.onap.vid.mso.MsoBusinessLogicImpl;
import org.onap.vid.mso.MsoProperties;
import org.onap.vid.mso.MsoUtil;
import org.onap.vid.mso.RestObject;
import org.onap.vid.mso.model.ModelInfo;
import org.onap.vid.mso.rest.AsyncRequestStatus;
import org.onap.vid.mso.rest.RequestStatus;
import org.onap.vid.properties.Features;
import org.onap.vid.utils.DaoUtils;
import org.onap.vid.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.togglz.core.manager.FeatureManager;

@Service
public class AsyncInstantiationBusinessLogicImpl implements
        AsyncInstantiationBusinessLogic {

    private static final int MAX_RETRIES_GETTING_COUNTER = 100;
    private static final int MAX_RETRIES_GETTING_FREE_NAME_FROM_AAI = 10000;
    public static final String NAME_FOR_CHECK_AAI_STATUS = "NAME_FOR_CHECK_AAI_STATUS";

    private final JobAdapter jobAdapter;

    private final JobsBrokerService jobService;

    private final CloudOwnerService cloudOwnerService;

    private final AsyncInstantiationRepository asyncInstantiationRepository;

    private SessionFactory sessionFactory;

    private AaiClientInterface aaiClient;

    private FeatureManager featureManager;

    private AuditService auditService;


    private int maxRetriesGettingFreeNameFromAai = MAX_RETRIES_GETTING_FREE_NAME_FROM_AAI;

    private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AsyncInstantiationBusinessLogicImpl.class);
    private Map<String, JobStatus> msoStateToJobStatusMap = ImmutableMap.<String, JobStatus>builder()
            .put("inprogress", JobStatus.IN_PROGRESS)
            .put("failed", JobStatus.FAILED)
            .put("pause", JobStatus.PAUSE)
            .put("paused", JobStatus.PAUSE)
            .put("complete", JobStatus.COMPLETED)
            .put("pending", JobStatus.IN_PROGRESS)
            .put("pendingmanualtask", JobStatus.PAUSE)
            .put("unlocked", JobStatus.IN_PROGRESS)
            .put("aborted", JobStatus.COMPLETED_WITH_ERRORS)
            .put("rolledback", JobStatus.FAILED)
            .put("rolledbacktoassigned", JobStatus.FAILED)
            .put("rolledbacktocreated", JobStatus.FAILED)
            .build();


    @Autowired
    public AsyncInstantiationBusinessLogicImpl(JobAdapter jobAdapter,
                                               JobsBrokerService jobService,
                                               SessionFactory sessionFactory,
                                               AaiClientInterface aaiClient,
                                               FeatureManager featureManager,
                                               CloudOwnerService cloudOwnerService, AsyncInstantiationRepository asyncInstantiationRepository,
                                               AuditService auditService) {
        this.jobAdapter = jobAdapter;
        this.jobService = jobService;
        this.sessionFactory = sessionFactory;
        this.aaiClient = aaiClient;
        this.featureManager = featureManager;
        this.cloudOwnerService = cloudOwnerService;
        this.asyncInstantiationRepository = asyncInstantiationRepository;
        this.auditService = auditService;
    }

    @Override
    public List<ServiceInfo> getAllServicesInfo() {
        return asyncInstantiationRepository.getAllServicesInfo();
    }

    JobType getJobType(ServiceInstantiation request) {
        if (request.isALaCarte()) {
            switch (defaultIfNull(request.getAction(), Action.Create)) {
                case Delete:
                    return JobType.ALaCarteService;
                case None:
                    return JobType.ALaCarteService;
                default:
                    return JobType.ALaCarteServiceInstantiation;
            }
        } else {
            return JobType.MacroServiceInstantiation;
        }
    }

    @Override
    public List<UUID> pushBulkJob(ServiceInstantiation request, String userId) {

        List<UUID> uuids = new ArrayList<>();
        Date createdBulkDate = Calendar.getInstance().getTime();
        int bulkSize = request.getBulkSize();
        UUID templateId = UUID.randomUUID();
        for (int i = 0; i < bulkSize; i++) {
            ServiceInstantiation clonedServiceInstantiation = cloneServiceInstantiation(request);
            ServiceInstantiation requestPerJob = prepareServiceToBeUnique(clonedServiceInstantiation);
            requestPerJob = clearStatusFromRequest(requestPerJob);
            ServiceInfo.ServiceAction serviceAction = getAction(requestPerJob);
            JobType jobType = getJobType(requestPerJob);
            final String optimisticUniqueServiceInstanceName = bulkSize>1 ? //only bulk with more than 1 service need to get multiple names
                    getOptimisticUniqueServiceInstanceName(requestPerJob.getInstanceName()) : requestPerJob.getInstanceName();
            Job job = jobAdapter.createServiceInstantiationJob(jobType, requestPerJob, templateId, userId, request.getTestApi(), optimisticUniqueServiceInstanceName, i);
            UUID jobId = job.getUuid();

            asyncInstantiationRepository.saveServiceInfo(createServiceInfo(
                userId, requestPerJob, jobId, templateId, createdBulkDate,
                optimisticUniqueServiceInstanceName, serviceAction,
                requestSummaryOrNull(requestPerJob)));
            asyncInstantiationRepository.addJobRequest(jobId, requestPerJob);
            auditService.auditVidStatus(jobId, job.getStatus());
            uuids.add(jobId);

            jobService.add(job);
        }
        return uuids;
    }

    Map<String, Long> requestSummaryOrNull(ServiceInstantiation request) {
        if (!featureManager.isActive(Features.FLAG_2004_CREATE_ANOTHER_INSTANCE_FROM_TEMPLATE)) {
            return null;
        }

        if (getAction(request) != ServiceAction.INSTANTIATE) {
            return null;
        }

        return summarizedChildrenMap(request);
    }

    public Map<String, Long> summarizedChildrenMap(ServiceInstantiation serviceInstantiation){
        Stream<String> existingTypesStream =
            allDeepChildResources(serviceInstantiation)
                .map(this::getModelTypes)
                .flatMap(identity());

        Map<String, Long> existingTypesCounters =
            existingTypesStream.collect(groupingBy(identity(), counting()));

        return existingTypesCounters;
    }

    private Stream<String> getModelTypes(BaseResource resource) {
        return concat(
            Stream.of(resource)
                .map(BaseResource::getModelInfo)
                .filter(Objects::nonNull)
                .map(ModelInfo::getModelType),
            streamVolumeGroups(resource)
        );
    }

    private Stream<String> streamVolumeGroups(BaseResource resource) {
        return hasVolumeGroup(resource)
            ? Stream.of("volumeGroup")
            : empty();
    }

    private boolean hasVolumeGroup(BaseResource resource) {
        return
            resource instanceof VfModule
            && isNotEmpty(((VfModule) resource).getVolumeGroupInstanceName());
    }

    private Stream<BaseResource> allDeepChildResources(BaseResource resource) {
        return resource
            .getChildren()
            .stream()
            .map(it -> concat(Stream.of(it), allDeepChildResources(it)))
            .flatMap(identity());
    }

    private ServiceInfo.ServiceAction getAction(ServiceInstantiation request) {
        if (request.getAction() == null) {
            //throw new GenericUncheckedException("Required 'action' field not provided at service level");
            return Action.Create.getServiceAction();
        }
        return request.getAction().getServiceAction();
    }


    private String getOptimisticUniqueServiceInstanceName(String instanceName) {
        return isNotEmpty(instanceName) ? getUniqueNameFromDbOnly(instanceName) : instanceName;
    }

    protected ServiceInfo createServiceInfo(String userId, ServiceInstantiation serviceInstantiation, UUID jobId,
        UUID templateId, Date createdBulkDate, String optimisticUniqueServiceInstanceName,
        ServiceInfo.ServiceAction serviceAction, Map<String, Long> requestSummary) {
        return new ServiceInfo(
                userId,
                serviceInstantiation.isALaCarte(),
                Job.JobStatus.PENDING, serviceInstantiation.isPause(), jobId, templateId,
                serviceInstantiation.getOwningEntityId(),
                serviceInstantiation.getOwningEntityName(),
                serviceInstantiation.getProjectName(),
                serviceInstantiation.getAicZoneId(),
                serviceInstantiation.getAicZoneName(),
                serviceInstantiation.getTenantId(),
                serviceInstantiation.getTenantName(),
                serviceInstantiation.getLcpCloudRegionId(),
                null,
                serviceInstantiation.getSubscriptionServiceType(),
                serviceInstantiation.getSubscriberName(),
                serviceInstantiation.getGlobalSubscriberId(),
                serviceInstantiation.getInstanceId(),
                optimisticUniqueServiceInstanceName,
                serviceInstantiation.getModelInfo().getModelVersionId(),
                serviceInstantiation.getModelInfo().getModelName(),
                serviceInstantiation.getModelInfo().getModelVersion(),
                createdBulkDate,
                serviceAction,
                false,
                requestSummary);
    }

    @Override
    public boolean isPartOfBulk(UUID jobId) {
        if (jobId == null) {
            return false;
    }
        ServiceInfo serviceInfo = asyncInstantiationRepository.getServiceInfoByJobId(jobId);
        UUID templateId = serviceInfo.getTemplateId();
        if (templateId != null) {
            return getNumberOfJobsInBulk(templateId) > 1;
    }
        return false;

    }

    private int getNumberOfJobsInBulk(UUID templateId) {
        String hqlSelectJob = "from JobDaoImpl where templateId = :templateId";
        return DaoUtils.tryWithSessionAndTransaction(sessionFactory, session ->
            session.createQuery(hqlSelectJob)
                    .setText("templateId", templateId.toString())
                    .list()
                    .size()
        );
    }

    @Override
    public String getServiceInstantiationPath(ServiceInstantiation serviceInstantiationRequest) {
        //in case pause flag is true - use assign , else - use create.
        return MsoBusinessLogicImpl.validateEndpointPath(
                serviceInstantiationRequest.isPause() ?
                        MsoProperties.MSO_REST_API_SERVICE_INSTANCE_ASSIGN : MsoProperties.MSO_RESTAPI_SERVICE_INSTANCE
        );
    }

    @Override
    public String getServiceDeletionPath(String serviceInstanceId) {
        return MsoBusinessLogicImpl.validateEndpointPath( MsoProperties.MSO_RESTAPI_SERVICE_INSTANCE)  + "/" + serviceInstanceId;
    }

    @Override
    public String getVnfInstantiationPath(String serviceInstanceId) {
        return MsoBusinessLogicImpl.validateEndpointPath(MsoProperties.MSO_REST_API_VNF_INSTANCE).
                replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
    }

    @Override
    public String getVnfDeletionPath(String serviceInstanceId, String vnfInstanceId) {
        return (MsoBusinessLogicImpl.validateEndpointPath(MsoProperties.MSO_REST_API_VNF_INSTANCE)
                + '/' + vnfInstanceId)
                .replaceFirst(SVC_INSTANCE_ID, serviceInstanceId).replaceFirst(VNF_INSTANCE_ID, vnfInstanceId);
    }

    @Override
    public String getNetworkInstantiationPath(String serviceInstanceId) {
        return MsoBusinessLogicImpl.validateEndpointPath(MsoProperties.MSO_REST_API_NETWORK_INSTANCE).
                replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
    }

    @Override
    public String getVfmoduleInstantiationPath(String serviceInstanceId, String vnfInstanceId) {
        return MsoBusinessLogicImpl.validateEndpointPath(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE)
                .replaceFirst(SVC_INSTANCE_ID, serviceInstanceId)
                .replaceFirst(VNF_INSTANCE_ID, vnfInstanceId);
    }

    @Override
    public String getVfModuleReplacePath(String serviceInstanceId, String vnfInstanceId, String vfModuleInstanceId)
    {
        return MsoBusinessLogicImpl.validateEndpointPath(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE)
                .replaceFirst(SVC_INSTANCE_ID, serviceInstanceId)
                .replaceFirst(VNF_INSTANCE_ID, vnfInstanceId)
                + "/" + vfModuleInstanceId
                + "/replace";
    }

    @Override
    public String getVfModuleDeletePath(String serviceInstanceId, String vnfInstanceId, String vfModuleInstanceId) {
        return MsoBusinessLogicImpl.validateEndpointPath(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE)
                .replaceFirst(SVC_INSTANCE_ID, serviceInstanceId)
                .replaceFirst(VNF_INSTANCE_ID, vnfInstanceId)
                + "/" + vfModuleInstanceId;
    }

    @Override
    public String getVolumeGroupInstantiationPath(String serviceInstanceId, String vnfInstanceId) {
        return MsoBusinessLogicImpl.validateEndpointPath(MsoProperties.MSO_REST_API_VOLUME_GROUP_INSTANCE)
                .replaceFirst(SVC_INSTANCE_ID, serviceInstanceId)
                .replaceFirst(VNF_INSTANCE_ID, vnfInstanceId);
    }

    @Override
    public String getInstanceGroupInstantiationPath() {
        return MsoBusinessLogicImpl.validateEndpointPath(MsoProperties.MSO_REST_API_INSTANCE_GROUP);
    }

    @Override
    public String getInstanceGroupMemberInstantiationPath(String vnfGroupInstanceId) {
        return MsoBusinessLogicImpl.validateEndpointPath(MsoProperties.MSO_REST_API_INSTANCE_GROUP)
                + '/' + vnfGroupInstanceId + "/addMembers";
    }

    @Override
    public String getInstanceGroupDeletePath(String instanceGroupId) {
        return MsoBusinessLogicImpl.validateEndpointPath(MsoProperties.MSO_REST_API_INSTANCE_GROUP)
                + '/' + instanceGroupId;
    }

    @Override
    public String getInstanceGroupMemberDeletePath(String vnfGroupInstanceId){
        return MsoBusinessLogicImpl.validateEndpointPath(MsoProperties.MSO_REST_API_INSTANCE_GROUP)
                + '/' + vnfGroupInstanceId + "/removeMembers";
    }

    @Override
    public String getNetworkDeletePath(String serviceInstanceId, String networkInstanceId) {
        return (MsoBusinessLogicImpl.validateEndpointPath(MsoProperties.MSO_REST_API_NETWORK_INSTANCE)
                + "/" + networkInstanceId)
                .replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
    }

    @Override
    public String getResumeRequestPath(String requestId) {
        return MsoBusinessLogicImpl.validateEndpointPath("mso.restapi.resume.orc.req")
                .replaceFirst("<request_id>", requestId);
    }

    @Override
    public String getOrchestrationRequestsPath() {
        return MsoBusinessLogicImpl.validateEndpointPath(MsoProperties.MSO_REST_API_GET_ORC_REQ);
    }

    @Override
    public ServiceInfo updateServiceInfo(UUID jobUUID, Consumer<ServiceInfo> serviceUpdater) {
        ServiceInfo serviceInfo = asyncInstantiationRepository.getServiceInfoByJobId(jobUUID);
        serviceUpdater.accept(serviceInfo);
        asyncInstantiationRepository.saveServiceInfo(serviceInfo);
        return serviceInfo;
    }

    @Override
    public ServiceInfo updateServiceInfoAndAuditStatus(UUID jobUuid, JobStatus jobStatus) {
        auditService.auditVidStatus(jobUuid, jobStatus);
        return updateServiceInfo(jobUuid, x -> setServiceInfoStatus(x, jobStatus));
    }

    private boolean isRetryEnabledForStatus(JobStatus jobStatus) {
        return jobStatus==JobStatus.COMPLETED_AND_PAUSED || (featureManager.isActive(Features.FLAG_1902_RETRY_JOB) &&
                (jobStatus==JobStatus.COMPLETED_WITH_ERRORS || jobStatus==JobStatus.FAILED || jobStatus==JobStatus.FAILED_AND_PAUSED));
    }

    private void setServiceInfoStatus(ServiceInfo serviceInfo, JobStatus jobStatus) {
        serviceInfo.setJobStatus(jobStatus);
        serviceInfo.setStatusModifiedDate(new Date());
        serviceInfo.setRetryEnabled(isRetryEnabledForStatus(jobStatus));
    }

    public Job.JobStatus calcStatus(AsyncRequestStatus asyncRequestStatus) {
        String msoRequestState = asyncRequestStatus.request.requestStatus.getRequestState().toLowerCase().replaceAll("[^a-z]+", "");
        JobStatus jobStatus = msoStateToJobStatusMap.get(msoRequestState);
        return (jobStatus != null ? jobStatus : JobStatus.IN_PROGRESS);
    }

    @Override
    public void handleFailedInstantiation(UUID jobUUID) {
        ServiceInfo serviceInfo = asyncInstantiationRepository.getServiceInfoByJobId(jobUUID);
        List<ServiceInfo> serviceInfoList = asyncInstantiationRepository.getServiceInfoByTemplateIdAndJobStatus(serviceInfo.getTemplateId(), JobStatus.PENDING);
        serviceInfoList.forEach(si -> updateServiceInfoAndAuditStatus(si.getJobId(), JobStatus.STOPPED));
    }

    @Override
    public void deleteJob(UUID jobId) {
        jobService.delete(jobId);
        Date now = new Date();
        updateServiceInfo(jobId, x -> x.setDeletedAt(now));
    }

    @Override
    public void hideServiceInfo(UUID jobUUID) {
        ServiceInfo serviceInfo = asyncInstantiationRepository.getServiceInfoByJobId(jobUUID);
        if (!serviceInfo.getJobStatus().isFinal()) {
            String message = String.format("jobId %s: Service status does not allow hide service, status = %s",
                    serviceInfo.getJobId(),
                    serviceInfo.getJobStatus());
            logger.error(EELFLoggerDelegate.errorLogger, message);
            throw new OperationNotAllowedException(message);
        }
        serviceInfo.setHidden(true);
        asyncInstantiationRepository.saveServiceInfo(serviceInfo);
    }

    @Override
    public int getCounterForName(String name) {

        String hqlSelectNC = "from NameCounter where name = :name";
        String hqlUpdateCounter = "update NameCounter set counter = :newCounter " +
                "where name= :name " +
                "and counter= :prevCounter";

        Integer counter = null;
        GenericUncheckedException lastException = null;
        for (int i = 0; i< MAX_RETRIES_GETTING_COUNTER && counter==null; i++) {
            try {
                counter = calcCounter(name, hqlSelectNC, hqlUpdateCounter);
            }
            catch (GenericUncheckedException exception) {
                lastException = exception; //do nothing, we will try again in the loop
            }
        }

        if (counter!=null) {
            return counter;
        }

        throw lastException!=null ? new DbFailureUncheckedException(lastException) :
                new DbFailureUncheckedException("Failed to get counter for "+name+" due to unknown error");

    }

    private Integer calcCounter(String name, String hqlSelectNC, String hqlUpdateCounter) {
        Integer counter;
        counter = DaoUtils.tryWithSessionAndTransaction(sessionFactory, session -> {
            NameCounter nameCounter = (NameCounter) session.createQuery(hqlSelectNC)
                    .setText("name", name)
                    .uniqueResult();
            if (nameCounter != null) {
                int updatedRows = session.createQuery(hqlUpdateCounter)
                        .setText("name", nameCounter.getName())
                        .setInteger("prevCounter", nameCounter.getCounter())
                        .setInteger("newCounter", nameCounter.getCounter() + 1)
                        .executeUpdate();
                if (updatedRows == 1) {
                    return nameCounter.getCounter() + 1;
                }
            } else {
                Object nameAsId = session.save(new NameCounter(name));
                //if save success
                if (nameAsId != null) {
                    return 0;
                }
            }
            //in case of failure return null, in order to continue the loop
            return null;
        });
        return counter;
    }

    @Override
    public int getMaxRetriesGettingFreeNameFromAai() {
        return maxRetriesGettingFreeNameFromAai;
    }

    @Override
    public void setMaxRetriesGettingFreeNameFromAai(int maxRetriesGettingFreeNameFromAai) {
        this.maxRetriesGettingFreeNameFromAai = maxRetriesGettingFreeNameFromAai;
    }

    @Override
    public String getUniqueName(String name, ResourceType resourceType) {
        //check that name aai response well before increasing counter from DB
        //Prevents unnecessary increasing of the counter while AAI doesn't response
        isNameFreeInAai(NAME_FOR_CHECK_AAI_STATUS, resourceType);

        for (int i=0; i<getMaxRetriesGettingFreeNameFromAai(); i++) {
            String newName = getUniqueNameFromDbOnly(name);
            if (isNameFreeInAai(newName, resourceType)) {
                return newName;
            }
        }

        throw new MaxRetriesException("can't find unused name for "+name, getMaxRetriesGettingFreeNameFromAai());
    }

    @Override
    public ServiceInstantiation prepareServiceToBeUnique(ServiceInstantiation serviceInstantiation) {
        serviceInstantiation.setBulkSize(1);
        return replaceAllTrackById(serviceInstantiation);
    }
    private<T extends BaseResource> T replaceAllTrackById(T resource) {
        resource.setTrackById(UUID.randomUUID().toString());
        resource.getChildren().forEach(this::replaceAllTrackById);
        return resource;
    }

    private ServiceInstantiation cloneServiceInstantiation(ServiceInstantiation serviceInstantiation) {
        try {
            return JACKSON_OBJECT_MAPPER.readValue(
                JACKSON_OBJECT_MAPPER.writeValueAsBytes(serviceInstantiation), ServiceInstantiation.class);
        } catch (IOException e) {
            throw new GenericUncheckedException(e);
        }
    }

    <T extends BaseResource> T clearStatusFromRequest(T resource) {
        resource.setIsFailed(false);
        resource.setStatusMessage(null);
        resource.getChildren().forEach(this::clearStatusFromRequest);
        return resource;
    }

    @Override
    public List<UUID> retryJob(ServiceInstantiation request, UUID jobId, String userId ) {
        updateServiceInfo(jobId, si->si.setRetryEnabled(false));
        return pushBulkJob(request, userId);
    }

    @Override
    public List<UUID> retryJob(UUID jobId, String userId) {
        ServiceInstantiation serviceInstantiationRequest = asyncInstantiationRepository.getJobRequest(jobId);
        enrichBulkForRetry(serviceInstantiationRequest, jobId);

        try {
            logger.debug(EELFLoggerDelegate.debugLogger, "retry ServiceInstantiation request: "+
                    JACKSON_OBJECT_MAPPER.writeValueAsString(serviceInstantiationRequest));
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "failed to log retry of ServiceInstantiation request ", e);
        }
        return retryJob(serviceInstantiationRequest, jobId, userId);
    }

    @Override
    public ServiceInstantiation getBulkForRetry(UUID jobId) {
         return enrichBulkForRetry( asyncInstantiationRepository.getJobRequest(jobId), jobId);
    }

    @Override
    public void addResourceInfo(JobSharedData sharedData, Job.JobStatus jobStatus, String instanceId) {
        String trackById = ((BaseResource) sharedData.getRequest()).getTrackById();
        ResourceInfo resourceInfo = new ResourceInfo(trackById, sharedData.getRootJobId(), instanceId, jobStatus, null);
        asyncInstantiationRepository.saveResourceInfo(resourceInfo);
    }

    @Override
    public void addFailedResourceInfo(JobSharedData sharedData, RestObject msoResponse) {
        String trackById = ((BaseResource) sharedData.getRequest()).getTrackById();
        String errorMessage = MsoUtil.formatExceptionAdditionalInfo(msoResponse.getStatusCode(), msoResponse.getRaw());
        AsyncRequestStatus asyncRequestStatus = convertMessageToAsyncRequestStatus(errorMessage);
        ResourceInfo resourceInfo = new ResourceInfo(trackById, sharedData.getRootJobId(), null, JobStatus.FAILED, asyncRequestStatus);

        asyncInstantiationRepository.saveResourceInfo(resourceInfo);
    }

    @Override
    public void updateResourceInfo(JobSharedData sharedData, JobStatus jobStatus, AsyncRequestStatus message) {
        ResourceInfo resourceInfo = asyncInstantiationRepository.getResourceInfoByTrackId(((BaseResource) sharedData.getRequest()).getTrackById());
        resourceInfo.setJobStatus(jobStatus);
        if (jobStatus.isFailure()) {
            resourceInfo.setErrorMessage(message);
        }
        asyncInstantiationRepository.saveResourceInfo(resourceInfo);
    }

    public AsyncRequestStatus convertMessageToAsyncRequestStatus(String message) {
        RequestStatus requestStatus = new RequestStatus("FAILED", message, TimeUtils.zonedDateTimeToString(ZonedDateTime.now()));
        AsyncRequestStatus.Request request = new AsyncRequestStatus.Request(requestStatus);
        return new AsyncRequestStatus(request);
    }

    protected String getUniqueNameFromDbOnly(String name) {
        int counter = getCounterForName(name);
        return formatNameAndCounter(name, counter);
    }

    //the method is protected so we can call it in the UT
    protected String formatNameAndCounter(String name, int counter) {
        return counter==0 ? name : name + "_" + String.format("%03d", counter);
    }

    private boolean isNameFreeInAai(String name, ResourceType resourceType){
        return !aaiClient.isNodeTypeExistsByName(name, resourceType);
    }

    @Override
    public ServiceInstantiation enrichBulkForRetry(ServiceInstantiation serviceInstantiation, UUID jobId){
        Map<String, ResourceInfo> resourceInfoByTrackId = asyncInstantiationRepository.getResourceInfoByRootJobId(jobId);

        return setResourceStatus(resourceInfoByTrackId, serviceInstantiation);
    }

    protected String readStatusMsg(ResourceInfo resourceInfo){
        if(resourceInfo!=null && resourceInfo.getErrorMessage()!=null && resourceInfo.getErrorMessage().request != null &&resourceInfo.getErrorMessage().request.requestStatus != null ) {
            return resourceInfo.getErrorMessage().request.requestStatus.getStatusMessage();
        }
        return null;
    }

    private<T extends BaseResource> T setResourceStatus(Map<String, ResourceInfo> resourceInfoByTrackId, T resource) {
        ResourceInfo resourceInfo = resourceInfoByTrackId.get(resource.getTrackById());
        if(resourceInfo != null) {
            boolean failed = resourceInfo.getJobStatus().isFailure();
            resource.setIsFailed(failed);
            resource.setStatusMessage(readStatusMsg(resourceInfo));
            if (!failed) {
                // if(resource.getAction().equals(Action.Delete)){
                // TODO not yet implemented- completed after delete should remove the node
                resource.setAction(Action.None);
                resource.setInstanceId(resourceInfo.getInstanceId());
            }
        }
        resource.getChildren().forEach(child -> setResourceStatus(resourceInfoByTrackId, child));
        return resource;
    }

}