aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/services/AsyncInstantiationBusinessLogicImpl.java
blob: df8e92d66bbcacae8db2857b6bc2d4c843be7e12 (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
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2018 Nokia. 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 com.google.common.collect.ImmutableMap;
import io.joshworks.restclient.http.HttpResponse;
import java.io.IOException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SessionFactory;
import org.onap.vid.aai.AaiClientInterface;
import org.onap.vid.aai.AaiOverTLSClientInterface;
import org.onap.vid.aai.AaiResponse;
import org.onap.vid.aai.exceptions.InvalidAAIResponseException;
import org.onap.vid.aai.model.AaiNodeQueryResponse;
import org.onap.vid.aai.model.ResourceType;
import org.onap.vid.changeManagement.RequestDetailsWrapper;
import org.onap.vid.domain.mso.CloudConfiguration;
import org.onap.vid.domain.mso.SubscriberInfo;
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.model.JobAuditStatus;
import org.onap.vid.model.NameCounter;
import org.onap.vid.model.ServiceInfo;
import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
import org.onap.vid.model.serviceInstantiation.VfModule;
import org.onap.vid.model.serviceInstantiation.Vnf;
import org.onap.vid.mso.MsoBusinessLogicImpl;
import org.onap.vid.mso.MsoProperties;
import org.onap.vid.mso.model.ServiceInstantiationRequestDetails;
import org.onap.vid.mso.rest.AsyncRequestStatus;
import org.onap.vid.utils.DaoUtils;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.portalsdk.core.service.DataAccessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static org.onap.vid.utils.Logging.debugRequestDetails;

@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;
    private static final String NAME_FOR_CHECK_AAI_STATUS = "NAME_FOR_CHECK_AAI_STATUS";

    private final DataAccessService dataAccessService;

    private final JobAdapter jobAdapter;

    private final JobsBrokerService jobService;

    private SessionFactory sessionFactory;

    private AaiOverTLSClientInterface aaiOverTLSClient;

    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)
        .build();


    @Autowired
    public AsyncInstantiationBusinessLogicImpl(DataAccessService dataAccessService,
        JobAdapter jobAdapter,
        JobsBrokerService jobService,
        SessionFactory sessionFactory,
        @Qualifier("aaiClientForFasterXmlMapping")  AaiOverTLSClientInterface aaiOverTLSClient) {
        this.dataAccessService = dataAccessService;
        this.jobAdapter = jobAdapter;
        this.jobService = jobService;
        this.sessionFactory = sessionFactory;
        this.aaiOverTLSClient = aaiOverTLSClient;
    }

    @Override
    public List<ServiceInfo> getAllServicesInfo() {
        return dataAccessService
            .getList(ServiceInfo.class, filterByCreationDateAndNotDeleted(), orderByCreatedDateAndStatus(), null);
    }

    private String filterByCreationDateAndNotDeleted() {
        LocalDateTime minus3Months = LocalDateTime.now().minusMonths(3);
        Timestamp filterDate = Timestamp.valueOf(minus3Months);
        return " where" +
            "   hidden = false" +
            "   and deleted_at is null" +  // don't fetch deleted
            "   and created >= '" + filterDate + "' ";
    }

    private String orderByCreatedDateAndStatus() {
        return " createdBulkDate DESC ,\n" +
            "  (CASE jobStatus\n" +
            "   WHEN 'COMPLETED' THEN 0\n" +
            "   WHEN 'FAILED' THEN 0\n" +
            "   WHEN 'IN_PROGRESS' THEN 1\n" +
            "   WHEN 'PAUSE' THEN 2\n" +
            "   WHEN 'PENDING' THEN 3\n" +
            "   WHEN 'STOPPED' THEN 3 END),\n" +
            "  statusModifiedDate ";
    }

    @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++) {
            Job job = jobAdapter.createJob(JobType.ServiceInstantiation, request, templateId, userId, i);
            UUID jobId = jobService.add(job);
            auditVidStatus(jobId, job.getStatus());
            uuids.add(jobId);
            dataAccessService.saveDomainObject(createServiceInfo(userId, request, jobId, templateId, createdBulkDate),
                DaoUtils.getPropsMap());
        }
        return uuids;
    }

    private ServiceInfo createServiceInfo(String userId, ServiceInstantiation serviceInstantiation, UUID jobId,
        UUID templateId, Date createdBulkDate) {
        return new ServiceInfo(
            userId, 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(),
            null,
            serviceInstantiation.getInstanceName(),
            serviceInstantiation.getModelInfo().getModelInvariantId(),
            serviceInstantiation.getModelInfo().getModelName(),
            serviceInstantiation.getModelInfo().getModelVersion(),
            createdBulkDate
        );
    }


    @Override
    public RequestDetailsWrapper<ServiceInstantiationRequestDetails> generateServiceInstantiationRequest(UUID jobId,
        ServiceInstantiation payload, String userId) {

        ServiceInstantiationRequestDetails.ServiceInstantiationOwningEntity owningEntity = new ServiceInstantiationRequestDetails.ServiceInstantiationOwningEntity(
            payload.getOwningEntityId(), payload.getOwningEntityName());

        SubscriberInfo subscriberInfo = new SubscriberInfo();
        subscriberInfo.setGlobalSubscriberId(payload.getGlobalSubscriberId());

        String serviceInstanceName = null;
        if (payload.isUserProvidedNaming()) {
            serviceInstanceName = getUniqueName(payload.getInstanceName(), ResourceType.SERVICE_INSTANCE);
            String finalServiceInstanceName = serviceInstanceName;
            updateServiceInfo(jobId, x -> x.setServiceInstanceName(finalServiceInstanceName));
        }
        ServiceInstantiationRequestDetails.RequestInfo requestInfo = new ServiceInstantiationRequestDetails.RequestInfo(
            serviceInstanceName,
            payload.getProductFamilyId(),
            "VID",
            payload.isRollbackOnFailure(),
            userId);

        List<ServiceInstantiationRequestDetails.ServiceInstantiationService> serviceInstantiationService = new LinkedList<>();
        List<Map<String, String>> unFilteredInstanceParams =
            payload.getInstanceParams() != null ? payload.getInstanceParams() : new LinkedList<>();
        List<Map<String, String>> filteredInstanceParams = removeUnNeededParams(unFilteredInstanceParams);
        ServiceInstantiationRequestDetails.ServiceInstantiationService serviceInstantiationService1 = new ServiceInstantiationRequestDetails.ServiceInstantiationService(
            payload.getModelInfo(),
            serviceInstanceName,
            filteredInstanceParams,
            createServiceInstantiationVnfList(payload)
        );
        serviceInstantiationService.add(serviceInstantiationService1);

        ServiceInstantiationRequestDetails.RequestParameters requestParameters = new ServiceInstantiationRequestDetails.RequestParameters(
            payload.getSubscriptionServiceType(), false, serviceInstantiationService);

        ServiceInstantiationRequestDetails.Project project =
            payload.getProjectName() != null ? new ServiceInstantiationRequestDetails.Project(payload.getProjectName())
                : null;

        ServiceInstantiationRequestDetails requestDetails = new ServiceInstantiationRequestDetails(
            payload.getModelInfo(), owningEntity, subscriberInfo,
            project, requestInfo, requestParameters);

        RequestDetailsWrapper<ServiceInstantiationRequestDetails> requestDetailsWrapper = new RequestDetailsWrapper(
            requestDetails);
        debugRequestDetails(requestDetailsWrapper, logger);
        return requestDetailsWrapper;
    }

    private List<Map<String, String>> removeUnNeededParams(List<Map<String, String>> instanceParams) {
        List<String> keysToRemove = new ArrayList<>();
        if (instanceParams != null && !instanceParams.isEmpty()) {
            for (String key : instanceParams.get(0).keySet()) {
                for (String paramToIgnore : PARAMS_TO_IGNORE) {
                    if ((key.equalsIgnoreCase(paramToIgnore))) {
                        keysToRemove.add(key);
                    }
                }
            }
            for (String key : keysToRemove) {
                instanceParams.get(0).remove(key);
            }
            //TODO will be removed on once we stop using List<Map<String, String>>
            if (instanceParams.get(0).isEmpty()) {
                return Collections.emptyList();
            }
        }
        return instanceParams;
    }

    private ServiceInstantiationRequestDetails.ServiceInstantiationVnfList createServiceInstantiationVnfList(
        ServiceInstantiation payload) {
        CloudConfiguration cloudConfiguration = new CloudConfiguration();
        cloudConfiguration.setTenantId(payload.getTenantId());
        cloudConfiguration.setLcpCloudRegionId(payload.getLcpCloudRegionId());

        Map<String, Vnf> vnfs = payload.getVnfs();
        List<ServiceInstantiationRequestDetails.ServiceInstantiationVnf> vnfList = new ArrayList<>();
        for (Vnf vnf : vnfs.values()) {
            Map<String, Map<String, VfModule>> vfModules = vnf.getVfModules();
            List<VfModule> convertedUnFilteredVfModules = convertVfModuleMapToList(vfModules);
            List<VfModule> filteredVfModules = filterInstanceParamsFromVfModuleAndUniqueNames(
                convertedUnFilteredVfModules, vnf.isUserProvidedNaming());
            ServiceInstantiationRequestDetails.ServiceInstantiationVnf serviceInstantiationVnf = new ServiceInstantiationRequestDetails.ServiceInstantiationVnf(
                vnf.getModelInfo(),
                cloudConfiguration,
                vnf.getPlatformName(),
                vnf.getLineOfBusiness(),
                payload.getProductFamilyId(),
                removeUnNeededParams(vnf.getInstanceParams()),
                filteredVfModules,
                vnf.isUserProvidedNaming() ? getUniqueName(vnf.getInstanceName(), ResourceType.GENERIC_VNF) : null
            );
            vnfList.add(serviceInstantiationVnf);
        }

        return new ServiceInstantiationRequestDetails.ServiceInstantiationVnfList(vnfList);
    }

    private List<VfModule> convertVfModuleMapToList(Map<String, Map<String, VfModule>> vfModules) {
        return vfModules.values().stream().flatMap(vfModule -> vfModule.values().stream()).collect(Collectors.toList());
    }

    private List<VfModule> filterInstanceParamsFromVfModuleAndUniqueNames(List<VfModule> unFilteredVfModules,
        boolean isUserProvidedNaming) {
        return unFilteredVfModules.stream().map(vfModule ->
            new VfModule(
                vfModule.getModelInfo(),
                getUniqueNameIfNeeded(isUserProvidedNaming, vfModule.getInstanceName(), ResourceType.VF_MODULE),
                getUniqueNameIfNeeded(isUserProvidedNaming, vfModule.getVolumeGroupInstanceName(),
                    ResourceType.VOLUME_GROUP),
                removeUnNeededParams(vfModule.getInstanceParams())))
            .collect(Collectors.toList());
    }

    private String getUniqueNameIfNeeded(boolean isUserProvidedNaming, String name, ResourceType resourceType) {
        return isUserProvidedNaming && !StringUtils.isEmpty(name) ?
            getUniqueName(name, resourceType) : null;
    }

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

    @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 = getServiceInfoByJobId(jobUUID);
        serviceUpdater.accept(serviceInfo);
        dataAccessService.saveDomainObject(serviceInfo, DaoUtils.getPropsMap());
        return serviceInfo;
    }

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

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

    public ServiceInfo getServiceInfoByJobId(UUID jobUUID) {
        List<ServiceInfo> serviceInfoList = dataAccessService
            .getList(ServiceInfo.class, String.format(" where jobId = '%s' ", jobUUID), null, null);
        if (serviceInfoList.size() != 1) {
            throw new GenericUncheckedException(
                "Failed to retrieve job with uuid " + jobUUID + " from ServiceInfo table. Instances found: "
                    + serviceInfoList.size());
        }
        return serviceInfoList.get(0);
    }

    public List<JobAuditStatus> getAuditStatuses(UUID jobUUID, JobAuditStatus.SourceStatus source) {
        return dataAccessService.getList(
            JobAuditStatus.class,
            String.format(" where SOURCE = '%s' and JOB_ID = '%s'", source, jobUUID),
            " CREATED_DATE ", null);
    }

    private JobAuditStatus getLatestAuditStatus(UUID jobUUID, JobAuditStatus.SourceStatus source) {
        List<JobAuditStatus> list = getAuditStatuses(jobUUID, source);
        return !list.isEmpty() ? list.get(list.size() - 1) : null;
    }

    @Override
    public void auditVidStatus(UUID jobUUID, JobStatus jobStatus) {
        JobAuditStatus vidStatus = new JobAuditStatus(jobUUID, jobStatus.toString(), JobAuditStatus.SourceStatus.VID);
        auditStatus(vidStatus);
    }

    @Override
    public void auditMsoStatus(UUID jobUUID, AsyncRequestStatus.Request msoRequestStatus) {
        auditMsoStatus(jobUUID, msoRequestStatus.requestStatus.getRequestState(), msoRequestStatus.requestId,
            msoRequestStatus.requestStatus.getStatusMessage());
    }

    @Override
    public void auditMsoStatus(UUID jobUUID, String jobStatus, String requestId, String additionalInfo) {
        JobAuditStatus msoStatus = new JobAuditStatus(jobUUID, jobStatus, JobAuditStatus.SourceStatus.MSO,
            requestId != null ? UUID.fromString(requestId) : null,
            additionalInfo);
        auditStatus(msoStatus);
    }

    private void auditStatus(JobAuditStatus jobAuditStatus) {
        JobAuditStatus latestStatus = getLatestAuditStatus(jobAuditStatus.getJobId(), jobAuditStatus.getSource());
        if (latestStatus == null || !latestStatus.equals(jobAuditStatus)) {
            dataAccessService.saveDomainObject(jobAuditStatus, DaoUtils.getPropsMap());
        }

    }

    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 = updateServiceInfoAndAuditStatus(jobUUID, JobStatus.FAILED);
        List<ServiceInfo> serviceInfoList = dataAccessService.getList(
            ServiceInfo.class,
            String.format(" where templateId = '%s' and jobStatus = '%s'",
                serviceInfo.getTemplateId(),
                JobStatus.PENDING),
            null, null);
        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 = 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);
        dataAccessService.saveDomainObject(serviceInfo, DaoUtils.getPropsMap());
    }

    @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 1;
                }
            }
            //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++) {
            int counter = getCounterForName(name);
            String newName = formatNameAndCounter(name, counter);
            if (isNameFreeInAai(newName, resourceType)) {
                return newName;
            }
        }

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

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

    private boolean isNameFreeInAai(String name, ResourceType resourceType) throws InvalidAAIResponseException {
        HttpResponse<AaiNodeQueryResponse> aaiResponse = aaiOverTLSClient
            .searchNodeTypeByName(name, resourceType);
        if (aaiResponse.getStatus() > 399 || aaiResponse.getBody() == null) {
            try {
                String message = IOUtils.toString(aaiResponse.getRawBody(), "UTF-8");
                throw new InvalidAAIResponseException(aaiResponse.getStatus(), message);
            } catch (IOException e) {
                throw new InvalidAAIResponseException(aaiResponse.getStatus(), aaiResponse.getStatusText());
            }
        }
        return CollectionUtils.isEmpty(aaiResponse.getBody().resultData);
    }

}