Added CSIT for Macroflow with HEAT
[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
64 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfmoduleVfModuleData;
65 import org.onap.sdnc.northbound.client.model.GenericResourceApiVfModuleTopology;
66 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfTopology;
67 import org.onap.sdnc.northbound.client.model.GenericResourceApiVfModuleOperationInformation;
68 import org.onap.sdnc.northbound.client.model.GenericResourceApiVfmoduleinformationVfModuleInformation;
69 import org.onap.sdnc.northbound.client.model.GenericResourceApiVfmoduletopologyVfModuleTopology;
70 import org.onap.sdnc.northbound.client.model.GenericResourceApiVfmoduletopologyidentifierVfModuleTopologyIdentifier;
71 import org.onap.sdnc.northbound.client.model.GenericResourceApiParam;
72 import org.onap.sdnc.northbound.client.model.GenericResourceApiParamParam;
73
74 import org.onap.so.sdncsimulator.models.Output;
75 import org.onap.so.sdncsimulator.utils.Constants;
76 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
77 import org.slf4j.Logger;
78 import org.slf4j.LoggerFactory;
79 import org.springframework.beans.factory.annotation.Autowired;
80 import org.springframework.cache.Cache;
81 import org.springframework.cache.CacheManager;
82 import org.springframework.http.HttpStatus;
83 import org.springframework.stereotype.Service;
84
85 /**
86  * @author Waqas Ikram (waqas.ikram@est.tech)
87  *
88  */
89 @Service
90 public class ServiceOperationsCacheServiceProviderimpl extends AbstractCacheServiceProvider
91         implements ServiceOperationsCacheServiceProvider {
92
93     private static final String HTTP_STATUS_BAD_REQUEST = Integer.toString(HttpStatus.BAD_REQUEST.value());
94     private static final String HTTP_STATUS_OK = Integer.toString(HttpStatus.OK.value());
95     private static final String EMPTY_STRING = "";
96     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceOperationsCacheServiceProviderimpl.class);
97
98     @Autowired
99     public ServiceOperationsCacheServiceProviderimpl(final CacheManager cacheManager) {
100         super(cacheManager);
101     }
102
103     @Override
104     public Output putServiceOperationInformation(final GenericResourceApiServiceOperationInformation input) {
105
106         final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
107         final String svcRequestId = getSvcRequestId(requestHeader);
108
109         final GenericResourceApiServiceinformationServiceInformation serviceInformation = input.getServiceInformation();
110         if (serviceInformation != null && isValid(serviceInformation.getServiceInstanceId())) {
111             final String serviceInstanceId = serviceInformation.getServiceInstanceId();
112
113             if (isServiceOperationInformationNotExists(serviceInstanceId, input)) {
114                 final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
115                 LOGGER.info("Adding GenericResourceApiServiceOperationInformation to cache with key: {}",
116                         serviceInstanceId);
117
118                 final GenericResourceApiServicemodelinfrastructureService service =
119                         getServiceItem(input, serviceInstanceId);
120                 cache.put(serviceInstanceId, service);
121
122                 final GenericResourceApiServicestatusServiceStatus serviceStatus = service.getServiceStatus();
123
124                 return new Output().ackFinalIndicator(serviceStatus.getFinalIndicator())
125                         .responseCode(serviceStatus.getResponseCode())
126                         .responseMessage(serviceStatus.getResponseMessage()).svcRequestId(svcRequestId)
127                         .serviceResponseInformation(new GenericResourceApiInstanceReference()
128                                 .instanceId(serviceInstanceId).objectPath(getObjectPath(serviceInstanceId)));
129             }
130             LOGGER.error("serviceInstanceId: {} already exists", serviceInstanceId);
131             return new Output().ackFinalIndicator(YES).responseCode(HTTP_STATUS_BAD_REQUEST)
132                     .responseMessage("serviceInstanceId: " + serviceInstanceId + " already exists")
133                     .svcRequestId(svcRequestId);
134         }
135
136         LOGGER.error(
137                 "Unable to add GenericResourceApiServiceOperationInformation in cache due to invalid input: {}... ",
138                 input);
139         return new Output().ackFinalIndicator(YES).responseCode(HTTP_STATUS_BAD_REQUEST)
140                 .responseMessage("Service instance not found").svcRequestId(svcRequestId);
141
142     }
143
144     @Override
145     public Output deleteServiceOperationInformation(final GenericResourceApiServiceOperationInformation input) {
146         final GenericResourceApiServiceinformationServiceInformation serviceInformation = input.getServiceInformation();
147         final String svcRequestId = getSvcRequestId(input.getSdncRequestHeader());
148
149         if (serviceInformation != null && isValid(serviceInformation.getServiceInstanceId())) {
150             final String serviceInstanceId = serviceInformation.getServiceInstanceId();
151             final Optional<GenericResourceApiServicemodelinfrastructureService> optional =
152                     getGenericResourceApiServicemodelinfrastructureService(serviceInstanceId);
153             if (optional.isPresent()) {
154                 final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
155                 LOGGER.info("Deleting GenericResourceApiServiceOperationInformation from cache using key: {}",
156                         serviceInstanceId);
157                 cache.evict(serviceInstanceId);
158                 return new Output().ackFinalIndicator(YES).responseCode(HTTP_STATUS_OK)
159                         .responseMessage(EMPTY_STRING).svcRequestId(svcRequestId).serviceResponseInformation(
160                                 new GenericResourceApiInstanceReference().instanceId(serviceInstanceId));
161             }
162             LOGGER.error(
163                     "Unable to find existing GenericResourceApiServiceModelInfrastructure in cache using service instance id: {}",
164                     serviceInstanceId);
165
166         }
167         LOGGER.error("Unable to remove service instance from cache due to invalid input: {}... ", input);
168         return new Output().ackFinalIndicator(YES).responseCode(HTTP_STATUS_BAD_REQUEST)
169                 .responseMessage("Unable to remove service").svcRequestId(svcRequestId);
170     }
171
172     @Override
173     public Optional<GenericResourceApiServicemodelinfrastructureService> getGenericResourceApiServicemodelinfrastructureService(
174             final String serviceInstanceId) {
175         final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
176
177         final GenericResourceApiServicemodelinfrastructureService value =
178                 cache.get(serviceInstanceId, GenericResourceApiServicemodelinfrastructureService.class);
179         if (value != null) {
180             LOGGER.info("Found {} in cahce for service instance id: {}", value, serviceInstanceId);
181             return Optional.of(value);
182         }
183         LOGGER.error("Unable to find GenericResourceApiServiceModelInfrastructure in cache for service instance id: {}",
184                 serviceInstanceId);
185         return Optional.empty();
186     }
187
188     @Override
189     public Output putVnfOperationInformation(final GenericResourceApiVnfOperationInformation input) {
190
191         final GenericResourceApiServiceinformationServiceInformation serviceInformation = input.getServiceInformation();
192         final GenericResourceApiVnfinformationVnfInformation vnfInformation = input.getVnfInformation();
193
194         final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
195         final String svcRequestId = getSvcRequestId(requestHeader);
196
197         if (serviceInformation != null && isValid(serviceInformation.getServiceInstanceId()) && vnfInformation != null
198                 && isValid(vnfInformation.getVnfId())) {
199             final String serviceInstanceId = serviceInformation.getServiceInstanceId();
200             final String vnfId = vnfInformation.getVnfId();
201             final Optional<GenericResourceApiServicemodelinfrastructureService> optional =
202                     getGenericResourceApiServicemodelinfrastructureService(serviceInstanceId);
203             if (optional.isPresent()) {
204                 final GenericResourceApiServicemodelinfrastructureService service = optional.get();
205                 final GenericResourceApiServicedataServiceData serviceData = service.getServiceData();
206                 if (serviceData != null) {
207                     final List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList = getVnfs(serviceData);
208                     final GenericResourceApiLastRpcActionEnumeration svcAction =
209                             GenericResourceApiLastRpcActionEnumeration.fromValue(getSvcAction(requestHeader));
210
211                     if (ifVnfNotExists(vnfId, svcAction, vnfsList)) {
212                         vnfsList.add(getGenericResourceApiServicedataVnf(serviceInstanceId, vnfId, input));
213                         setVnfsData(vnfsList);
214                         final GenericResourceApiServicestatusServiceStatus serviceStatus = service.getServiceStatus();
215
216                         return new Output().ackFinalIndicator(serviceStatus.getFinalIndicator())
217                                 .responseCode(serviceStatus.getResponseCode())
218                                 .responseMessage(serviceStatus.getResponseMessage()).svcRequestId(svcRequestId)
219                                 .serviceResponseInformation(new GenericResourceApiInstanceReference()
220                                         .instanceId(serviceInstanceId).objectPath(getObjectPath(serviceInstanceId)))
221                                 .vnfResponseInformation(new GenericResourceApiInstanceReference().instanceId(vnfId)
222                                         .objectPath(getObjectPath(serviceInstanceId, vnfId)));
223                     }
224                     LOGGER.error("vnfId: {} already exists with SVC Action: {}", vnfId, svcAction);
225                     return new Output().ackFinalIndicator(YES).responseCode(HTTP_STATUS_BAD_REQUEST)
226                             .responseMessage("vnfId: " + vnfId + " already exists").svcRequestId(svcRequestId);
227                 }
228             }
229             LOGGER.error(
230                     "Unable to find existing GenericResourceApiServiceModelInfrastructure in cache using service instance id: {}",
231                     serviceInstanceId);
232
233         }
234         LOGGER.error(
235                 "Unable to add GenericResourceApiServiceOperationInformation in cache due to invalid input: {}... ",
236                 input);
237         return new Output().ackFinalIndicator(YES).responseCode(HTTP_STATUS_BAD_REQUEST)
238                 .responseMessage("Unable to add vnf").svcRequestId(svcRequestId);
239     }
240
241     @Override
242     public Output deleteVnfOperationInformation(final GenericResourceApiVnfOperationInformation input) {
243         final GenericResourceApiServiceinformationServiceInformation serviceInformation = input.getServiceInformation();
244         final GenericResourceApiVnfinformationVnfInformation vnfInformation = input.getVnfInformation();
245
246         final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
247         final String svcRequestId = getSvcRequestId(requestHeader);
248
249         if (serviceInformation != null && isValid(serviceInformation.getServiceInstanceId()) && vnfInformation != null
250                 && isValid(vnfInformation.getVnfId())) {
251             final String serviceInstanceId = serviceInformation.getServiceInstanceId();
252             final String vnfId = vnfInformation.getVnfId();
253             final Optional<GenericResourceApiServicemodelinfrastructureService> optional =
254                     getGenericResourceApiServicemodelinfrastructureService(serviceInstanceId);
255             if (optional.isPresent()) {
256                 final GenericResourceApiServicemodelinfrastructureService service = optional.get();
257                 final GenericResourceApiServicedataServiceData serviceData = service.getServiceData();
258                 if (serviceData != null) {
259                     final List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList = getVnfs(serviceData);
260                     final Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnfInstanceOptional =
261                             getExistingVnf(vnfId, vnfsList);
262
263                     if (vnfInstanceOptional.isPresent()) {
264                         vnfsList.removeIf(vnf -> {
265                             final String existingVnfId = vnf.getVnfId();
266                             if (existingVnfId != null && existingVnfId.equals(vnfId)) {
267                                 LOGGER.info("Remove vnf with id: {} ... ", existingVnfId);
268                                 return true;
269                             }
270                             return false;
271                         });
272
273                         return new Output().ackFinalIndicator(YES).responseCode(HTTP_STATUS_OK)
274                                 .responseMessage(EMPTY_STRING).svcRequestId(svcRequestId)
275                                 .serviceResponseInformation(
276                                         new GenericResourceApiInstanceReference().instanceId(serviceInstanceId))
277                                 .vnfResponseInformation(new GenericResourceApiInstanceReference().instanceId(vnfId));
278                     }
279
280                 }
281             }
282             LOGGER.error(
283                     "Unable to find existing GenericResourceApiServiceModelInfrastructure in cache using service instance id: {}",
284                     serviceInstanceId);
285
286         }
287         LOGGER.error("Unable to remove vnf instance from cache due to invalid input: {}... ", input);
288         return new Output().ackFinalIndicator(YES).responseCode(HTTP_STATUS_BAD_REQUEST)
289                 .responseMessage("Unable to remove vnf").svcRequestId(svcRequestId);
290
291     }
292
293     private String getSvcRequestId(final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader) {
294         return requestHeader != null ? requestHeader.getSvcRequestId() : null;
295     }
296
297     @Override
298     public void clearAll() {
299         clearCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
300     }
301
302     private String getObjectPath(final String serviceInstanceId, final String vnfId) {
303         return getObjectPath(serviceInstanceId) + SERVICE_DATA_VNFS_VNF + vnfId + VNF_DATA_VNF_TOPOLOGY;
304     }
305
306     private String getObjectPath(final String serviceInstanceId) {
307         return RESTCONF_CONFIG_END_POINT + serviceInstanceId;
308     }
309
310
311     private boolean ifVnfNotExists(final String vnfId, final GenericResourceApiLastRpcActionEnumeration svcAction,
312             final List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList) {
313         final Optional<GenericResourceApiServicedataServicedataVnfsVnf> optional = getExistingVnf(vnfId, vnfsList);
314         if (optional.isPresent()) {
315             final GenericResourceApiServicedataServicedataVnfsVnf existingVnf = optional.get();
316             final GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData = existingVnf.getVnfData();
317
318             if (vnfData != null && vnfData.getVnfLevelOperStatus() != null
319                     && vnfData.getVnfLevelOperStatus().getLastRpcAction() != null) {
320                 final GenericResourceApiLastRpcActionEnumeration existingVnflastRpcAction =
321                         vnfData.getVnfLevelOperStatus().getLastRpcAction();
322                 if (existingVnflastRpcAction.equals(svcAction)) {
323                     LOGGER.error("Found vnf with id: {} and LastRpcAction: {} same as SvcAction:  {}", vnfId,
324                             existingVnflastRpcAction, svcAction);
325                     return false;
326                 }
327                 LOGGER.warn("Will remove and replace existing vnf with id: {} as SvcAction is changed from {} to {}",
328                         vnfId, existingVnflastRpcAction, svcAction);
329                 vnfsList.removeIf(vnf -> vnf.getVnfId() != null && vnf.getVnfId().equals(vnfId));
330
331             }
332         }
333
334         return true;
335     }
336
337     private Optional<GenericResourceApiServicedataServicedataVnfsVnf> getExistingVnf(final String vnfId,
338             final List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList) {
339         return vnfsList.stream().filter(vnf -> vnf.getVnfId() != null && vnf.getVnfId().equals(vnfId)).findFirst();
340     }
341
342     private List<GenericResourceApiServicedataServicedataVnfsVnf> getVnfs(
343             final GenericResourceApiServicedataServiceData serviceData) {
344         GenericResourceApiServicedataServicedataVnfs vnfs = serviceData.getVnfs();
345         if (vnfs == null) {
346             vnfs = new GenericResourceApiServicedataServicedataVnfs();
347             serviceData.setVnfs(vnfs);
348         }
349
350         List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList = vnfs.getVnf();
351         if (vnfsList == null) {
352             vnfsList = new ArrayList<>();
353             vnfs.setVnf(vnfsList);
354         }
355         return vnfsList;
356     }
357
358     private GenericResourceApiServicedataServicedataVnfsVnf getGenericResourceApiServicedataVnf(
359             final String serviceInstanceId, final String vnfId, final GenericResourceApiVnfOperationInformation input) {
360         return new GenericResourceApiServicedataServicedataVnfsVnf().vnfId(vnfId).vnfData(getVnfData(input));
361     }
362
363     private GenericResourceApiServicedataServicedataVnfsVnfVnfData getVnfData(
364             final GenericResourceApiVnfOperationInformation input) {
365
366         final GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData =
367                 new GenericResourceApiServicedataServicedataVnfsVnfVnfData();
368
369         vnfData.vnfLevelOperStatus(
370                 getServiceLevelOperStatus(PENDINGCREATE, input.getRequestInformation(), input.getSdncRequestHeader()));
371         vnfData.serviceInformation(input.getServiceInformation());
372         vnfData.sdncRequestHeader(input.getSdncRequestHeader());
373         vnfData.vnfInformation(input.getVnfInformation());
374         vnfData.requestInformation(input.getRequestInformation());
375         vnfData.vnfRequestInput(input.getVnfRequestInput());
376
377         vnfData.vnfTopology(getVnfTopology(input.getVnfInformation(), input.getVnfRequestInput()));
378
379         return vnfData;
380     }
381
382     private GenericResourceApiVnftopologyVnfTopology getVnfTopology(
383             final GenericResourceApiVnfinformationVnfInformation vnfInformation,
384             final GenericResourceApiVnfrequestinputVnfRequestInput vnfRequestInput) {
385
386         final GenericResourceApiVnftopologyVnfTopology apiVnftopologyVnfTopology =
387                 new GenericResourceApiVnftopologyVnfTopology();
388
389         if (vnfInformation != null) {
390             apiVnftopologyVnfTopology.onapModelInformation(vnfInformation.getOnapModelInformation());
391             apiVnftopologyVnfTopology.vnfTopologyIdentifierStructure(getTopologyIdentifierStructure(vnfInformation));
392         }
393         if (vnfRequestInput != null) {
394             apiVnftopologyVnfTopology.tenant(vnfRequestInput.getTenant());
395             apiVnftopologyVnfTopology.aicClli(vnfRequestInput.getAicClli());
396             apiVnftopologyVnfTopology.aicCloudRegion(vnfRequestInput.getAicCloudRegion());
397         }
398         return apiVnftopologyVnfTopology;
399     }
400
401     private GenericResourceApiVnftopologyidentifierstructureVnfTopologyIdentifierStructure getTopologyIdentifierStructure(
402             @Valid final GenericResourceApiVnfinformationVnfInformation vnfInformation) {
403         return new GenericResourceApiVnftopologyidentifierstructureVnfTopologyIdentifierStructure()
404                 .vnfId(vnfInformation.getVnfId()).vnfName(vnfInformation.getVnfName())
405                 .vnfType(vnfInformation.getVnfType());
406     }
407
408     private GenericResourceApiServicemodelinfrastructureService getServiceItem(
409             final GenericResourceApiServiceOperationInformation input, final String serviceInstanceId) {
410
411         final GenericResourceApiServicedataServiceData apiServicedataServiceData =
412                 new GenericResourceApiServicedataServiceData();
413
414         apiServicedataServiceData.requestInformation(input.getRequestInformation());
415         apiServicedataServiceData.serviceRequestInput(input.getServiceRequestInput());
416         apiServicedataServiceData.serviceInformation(input.getServiceInformation());
417         apiServicedataServiceData.serviceTopology(getServiceTopology(input));
418         apiServicedataServiceData.sdncRequestHeader(input.getSdncRequestHeader());
419         apiServicedataServiceData.serviceLevelOperStatus(getServiceLevelOperStatus(input));
420
421         final GenericResourceApiServicestatusServiceStatus serviceStatus =
422                 getServiceStatus(getSvcAction(input.getSdncRequestHeader()),
423                         getRequestAction(input.getRequestInformation()), HTTP_STATUS_OK);
424
425         return new GenericResourceApiServicemodelinfrastructureService().serviceData(apiServicedataServiceData)
426                 .serviceStatus(serviceStatus).serviceInstanceId(serviceInstanceId);
427     }
428
429     private String getSvcAction(final GenericResourceApiSdncrequestheaderSdncRequestHeader input) {
430         return input != null ? getStringOrNull(input.getSvcAction()) : null;
431     }
432
433     private GenericResourceApiServicestatusServiceStatus getServiceStatus(final String rpcAction, final String action,
434             final String responseCode) {
435         return new GenericResourceApiServicestatusServiceStatus().finalIndicator(YES)
436                 .rpcAction(GenericResourceApiRpcActionEnumeration.fromValue(rpcAction))
437                 .rpcName(SERVICE_TOPOLOGY_OPERATION).responseTimestamp(LocalDateTime.now().toString())
438                 .responseCode(responseCode).requestStatus(SYNCCOMPLETE).responseMessage(EMPTY_STRING).action(action);
439     }
440
441     private GenericResourceApiOperStatusData getServiceLevelOperStatus(
442             final GenericResourceApiServiceOperationInformation input) {
443         return getServiceLevelOperStatus(CREATED, input.getRequestInformation(), input.getSdncRequestHeader());
444     }
445
446     private GenericResourceApiOperStatusData getServiceLevelOperStatus(
447             final GenericResourceApiOrderStatusEnumeration statusEnumeration,
448             final GenericResourceApiRequestinformationRequestInformation requestInformation,
449             final GenericResourceApiSdncrequestheaderSdncRequestHeader sdncRequestHeader) {
450         return new GenericResourceApiOperStatusData().orderStatus(statusEnumeration)
451                 .lastAction(GenericResourceApiLastActionEnumeration.fromValue(getRequestAction(requestInformation)))
452                 .lastRpcAction(GenericResourceApiLastRpcActionEnumeration.fromValue(getSvcAction(sdncRequestHeader)));
453     }
454
455     private String getRequestAction(final GenericResourceApiRequestinformationRequestInformation input) {
456         return getRequestAction(input, EMPTY_STRING);
457     }
458
459     private String getRequestAction(final GenericResourceApiRequestinformationRequestInformation input,
460             final String defaultValue) {
461         return input != null ? getString(input.getRequestAction(), defaultValue) : defaultValue;
462     }
463
464     private GenericResourceApiServicetopologyServiceTopology getServiceTopology(
465             final GenericResourceApiServiceOperationInformation input) {
466         final GenericResourceApiOnapmodelinformationOnapModelInformation modelInformation =
467                 input.getServiceInformation() != null ? input.getServiceInformation().getOnapModelInformation() : null;
468         return new GenericResourceApiServicetopologyServiceTopology().onapModelInformation(modelInformation)
469                 .serviceTopologyIdentifier(getServiceTopologyIdentifier(input));
470     }
471
472     private GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier getServiceTopologyIdentifier(
473             final GenericResourceApiServiceOperationInformation input) {
474         final GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier identifier =
475                 new GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier();
476
477         if (input.getServiceInformation() != null) {
478             final GenericResourceApiServiceinformationServiceInformation serviceInformation =
479                     input.getServiceInformation();
480             identifier.globalCustomerId(serviceInformation.getGlobalCustomerId())
481                     .serviceType(input.getServiceInformation().getSubscriptionServiceType())
482                     .serviceInstanceId(input.getServiceInformation().getServiceInstanceId());;
483         }
484
485         if (input.getServiceRequestInput() != null) {
486             identifier.serviceInstanceName(input.getServiceRequestInput().getServiceInstanceName());
487         }
488
489         return identifier;
490
491     }
492
493     private boolean isServiceOperationInformationNotExists(final String serviceInstanceId,
494             final GenericResourceApiServiceOperationInformation input) {
495         final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
496         final Optional<GenericResourceApiServicemodelinfrastructureService> optional =
497                 getGenericResourceApiServicemodelinfrastructureService(serviceInstanceId);
498
499         if (optional.isPresent()) {
500             final GenericResourceApiServicemodelinfrastructureService existingService = optional.get();
501             final GenericResourceApiServicestatusServiceStatus serviceStatus = existingService.getServiceStatus();
502             if (serviceStatus != null) {
503                 final GenericResourceApiRpcActionEnumeration rpcAction = serviceStatus.getRpcAction();
504                 final String svcAction = getSvcAction(requestHeader);
505                 if (rpcAction != null && rpcAction.toString().equals(svcAction)) {
506                     LOGGER.error("Found Service with id: {} and RpcAction: {} same as SvcAction:  {}",
507                             serviceInstanceId, rpcAction, svcAction);
508                     return false;
509                 }
510
511                 final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
512                 LOGGER.info(
513                         "Deleting existing GenericResourceApiServiceOperationInformation from cache using key: {} as SvcAction is changed from {} to {}",
514                         serviceInstanceId, rpcAction, svcAction);
515                 cache.evict(serviceInstanceId);
516             }
517         }
518         return true;
519
520     }
521
522     @Override
523     public Output putVfModuleOperationInformation(
524             final GenericResourceApiVfModuleOperationInformation input) {
525
526         final GenericResourceApiServiceinformationServiceInformation serviceInformation = input.getServiceInformation();
527         final GenericResourceApiVnfinformationVnfInformation vnfInformation = input.getVnfInformation();
528         final GenericResourceApiVfmoduleinformationVfModuleInformation vfModuleInformation = input.getVfModuleInformation();
529         // Call getVfModule to make a vfList for get the vf-module-information while GET reqest
530         getVfModule(input);
531
532         final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
533         final String svcRequestId = getSvcRequestId(requestHeader);
534
535         if (serviceInformation != null && isValid(serviceInformation.getServiceInstanceId()) && vnfInformation != null
536                 && isValid(vnfInformation.getVnfId()) && vfModuleInformation !=null && isValid(vfModuleInformation.getVfModuleId())) {
537
538
539             final String serviceInstanceId = serviceInformation.getServiceInstanceId();
540             final String vnfId = vnfInformation.getVnfId();
541             final String vfModuleId =  vfModuleInformation.getVfModuleId();
542
543             final Optional<GenericResourceApiServicemodelinfrastructureService> optional =
544                     getGenericResourceApiServicemodelinfrastructureService(serviceInstanceId);
545             if (optional.isPresent()) {
546                 final GenericResourceApiServicemodelinfrastructureService service = optional.get();
547                 final GenericResourceApiServicedataServiceData serviceData = service.getServiceData();
548                 if (serviceData != null) {
549
550                     final GenericResourceApiServicestatusServiceStatus serviceStatus = service.getServiceStatus();
551
552                     return new Output().ackFinalIndicator(serviceStatus.getFinalIndicator())
553                             .responseCode(serviceStatus.getResponseCode())
554                             .responseMessage(serviceStatus.getResponseMessage()).svcRequestId(svcRequestId)
555                             .serviceResponseInformation(new GenericResourceApiInstanceReference()
556                                     .instanceId(serviceInstanceId).objectPath(getObjectPath(serviceInstanceId)))
557                             .vnfResponseInformation(new GenericResourceApiInstanceReference().instanceId(vnfId)
558                                     .objectPath(getObjectPath(serviceInstanceId, vnfId)))
559                             .vfModuleResponseInformation(new GenericResourceApiInstanceReference().instanceId(vfModuleId)
560                                     .objectPath(getObjectPath(vnfId, vfModuleId)));
561                 }
562             }
563             LOGGER.error(
564                     "Unable to find existing GenericResourceApiServiceModelInfrastructure in cache using service instance id: {}",
565                     serviceInstanceId);
566         }
567         LOGGER.error(
568                 "Unable to add GenericResourceApiServiceOperationInformation in cache due to invalid input: {}... ",
569                 input);
570         return new Output().ackFinalIndicator(YES).responseCode(HTTP_STATUS_BAD_REQUEST)
571                 .responseMessage("Unable to add vfModule").svcRequestId(svcRequestId);
572     }
573
574     private void getVfModule
575             (final GenericResourceApiVfModuleOperationInformation input) {
576
577         final GenericResourceApiVfmoduletopologyVfModuleTopology apiVfModuletopologyVfModuleTopology =
578                 new GenericResourceApiVfmoduletopologyVfModuleTopology();
579
580         final GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfmoduleVfModuleData vfModuleData =
581                 new GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfmoduleVfModuleData();
582
583         final String vfModuleId = input.getVfModuleInformation().getVfModuleId();
584
585         vfModuleData.setVfModuleInformation(input.getVfModuleInformation());
586         vfModuleData.setVfModuleRequestInput(input.getVfModuleRequestInput());
587
588         final GenericResourceApiVfmoduletopologyVfModuleTopology vfModuleTopology =
589                 new GenericResourceApiVfmoduletopologyVfModuleTopology();
590
591         vfModuleTopology.setSdncGeneratedCloudResources(true);
592         final GenericResourceApiParam vfModuleParametersData = new GenericResourceApiParam();
593         final List<GenericResourceApiParamParam> params = new ArrayList<GenericResourceApiParamParam>();
594         final GenericResourceApiParamParam param = new GenericResourceApiParamParam();
595         param.setName("k8s-rb-profile-name");
596         param.setValue("k8s-rb-profile-value");
597
598         params.add(param);
599         vfModuleParametersData.setParam(params);
600         vfModuleTopology.setVfModuleParameters(vfModuleParametersData);
601
602         vfModuleTopology.setOnapModelInformation(vfModuleData.getVfModuleInformation().getOnapModelInformation());
603         vfModuleTopology.setVfModuleParameters(vfModuleData.getVfModuleRequestInput().getVfModuleInputParameters());
604         vfModuleTopology.setAicClli(vfModuleData.getVfModuleRequestInput().getAicClli());
605         vfModuleTopology.setAicCloudRegion(vfModuleData.getVfModuleRequestInput().getAicCloudRegion());
606         vfModuleTopology.setCloudOwner(vfModuleData.getVfModuleRequestInput().getCloudOwner());
607
608         apiVfModuletopologyVfModuleTopology.vfModuleTopologyIdentifier(getVfModuleTopologyIdentifierStructure(input));
609
610         vfModuleTopology.setVfModuleTopologyIdentifier(apiVfModuletopologyVfModuleTopology.getVfModuleTopologyIdentifier());
611         vfModuleTopology.setTenant(vfModuleData.getVfModuleRequestInput().getTenant());
612
613         final GenericResourceApiVfModuleTopology genericResourceApiVfModuleTopology = new GenericResourceApiVfModuleTopology();
614         genericResourceApiVfModuleTopology.setVfModuleTopology(vfModuleTopology);
615
616         final Cache cache = getCache(Constants.SERVICE_TOPOLOGY_OPERATION_CACHE);
617         cache.put(vfModuleId, genericResourceApiVfModuleTopology);
618     }
619
620     @Override
621     public Optional<GenericResourceApiVfModuleTopology> getGenericResourceApiVfModuleTopology(final String vfModueId) {
622         LOGGER.info("getting GenericResourceApiVfModuleTopology from cache using key: {}", vfModueId);
623         final Cache cache = getCache(Constants.SERVICE_TOPOLOGY_OPERATION_CACHE);
624         final GenericResourceApiVfModuleTopology value =
625                 cache.get(vfModueId, GenericResourceApiVfModuleTopology.class);
626         if (value != null) {
627             return Optional.of(value);
628         }
629         LOGGER.error("Unable to find GenericResourceApiVfModuleTopology ...");
630         return Optional.empty();
631     }
632
633     private GenericResourceApiVfmoduletopologyidentifierVfModuleTopologyIdentifier getVfModuleTopologyIdentifierStructure(
634             @Valid final GenericResourceApiVfModuleOperationInformation input) {
635
636         final GenericResourceApiVfmoduleinformationVfModuleInformation vfModuleInformation = input.getVfModuleInformation();
637         return new GenericResourceApiVfmoduletopologyidentifierVfModuleTopologyIdentifier()
638                 .vfModuleId(vfModuleInformation.getVfModuleId()).vfModuleType(vfModuleInformation.getVfModuleType()).vfModuleName(input.getVfModuleRequestInput().getVfModuleName());
639     }
640
641
642     public void setVnfsData(List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList) {
643
644         final GenericResourceApiVnftopologyVnfTopology vnfTopology = new GenericResourceApiVnftopologyVnfTopology();
645         LOGGER.info(String.valueOf(vnfsList));
646         final String vnfId = vnfsList.get(0).getVnfId();
647         vnfTopology.setOnapModelInformation(vnfsList.get(0).getVnfData().getVnfInformation().getOnapModelInformation());
648         vnfTopology.setAicClli(String.valueOf(vnfsList.get(0).getVnfData().getVnfRequestInput().getAicClli()));
649         vnfTopology.setAicCloudRegion(String.valueOf(vnfsList.get(0).getVnfData().getVnfRequestInput().getAicCloudRegion()));
650         vnfTopology.setCloudOwner(String.valueOf(vnfsList.get(0).getVnfData().getVnfRequestInput().getCloudOwner()));
651         vnfTopology.setTenant(String.valueOf(vnfsList.get(0).getVnfData().getVnfRequestInput().getTenant()));
652         vnfTopology.setVnfResourceAssignments(vnfsList.get(0).getVnfData().getVnfTopology().getVnfResourceAssignments());
653         vnfTopology.setVnfTopologyIdentifierStructure(vnfsList.get(0).getVnfData().getVnfTopology().getVnfTopologyIdentifierStructure());
654         vnfTopology.setVnfParametersData(vnfsList.get(0).getVnfData().getVnfTopology().getVnfParametersData());
655         vnfTopology.setSdncGeneratedCloudResources(vnfsList.get(0).getVnfData().getVnfTopology().getSdncGeneratedCloudResources());
656
657         final GenericResourceApiVnfTopology genericResourceApiVnfTopology = new GenericResourceApiVnfTopology();
658         genericResourceApiVnfTopology.setVnfTopology(vnfTopology);
659
660         final Cache cache = getCache(Constants.SERVICE_TOPOLOGY_OPERATION_CACHE);
661         cache.put(vnfId, genericResourceApiVnfTopology);
662     }
663
664     @Override
665     public Optional<GenericResourceApiVnfTopology> getGenericResourceApiVnfTopology(final String vnfId) {
666         LOGGER.info("getting GenericResourceApiVnfTopology from cache using key: {}", vnfId);
667         final Cache cache = getCache(Constants.SERVICE_TOPOLOGY_OPERATION_CACHE);
668         final GenericResourceApiVnfTopology value =
669                 cache.get(vnfId, GenericResourceApiVnfTopology.class);
670         if (value != null) {
671             return Optional.of(value);
672         }
673         LOGGER.error("Unable to find GenericResourceApiVnfTopology ...");
674         return Optional.empty();
675     }
676 }