88db4c13d28e2867ad345909eeadcf545ec8ce00
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / sdnc-simulator / src / main / java / org / onap / so / sdncsimulator / providers / ServiceOperationsCacheServiceProviderimpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.sdncsimulator.providers;
21
22 import static org.onap.sdnc.northbound.client.model.GenericResourceApiOrderStatusEnumeration.CREATED;
23 import static org.onap.sdnc.northbound.client.model.GenericResourceApiOrderStatusEnumeration.PENDINGCREATE;
24 import static org.onap.sdnc.northbound.client.model.GenericResourceApiRequestStatusEnumeration.SYNCCOMPLETE;
25 import static org.onap.so.sdncsimulator.utils.Constants.RESTCONF_CONFIG_END_POINT;
26 import static org.onap.so.sdncsimulator.utils.Constants.SERVICE_DATA_VNFS_VNF;
27 import static org.onap.so.sdncsimulator.utils.Constants.SERVICE_TOPOLOGY_OPERATION;
28 import static org.onap.so.sdncsimulator.utils.Constants.SERVICE_TOPOLOGY_OPERATION_CACHE;
29 import static org.onap.so.sdncsimulator.utils.Constants.VNF_DATA_VNF_TOPOLOGY;
30 import static org.onap.so.sdncsimulator.utils.Constants.YES;
31 import static org.onap.so.sdncsimulator.utils.ObjectUtils.getString;
32 import static org.onap.so.sdncsimulator.utils.ObjectUtils.getStringOrNull;
33 import static org.onap.so.sdncsimulator.utils.ObjectUtils.isValid;
34 import java.time.LocalDateTime;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Optional;
38 import javax.validation.Valid;
39 import org.onap.sdnc.northbound.client.model.GenericResourceApiInstanceReference;
40 import org.onap.sdnc.northbound.client.model.GenericResourceApiLastActionEnumeration;
41 import org.onap.sdnc.northbound.client.model.GenericResourceApiLastRpcActionEnumeration;
42 import org.onap.sdnc.northbound.client.model.GenericResourceApiOnapmodelinformationOnapModelInformation;
43 import org.onap.sdnc.northbound.client.model.GenericResourceApiOperStatusData;
44 import org.onap.sdnc.northbound.client.model.GenericResourceApiOrderStatusEnumeration;
45 import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestinformationRequestInformation;
46 import org.onap.sdnc.northbound.client.model.GenericResourceApiRpcActionEnumeration;
47 import org.onap.sdnc.northbound.client.model.GenericResourceApiSdncrequestheaderSdncRequestHeader;
48 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceOperationInformation;
49 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicedataServiceData;
50 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicedataServicedataVnfs;
51 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicedataServicedataVnfsVnf;
52 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicedataServicedataVnfsVnfVnfData;
53 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceinformationServiceInformation;
54 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicemodelinfrastructureService;
55 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicestatusServiceStatus;
56 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicetopologyServiceTopology;
57 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier;
58 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfOperationInformation;
59 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfinformationVnfInformation;
60 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfrequestinputVnfRequestInput;
61 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnftopologyVnfTopology;
62 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnftopologyidentifierstructureVnfTopologyIdentifierStructure;
63 import org.onap.so.sdncsimulator.models.Output;
64 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
67 import org.springframework.beans.factory.annotation.Autowired;
68 import org.springframework.cache.Cache;
69 import org.springframework.cache.CacheManager;
70 import org.springframework.http.HttpStatus;
71 import org.springframework.stereotype.Service;
72
73 /**
74  * @author Waqas Ikram (waqas.ikram@est.tech)
75  *
76  */
77 @Service
78 public class ServiceOperationsCacheServiceProviderimpl extends AbstractCacheServiceProvider
79         implements ServiceOperationsCacheServiceProvider {
80
81     private static final String HTTP_STATUS_BAD_REQUEST = Integer.toString(HttpStatus.BAD_REQUEST.value());
82     private static final String HTTP_STATUS_OK = Integer.toString(HttpStatus.OK.value());
83     private static final String EMPTY_STRING = "";
84     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceOperationsCacheServiceProviderimpl.class);
85
86     @Autowired
87     public ServiceOperationsCacheServiceProviderimpl(final CacheManager cacheManager) {
88         super(cacheManager);
89     }
90
91     @Override
92     public Output putServiceOperationInformation(final GenericResourceApiServiceOperationInformation input) {
93
94         final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
95         final String svcRequestId = getSvcRequestId(requestHeader);
96
97         final GenericResourceApiServiceinformationServiceInformation serviceInformation = input.getServiceInformation();
98         if (serviceInformation != null && isValid(serviceInformation.getServiceInstanceId())) {
99             final String serviceInstanceId = serviceInformation.getServiceInstanceId();
100
101             if (isServiceOperationInformationNotExists(serviceInstanceId, input)) {
102                 final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
103                 LOGGER.info("Adding GenericResourceApiServiceOperationInformation to cache with key: {}",
104                         serviceInstanceId);
105
106                 final GenericResourceApiServicemodelinfrastructureService service =
107                         getServiceItem(input, serviceInstanceId);
108                 cache.put(serviceInstanceId, service);
109
110                 final GenericResourceApiServicestatusServiceStatus serviceStatus = service.getServiceStatus();
111
112                 return new Output().ackFinalIndicator(serviceStatus.getFinalIndicator())
113                         .responseCode(serviceStatus.getResponseCode())
114                         .responseMessage(serviceStatus.getResponseMessage()).svcRequestId(svcRequestId)
115                         .serviceResponseInformation(new GenericResourceApiInstanceReference()
116                                 .instanceId(serviceInstanceId).objectPath(getObjectPath(serviceInstanceId)));
117             }
118             LOGGER.error("serviceInstanceId: {} already exists", serviceInstanceId);
119             return new Output().ackFinalIndicator(YES).responseCode(HTTP_STATUS_BAD_REQUEST)
120                     .responseMessage("serviceInstanceId: " + serviceInstanceId + " already exists")
121                     .svcRequestId(svcRequestId);
122         }
123
124         LOGGER.error(
125                 "Unable to add GenericResourceApiServiceOperationInformation in cache due to invalid input: {}... ",
126                 input);
127         return new Output().ackFinalIndicator(YES).responseCode(HTTP_STATUS_BAD_REQUEST)
128                 .responseMessage("Service instance not found").svcRequestId(svcRequestId);
129
130     }
131
132     @Override
133     public Output deleteServiceOperationInformation(final GenericResourceApiServiceOperationInformation input) {
134         final GenericResourceApiServiceinformationServiceInformation serviceInformation = input.getServiceInformation();
135         final String svcRequestId = getSvcRequestId(input.getSdncRequestHeader());
136
137         if (serviceInformation != null && isValid(serviceInformation.getServiceInstanceId())) {
138             final String serviceInstanceId = serviceInformation.getServiceInstanceId();
139             final Optional<GenericResourceApiServicemodelinfrastructureService> optional =
140                     getGenericResourceApiServicemodelinfrastructureService(serviceInstanceId);
141             if (optional.isPresent()) {
142                 final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
143                 LOGGER.info("Deleting GenericResourceApiServiceOperationInformation from cache using key: {}",
144                         serviceInstanceId);
145                 cache.evict(serviceInstanceId);
146                 return new Output().ackFinalIndicator(YES).responseCode(HTTP_STATUS_OK)
147                         .responseMessage(EMPTY_STRING).svcRequestId(svcRequestId).serviceResponseInformation(
148                                 new GenericResourceApiInstanceReference().instanceId(serviceInstanceId));
149             }
150             LOGGER.error(
151                     "Unable to find existing GenericResourceApiServiceModelInfrastructure in cache using service instance id: {}",
152                     serviceInstanceId);
153
154         }
155         LOGGER.error("Unable to remove service instance from cache due to invalid input: {}... ", input);
156         return new Output().ackFinalIndicator(YES).responseCode(HTTP_STATUS_BAD_REQUEST)
157                 .responseMessage("Unable to remove service").svcRequestId(svcRequestId);
158     }
159
160     @Override
161     public Optional<GenericResourceApiServicemodelinfrastructureService> getGenericResourceApiServicemodelinfrastructureService(
162             final String serviceInstanceId) {
163         final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
164
165         final GenericResourceApiServicemodelinfrastructureService value =
166                 cache.get(serviceInstanceId, GenericResourceApiServicemodelinfrastructureService.class);
167         if (value != null) {
168             LOGGER.info("Found {} in cahce for service instance id: {}", value, serviceInstanceId);
169             return Optional.of(value);
170         }
171         LOGGER.error("Unable to find GenericResourceApiServiceModelInfrastructure in cache for service instance id: {}",
172                 serviceInstanceId);
173         return Optional.empty();
174     }
175
176     @Override
177     public Output putVnfOperationInformation(final GenericResourceApiVnfOperationInformation input) {
178
179         final GenericResourceApiServiceinformationServiceInformation serviceInformation = input.getServiceInformation();
180         final GenericResourceApiVnfinformationVnfInformation vnfInformation = input.getVnfInformation();
181
182         final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
183         final String svcRequestId = getSvcRequestId(requestHeader);
184
185         if (serviceInformation != null && isValid(serviceInformation.getServiceInstanceId()) && vnfInformation != null
186                 && isValid(vnfInformation.getVnfId())) {
187             final String serviceInstanceId = serviceInformation.getServiceInstanceId();
188             final String vnfId = vnfInformation.getVnfId();
189             final Optional<GenericResourceApiServicemodelinfrastructureService> optional =
190                     getGenericResourceApiServicemodelinfrastructureService(serviceInstanceId);
191             if (optional.isPresent()) {
192                 final GenericResourceApiServicemodelinfrastructureService service = optional.get();
193                 final GenericResourceApiServicedataServiceData serviceData = service.getServiceData();
194                 if (serviceData != null) {
195                     final List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList = getVnfs(serviceData);
196                     final GenericResourceApiLastRpcActionEnumeration svcAction =
197                             GenericResourceApiLastRpcActionEnumeration.fromValue(getSvcAction(requestHeader));
198
199                     if (ifVnfNotExists(vnfId, svcAction, vnfsList)) {
200                         vnfsList.add(getGenericResourceApiServicedataVnf(serviceInstanceId, vnfId, input));
201
202                         final GenericResourceApiServicestatusServiceStatus serviceStatus = service.getServiceStatus();
203
204                         return new Output().ackFinalIndicator(serviceStatus.getFinalIndicator())
205                                 .responseCode(serviceStatus.getResponseCode())
206                                 .responseMessage(serviceStatus.getResponseMessage()).svcRequestId(svcRequestId)
207                                 .serviceResponseInformation(new GenericResourceApiInstanceReference()
208                                         .instanceId(serviceInstanceId).objectPath(getObjectPath(serviceInstanceId)))
209                                 .vnfResponseInformation(new GenericResourceApiInstanceReference().instanceId(vnfId)
210                                         .objectPath(getObjectPath(serviceInstanceId, vnfId)));
211                     }
212                     LOGGER.error("vnfId: {} already exists with SVC Action: {}", vnfId, svcAction);
213                     return new Output().ackFinalIndicator(YES).responseCode(HTTP_STATUS_BAD_REQUEST)
214                             .responseMessage("vnfId: " + vnfId + " already exists").svcRequestId(svcRequestId);
215                 }
216             }
217             LOGGER.error(
218                     "Unable to find existing GenericResourceApiServiceModelInfrastructure in cache using service instance id: {}",
219                     serviceInstanceId);
220
221         }
222         LOGGER.error(
223                 "Unable to add GenericResourceApiServiceOperationInformation in cache due to invalid input: {}... ",
224                 input);
225         return new Output().ackFinalIndicator(YES).responseCode(HTTP_STATUS_BAD_REQUEST)
226                 .responseMessage("Unable to add vnf").svcRequestId(svcRequestId);
227     }
228
229     @Override
230     public Output deleteVnfOperationInformation(final GenericResourceApiVnfOperationInformation input) {
231         final GenericResourceApiServiceinformationServiceInformation serviceInformation = input.getServiceInformation();
232         final GenericResourceApiVnfinformationVnfInformation vnfInformation = input.getVnfInformation();
233
234         final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
235         final String svcRequestId = getSvcRequestId(requestHeader);
236
237         if (serviceInformation != null && isValid(serviceInformation.getServiceInstanceId()) && vnfInformation != null
238                 && isValid(vnfInformation.getVnfId())) {
239             final String serviceInstanceId = serviceInformation.getServiceInstanceId();
240             final String vnfId = vnfInformation.getVnfId();
241             final Optional<GenericResourceApiServicemodelinfrastructureService> optional =
242                     getGenericResourceApiServicemodelinfrastructureService(serviceInstanceId);
243             if (optional.isPresent()) {
244                 final GenericResourceApiServicemodelinfrastructureService service = optional.get();
245                 final GenericResourceApiServicedataServiceData serviceData = service.getServiceData();
246                 if (serviceData != null) {
247                     final List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList = getVnfs(serviceData);
248                     final Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnfInstanceOptional =
249                             getExistingVnf(vnfId, vnfsList);
250
251                     if (vnfInstanceOptional.isPresent()) {
252                         vnfsList.removeIf(vnf -> {
253                             final String existingVnfId = vnf.getVnfId();
254                             if (existingVnfId != null && existingVnfId.equals(vnfId)) {
255                                 LOGGER.info("Remove vnf with id: {} ... ", existingVnfId);
256                                 return true;
257                             }
258                             return false;
259                         });
260
261                         return new Output().ackFinalIndicator(YES).responseCode(HTTP_STATUS_OK)
262                                 .responseMessage(EMPTY_STRING).svcRequestId(svcRequestId)
263                                 .serviceResponseInformation(
264                                         new GenericResourceApiInstanceReference().instanceId(serviceInstanceId))
265                                 .vnfResponseInformation(new GenericResourceApiInstanceReference().instanceId(vnfId));
266                     }
267
268                 }
269             }
270             LOGGER.error(
271                     "Unable to find existing GenericResourceApiServiceModelInfrastructure in cache using service instance id: {}",
272                     serviceInstanceId);
273
274         }
275         LOGGER.error("Unable to remove vnf instance from cache due to invalid input: {}... ", input);
276         return new Output().ackFinalIndicator(YES).responseCode(HTTP_STATUS_BAD_REQUEST)
277                 .responseMessage("Unable to remove vnf").svcRequestId(svcRequestId);
278
279     }
280
281     private String getSvcRequestId(final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader) {
282         return requestHeader != null ? requestHeader.getSvcRequestId() : null;
283     }
284
285     @Override
286     public void clearAll() {
287         clearCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
288     }
289
290     private String getObjectPath(final String serviceInstanceId, final String vnfId) {
291         return getObjectPath(serviceInstanceId) + SERVICE_DATA_VNFS_VNF + vnfId + VNF_DATA_VNF_TOPOLOGY;
292     }
293
294     private String getObjectPath(final String serviceInstanceId) {
295         return RESTCONF_CONFIG_END_POINT + serviceInstanceId;
296     }
297
298
299     private boolean ifVnfNotExists(final String vnfId, final GenericResourceApiLastRpcActionEnumeration svcAction,
300             final List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList) {
301         final Optional<GenericResourceApiServicedataServicedataVnfsVnf> optional = getExistingVnf(vnfId, vnfsList);
302         if (optional.isPresent()) {
303             final GenericResourceApiServicedataServicedataVnfsVnf existingVnf = optional.get();
304             final GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData = existingVnf.getVnfData();
305
306             if (vnfData != null && vnfData.getVnfLevelOperStatus() != null
307                     && vnfData.getVnfLevelOperStatus().getLastRpcAction() != null) {
308                 final GenericResourceApiLastRpcActionEnumeration existingVnflastRpcAction =
309                         vnfData.getVnfLevelOperStatus().getLastRpcAction();
310                 if (existingVnflastRpcAction.equals(svcAction)) {
311                     LOGGER.error("Found vnf with id: {} and LastRpcAction: {} same as SvcAction:  {}", vnfId,
312                             existingVnflastRpcAction, svcAction);
313                     return false;
314                 }
315                 LOGGER.warn("Will remove and replace existing vnf with id: {} as SvcAction is changed from {} to {}",
316                         vnfId, existingVnflastRpcAction, svcAction);
317                 vnfsList.removeIf(vnf -> vnf.getVnfId() != null && vnf.getVnfId().equals(vnfId));
318
319             }
320         }
321
322         return true;
323     }
324
325     private Optional<GenericResourceApiServicedataServicedataVnfsVnf> getExistingVnf(final String vnfId,
326             final List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList) {
327         return vnfsList.stream().filter(vnf -> vnf.getVnfId() != null && vnf.getVnfId().equals(vnfId)).findFirst();
328     }
329
330     private List<GenericResourceApiServicedataServicedataVnfsVnf> getVnfs(
331             final GenericResourceApiServicedataServiceData serviceData) {
332         GenericResourceApiServicedataServicedataVnfs vnfs = serviceData.getVnfs();
333         if (vnfs == null) {
334             vnfs = new GenericResourceApiServicedataServicedataVnfs();
335             serviceData.setVnfs(vnfs);
336         }
337
338         List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList = vnfs.getVnf();
339         if (vnfsList == null) {
340             vnfsList = new ArrayList<>();
341             vnfs.setVnf(vnfsList);
342         }
343         return vnfsList;
344     }
345
346     private GenericResourceApiServicedataServicedataVnfsVnf getGenericResourceApiServicedataVnf(
347             final String serviceInstanceId, final String vnfId, final GenericResourceApiVnfOperationInformation input) {
348         return new GenericResourceApiServicedataServicedataVnfsVnf().vnfId(vnfId).vnfData(getVnfData(input));
349     }
350
351     private GenericResourceApiServicedataServicedataVnfsVnfVnfData getVnfData(
352             final GenericResourceApiVnfOperationInformation input) {
353
354         final GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData =
355                 new GenericResourceApiServicedataServicedataVnfsVnfVnfData();
356
357         vnfData.vnfLevelOperStatus(
358                 getServiceLevelOperStatus(PENDINGCREATE, input.getRequestInformation(), input.getSdncRequestHeader()));
359         vnfData.serviceInformation(input.getServiceInformation());
360         vnfData.sdncRequestHeader(input.getSdncRequestHeader());
361         vnfData.vnfInformation(input.getVnfInformation());
362         vnfData.requestInformation(input.getRequestInformation());
363         vnfData.vnfRequestInput(input.getVnfRequestInput());
364
365         vnfData.vnfTopology(getVnfTopology(input.getVnfInformation(), input.getVnfRequestInput()));
366
367         return vnfData;
368     }
369
370     private GenericResourceApiVnftopologyVnfTopology getVnfTopology(
371             final GenericResourceApiVnfinformationVnfInformation vnfInformation,
372             final GenericResourceApiVnfrequestinputVnfRequestInput vnfRequestInput) {
373
374         final GenericResourceApiVnftopologyVnfTopology apiVnftopologyVnfTopology =
375                 new GenericResourceApiVnftopologyVnfTopology();
376
377         if (vnfInformation != null) {
378             apiVnftopologyVnfTopology.onapModelInformation(vnfInformation.getOnapModelInformation());
379             apiVnftopologyVnfTopology.vnfTopologyIdentifierStructure(getTopologyIdentifierStructure(vnfInformation));
380         }
381         if (vnfRequestInput != null) {
382             apiVnftopologyVnfTopology.tenant(vnfRequestInput.getTenant());
383             apiVnftopologyVnfTopology.aicClli(vnfRequestInput.getAicClli());
384             apiVnftopologyVnfTopology.aicCloudRegion(vnfRequestInput.getAicCloudRegion());
385         }
386         return apiVnftopologyVnfTopology;
387     }
388
389     private GenericResourceApiVnftopologyidentifierstructureVnfTopologyIdentifierStructure getTopologyIdentifierStructure(
390             @Valid final GenericResourceApiVnfinformationVnfInformation vnfInformation) {
391         return new GenericResourceApiVnftopologyidentifierstructureVnfTopologyIdentifierStructure()
392                 .vnfId(vnfInformation.getVnfId()).vnfName(vnfInformation.getVnfName())
393                 .vnfType(vnfInformation.getVnfType());
394     }
395
396     private GenericResourceApiServicemodelinfrastructureService getServiceItem(
397             final GenericResourceApiServiceOperationInformation input, final String serviceInstanceId) {
398
399         final GenericResourceApiServicedataServiceData apiServicedataServiceData =
400                 new GenericResourceApiServicedataServiceData();
401
402         apiServicedataServiceData.requestInformation(input.getRequestInformation());
403         apiServicedataServiceData.serviceRequestInput(input.getServiceRequestInput());
404         apiServicedataServiceData.serviceInformation(input.getServiceInformation());
405         apiServicedataServiceData.serviceTopology(getServiceTopology(input));
406         apiServicedataServiceData.sdncRequestHeader(input.getSdncRequestHeader());
407         apiServicedataServiceData.serviceLevelOperStatus(getServiceLevelOperStatus(input));
408
409         final GenericResourceApiServicestatusServiceStatus serviceStatus =
410                 getServiceStatus(getSvcAction(input.getSdncRequestHeader()),
411                         getRequestAction(input.getRequestInformation()), HTTP_STATUS_OK);
412
413         return new GenericResourceApiServicemodelinfrastructureService().serviceData(apiServicedataServiceData)
414                 .serviceStatus(serviceStatus).serviceInstanceId(serviceInstanceId);
415     }
416
417     private String getSvcAction(final GenericResourceApiSdncrequestheaderSdncRequestHeader input) {
418         return input != null ? getStringOrNull(input.getSvcAction()) : null;
419     }
420
421     private GenericResourceApiServicestatusServiceStatus getServiceStatus(final String rpcAction, final String action,
422             final String responseCode) {
423         return new GenericResourceApiServicestatusServiceStatus().finalIndicator(YES)
424                 .rpcAction(GenericResourceApiRpcActionEnumeration.fromValue(rpcAction))
425                 .rpcName(SERVICE_TOPOLOGY_OPERATION).responseTimestamp(LocalDateTime.now().toString())
426                 .responseCode(responseCode).requestStatus(SYNCCOMPLETE).responseMessage(EMPTY_STRING).action(action);
427     }
428
429     private GenericResourceApiOperStatusData getServiceLevelOperStatus(
430             final GenericResourceApiServiceOperationInformation input) {
431         return getServiceLevelOperStatus(CREATED, input.getRequestInformation(), input.getSdncRequestHeader());
432     }
433
434     private GenericResourceApiOperStatusData getServiceLevelOperStatus(
435             final GenericResourceApiOrderStatusEnumeration statusEnumeration,
436             final GenericResourceApiRequestinformationRequestInformation requestInformation,
437             final GenericResourceApiSdncrequestheaderSdncRequestHeader sdncRequestHeader) {
438         return new GenericResourceApiOperStatusData().orderStatus(statusEnumeration)
439                 .lastAction(GenericResourceApiLastActionEnumeration.fromValue(getRequestAction(requestInformation)))
440                 .lastRpcAction(GenericResourceApiLastRpcActionEnumeration.fromValue(getSvcAction(sdncRequestHeader)));
441     }
442
443     private String getRequestAction(final GenericResourceApiRequestinformationRequestInformation input) {
444         return getRequestAction(input, EMPTY_STRING);
445     }
446
447     private String getRequestAction(final GenericResourceApiRequestinformationRequestInformation input,
448             final String defaultValue) {
449         return input != null ? getString(input.getRequestAction(), defaultValue) : defaultValue;
450     }
451
452     private GenericResourceApiServicetopologyServiceTopology getServiceTopology(
453             final GenericResourceApiServiceOperationInformation input) {
454         final GenericResourceApiOnapmodelinformationOnapModelInformation modelInformation =
455                 input.getServiceInformation() != null ? input.getServiceInformation().getOnapModelInformation() : null;
456         return new GenericResourceApiServicetopologyServiceTopology().onapModelInformation(modelInformation)
457                 .serviceTopologyIdentifier(getServiceTopologyIdentifier(input));
458     }
459
460     private GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier getServiceTopologyIdentifier(
461             final GenericResourceApiServiceOperationInformation input) {
462         final GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier identifier =
463                 new GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier();
464
465         if (input.getServiceInformation() != null) {
466             final GenericResourceApiServiceinformationServiceInformation serviceInformation =
467                     input.getServiceInformation();
468             identifier.globalCustomerId(serviceInformation.getGlobalCustomerId())
469                     .serviceType(input.getServiceInformation().getSubscriptionServiceType())
470                     .serviceInstanceId(input.getServiceInformation().getServiceInstanceId());;
471         }
472
473         if (input.getServiceRequestInput() != null) {
474             identifier.serviceInstanceName(input.getServiceRequestInput().getServiceInstanceName());
475         }
476
477         return identifier;
478
479     }
480
481     private boolean isServiceOperationInformationNotExists(final String serviceInstanceId,
482             final GenericResourceApiServiceOperationInformation input) {
483         final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
484         final Optional<GenericResourceApiServicemodelinfrastructureService> optional =
485                 getGenericResourceApiServicemodelinfrastructureService(serviceInstanceId);
486
487         if (optional.isPresent()) {
488             final GenericResourceApiServicemodelinfrastructureService existingService = optional.get();
489             final GenericResourceApiServicestatusServiceStatus serviceStatus = existingService.getServiceStatus();
490             if (serviceStatus != null) {
491                 final GenericResourceApiRpcActionEnumeration rpcAction = serviceStatus.getRpcAction();
492                 final String svcAction = getSvcAction(requestHeader);
493                 if (rpcAction != null && rpcAction.toString().equals(svcAction)) {
494                     LOGGER.error("Found Service with id: {} and RpcAction: {} same as SvcAction:  {}",
495                             serviceInstanceId, rpcAction, svcAction);
496                     return false;
497                 }
498
499                 final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
500                 LOGGER.info(
501                         "Deleting existing GenericResourceApiServiceOperationInformation from cache using key: {} as SvcAction is changed from {} to {}",
502                         serviceInstanceId, rpcAction, svcAction);
503                 cache.evict(serviceInstanceId);
504             }
505         }
506         return true;
507
508     }
509
510 }