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