diff options
author | 2021-03-26 09:57:35 -0400 | |
---|---|---|
committer | 2021-03-26 09:57:35 -0400 | |
commit | 3e68599d1433c0200d20332c6bda3b2cfcf2dd93 (patch) | |
tree | a4b78bf4ae416e41215751d8e6ab2cfc36e33064 /ms/generic-resource-api/src/main/java/org/onap | |
parent | 32bcdd700561be5457bbc0658fc27a87e1419f36 (diff) |
Refactor CRUD to new table structure
Refactor operations on config tree to use new table structure
for service/network/vnf/vf-module data.
Change-Id: I2e673dfb7bd335e48a3bcf212399d021d37dd9ee
Issue-ID: SDNC-1481
Signed-off-by: Dan Timoney <dtimoney@att.com>
Diffstat (limited to 'ms/generic-resource-api/src/main/java/org/onap')
4 files changed, 871 insertions, 810 deletions
diff --git a/ms/generic-resource-api/src/main/java/org/onap/sdnc/apps/ms/gra/controllers/ConfigApiController.java b/ms/generic-resource-api/src/main/java/org/onap/sdnc/apps/ms/gra/controllers/ConfigApiController.java index 2cef1a0..7508eb8 100644 --- a/ms/generic-resource-api/src/main/java/org/onap/sdnc/apps/ms/gra/controllers/ConfigApiController.java +++ b/ms/generic-resource-api/src/main/java/org/onap/sdnc/apps/ms/gra/controllers/ConfigApiController.java @@ -20,24 +20,61 @@ package org.onap.sdnc.apps.ms.gra.controllers; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Optional; + +import javax.servlet.http.HttpServletRequest; +import javax.validation.Valid; + import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; + import org.onap.ccsdk.apps.services.RestApplicationException; import org.onap.ccsdk.apps.services.RestException; -import org.onap.ccsdk.apps.services.RestProtocolError; import org.onap.ccsdk.apps.services.RestProtocolException; +import org.onap.sdnc.apps.ms.gra.controllers.ServiceDataHelper.ServiceDataTransaction; +import org.onap.sdnc.apps.ms.gra.data.ConfigContrailRouteAllottedResources; +import org.onap.sdnc.apps.ms.gra.data.ConfigContrailRouteAllottedResourcesRepository; +import org.onap.sdnc.apps.ms.gra.data.ConfigNetworksRepository; +import org.onap.sdnc.apps.ms.gra.data.ConfigPortMirrorConfigurations; +import org.onap.sdnc.apps.ms.gra.data.ConfigPortMirrorConfigurationsRepository; import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadData; import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadDataRepository; import org.onap.sdnc.apps.ms.gra.data.ConfigServices; import org.onap.sdnc.apps.ms.gra.data.ConfigServicesRepository; -import org.onap.sdnc.apps.ms.gra.data.ConfigPortMirrorConfigurations; -import org.onap.sdnc.apps.ms.gra.data.ConfigPortMirrorConfigurationsRepository; -import org.onap.sdnc.apps.ms.gra.data.ConfigContrailRouteAllottedResources; -import org.onap.sdnc.apps.ms.gra.data.ConfigContrailRouteAllottedResourcesRepository; +import org.onap.sdnc.apps.ms.gra.data.ConfigVfModules; +import org.onap.sdnc.apps.ms.gra.data.ConfigVfModulesRepository; +import org.onap.sdnc.apps.ms.gra.data.ConfigVnfsRepository; import org.onap.sdnc.apps.ms.gra.swagger.ConfigApi; -import org.onap.sdnc.apps.ms.gra.swagger.model.*; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiContrailrouteallottedresourcesContrailRouteAllottedResource; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiContrailrouteallottedresourcesContrailrouteallottedresourceAllottedResourceData; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiContrailroutetopologyContrailRouteTopology; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiOnapmodelinformationOnapModelInformation; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiOperStatusData; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiPortmirrorconfigurationsPortMirrorConfiguration; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiPortmirrorconfigurationsPortmirrorconfigurationConfigurationData; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiPortmirrorconfigurationtopologyPortMirrorConfigurationTopology; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiPreloadModelInformation; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiPreloaddataPreloadData; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiPreloadmodelinformationPreloadList; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiPreloadnetworktopologyinformationPreloadNetworkTopologyInformation; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServiceModelInfrastructure; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServiceData; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataVnfsVnf; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataVnfsVnfVnfData; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicemodelinfrastructureService; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicestatusServiceStatus; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicetopologyServiceTopology; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiVfmoduletopologyVfModuleTopology; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiVnfNetworkData; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiVnfresourceassignmentsVnfResourceAssignments; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworks; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiVnftopologyVnfTopology; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -47,17 +84,6 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; -import javax.servlet.http.HttpServletRequest; -import javax.validation.Valid; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Optional; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.stream.Collectors; -import java.util.stream.Stream; - @Controller @ComponentScan(basePackages = { "org.onap.sdnc.apps.ms.gra.*" }) @EntityScan("org.onap.sdnc.apps.ms.gra.springboot.*") @@ -75,11 +101,22 @@ public class ConfigApiController implements ConfigApi { private ConfigServicesRepository configServicesRepository; @Autowired + private ConfigNetworksRepository configNetworksRepository; + + @Autowired + private ConfigVnfsRepository configVnfsRepository; + + @Autowired + private ConfigVfModulesRepository configVfModulesRepository; + + @Autowired private ConfigPortMirrorConfigurationsRepository configPortMirrorConfigurationsRepository; @Autowired private ConfigContrailRouteAllottedResourcesRepository configContrailRouteAllottedResourcesRepository; - + + @Autowired + private ServiceDataHelper serviceDataHelper; @Autowired public ConfigApiController(ObjectMapper objectMapper, HttpServletRequest request) { @@ -667,8 +704,7 @@ public class ConfigApiController implements ConfigApi { public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypePreloadDataPost( String preloadId, String preloadType, @Valid GenericResourceApiPreloaddataPreloadData preloadData) throws RestApplicationException, RestProtocolException { - List<ConfigPreloadData> preloadDataEntries = configPreloadDataRepository - .findByPreloadIdAndPreloadType(preloadId, preloadType); + List<ConfigPreloadData> preloadDataItems = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType); @@ -743,6 +779,9 @@ public class ConfigApiController implements ConfigApi { @Override public ResponseEntity<Void> configGENERICRESOURCEAPIservicesDelete() { + configVfModulesRepository.deleteAll(); + configVnfsRepository.deleteAll(); + configNetworksRepository.deleteAll(); configServicesRepository.deleteAll(); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } @@ -760,20 +799,26 @@ public class ConfigApiController implements ConfigApi { for (ConfigServices service : configServicesRepository.findAll()) { GenericResourceApiServicemodelinfrastructureService serviceItem = new GenericResourceApiServicemodelinfrastructureService(); - serviceItem.setServiceInstanceId(service.getSvcInstanceId()); - if (service.getSvcData() != null) { - try { - serviceItem.setServiceData(objectMapper.readValue(service.getSvcData(), - GenericResourceApiServicedataServiceData.class)); - } catch (JsonProcessingException e) { - log.error("Could not deserialize service data for {}", service.getSvcInstanceId(), e); - throw new RestApplicationException("data-conversion", - "Request could not be completed due to internal error", e, - HttpStatus.INTERNAL_SERVER_ERROR.value()); + String svcInstanceId = service.getSvcInstanceId(); - } - } + serviceItem.setServiceInstanceId(svcInstanceId); serviceItem.setServiceStatus(service.getServiceStatus()); + + // Construct service data from networks/vnfs/vfModules + GenericResourceApiServicedataServiceData serviceItemSvcData; + try { + serviceItemSvcData = serviceDataHelper.getServiceData(service); + } catch (JsonProcessingException e) { + log.error("Could not deserialize service data for {}", service.getSvcInstanceId(), e); + throw new RestApplicationException("data-conversion", + "Request could not be completed due to internal error", e, + HttpStatus.INTERNAL_SERVER_ERROR.value()); + } + + + if (serviceItemSvcData != null) { + serviceItem.setServiceData(serviceItemSvcData); + } modelInfrastructure.addServiceItem(serviceItem); } @@ -782,10 +827,10 @@ public class ConfigApiController implements ConfigApi { } @Override - public ResponseEntity<Void> configGENERICRESOURCEAPIservicesPost( - @Valid GenericResourceApiServiceModelInfrastructure modelInfrastructure) + public ResponseEntity<Void> configGENERICRESOURCEAPIservicesPost(@Valid GenericResourceApiServiceModelInfrastructure modelInfrastructure) throws RestApplicationException, RestProtocolException { - List<ConfigServices> newServices = new LinkedList<>(); + + ServiceDataTransaction transaction = serviceDataHelper.createTransaction(); for (GenericResourceApiServicemodelinfrastructureService serviceItem : modelInfrastructure.getService()) { String svcInstanceId = serviceItem.getServiceInstanceId(); @@ -797,8 +842,10 @@ public class ConfigApiController implements ConfigApi { } ConfigServices service = new ConfigServices(); service.setSvcInstanceId(svcInstanceId); + service.setServiceStatus(serviceItem.getServiceStatus()); + try { - service.setSvcData(objectMapper.writeValueAsString(serviceItem.getServiceData())); + serviceDataHelper.saveService(service, serviceItem.getServiceData(), transaction); } catch (JsonProcessingException e) { log.error("Could not serialize service data for {}", service.getSvcInstanceId(), e); throw new RestApplicationException("data-conversion", @@ -806,13 +853,9 @@ public class ConfigApiController implements ConfigApi { HttpStatus.INTERNAL_SERVER_ERROR.value()); } - service.setServiceStatus(serviceItem.getServiceStatus()); - newServices.add(service); } - for (ConfigServices service : newServices) { - configServicesRepository.save(service); - } + transaction.commit(); return new ResponseEntity<>(HttpStatus.CREATED); @@ -822,7 +865,7 @@ public class ConfigApiController implements ConfigApi { public ResponseEntity<Void> configGENERICRESOURCEAPIservicesPut( @Valid GenericResourceApiServiceModelInfrastructure modelInfrastructure) throws RestApplicationException { - List<ConfigServices> newServices = new LinkedList<>(); + ServiceDataTransaction transaction = serviceDataHelper.createTransaction(); boolean dataExists = false; for (GenericResourceApiServicemodelinfrastructureService serviceItem : modelInfrastructure.getService()) { @@ -833,8 +876,9 @@ public class ConfigApiController implements ConfigApi { } ConfigServices service = new ConfigServices(); service.setSvcInstanceId(svcInstanceId); + service.setServiceStatus(serviceItem.getServiceStatus()); try { - service.setSvcData(objectMapper.writeValueAsString(serviceItem.getServiceData())); + serviceDataHelper.saveService(service, serviceItem.getServiceData(), transaction); } catch (JsonProcessingException e) { log.error("Could not serialize service data for {}", service.getSvcInstanceId(), e); throw new RestApplicationException("data-conversion", @@ -842,13 +886,9 @@ public class ConfigApiController implements ConfigApi { HttpStatus.INTERNAL_SERVER_ERROR.value()); } - service.setServiceStatus(serviceItem.getServiceStatus()); - newServices.add(service); } - for (ConfigServices service : newServices) { - configServicesRepository.save(service); - } + transaction.commit(); if (dataExists) { return new ResponseEntity<>(HttpStatus.NO_CONTENT); @@ -863,10 +903,8 @@ public class ConfigApiController implements ConfigApi { @Valid GenericResourceApiServicemodelinfrastructureService servicesData) throws RestApplicationException { String svcInstanceId = servicesData.getServiceInstanceId(); try { - String svcData = objectMapper.writeValueAsString(servicesData.getServiceData()); - ConfigServices configService = new ConfigServices(svcInstanceId, svcData, servicesData.getServiceStatus()); - configServicesRepository.deleteBySvcInstanceId(svcInstanceId); - configServicesRepository.save(configService); + ConfigServices configService = new ConfigServices(svcInstanceId, servicesData.getServiceStatus()); + serviceDataHelper.saveService(configService, servicesData.getServiceData()); } catch (JsonProcessingException e) { log.error("Cannot convert service data", e); throw new RestApplicationException("data-conversion", @@ -880,7 +918,7 @@ public class ConfigApiController implements ConfigApi { @Override public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdDelete( String serviceInstanceId) { - configServicesRepository.deleteBySvcInstanceId(serviceInstanceId); + serviceDataHelper.deleteService(serviceInstanceId); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } @@ -899,8 +937,7 @@ public class ConfigApiController implements ConfigApi { retval.setServiceInstanceId(serviceInstanceId); retval.setServiceStatus(service.getServiceStatus()); try { - retval.setServiceData( - objectMapper.readValue(service.getSvcData(), GenericResourceApiServicedataServiceData.class)); + retval.setServiceData(serviceDataHelper.getServiceData(service)); } catch (JsonProcessingException e) { log.error("Could not deserialize service data for service instance id {}", serviceInstanceId, e); throw new RestApplicationException("data-conversion", @@ -927,8 +964,9 @@ public class ConfigApiController implements ConfigApi { } ConfigServices service = new ConfigServices(); service.setSvcInstanceId(svcInstanceId); + service.setServiceStatus(newService.getServiceStatus()); try { - service.setSvcData(objectMapper.writeValueAsString(newService.getServiceData())); + serviceDataHelper.saveService(service, newService.getServiceData()); } catch (JsonProcessingException e) { log.error("Could not serialize service data for {}", service.getSvcInstanceId(), e); throw new RestApplicationException("data-conversion", @@ -936,8 +974,6 @@ public class ConfigApiController implements ConfigApi { HttpStatus.INTERNAL_SERVER_ERROR.value()); } - service.setServiceStatus(newService.getServiceStatus()); - configServicesRepository.save(service); return new ResponseEntity<>(HttpStatus.CREATED); } @@ -960,8 +996,9 @@ public class ConfigApiController implements ConfigApi { service.setSvcInstanceId(svcInstanceId); } + service.setServiceStatus(newService.getServiceStatus()); try { - service.setSvcData(objectMapper.writeValueAsString(newService.getServiceData())); + serviceDataHelper.saveService(service, newService.getServiceData()); } catch (JsonProcessingException e) { log.error("Could not serialize service data for {}", service.getSvcInstanceId(), e); throw new RestApplicationException("data-conversion", @@ -969,8 +1006,6 @@ public class ConfigApiController implements ConfigApi { HttpStatus.INTERNAL_SERVER_ERROR.value()); } - service.setServiceStatus(newService.getServiceStatus()); - configServicesRepository.save(service); if (dataExists) { return new ResponseEntity<>(HttpStatus.NO_CONTENT); @@ -989,11 +1024,10 @@ public class ConfigApiController implements ConfigApi { } ConfigServices service = services.get(0); - if (service.getSvcData() == null) { + if (!serviceDataHelper.hasServiceData(service)) { throw new RestProtocolException("data-missing", "No service-data found", HttpStatus.NOT_FOUND.value()); } - service.setSvcData(null); - configServicesRepository.save(service); + serviceDataHelper.clearServiceData(service); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } @@ -1009,8 +1043,7 @@ public class ConfigApiController implements ConfigApi { } try { - serviceData = objectMapper.readValue(services.get(0).getSvcData(), - GenericResourceApiServicedataServiceData.class); + serviceData = serviceDataHelper.getServiceData(services.get(0)); return new ResponseEntity<>(serviceData, HttpStatus.OK); } catch (JsonProcessingException e) { log.error("Could not parse service data", e); @@ -1038,15 +1071,14 @@ public class ConfigApiController implements ConfigApi { } service = services.get(0); - if ((service.getSvcData() != null) && (service.getSvcData().length() > 0)) { + if (serviceDataHelper.hasServiceData(service)) { log.error("service-data already exists for svcInstanceId {}", serviceInstanceId); throw new RestProtocolException("data-exists", "Data already exists for " + serviceInstanceId, HttpStatus.CONFLICT.value()); } try { - service.setSvcData(objectMapper.writeValueAsString(serviceData)); - configServicesRepository.save(service); + serviceDataHelper.saveService(service, serviceData); } catch (JsonProcessingException e) { log.error("Could not serialize service data for svc instance id {}", serviceInstanceId, e); throw new RestApplicationException("data-conversion", @@ -1077,13 +1109,10 @@ public class ConfigApiController implements ConfigApi { } service = services.get(0); - if ((service.getSvcData() != null) && (service.getSvcData().length() > 0)) { - dataExists = true; - } + dataExists = serviceDataHelper.hasServiceData(service); try { - service.setSvcData(objectMapper.writeValueAsString(serviceData)); - configServicesRepository.save(service); + serviceDataHelper.saveService(service, serviceData); } catch (JsonProcessingException e) { log.error("Could not serialize service data for svc instance id {}", serviceInstanceId, e); throw new RestApplicationException("data-conversion", @@ -1217,17 +1246,6 @@ public class ConfigApiController implements ConfigApi { String serviceInstanceId, String vnfId) throws RestException { log.info("DELETE | VNF Data for ({})", vnfId); - /* - * The logic may need to be moved inside of this check or this check may need to - * be removed. - */ - if (getObjectMapper().isPresent() && getAcceptHeader().isPresent()) { - log.info("Something with header."); - } else { - log.warn( - "ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated"); - } - List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId); ConfigServices data; if ((services == null) || (services.isEmpty())) { @@ -1239,49 +1257,18 @@ public class ConfigApiController implements ConfigApi { data = services.get(0); } - GenericResourceApiServicedataServiceData svcData; - try { - svcData = objectMapper.readValue(data.getSvcData(), GenericResourceApiServicedataServiceData.class); - } catch (JsonProcessingException e) { - // Or throw the data not found error? - log.error("Could not map service data for ({})", serviceInstanceId); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } - if (svcData == null) { - // Or throw the data not found error? + if (!serviceDataHelper.hasServiceData(data)) { log.info("Could not find Service Data for ({}).", serviceInstanceId); throw new RestProtocolException("data-missing", "Service data not found.", HttpStatus.NOT_FOUND.value()); } - GenericResourceApiServicedataServicedataVnfs vnfs = svcData.getVnfs(); - if (vnfs == null) { - // Or throw the data not found error? - log.info("VNF List not found for ({}).", serviceInstanceId); - throw new RestProtocolException("data-missing", "VNFs not found.", HttpStatus.NOT_FOUND.value()); - } - - Stream<GenericResourceApiServicedataServicedataVnfsVnf> vnfStream = svcData.getVnfs().getVnf().stream(); - if (vnfStream.noneMatch(targetVnf -> targetVnf.getVnfId().equals(vnfId))) { + if (serviceDataHelper.deleteVnf(serviceInstanceId, vnfId) > 0) { + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } else { // Data was not found log.error("Did not find VNF ({}) in data.", vnfId); throw new RestProtocolException("data-missing", "VNF ID not found.", HttpStatus.NOT_FOUND.value()); } - // Recreate the stream per Sonar? - vnfStream = svcData.getVnfs().getVnf().stream(); - svcData.getVnfs().setVnf( - vnfStream.filter(targetVnf -> !targetVnf.getVnfId().equals(vnfId)).collect(Collectors.toList())); - - // Map and save the new data - try { - data.setSvcData(objectMapper.writeValueAsString(svcData)); - configServicesRepository.save(data); - return new ResponseEntity<>(HttpStatus.NO_CONTENT); - } catch (JsonProcessingException e) { - log.error("Error mapping object to JSON", e); - // Should probably be a 500 INTERNAL_SERVICE_ERROR - throw new RestProtocolException("internal-service-error", "Failed to save data.", - HttpStatus.BAD_REQUEST.value()); - } } /** @@ -1299,21 +1286,24 @@ public class ConfigApiController implements ConfigApi { public ResponseEntity<GenericResourceApiServicedataServicedataVnfsVnf> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdGet( String serviceInstanceId, String vnfId) throws RestException { log.info("GET | VNF Data for ({})", vnfId); - if (getObjectMapper().isPresent() && getAcceptHeader().isPresent()) { - if (getAcceptHeader().get().contains("application/json")) { - } - } else { - log.warn( - "ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated"); - } + List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId); if ((services == null) || (services.isEmpty())) { throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value()); } - Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnf = getVnfObject(services.get(0), vnfId); - if (vnf.isPresent()) { - return new ResponseEntity<>(vnf.get(), HttpStatus.OK); + GenericResourceApiServicedataServicedataVnfsVnf vnf = null; + try { + vnf = serviceDataHelper.getVnf(serviceInstanceId, vnfId); + } catch (JsonProcessingException e) { + log.error("Could not parse service data", e); + throw new RestApplicationException("data-conversion", + "Request could not be completed due to internal error", e, + HttpStatus.INTERNAL_SERVER_ERROR.value()); + } + + if (vnf != null) { + return new ResponseEntity<>(vnf, HttpStatus.OK); } else { log.info("No information found for {}", vnfId); throw new RestApplicationException("data-missing", @@ -1323,30 +1313,6 @@ public class ConfigApiController implements ConfigApi { } /** - * Extracts a vf-module object from the database, - * @param configServices A Config Services option created from a Service - * Instance ID - * @param vnfId the target VNF ID - * @param vfModuleId the target vf-module ID - * @return An empty Optional if the Service Data does not exist, an empty - * Optional if the VNF is not found, or an optional containing the - * found VNF. - */ - private Optional<GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule> getVfModuleObject ( - ConfigServices configServices, String vnfId, String vfModuleId) { - // Map the Marshall the JSON String into a Java Object - log.info("Getting vf-module Data for ({})", vfModuleId); - - Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnf = getVnfObject(configServices, vnfId); - GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData = vnf.get().getVnfData(); - - return vnfData.getVfModules().getVfModule() - .stream() - .filter(vf -> vf.getVfModuleId().equals(vfModuleId)) - .findFirst(); - } - - /** * Creates or updates VNF data in the Config table for a specified Service * Instance. If it is a new Service Instance or a new VNF, creates all necessary * parent data containers, then performs the updates. @@ -1366,68 +1332,35 @@ public class ConfigApiController implements ConfigApi { @Override public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdPut( String serviceInstanceId, String vnfId, - GenericResourceApiServicedataServicedataVnfsVnf genericResourceApiServicedataServicedataVnfsVnfBodyParam) + GenericResourceApiServicedataServicedataVnfsVnf vnf) throws RestException { log.info("PUT | VNF Data for ({})", vnfId); - if (!vnfId.equals(genericResourceApiServicedataServicedataVnfsVnfBodyParam.getVnfId())) { + if (!vnfId.equals(vnf.getVnfId())) { throw new RestProtocolException("bad-attribute", "vnf-id mismatch", HttpStatus.BAD_REQUEST.value()); } - if (getObjectMapper().isPresent() && getAcceptHeader().isPresent()) { - log.info("Something with header"); - } else { - log.warn( - "ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated"); - } List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId); ConfigServices data; + ServiceDataTransaction transaction = serviceDataHelper.createTransaction(); if ((services == null) || (services.isEmpty())) { log.info("Could not find data for ({}). Creating new Service Object.", serviceInstanceId); data = new ConfigServices(); data.setSvcInstanceId(serviceInstanceId); + transaction.save(data); } else { data = services.get(0); } - GenericResourceApiServicedataServiceData svcData = null; - try { - svcData = objectMapper.readValue(data.getSvcData(), GenericResourceApiServicedataServiceData.class); - } catch (JsonProcessingException e) { - log.error("Could not map service data for ({})", serviceInstanceId); - } - if (svcData == null) { - log.info("Could not find Service Data for ({}). Creating new Service Data Container", serviceInstanceId); - svcData = new GenericResourceApiServicedataServiceData(); - } - if (svcData.getVnfs() == null) { - log.info("VNF List not found for ({}). Creating new VNF List Container.", serviceInstanceId); - svcData.setVnfs(new GenericResourceApiServicedataServicedataVnfs()); - svcData.getVnfs().setVnf(new ArrayList<>()); - } - - GenericResourceApiServicedataServicedataVnfs vnflist = new GenericResourceApiServicedataServicedataVnfs(); - HttpStatus responseStatus = HttpStatus.NO_CONTENT; - if (svcData.getVnfs().getVnf().isEmpty()) { - log.info("Creating VNF data for ({})", vnfId); - vnflist.addVnfItem(genericResourceApiServicedataServicedataVnfsVnfBodyParam); - responseStatus = HttpStatus.CREATED; - } else { - log.info("Updating VNF data for ({})", vnfId); - // Filter out all of the other vnf objects into a new VNF List - // Replace if a delete method exists - svcData.getVnfs().getVnf().stream().filter(targetVnf -> !targetVnf.getVnfId().equals(vnfId)) - .forEach(vnflist::addVnfItem); - vnflist.addVnfItem(genericResourceApiServicedataServicedataVnfsVnfBodyParam); - } - svcData.setVnfs(vnflist); - // Map and save the new data try { - data.setSvcData(objectMapper.writeValueAsString(svcData)); - configServicesRepository.save(data); + HttpStatus responseStatus = HttpStatus.NO_CONTENT; + if (serviceDataHelper.getVnf(serviceInstanceId, vnfId) == null) { + responseStatus = HttpStatus.CREATED; + } + serviceDataHelper.saveVnf(serviceInstanceId, vnf, transaction); + transaction.commit(); return new ResponseEntity<>(responseStatus); } catch (JsonProcessingException e) { - log.error("Error mapping object to JSON", e); - // Should probably be a 500 INTERNAL_SERVICE_ERROR + log.error("Error saving vnf", e); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } } @@ -1448,23 +1381,26 @@ public class ConfigApiController implements ConfigApi { public ResponseEntity<GenericResourceApiVnftopologyVnfTopology> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfTopologyGet( String serviceInstanceId, String vnfId) throws RestException { log.info("GET | VNF Topology for ({})", vnfId); - if (getObjectMapper().isPresent() && getAcceptHeader().isPresent()) { - if (getAcceptHeader().get().contains("application/json")) { - } - } else { - log.warn( - "ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated"); - } List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId); if ((services == null) || (services.isEmpty())) { throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value()); } - Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnf = getVnfObject(services.get(0), vnfId); + GenericResourceApiServicedataServicedataVnfsVnf vnf = null; + + try { + vnf = serviceDataHelper.getVnf(serviceInstanceId, vnfId); + } catch (JsonProcessingException e) { + log.error("Could not parse service data", e); + throw new RestApplicationException("data-conversion", + "Request could not be completed due to internal error", e, + HttpStatus.INTERNAL_SERVER_ERROR.value()); + } + // Drill down to find the data - if (vnf.isPresent() && vnf.get().getVnfData() != null && vnf.get().getVnfData().getVnfTopology() != null) { - return new ResponseEntity<>(vnf.get().getVnfData().getVnfTopology(), HttpStatus.OK); + if (vnf != null && vnf.getVnfData() != null && vnf.getVnfData().getVnfTopology() != null) { + return new ResponseEntity<>(vnf.getVnfData().getVnfTopology(), HttpStatus.OK); } else { log.info("No information found for {}", vnfId); throw new RestApplicationException("data-missing", @@ -1498,11 +1434,7 @@ public class ConfigApiController implements ConfigApi { String serviceInstanceId, String vnfId, GenericResourceApiOperStatusData genericResourceApiOperStatusDataBodyParam) throws RestException { log.info("PUT | VNF Level Oper Status ({})", vnfId); - if (getObjectMapper().isPresent() && getAcceptHeader().isPresent()) { - } else { - log.warn( - "ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated"); - } + ServiceDataTransaction transaction = serviceDataHelper.createTransaction(); List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId); ConfigServices data; @@ -1510,66 +1442,31 @@ public class ConfigApiController implements ConfigApi { log.info("Could not find data for ({}). Creating new Service Object.", serviceInstanceId); data = new ConfigServices(); data.setSvcInstanceId(serviceInstanceId); + transaction.save(data); } else { data = services.get(0); } - GenericResourceApiServicedataServiceData svcData = null; - try { - svcData = objectMapper.readValue(data.getSvcData(), GenericResourceApiServicedataServiceData.class); - } catch (JsonProcessingException e) { - log.error("Could not map service data for ({})", serviceInstanceId); - } - if (svcData == null) { - log.info("Could not find Service Data for ({}). Creating new Service Data Container", serviceInstanceId); - svcData = new GenericResourceApiServicedataServiceData(); - } - if (svcData.getVnfs() == null) { - log.info("VNF List not found for ({}). Creating new VNF List Container.", serviceInstanceId); - svcData.setVnfs(new GenericResourceApiServicedataServicedataVnfs()); - svcData.getVnfs().setVnf(new ArrayList<>()); - } - - GenericResourceApiServicedataServicedataVnfs vnflist = new GenericResourceApiServicedataServicedataVnfs(); HttpStatus responseStatus = HttpStatus.NO_CONTENT; - if (svcData.getVnfs().getVnf().isEmpty()) { - log.info("Creating VNF data for ({})", vnfId); - GenericResourceApiServicedataServicedataVnfsVnf vnf = new GenericResourceApiServicedataServicedataVnfsVnf(); - vnf.setVnfId(vnfId); - vnf.setVnfData(new GenericResourceApiServicedataServicedataVnfsVnfVnfData()); - vnf.getVnfData().setVnfLevelOperStatus(genericResourceApiOperStatusDataBodyParam); - vnflist.addVnfItem(vnf); - responseStatus = HttpStatus.CREATED; - } else { - log.info("Updating VNF data for ({})", vnfId); - // Filter out all of the other vnf objects into a new VNF List - // Replace if a delete method exists - svcData.getVnfs().getVnf().stream().filter(targetVnf -> !targetVnf.getVnfId().equals(vnfId)) - .forEach(vnflist::addVnfItem); - GenericResourceApiServicedataServicedataVnfsVnf vnf = new GenericResourceApiServicedataServicedataVnfsVnf(); - // If the vnf exists, set it up with new data - Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnfOptional = getVnfObject(data, vnfId); - if (vnfOptional.isPresent()) { - vnf = vnfOptional.get(); - } - if (vnf.getVnfData() == null) { - vnf.setVnfData(new GenericResourceApiServicedataServicedataVnfsVnfVnfData()); - responseStatus = HttpStatus.CREATED; - } - - vnf.getVnfData().setVnfLevelOperStatus(genericResourceApiOperStatusDataBodyParam); - vnflist.addVnfItem(vnf); - } - svcData.setVnfs(vnflist); - // Map and save the new data try { - data.setSvcData(objectMapper.writeValueAsString(svcData)); - configServicesRepository.save(data); + GenericResourceApiServicedataServicedataVnfsVnf vnf = serviceDataHelper.getVnf(serviceInstanceId, vnfId); + if (vnf == null) { + responseStatus = HttpStatus.CREATED; + vnf = new GenericResourceApiServicedataServicedataVnfsVnf(); + vnf.setVnfId(vnfId); + } + GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData = vnf.getVnfData(); + if (vnfData == null) { + vnfData = new GenericResourceApiServicedataServicedataVnfsVnfVnfData(); + } + vnfData.setVnfLevelOperStatus(genericResourceApiOperStatusDataBodyParam); + vnf.setVnfData(vnfData); + serviceDataHelper.saveVnf(serviceInstanceId, vnf, transaction); + transaction.commit(); return new ResponseEntity<>(responseStatus); } catch (JsonProcessingException e) { - log.error("Error mapping object to JSON", e); - // Should probably be a 500 INTERNAL_SERVICE_ERROR + log.error("Error saving vnf", e); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } } @@ -1614,11 +1511,7 @@ public class ConfigApiController implements ConfigApi { GenericResourceApiOnapmodelinformationOnapModelInformation genericResourceApiOnapmodelinformationOnapModelInformationBodyParam) throws RestException { log.info("PUT | VNF Topology Onap Model Information ({})", vnfId); - if (getObjectMapper().isPresent() && getAcceptHeader().isPresent()) { - } else { - log.warn( - "ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated"); - } + ServiceDataTransaction transaction = serviceDataHelper.createTransaction(); List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId); ConfigServices data; @@ -1626,68 +1519,40 @@ public class ConfigApiController implements ConfigApi { log.info("Could not find data for ({}). Creating new Service Object.", serviceInstanceId); data = new ConfigServices(); data.setSvcInstanceId(serviceInstanceId); + transaction.save(data); } else { data = services.get(0); } - GenericResourceApiServicedataServiceData svcData = null; + + GenericResourceApiServicedataServicedataVnfsVnf vnf = null; + HttpStatus responseStatus = HttpStatus.NO_CONTENT; + try { - svcData = objectMapper.readValue(data.getSvcData(), GenericResourceApiServicedataServiceData.class); - } catch (JsonProcessingException e) { - log.error("Could not map service data for ({})", serviceInstanceId); - } - if (svcData == null) { - log.info("Could not find Service Data for ({}). Creating new Service Data Container", serviceInstanceId); - svcData = new GenericResourceApiServicedataServiceData(); - } - if (svcData.getVnfs() == null) { - log.info("VNF List not found for ({}). Creating new VNF List Container.", serviceInstanceId); - svcData.setVnfs(new GenericResourceApiServicedataServicedataVnfs()); - svcData.getVnfs().setVnf(new ArrayList<>()); + vnf = serviceDataHelper.getVnf(serviceInstanceId, vnfId); + } catch(JsonProcessingException e) { + + log.error("Could not parse service data", e); + throw new RestApplicationException("data-conversion", + "Request could not be completed due to internal error", e, + HttpStatus.INTERNAL_SERVER_ERROR.value()); } - GenericResourceApiServicedataServicedataVnfs vnflist = new GenericResourceApiServicedataServicedataVnfs(); - HttpStatus responseStatus = HttpStatus.NO_CONTENT; - if (svcData.getVnfs().getVnf().isEmpty()) { - log.info("Creating VNF data for ({})", vnfId); - GenericResourceApiServicedataServicedataVnfsVnf vnf = new GenericResourceApiServicedataServicedataVnfsVnf(); + if (vnf == null) { + responseStatus = HttpStatus.CREATED; + vnf = new GenericResourceApiServicedataServicedataVnfsVnf(); vnf.setVnfId(vnfId); vnf.setVnfData(new GenericResourceApiServicedataServicedataVnfsVnfVnfData()); vnf.getVnfData().setVnfTopology(new GenericResourceApiVnftopologyVnfTopology()); - vnf.getVnfData().getVnfTopology() - .setOnapModelInformation(genericResourceApiOnapmodelinformationOnapModelInformationBodyParam); - vnflist.addVnfItem(vnf); - responseStatus = HttpStatus.CREATED; - } else { - log.info("Updating VNF data for ({})", vnfId); - // Filter out all of the other vnf objects into a new VNF List - // Replace if a delete method exists - svcData.getVnfs().getVnf().stream().filter(targetVnf -> !targetVnf.getVnfId().equals(vnfId)) - .forEach(vnflist::addVnfItem); - GenericResourceApiServicedataServicedataVnfsVnf vnf = new GenericResourceApiServicedataServicedataVnfsVnf(); - // If the vnf exists, set it up with new data - Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnfOptional = getVnfObject(data, vnfId); - if (vnfOptional.isPresent()) { - vnf = vnfOptional.get(); - } - if (vnf.getVnfData() == null) { - vnf.setVnfData(new GenericResourceApiServicedataServicedataVnfsVnfVnfData()); - } - if (vnf.getVnfData().getVnfTopology() == null) { - vnf.getVnfData().setVnfTopology(new GenericResourceApiVnftopologyVnfTopology()); - responseStatus = HttpStatus.CREATED; - } - - vnf.getVnfData().getVnfTopology() - .setOnapModelInformation(genericResourceApiOnapmodelinformationOnapModelInformationBodyParam); - vnflist.addVnfItem(vnf); } - svcData.setVnfs(vnflist); - // Map and save the new data + vnf.getVnfData().getVnfTopology() + .setOnapModelInformation(genericResourceApiOnapmodelinformationOnapModelInformationBodyParam); + + // Save the updated VNF (and service, if new) try { - data.setSvcData(objectMapper.writeValueAsString(svcData)); - configServicesRepository.save(data); + serviceDataHelper.saveVnf(serviceInstanceId, vnf, transaction); + transaction.commit(); return new ResponseEntity<>(responseStatus); } catch (JsonProcessingException e) { log.error("Error mapping object to JSON", e); @@ -1718,86 +1583,61 @@ public class ConfigApiController implements ConfigApi { GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworks genericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworksBodyParam) throws RestException { log.info("PUT | VNF Topology VNF Resource Assignments VNF Networks ({})", vnfId); - if (getObjectMapper().isPresent() && getAcceptHeader().isPresent()) { - } else { - log.warn( - "ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated"); - } + ServiceDataTransaction transaction = serviceDataHelper.createTransaction(); List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId); ConfigServices data; if ((services == null) || (services.isEmpty())) { log.info("Could not find data for ({}). Creating new Service Object.", serviceInstanceId); data = new ConfigServices(); data.setSvcInstanceId(serviceInstanceId); + transaction.save(data); } else { data = services.get(0); } - GenericResourceApiServicedataServiceData svcData = null; + HttpStatus responseStatus = HttpStatus.NO_CONTENT; + GenericResourceApiServicedataServicedataVnfsVnf vnf = null; + try { - svcData = objectMapper.readValue(data.getSvcData(), GenericResourceApiServicedataServiceData.class); - } catch (JsonProcessingException e) { - log.error("Could not map service data for ({})", serviceInstanceId); - } - if (svcData == null) { - log.info("Could not find Service Data for ({}). Creating new Service Data Container", serviceInstanceId); - svcData = new GenericResourceApiServicedataServiceData(); - } - if (svcData.getVnfs() == null) { - log.info("VNF List not found for ({}). Creating new VNF List Container.", serviceInstanceId); - svcData.setVnfs(new GenericResourceApiServicedataServicedataVnfs()); - svcData.getVnfs().setVnf(new ArrayList<>()); + vnf = serviceDataHelper.getVnf(serviceInstanceId, vnfId); + } catch(JsonProcessingException e) { + + log.error("Could not parse service data", e); + throw new RestApplicationException("data-conversion", + "Request could not be completed due to internal error", e, + HttpStatus.INTERNAL_SERVER_ERROR.value()); } - GenericResourceApiServicedataServicedataVnfs vnflist = new GenericResourceApiServicedataServicedataVnfs(); - HttpStatus responseStatus = HttpStatus.NO_CONTENT; - if (svcData.getVnfs().getVnf().isEmpty()) { + if (vnf == null) { log.info("Creating VNF data for ({})", vnfId); - GenericResourceApiServicedataServicedataVnfsVnf vnf = new GenericResourceApiServicedataServicedataVnfsVnf(); + vnf = new GenericResourceApiServicedataServicedataVnfsVnf(); vnf.setVnfId(vnfId); - vnf.setVnfData(new GenericResourceApiServicedataServicedataVnfsVnfVnfData()); - vnf.getVnfData().setVnfTopology(new GenericResourceApiVnftopologyVnfTopology()); - vnf.getVnfData().getVnfTopology() - .setVnfResourceAssignments(new GenericResourceApiVnfresourceassignmentsVnfResourceAssignments()); - vnf.getVnfData().getVnfTopology().getVnfResourceAssignments() - .setVnfNetworks(genericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworksBodyParam); - vnflist.addVnfItem(vnf); responseStatus = HttpStatus.CREATED; - } else { - log.info("Updating VNF data for ({})", vnfId); - // Filter out all of the other vnf objects into a new VNF List - // Replace if a delete method exists - svcData.getVnfs().getVnf().stream().filter(targetVnf -> !targetVnf.getVnfId().equals(vnfId)) - .forEach(vnflist::addVnfItem); - GenericResourceApiServicedataServicedataVnfsVnf vnf = new GenericResourceApiServicedataServicedataVnfsVnf(); - // If the vnf exists, set it up with new data - Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnfOptional = getVnfObject(data, vnfId); - if (vnfOptional.isPresent()) { - vnf = vnfOptional.get(); - } - if (vnf.getVnfData() == null) { - vnf.setVnfData(new GenericResourceApiServicedataServicedataVnfsVnfVnfData()); - } - if (vnf.getVnfData().getVnfTopology() == null) { - vnf.getVnfData().setVnfTopology(new GenericResourceApiVnftopologyVnfTopology()); - } - if (vnf.getVnfData().getVnfTopology().getVnfResourceAssignments() == null) { - vnf.getVnfData().getVnfTopology().setVnfResourceAssignments( - new GenericResourceApiVnfresourceassignmentsVnfResourceAssignments()); - responseStatus = HttpStatus.CREATED; - } + } + GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData = vnf.getVnfData(); + if (vnfData == null) { + vnfData = new GenericResourceApiServicedataServicedataVnfsVnfVnfData(); + } + + GenericResourceApiVnftopologyVnfTopology vnfTopology = vnfData.getVnfTopology(); + if (vnfTopology == null) { + vnfTopology = new GenericResourceApiVnftopologyVnfTopology(); + } - vnf.getVnfData().getVnfTopology().getVnfResourceAssignments() - .setVnfNetworks(genericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworksBodyParam); - vnflist.addVnfItem(vnf); + GenericResourceApiVnfresourceassignmentsVnfResourceAssignments vnfResourceAssignments = vnfTopology.getVnfResourceAssignments(); + if (vnfResourceAssignments == null) { + vnfResourceAssignments = new GenericResourceApiVnfresourceassignmentsVnfResourceAssignments(); } + vnfResourceAssignments.setVnfNetworks(genericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworksBodyParam); + vnfTopology.setVnfResourceAssignments(vnfResourceAssignments); + vnfData.setVnfTopology(vnfTopology); + vnf.setVnfData(vnfData); - svcData.setVnfs(vnflist); // Map and save the new data try { - data.setSvcData(objectMapper.writeValueAsString(svcData)); - configServicesRepository.save(data); + serviceDataHelper.saveVnf(serviceInstanceId, vnf, transaction); + transaction.commit(); return new ResponseEntity<>(responseStatus); } catch (JsonProcessingException e) { log.error("Error mapping object to JSON", e); @@ -1834,11 +1674,8 @@ public class ConfigApiController implements ConfigApi { if (!networkRole.equals(genericResourceApiVnfNetworkDataBodyParam.getNetworkRole())) { throw new RestProtocolException("bad-attribute", "network-role mismatch", HttpStatus.BAD_REQUEST.value()); } - if (getObjectMapper().isPresent() && getAcceptHeader().isPresent()) { - } else { - log.warn( - "ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated"); - } + + ServiceDataTransaction transaction = serviceDataHelper.createTransaction(); List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId); ConfigServices data; @@ -1846,102 +1683,71 @@ public class ConfigApiController implements ConfigApi { log.info("Could not find data for ({}). Creating new Service Object.", serviceInstanceId); data = new ConfigServices(); data.setSvcInstanceId(serviceInstanceId); + transaction.save(data); } else { data = services.get(0); } - GenericResourceApiServicedataServiceData svcData = null; + HttpStatus responseStatus = HttpStatus.NO_CONTENT; + GenericResourceApiServicedataServicedataVnfsVnf vnf = null; + try { - svcData = objectMapper.readValue(data.getSvcData(), GenericResourceApiServicedataServiceData.class); - } catch (JsonProcessingException e) { - log.error("Could not map service data for ({})", serviceInstanceId); - } - if (svcData == null) { - log.info("Could not find Service Data for ({}). Creating new Service Data Container", serviceInstanceId); - svcData = new GenericResourceApiServicedataServiceData(); - } - if (svcData.getVnfs() == null) { - log.info("VNF List not found for ({}). Creating new VNF List Container.", serviceInstanceId); - svcData.setVnfs(new GenericResourceApiServicedataServicedataVnfs()); - svcData.getVnfs().setVnf(new ArrayList<>()); + vnf = serviceDataHelper.getVnf(serviceInstanceId, vnfId); + } catch(JsonProcessingException e) { + + log.error("Could not parse service data", e); + throw new RestApplicationException("data-conversion", + "Request could not be completed due to internal error", e, + HttpStatus.INTERNAL_SERVER_ERROR.value()); } - GenericResourceApiServicedataServicedataVnfs vnflist = new GenericResourceApiServicedataServicedataVnfs(); - HttpStatus responseStatus = HttpStatus.NO_CONTENT; - if (svcData.getVnfs().getVnf().isEmpty()) { + if (vnf == null) { log.info("Creating VNF data for ({})", vnfId); - GenericResourceApiServicedataServicedataVnfsVnf vnf = new GenericResourceApiServicedataServicedataVnfsVnf(); + vnf = new GenericResourceApiServicedataServicedataVnfsVnf(); vnf.setVnfId(vnfId); - vnf.setVnfData(new GenericResourceApiServicedataServicedataVnfsVnfVnfData()); - vnf.getVnfData().setVnfTopology(new GenericResourceApiVnftopologyVnfTopology()); - vnf.getVnfData().getVnfTopology() - .setVnfResourceAssignments(new GenericResourceApiVnfresourceassignmentsVnfResourceAssignments()); - vnf.getVnfData().getVnfTopology().getVnfResourceAssignments() - .setVnfNetworks(new GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworks()); - vnf.getVnfData().getVnfTopology().getVnfResourceAssignments().getVnfNetworks() - .setVnfNetwork(new ArrayList<>()); - vnf.getVnfData().getVnfTopology().getVnfResourceAssignments().getVnfNetworks() - .addVnfNetworkItem(genericResourceApiVnfNetworkDataBodyParam); - vnflist.addVnfItem(vnf); responseStatus = HttpStatus.CREATED; - } else { - log.info("Updating VNF data for ({})", vnfId); - // Filter out all of the other vnf objects into a new VNF List - // Replace if a delete method exists - svcData.getVnfs().getVnf().stream().filter(targetVnf -> !targetVnf.getVnfId().equals(vnfId)) - .forEach(vnflist::addVnfItem); - GenericResourceApiServicedataServicedataVnfsVnf vnf = new GenericResourceApiServicedataServicedataVnfsVnf(); - // If the vnf exists, set it up with new data - Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnfOptional = getVnfObject(data, vnfId); - if (vnfOptional.isPresent()) { - vnf = vnfOptional.get(); - } - if (vnf.getVnfData() == null) { - vnf.setVnfData(new GenericResourceApiServicedataServicedataVnfsVnfVnfData()); - } - if (vnf.getVnfData().getVnfTopology() == null) { - vnf.getVnfData().setVnfTopology(new GenericResourceApiVnftopologyVnfTopology()); - } - if (vnf.getVnfData().getVnfTopology().getVnfResourceAssignments() == null) { - vnf.getVnfData().getVnfTopology().setVnfResourceAssignments( - new GenericResourceApiVnfresourceassignmentsVnfResourceAssignments()); - } - if (vnf.getVnfData().getVnfTopology().getVnfResourceAssignments().getVnfNetworks() == null) { - log.info("Creating new VnfNetworks"); - vnf.getVnfData().getVnfTopology().getVnfResourceAssignments().setVnfNetworks( - new GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworks()); - } + } + GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData = vnf.getVnfData(); + if (vnfData == null) { + vnfData = new GenericResourceApiServicedataServicedataVnfsVnfVnfData(); + } - GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworks networkList = new GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworks(); - if (vnf.getVnfData().getVnfTopology().getVnfResourceAssignments().getVnfNetworks().getVnfNetwork() - .isEmpty()) { - log.info("First entry into network info."); - vnf.getVnfData().getVnfTopology().getVnfResourceAssignments().getVnfNetworks() - .addVnfNetworkItem(genericResourceApiVnfNetworkDataBodyParam); - responseStatus = HttpStatus.CREATED; - } else { - log.info("Found networks. Filtering."); - vnf.getVnfData().getVnfTopology().getVnfResourceAssignments().getVnfNetworks().getVnfNetwork().stream() - .filter(targetNetwork -> !targetNetwork.getNetworkRole().equals(networkRole)) - .forEach(networkList::addVnfNetworkItem); - networkList.addVnfNetworkItem(genericResourceApiVnfNetworkDataBodyParam); - - if (networkList.getVnfNetwork().size() != vnf.getVnfData().getVnfTopology().getVnfResourceAssignments() - .getVnfNetworks().getVnfNetwork().size()) { - log.info("Added a new Item"); - responseStatus = HttpStatus.CREATED; - } - vnf.getVnfData().getVnfTopology().getVnfResourceAssignments().setVnfNetworks(networkList); - } + GenericResourceApiVnftopologyVnfTopology vnfTopology = vnfData.getVnfTopology(); + if (vnfTopology == null) { + vnfTopology = new GenericResourceApiVnftopologyVnfTopology(); + } - vnflist.addVnfItem(vnf); + GenericResourceApiVnfresourceassignmentsVnfResourceAssignments vnfResourceAssignments = vnfTopology.getVnfResourceAssignments(); + if (vnfResourceAssignments == null) { + vnfResourceAssignments = new GenericResourceApiVnfresourceassignmentsVnfResourceAssignments(); } - svcData.setVnfs(vnflist); - // Map and save the new data + GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworks vnfNetworks = vnfResourceAssignments.getVnfNetworks(); + if (vnfNetworks == null) { + vnfNetworks = new GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworks(); + vnfNetworks.setVnfNetwork(new ArrayList<>()); + } + + + if (vnfNetworks.getVnfNetwork().isEmpty()) { + vnfNetworks.addVnfNetworkItem(genericResourceApiVnfNetworkDataBodyParam); + } else { + GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworks vnfNetworksNew = new GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworks(); + vnfNetworksNew.setVnfNetwork(new ArrayList<>()); + vnfNetworks.getVnfNetwork().stream() + .filter(targetNetwork -> !targetNetwork.getNetworkRole().equals(networkRole)).forEach(vnfNetworksNew::addVnfNetworkItem); + + vnfNetworksNew.addVnfNetworkItem(genericResourceApiVnfNetworkDataBodyParam); + vnfNetworks = vnfNetworksNew; + } + + vnfResourceAssignments.setVnfNetworks(vnfNetworks); + vnfTopology.setVnfResourceAssignments(vnfResourceAssignments); + vnfData.setVnfTopology(vnfTopology); + vnf.setVnfData(vnfData); try { - data.setSvcData(objectMapper.writeValueAsString(svcData)); - configServicesRepository.save(data); + serviceDataHelper.saveVnf(serviceInstanceId, vnf, transaction); + transaction.commit(); return new ResponseEntity<>(responseStatus); } catch (JsonProcessingException e) { log.error("Error mapping object to JSON", e); @@ -1950,35 +1756,6 @@ public class ConfigApiController implements ConfigApi { } } - /** - * Extracts a VNF object from the database, - * - * @param configServices A Config Services option created from a Service - * Instance ID - * @param vnfId the target VNF ID - * @return An empty Optional if the Service Data does not exist, an empty - * Optional if the VNF is not found, or an optional containing the found - * VNF. - */ - private Optional<GenericResourceApiServicedataServicedataVnfsVnf> getVnfObject(ConfigServices configServices, - String vnfId) { - // Map the Marshall the JSON String into a Java Object - log.info("Getting VNF Data for ({})", vnfId); - GenericResourceApiServicedataServiceData svcData; - try { - svcData = objectMapper.readValue(configServices.getSvcData(), - GenericResourceApiServicedataServiceData.class); - } catch (JsonProcessingException e) { - log.error("Error", e); - return Optional.empty(); - } - - /* - * Get a stream of the VNF Objects and return the target if it's found, assuming - * that each VNF ID is unique within a Service Instance Object - */ - return svcData.getVnfs().getVnf().stream().filter(targetVnf -> targetVnf.getVnfId().equals(vnfId)).findFirst(); - } @Override public ResponseEntity<GenericResourceApiServicetopologyServiceTopology> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataServiceTopologyGet( @@ -1991,13 +1768,14 @@ public class ConfigApiController implements ConfigApi { throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value()); } + ConfigServices service = services.get(0); + try { - if (services.get(0).getSvcData().isEmpty()) { + if ( (service.getSvcData() == null) || service.getSvcData().isEmpty()) { throw new RestProtocolException("data-missing", "No service-data entry found", HttpStatus.NOT_FOUND.value()); } else { - serviceData = objectMapper.readValue(services.get(0).getSvcData(), - GenericResourceApiServicedataServiceData.class); + serviceData = serviceDataHelper.getServiceData(service); serviceTopology = serviceData.getServiceTopology(); } if (serviceTopology == null) { @@ -2031,21 +1809,24 @@ public class ConfigApiController implements ConfigApi { log.info("GET | Vf Module Data for ({})", vfModuleId); - if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) { - if(getAcceptHeader().get().contains("application/json")) { - } - } else { - log.warn("ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated"); - } + List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId); if((services == null) || (services.isEmpty())) { throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value()); } - Optional<GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule> vfModule = - getVfModuleObject(services.get(0), vnfId, vfModuleId); - if(vfModule.isPresent()) { - return new ResponseEntity<>(vfModule.get(), HttpStatus.OK); + GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule vfModule; + try { + vfModule = serviceDataHelper.getVfModule(serviceInstanceId, vnfId, vfModuleId); + } catch (JsonProcessingException e) { + log.error("Could not parse service data", e); + throw new RestApplicationException("data-conversion", + "Request could not be completed due to internal error", e, + HttpStatus.INTERNAL_SERVER_ERROR.value()); + } + + if(vfModule != null) { + return new ResponseEntity<>(vfModule, HttpStatus.OK); } else { log.info("No vf-module found for [{}]", vfModuleId); throw new RestApplicationException("data-missing", @@ -2074,75 +1855,42 @@ public class ConfigApiController implements ConfigApi { if(! vfModuleId.equals(genericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModuleBodyParam.getVfModuleId())) { throw new RestProtocolException("bad-attribute", "vf-module-id mismatch", HttpStatus.BAD_REQUEST.value()); } - if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) { - log.info("Something with header"); - } else { - log.warn("ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated"); - } + HttpStatus responseStatus = HttpStatus.NO_CONTENT; - ConfigServices service; - GenericResourceApiServicedataServiceData svcData; List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId); if((services == null) || (services.isEmpty())) { log.error("service-instance-id ({}) not found in SDN.", serviceInstanceId); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } else { - service = services.get(0); - } + } + + GenericResourceApiServicedataServicedataVnfsVnf vnf; try { - svcData = objectMapper.readValue(service.getSvcData(), GenericResourceApiServicedataServiceData.class); - } catch(JsonProcessingException e) { - log.error("Error", e); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + vnf = serviceDataHelper.getVnf(serviceInstanceId, vnfId); + } catch (JsonProcessingException e1) { + log.error("Could not parse service data", e1); + throw new RestApplicationException("data-conversion", + "Request could not be completed due to internal error", e1, + HttpStatus.INTERNAL_SERVER_ERROR.value()); } - GenericResourceApiServicedataServicedataVnfs vnfs = svcData.getVnfs(); - Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnf = - vnfs.getVnf() - .stream() - .filter(targetVnf -> targetVnf.getVnfId().equals(vnfId)) - .findFirst(); - - if(! vnf.isPresent()) { + if(vnf == null) { log.error("vnf-id ({}) not found in SDN.", vnfId); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } - GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData = vnf.get().getVnfData(); - GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules existingVfModules = vnfData.getVfModules(); - GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules newVfModules = new GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules(); + // See if this is the first vf module being added + List<ConfigVfModules> configVfModules = configVfModulesRepository.findBySvcInstanceIdAndVnfId(serviceInstanceId, vnfId); - if (existingVfModules == null || existingVfModules.getVfModule().isEmpty()) { - log.info("No existing vf-module found. Creating the first vf-module for vnf [{}]", vnfId); - newVfModules.addVfModuleItem(genericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModuleBodyParam); + if ((configVfModules == null) || configVfModules.isEmpty()) { responseStatus = HttpStatus.CREATED; } - else { - ArrayList<String> vfModuleIds = new ArrayList<>(); - for (GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule vf : existingVfModules.getVfModule()) { - vfModuleIds.add(vf.getVfModuleId()); - } - log.info("[{}] vf-module(s) {} found in vnf [{}]", existingVfModules.getVfModule().size(), vfModuleIds, vnfId); - if (!vfModuleIds.isEmpty() && vfModuleIds.contains(vfModuleId)) { - log.info("Overwriting vf-module [{}] in vnf [{}]", vfModuleId, vnfId); - } else { - log.info("Adding vf-module [{}] to vnf [{}]", vfModuleId, vnfId); - } - existingVfModules.getVfModule() - .stream() - .filter(vf -> ! vf.getVfModuleId().equals(vfModuleId)) - .forEach(newVfModules::addVfModuleItem); - newVfModules.addVfModuleItem(genericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModuleBodyParam); - responseStatus = HttpStatus.OK; - } - vnfData.setVfModules(newVfModules); - // Map and save the new data + + // Add vf module try { - service.setSvcData(objectMapper.writeValueAsString(svcData)); - configServicesRepository.save(service); + serviceDataHelper.saveVfModule(serviceInstanceId, vnfId, genericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModuleBodyParam, null); return new ResponseEntity<>(responseStatus); } catch(JsonProcessingException e) { log.error("Error mapping object to JSON", e); @@ -2167,13 +1915,7 @@ public class ConfigApiController implements ConfigApi { configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVfModulesVfModuleVfModuleIdVfModuleDataVfModuleTopologyGet( String serviceInstanceId, String vnfId, String vfModuleId) throws RestException { log.info("GET | vf-module-topology for ({})", vfModuleId); - if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) { - if (getAcceptHeader().get().contains("application/json")) { - log.info("Something with header"); - } - } else { - log.warn("ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated"); - } + List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId); if((services == null) || (services.isEmpty())) { throw new RestApplicationException("data-missing", @@ -2181,12 +1923,19 @@ public class ConfigApiController implements ConfigApi { HttpStatus.NOT_FOUND.value()); } - Optional<GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule> vfModule = - getVfModuleObject(services.get(0), vnfId, vfModuleId); - if(vfModule.isPresent() - && vfModule.get().getVfModuleData() != null - && vfModule.get().getVfModuleData().getVfModuleTopology() != null) { - return new ResponseEntity<>(vfModule.get().getVfModuleData().getVfModuleTopology(), HttpStatus.OK); + GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule vfModule; + try { + vfModule = serviceDataHelper.getVfModule(serviceInstanceId, vnfId, vfModuleId); + } catch (JsonProcessingException e) { + log.error("Could not parse service data", e); + throw new RestApplicationException("data-conversion", + "Request could not be completed due to internal error", e, + HttpStatus.INTERNAL_SERVER_ERROR.value()); + } + if (vfModule != null + && vfModule.getVfModuleData() != null + && vfModule.getVfModuleData().getVfModuleTopology() != null) { + return new ResponseEntity<>(vfModule.getVfModuleData().getVfModuleTopology(), HttpStatus.OK); } else { log.info("No information found for {}", vfModuleId); throw new RestApplicationException("data-missing", @@ -2201,78 +1950,20 @@ public class ConfigApiController implements ConfigApi { log.info("DELETE | vf-module Data for ({})", vfModuleId); - if (getObjectMapper().isPresent() && getAcceptHeader().isPresent()) { - log.info("Something with header."); - } else { - log.warn("ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated"); - } - ConfigServices service; - GenericResourceApiServicedataServiceData svcData; + List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId); if((services == null) || (services.isEmpty())) { log.error("service-instance-id ({}) not found in SDN.", serviceInstanceId); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } else { - service = services.get(0); - } + } - try { - svcData = objectMapper.readValue(service.getSvcData(), GenericResourceApiServicedataServiceData.class); - } catch(JsonProcessingException e) { - log.error("Error", e); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } - GenericResourceApiServicedataServicedataVnfs vnfs = svcData.getVnfs(); - Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnf = - vnfs.getVnf() - .stream() - .filter(targetVnf -> targetVnf.getVnfId().equals(vnfId)) - .findFirst(); - if(! vnf.isPresent()) { - log.error("vnf-id ({}) not found in SDN.", vnfId); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } - - GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData = vnf.get().getVnfData(); - GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules existingVfModules = vnfData.getVfModules(); - GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules newVfModules = new GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules(); - - if (existingVfModules == null || existingVfModules.getVfModule().isEmpty()) { - log.info("No existing vf-module found. Creating the first vf-module for vnf [{}]", vnfId); - return new ResponseEntity<>(HttpStatus.OK); - } - - ArrayList<String> vfModuleIds = new ArrayList<>(); - for (GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule vf : existingVfModules.getVfModule()) { - vfModuleIds.add(vf.getVfModuleId()); - } - log.info("[{}] vf-module(s) {} found in vnf [{}]", existingVfModules.getVfModule().size(), vfModuleIds, vnfId); - if (!vfModuleIds.isEmpty() && vfModuleIds.contains(vfModuleId)) { - log.info("Deleting vf-module [{}] from vnf [{}]", vfModuleId, vnfId); - } else { - log.info("vf-module [{}] not found in vnf [{}]", vfModuleId, vnfId); - return new ResponseEntity<>(HttpStatus.OK); - } - existingVfModules.getVfModule() - .stream() - .filter(vf -> ! vf.getVfModuleId().equals(vfModuleId)) - .forEach(newVfModules::addVfModuleItem); - vnfData.setVfModules(newVfModules); - log.info("vf-module [{}] deleted from vnf [{}]", vfModuleId, vnfId); - - // Map and save the new data - try { - service.setSvcData(objectMapper.writeValueAsString(svcData)); - configServicesRepository.save(service); - return new ResponseEntity<>(HttpStatus.OK); - } catch(JsonProcessingException e) { - log.error("Error mapping object to JSON", e); - // Should probably be a 500 INTERNAL_SERVICE_ERROR - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } + serviceDataHelper.deleteVfModule(serviceInstanceId, vnfId, vfModuleId); + return new ResponseEntity<>(HttpStatus.OK); + } } + diff --git a/ms/generic-resource-api/src/main/java/org/onap/sdnc/apps/ms/gra/controllers/OperationsApiController.java b/ms/generic-resource-api/src/main/java/org/onap/sdnc/apps/ms/gra/controllers/OperationsApiController.java index 2b52fca..19c8ca0 100644 --- a/ms/generic-resource-api/src/main/java/org/onap/sdnc/apps/ms/gra/controllers/OperationsApiController.java +++ b/ms/generic-resource-api/src/main/java/org/onap/sdnc/apps/ms/gra/controllers/OperationsApiController.java @@ -42,7 +42,6 @@ import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase; import org.onap.sdnc.apps.ms.gra.data.ConfigContrailRouteAllottedResourcesRepository; -import org.onap.sdnc.apps.ms.gra.data.ConfigNetworks; import org.onap.sdnc.apps.ms.gra.data.ConfigNetworksRepository; import org.onap.sdnc.apps.ms.gra.data.ConfigPortMirrorConfigurations; import org.onap.sdnc.apps.ms.gra.data.ConfigPortMirrorConfigurationsRepository; @@ -50,9 +49,7 @@ import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadData; import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadDataRepository; import org.onap.sdnc.apps.ms.gra.data.ConfigServices; import org.onap.sdnc.apps.ms.gra.data.ConfigServicesRepository; -import org.onap.sdnc.apps.ms.gra.data.ConfigVfModules; import org.onap.sdnc.apps.ms.gra.data.ConfigVfModulesRepository; -import org.onap.sdnc.apps.ms.gra.data.ConfigVnfs; import org.onap.sdnc.apps.ms.gra.data.ConfigVnfsRepository; import org.onap.sdnc.apps.ms.gra.data.OperationalContrailRouteAllottedResourcesRepository; import org.onap.sdnc.apps.ms.gra.data.OperationalPortMirrorConfigurationsRepository; @@ -84,16 +81,6 @@ import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiRequestStatusEn import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServiceOperationInformation; import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServiceOperationInformationBodyparam; import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServiceTopologyOperation; -import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServiceData; -import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataNetworks; -import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataNetworksNetwork; -import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataNetworksNetworkNetworkData; -import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataVnfs; -import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataVnfsVnf; -import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataVnfsVnfVnfData; -import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules; -import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule; -import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfmoduleVfModuleData; import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicetopologyoperationOutput; import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiVfModuleOperationInformation; import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiVfModuleOperationInformationBodyparam; @@ -175,6 +162,9 @@ public class OperationsApiController implements OperationsApi { @Autowired private OperationalPortMirrorConfigurationsRepository operationalPortMirrorConfigurationsRepository; + @Autowired + private ServiceDataHelper serviceDataHelper; + private static class Iso8601Util { private static TimeZone timeZone = TimeZone.getTimeZone("UTC"); @@ -552,147 +542,6 @@ public class OperationsApiController implements OperationsApi { } - private String getConfigServiceDataAsString(String svcInstanceId) throws JsonProcessingException { - return(objectMapper.writeValueAsString(getConfigServiceData(svcInstanceId))); - } - - private GenericResourceApiServicedataServiceData getConfigServiceData(String svcInstanceId) - throws JsonProcessingException { - - List<ConfigServices> configServices = configServicesRepository.findBySvcInstanceId(svcInstanceId); - - if (configServices.isEmpty()) { - return (null); - } - - GenericResourceApiServicedataServiceData svcData = (objectMapper.readValue(configServices.get(0).getSvcData(), GenericResourceApiServicedataServiceData.class)); - - // Get networks - List<ConfigNetworks> configNetworks = configNetworksRepository.findBySvcInstanceId(svcInstanceId); - GenericResourceApiServicedataServicedataNetworks networks = new GenericResourceApiServicedataServicedataNetworks(); - for (ConfigNetworks configNetwork : configNetworks) { - GenericResourceApiServicedataServicedataNetworksNetwork network = new GenericResourceApiServicedataServicedataNetworksNetwork(); - network.setNetworkId(configNetwork.getNetworkId()); - network.setNetworkData(objectMapper.readValue(configNetwork.getNetworkData(), GenericResourceApiServicedataServicedataNetworksNetworkNetworkData.class)); - networks.addNetworkItem(network); - } - svcData.setNetworks(networks); - - // Get VNFs - List<ConfigVnfs> configVnfs = configVnfsRepository.findBySvcInstanceId(svcInstanceId); - GenericResourceApiServicedataServicedataVnfs vnfs = new GenericResourceApiServicedataServicedataVnfs(); - for (ConfigVnfs configVnf : configVnfs) { - GenericResourceApiServicedataServicedataVnfsVnf vnf = new GenericResourceApiServicedataServicedataVnfsVnf(); - vnf.setVnfId(configVnf.getVnfId()); - GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData = objectMapper.readValue(configVnf.getVnfData(), GenericResourceApiServicedataServicedataVnfsVnfVnfData.class); - - // Get vf modules for this vnf - List<ConfigVfModules> configVfModules = configVfModulesRepository.findBySvcInstanceIdAndVnfId(svcInstanceId, configVnf.getVnfId()); - GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules vfModules = new GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules(); - for (ConfigVfModules configVfModule : configVfModules) { - GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule vfModule = new GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule(); - vfModule.setVfModuleId(configVfModule.getVfModuleId()); - vfModule.setVfModuleData(objectMapper.readValue(configVfModule.getVfModuleData(), GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfmoduleVfModuleData.class)); - vfModules.addVfModuleItem(vfModule); - } - vnfData.setVfModules(vfModules); - vnf.setVnfData(vnfData); - vnfs.addVnfItem(vnf); - } - svcData.setVnfs(vnfs); - return(svcData); - } - - private void saveSvcData(ConfigServices configService, String svcDataAsString) throws JsonProcessingException { - if (svcDataAsString == null) { - configServicesRepository.save(configService); - return; - } - saveSvcData(configService, objectMapper.readValue(svcDataAsString, GenericResourceApiServicedataServiceData.class)); - } - - private void saveSvcData(ConfigServices configService, GenericResourceApiServicedataServiceData svcData) throws JsonProcessingException { - if (svcData == null) { - configServicesRepository.save(configService); - return; - } - - String svcInstanceId = configService.getSvcInstanceId(); - - // Write networks - GenericResourceApiServicedataServicedataNetworks networks = svcData.getNetworks(); - if (networks != null) { - List<GenericResourceApiServicedataServicedataNetworksNetwork> networkItems = networks.getNetwork(); - if ((networkItems != null) && !networkItems.isEmpty()) { - for (GenericResourceApiServicedataServicedataNetworksNetwork networkItem : networkItems) { - List<ConfigNetworks> configNetworks = configNetworksRepository - .findBySvcInstanceIdAndNetworkId(svcInstanceId, networkItem.getNetworkId()); - ConfigNetworks configNetwork; - if ((configNetworks == null) || (configNetworks.isEmpty())) { - configNetwork = new ConfigNetworks(svcInstanceId, networkItem.getNetworkId()); - } else { - configNetwork = configNetworks.get(0); - } - configNetwork.setNetworkData(objectMapper.writeValueAsString(networkItem.getNetworkData())); - configNetworksRepository.save(configNetwork); - } - } - svcData.setNetworks(null); - } - - // Write vnfs - GenericResourceApiServicedataServicedataVnfs vnfs = svcData.getVnfs(); - if (vnfs != null) { - List<GenericResourceApiServicedataServicedataVnfsVnf> vnfItems = vnfs.getVnf(); - if ((vnfItems != null) && !vnfItems.isEmpty()) { - for (GenericResourceApiServicedataServicedataVnfsVnf vnfItem : vnfItems) { - String vnfId = vnfItem.getVnfId(); - List<ConfigVnfs> configVnfs = configVnfsRepository.findBySvcInstanceIdAndVnfId(svcInstanceId, - vnfId); - ConfigVnfs configVnf; - if ((configVnfs == null) || (configVnfs.isEmpty())) { - configVnf = new ConfigVnfs(svcInstanceId, vnfId); - } else { - configVnf = configVnfs.get(0); - } - - GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData = vnfItem.getVnfData(); - - // Write vf modules - GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules vfModules = vnfData.getVfModules(); - if (vfModules != null) { - List<GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule> vfModuleItems = vfModules - .getVfModule(); - if ((vfModuleItems != null) && !vfModuleItems.isEmpty()) { - for (GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule vfModuleItem : vfModuleItems) { - List<ConfigVfModules> configVfModules = configVfModulesRepository - .findBySvcInstanceIdAndVnfIdAndVfModuleId(svcInstanceId, vnfId, - vfModuleItem.getVfModuleId()); - ConfigVfModules configVfModule; - if ((configVfModules == null) || (configVfModules.isEmpty())) { - configVfModule = new ConfigVfModules(svcInstanceId, vnfId, - vfModuleItem.getVfModuleId()); - } else { - configVfModule = configVfModules.get(0); - } - configVfModule.setVfModuleData( - objectMapper.writeValueAsString(vfModuleItem.getVfModuleData())); - configVfModulesRepository.save(configVfModule); - } - vnfData.setVfModules(null); - - configVnf.setVnfData(objectMapper.writeValueAsString(vnfData)); - configVnfsRepository.save(configVnf); - } - } - } - } - svcData.setVnfs(null); - } - - configService.setSvcData(objectMapper.writeValueAsString(svcData)); - configServicesRepository.save(configService); - } @Override public ResponseEntity<GenericResourceApiNetworkTopologyOperation> operationsGENERICRESOURCEAPInetworkTopologyOperationPost( @@ -738,7 +587,7 @@ public class OperationsApiController implements OperationsApi { if (configServices != null && !configServices.isEmpty()) { configService = configServices.get(0); try { - svcData = getConfigServiceDataAsString(svcInstanceId); + svcData = serviceDataHelper.getServiceDataAsString(svcInstanceId); } catch (JsonProcessingException e) { log.error("exiting {} due to parse error on service data", svcOperation); resp.setResponseCode("500"); @@ -836,7 +685,7 @@ public class OperationsApiController implements OperationsApi { // Save service data try { - saveSvcData(configService, ctxSvcDataJson); + serviceDataHelper.saveService(configService, ctxSvcDataJson); } catch (JsonProcessingException e) { log.error("exiting {} due to error saving service data", svcOperation); resp.setResponseCode("500"); @@ -901,10 +750,12 @@ public class OperationsApiController implements OperationsApi { if (configServices != null && !configServices.isEmpty()) { configService = configServices.get(0); try { - svcData = getConfigServiceDataAsString(svcInstanceId); - ctxIn.mergeJson("service-data", svcData); + svcData = serviceDataHelper.getServiceDataAsString(svcInstanceId); + if (svcData != null) { + ctxIn.mergeJson("service-data", svcData); + } } catch (JsonProcessingException e) { - log.error("exiting {} due to parse error on service data", svcOperation); + log.error("exiting {} due to parse error on service data", svcOperation, e); resp.setResponseCode("500"); resp.setResponseMessage("internal error"); resp.setAckFinalIndicator("Y"); @@ -912,7 +763,7 @@ public class OperationsApiController implements OperationsApi { return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR); } } else { - configService = new ConfigServices(svcInstanceId, null); + configService = new ConfigServices(svcInstanceId); } @@ -981,7 +832,7 @@ public class OperationsApiController implements OperationsApi { // Save service data try { - saveSvcData(configService, ctxSvcDataJson); + serviceDataHelper.saveService(configService, ctxSvcDataJson); } catch (JsonProcessingException e) { log.error("exiting {} due to error saving service data", svcOperation); resp.setResponseCode("500"); @@ -1052,7 +903,7 @@ public class OperationsApiController implements OperationsApi { if (configServices != null && !configServices.isEmpty()) { configService = configServices.get(0); try { - svcData = getConfigServiceDataAsString(svcInstanceId); + svcData = serviceDataHelper.getServiceDataAsString(svcInstanceId); } catch (JsonProcessingException e) { log.error("exiting {} due to parse error on service data", svcOperation); resp.setResponseCode("500"); @@ -1153,7 +1004,7 @@ public class OperationsApiController implements OperationsApi { // Save service data try { - saveSvcData(configService, ctxSvcDataJson); + serviceDataHelper.saveService(configService, ctxSvcDataJson); } catch (JsonProcessingException e) { log.error("exiting {} due to error saving service data", svcOperation); resp.setResponseCode("500"); @@ -1210,7 +1061,7 @@ public class OperationsApiController implements OperationsApi { if (configServices != null && !configServices.isEmpty()) { configService = configServices.get(0); try { - svcData = getConfigServiceDataAsString(svcInstanceId); + svcData = serviceDataHelper.getServiceDataAsString(svcInstanceId); } catch (JsonProcessingException e) { log.error("exiting {} due to parse error on service data", svcOperation); return; @@ -1285,7 +1136,7 @@ public class OperationsApiController implements OperationsApi { // Save service data try { - saveSvcData(configService, ctxSvcDataJson); + serviceDataHelper.saveService(configService, ctxSvcDataJson); } catch (JsonProcessingException e) { log.error("exiting {} due to error saving service data", svcOperation); return; @@ -1311,6 +1162,8 @@ public class OperationsApiController implements OperationsApi { GenericResourceApiVfmoduletopologyoperationOutput resp = new GenericResourceApiVfmoduletopologyoperationOutput(); log.info(CALLED_STR, svcOperation); + + log.info("MYSQL_DATABASE = {}", System.getenv("MYSQL_DATABASE")); // Verify input contains service instance id if (hasInvalidServiceId(input.getInput())) { log.debug("exiting {} because of null or empty service-instance-id", svcOperation); @@ -1358,7 +1211,7 @@ public class OperationsApiController implements OperationsApi { if (configServices != null && !configServices.isEmpty()) { configService = configServices.get(0); try { - svcData = getConfigServiceDataAsString(svcInstanceId); + svcData = serviceDataHelper.getServiceDataAsString(svcInstanceId); } catch (JsonProcessingException e) { log.error("exiting {} due to parse error on service data", svcOperation); resp.setResponseCode("500"); @@ -1404,7 +1257,7 @@ public class OperationsApiController implements OperationsApi { String ackFinal = "Y"; String skipMdsalUpdate; - String ctxSvcDataJson = svcData; + // Call DG try { // Any of these can throw a nullpointer exception @@ -1450,8 +1303,9 @@ public class OperationsApiController implements OperationsApi { // ONLY update svcData in config and operational trees // and remember to save operational data when skip-mdsal-update is Y in ctx. String ctxJson = ctxOut.toJsonString("service-data"); - log.info("Saving service-data in SDN because skiMdsalUpdate is {}", skipMdsalUpdate); - saveSvcData(configService, ctxJson); + log.info("Saving service-data in SDN because skipMdsalUpdate is {}", skipMdsalUpdate); + log.info("Service data : {}", ctxJson); + serviceDataHelper.saveService(configService, ctxJson); log.info("Copying service-data to operational-data"); operService.setSvcData(ctxJson); @@ -1510,7 +1364,7 @@ public class OperationsApiController implements OperationsApi { if (configServices != null && !configServices.isEmpty()) { configService = configServices.get(0); try { - svcData = getConfigServiceDataAsString(svcInstanceId); + svcData = serviceDataHelper.getServiceDataAsString(svcInstanceId); } catch (JsonProcessingException e) { log.error("exiting {} due to parse error on service data", svcOperation); return; @@ -1578,7 +1432,7 @@ public class OperationsApiController implements OperationsApi { // Save service data try { - saveSvcData(configService, ctxSvcDataJson); + serviceDataHelper.saveService(configService, ctxSvcDataJson); } catch (JsonProcessingException e) { log.error("exiting {} due to error saving service data", svcOperation); return; @@ -1909,7 +1763,12 @@ public class OperationsApiController implements OperationsApi { ConfigServices configService = null; if (configServices != null && !configServices.isEmpty()) { configService = configServices.get(0); - ctxIn.mergeJson("service-data", configService.getSvcData()); + try { + ctxIn.mergeJson("service-data", serviceDataHelper.getServiceDataAsString(svcInstanceId)); + } catch (JsonProcessingException e) { + log.error("exiting {} due to parse error on service data", svcOperation); + return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR); + } } else { log.debug("exiting {} because the service-instance does not have any service data in SDN", svcOperation); return new ResponseEntity<>(retval, HttpStatus.OK); @@ -1920,13 +1779,7 @@ public class OperationsApiController implements OperationsApi { // Any of these can throw a nullpointer exception // execute should only throw a SvcLogicException SvcLogicContext ctxOut = svc.execute(MODULE_NAME, svcOperation, null, "sync", ctxIn); - Properties respProps = ctxOut.toProperties(); - /* For debugging Only - respProps.forEach((k,v) -> { - log.debug("prop: {} -> {}",k,v); - }); - */ String ctxJson = ctxOut.toJsonString("vnf-get-resource-request-output"); GenericResourceApiVnfgetresourcerequestOutput vnfgetresourcerequestOutput = diff --git a/ms/generic-resource-api/src/main/java/org/onap/sdnc/apps/ms/gra/controllers/ServiceDataHelper.java b/ms/generic-resource-api/src/main/java/org/onap/sdnc/apps/ms/gra/controllers/ServiceDataHelper.java new file mode 100644 index 0000000..f120f8c --- /dev/null +++ b/ms/generic-resource-api/src/main/java/org/onap/sdnc/apps/ms/gra/controllers/ServiceDataHelper.java @@ -0,0 +1,498 @@ +package org.onap.sdnc.apps.ms.gra.controllers; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.onap.sdnc.apps.ms.gra.data.ConfigNetworks; +import org.onap.sdnc.apps.ms.gra.data.ConfigNetworksRepository; +import org.onap.sdnc.apps.ms.gra.data.ConfigServices; +import org.onap.sdnc.apps.ms.gra.data.ConfigServicesRepository; +import org.onap.sdnc.apps.ms.gra.data.ConfigVfModules; +import org.onap.sdnc.apps.ms.gra.data.ConfigVfModulesRepository; +import org.onap.sdnc.apps.ms.gra.data.ConfigVnfs; +import org.onap.sdnc.apps.ms.gra.data.ConfigVnfsRepository; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServiceData; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataNetworks; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataNetworksNetwork; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataNetworksNetworkNetworkData; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataVnfs; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataVnfsVnf; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataVnfsVnfVnfData; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfmoduleVfModuleData; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.stereotype.Component; + +@Component +@ComponentScan(basePackages = { "org.onap.sdnc.apps.ms.gra.*", "org.onap.ccsdk.apps.services" }) +public class ServiceDataHelper { + + @Autowired + private ObjectMapper objectMapper; + + @Autowired + private ConfigServicesRepository configServicesRepository; + + @Autowired + private ConfigNetworksRepository configNetworksRepository; + + @Autowired + private ConfigVnfsRepository configVnfsRepository; + + @Autowired + private ConfigVfModulesRepository configVfModulesRepository; + + public ServiceDataHelper() { + + } + + public class ServiceDataTransaction { + private List<ConfigServices> servicesToSave = new LinkedList<ConfigServices>(); + private List<ConfigNetworks> networksToSave = new LinkedList<ConfigNetworks>(); + private List<ConfigVnfs> vnfsToSave = new LinkedList<ConfigVnfs>(); + private List<ConfigVfModules> vfModulesToSave = new LinkedList<ConfigVfModules>(); + + private List<ConfigServices> servicesToRemove = new LinkedList<ConfigServices>(); + private List<ConfigNetworks> networksToRemove = new LinkedList<ConfigNetworks>(); + private List<ConfigVnfs> vnfsToRemove = new LinkedList<ConfigVnfs>(); + private List<ConfigVfModules> vfModulesToRemove = new LinkedList<ConfigVfModules>(); + + public void save(ConfigServices service) { + servicesToSave.add(service); + } + + public void save(ConfigNetworks network) { + networksToSave.add(network); + } + + public void save(ConfigVnfs vnf) { + vnfsToSave.add(vnf); + } + + public void save(ConfigVfModules vfModule) { + vfModulesToSave.add(vfModule); + } + + public void remove(ConfigServices service) { + servicesToRemove.add(service); + } + + public void remove(ConfigNetworks network) { + networksToRemove.add(network); + } + + public void remove(ConfigVnfs vnf) { + vnfsToRemove.add(vnf); + } + + public void remove(ConfigVfModules vfModule) { + vfModulesToRemove.add(vfModule); + } + + + public void commit() { + + for (ConfigServices service : servicesToSave) { + configServicesRepository.save(service); + } + for (ConfigNetworks network : networksToSave) { + configNetworksRepository.save(network); + } + for (ConfigVnfs vnf: vnfsToSave) { + configVnfsRepository.save(vnf); + } + for (ConfigVfModules vfModule : vfModulesToSave) { + configVfModulesRepository.save(vfModule); + } + for (ConfigServices service : servicesToRemove) { + configServicesRepository.delete(service); + } + for (ConfigNetworks network : networksToSave) { + configNetworksRepository.delete(network); + } + for (ConfigVnfs vnf: vnfsToSave) { + configVnfsRepository.delete(vnf); + } + for (ConfigVfModules vfModule : vfModulesToSave) { + configVfModulesRepository.delete(vfModule); + } + } + } + + public ServiceDataTransaction createTransaction() { + return new ServiceDataTransaction(); + } + + public GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule getVfModule(String svcInstanceId, + String vnfId, String vfModuleId) throws JsonProcessingException { + GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule vfModule = null; + List<ConfigVfModules> configVfModules = configVfModulesRepository + .findBySvcInstanceIdAndVnfIdAndVfModuleId(svcInstanceId, vnfId, vfModuleId); + if ((configVfModules != null) && !configVfModules.isEmpty()) { + ConfigVfModules configVfModule = configVfModules.get(0); + vfModule = new GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule(); + vfModule.setVfModuleId(configVfModule.getVfModuleId()); + String vfModuleDataAsString = configVfModule.getVfModuleData(); + if (vfModuleDataAsString != null) { + vfModule.setVfModuleData(objectMapper.readValue(vfModuleDataAsString, + GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfmoduleVfModuleData.class)); + } + + } + return (vfModule); + } + + public GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules getVfModules(String svcInstanceId, String vnfId) throws JsonProcessingException { + GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules vfModules = null; + List<ConfigVfModules> configVfModules = configVfModulesRepository.findBySvcInstanceIdAndVnfId(svcInstanceId, vnfId); + if ((configVfModules != null) && !configVfModules.isEmpty()) { + vfModules = new GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules(); + for (ConfigVfModules configVfModule : configVfModules) { + GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule vfModule = new GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule(); + vfModule.setVfModuleId(configVfModule.getVfModuleId()); + String vfModuleDataAsString = configVfModule.getVfModuleData(); + if (vfModuleDataAsString != null) { + vfModule.setVfModuleData(objectMapper.readValue(vfModuleDataAsString, GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfmoduleVfModuleData.class)); + } + vfModules.addVfModuleItem(vfModule); + } + } + return(vfModules); + } + + public GenericResourceApiServicedataServicedataVnfsVnf getVnf(String svcInstanceId, String vnfId) throws JsonProcessingException { + GenericResourceApiServicedataServicedataVnfsVnf vnf = null; + List<ConfigVnfs> configVnfs = configVnfsRepository.findBySvcInstanceIdAndVnfId(svcInstanceId, vnfId); + if ((configVnfs != null) && !configVnfs.isEmpty()) { + ConfigVnfs configVnf = configVnfs.get(0); + vnf = new GenericResourceApiServicedataServicedataVnfsVnf(); + vnf.setVnfId(configVnf.getVnfId()); + GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfDataAsObject = null; + String vnfDataAsString = configVnf.getVnfData(); + if (vnfDataAsString != null) { + vnfDataAsObject = objectMapper.readValue(vnfDataAsString, GenericResourceApiServicedataServicedataVnfsVnfVnfData.class); + } + + GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules vfModules = getVfModules(svcInstanceId, vnfId); + if (vfModules != null) { + if (vnfDataAsObject == null) { + vnfDataAsObject = new GenericResourceApiServicedataServicedataVnfsVnfVnfData(); + } + vnfDataAsObject.setVfModules(vfModules); + } + vnf.setVnfData(vnfDataAsObject); + } + return(vnf); + } + + public GenericResourceApiServicedataServicedataVnfs getVnfs(String svcInstanceId) throws JsonProcessingException { + + GenericResourceApiServicedataServicedataVnfs vnfs = null; + + List<ConfigVnfs> configVnfs = configVnfsRepository.findBySvcInstanceId(svcInstanceId); + if ((configVnfs != null) && !configVnfs.isEmpty()) { + vnfs = new GenericResourceApiServicedataServicedataVnfs(); + for (ConfigVnfs configVnf : configVnfs) { + GenericResourceApiServicedataServicedataVnfsVnf vnf = new GenericResourceApiServicedataServicedataVnfsVnf(); + vnf.setVnfId(configVnf.getVnfId()); + GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfDataAsObject = null; + String vnfDataAsString = configVnf.getVnfData(); + if (vnfDataAsString != null) { + vnfDataAsObject = objectMapper.readValue(vnfDataAsString, + GenericResourceApiServicedataServicedataVnfsVnfVnfData.class); + } + + GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules vfModules = getVfModules(svcInstanceId, configVnf.getVnfId()); + if (vfModules != null) { + if (vnfDataAsObject == null) { + vnfDataAsObject = new GenericResourceApiServicedataServicedataVnfsVnfVnfData(); + } + vnfDataAsObject.setVfModules(vfModules); + } + vnf.setVnfData(vnfDataAsObject); + vnfs.addVnfItem(vnf); + } + } + return (vnfs); + } + + public GenericResourceApiServicedataServicedataNetworks getNetworks(String svcInstanceId) throws JsonProcessingException { + GenericResourceApiServicedataServicedataNetworks networks = null; + List<ConfigNetworks> configNetworks = configNetworksRepository.findBySvcInstanceId(svcInstanceId); + if ((configNetworks != null) && !configNetworks.isEmpty()) { + networks = new GenericResourceApiServicedataServicedataNetworks(); + + for (ConfigNetworks configNetwork : configNetworks) { + GenericResourceApiServicedataServicedataNetworksNetwork network = new GenericResourceApiServicedataServicedataNetworksNetwork(); + network.setNetworkId(configNetwork.getNetworkId()); + String networkDataAsString = configNetwork.getNetworkData(); + if (networkDataAsString != null) { + network.setNetworkData(objectMapper.readValue(networkDataAsString, GenericResourceApiServicedataServicedataNetworksNetworkNetworkData.class)); + } + networks.addNetworkItem(network); + } + } + + return(networks); + } + + public String getServiceDataAsString(String svcInstanceId) throws JsonProcessingException { + GenericResourceApiServicedataServiceData svcData = getServiceData(svcInstanceId); + if (svcData == null) { + return(null); + } else { + return(objectMapper.writeValueAsString(getServiceData(svcInstanceId))); + } + } + + public GenericResourceApiServicedataServiceData getServiceData(String svcInstanceId) throws JsonProcessingException { + + List<ConfigServices> configServices = configServicesRepository.findBySvcInstanceId(svcInstanceId); + if ((configServices != null) && !configServices.isEmpty()) { + return(getServiceData(configServices.get(0))); + } else { + return(null); + } + } + + public GenericResourceApiServicedataServiceData getServiceData(ConfigServices configService) throws JsonProcessingException { + if (configService == null) { + return(null); + } + + GenericResourceApiServicedataServiceData svcData; + String svcInstanceId = configService.getSvcInstanceId(); + String svcDataAsString = configService.getSvcData(); + + if (svcDataAsString != null) { + svcData = objectMapper.readValue(svcDataAsString, GenericResourceApiServicedataServiceData.class); + } else { + svcData = new GenericResourceApiServicedataServiceData(); + } + + // Add networks + GenericResourceApiServicedataServicedataNetworks networks = getNetworks(svcInstanceId); + if (networks != null) { + svcData.setNetworks(networks); + } + + //Add Vnfs + GenericResourceApiServicedataServicedataVnfs vnfs = getVnfs(svcInstanceId); + if (vnfs != null) { + svcData.setVnfs(vnfs); + } + + return(svcData); + + } + + public void saveVfModule(String svcInstanceId, String vnfId, GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule vfModule, ServiceDataTransaction transaction) throws JsonProcessingException { + if (vfModule == null) { + return; + } + List<ConfigVfModules> configVfModules = configVfModulesRepository.findBySvcInstanceIdAndVnfIdAndVfModuleId(svcInstanceId, vnfId, vfModule.getVfModuleId()); + ConfigVfModules configVfModule; + if ((configVfModules != null) && !configVfModules.isEmpty()) { + configVfModule = configVfModules.get(0); + } else { + configVfModule = new ConfigVfModules(svcInstanceId, vnfId, vfModule.getVfModuleId()); + } + if (vfModule.getVfModuleData() != null) { + configVfModule.setVfModuleData(objectMapper.writeValueAsString(vfModule.getVfModuleData())); + } + if (transaction != null) { + transaction.save(configVfModule); + } else { + configVfModulesRepository.save(configVfModule); + } + } + + public void saveVnf(String svcInstanceId, GenericResourceApiServicedataServicedataVnfsVnf vnf, ServiceDataTransaction transaction) throws JsonProcessingException { + if (vnf == null) { + return; + } + + GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData = vnf.getVnfData(); + + // Make a list of potential vf modules to remove + HashMap<String, ConfigVfModules> vfModuleMap = new HashMap<>(); + List<ConfigVfModules> configVfModuleList = configVfModulesRepository.findBySvcInstanceIdAndVnfId(svcInstanceId, vnf.getVnfId()); + if ((configVfModuleList != null) && !configVfModuleList.isEmpty()) { + for (ConfigVfModules configVfModuleItem : configVfModuleList) { + vfModuleMap.put(configVfModuleItem.getVfModuleId(), configVfModuleItem); + } + } + + + // Save vf modules + GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules vfModules = vnfData.getVfModules(); + if (vfModules != null) { + List<GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule> vfModuleList = vfModules.getVfModule(); + if ((vfModuleList != null) && !vfModuleList.isEmpty()) { + for (GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule vfModule : vfModuleList) { + saveVfModule(svcInstanceId, vnf.getVnfId(), vfModule, transaction); + if (vfModuleMap.containsKey(vfModule.getVfModuleId())) { + vfModuleMap.remove(vfModule.getVfModuleId()); + } + } + } + + // Remove any vf Modules for this vnf id not in this list + for (ConfigVfModules vfModuleToRemove : vfModuleMap.values()) { + if (transaction != null) { + transaction.remove(vfModuleToRemove); + } else { + configVfModulesRepository.delete(vfModuleToRemove); + } + } + + // Clear vf modules from vnfData before saving to avoid duplication + vnfData.setVfModules(null); + } + + // Save Vnf itself + List<ConfigVnfs> configVnfs = configVnfsRepository.findBySvcInstanceIdAndVnfId(svcInstanceId, vnf.getVnfId()); + ConfigVnfs configVnf; + if ((configVnfs != null) && !configVnfs.isEmpty()) { + configVnf = configVnfs.get(0); + } else { + configVnf = new ConfigVnfs(svcInstanceId, vnf.getVnfId()); + } + configVnf.setVnfData(objectMapper.writeValueAsString(vnfData)); + + if (transaction != null) { + transaction.save(configVnf); + } else { + configVnfsRepository.save(configVnf); + } + } + + public void saveNetwork(String svcInstanceId, GenericResourceApiServicedataServicedataNetworksNetwork network, ServiceDataTransaction transaction) throws JsonProcessingException { + List<ConfigNetworks> configNetworks = configNetworksRepository.findBySvcInstanceIdAndNetworkId(svcInstanceId, network.getNetworkId()); + ConfigNetworks configNetwork; + + if ((configNetworks != null) && !configNetworks.isEmpty()) { + configNetwork = configNetworks.get(0); + } else { + configNetwork = new ConfigNetworks(svcInstanceId, network.getNetworkId()); + } + configNetwork.setNetworkData(objectMapper.writeValueAsString(network.getNetworkData())); + if (transaction != null) { + transaction.save(configNetwork); + } else { + configNetworksRepository.save(configNetwork); + } + } + + public void saveService(ConfigServices configService, String svcDataAsString) throws JsonMappingException, JsonProcessingException { + saveService(configService, svcDataAsString, null); + } + + + public void saveService(ConfigServices configService, String svcDataAsString, ServiceDataTransaction transaction) throws JsonMappingException, JsonProcessingException { + if (svcDataAsString == null) { + configServicesRepository.save(configService); + } else { + saveService(configService, objectMapper.readValue(svcDataAsString, GenericResourceApiServicedataServiceData.class), transaction); + } + } + + public void saveService(ConfigServices configService, GenericResourceApiServicedataServiceData svcData) throws JsonProcessingException { + saveService(configService, svcData, null); + } + + public void saveService(ConfigServices configService, GenericResourceApiServicedataServiceData svcData, ServiceDataTransaction transaction) throws JsonProcessingException { + + if (svcData != null) { + String svcInstanceId = configService.getSvcInstanceId(); + + // Save networks + GenericResourceApiServicedataServicedataNetworks networks = svcData.getNetworks(); + if (networks != null) { + List<GenericResourceApiServicedataServicedataNetworksNetwork> networkList = networks.getNetwork(); + if ((networkList != null) && !networkList.isEmpty()) { + for (GenericResourceApiServicedataServicedataNetworksNetwork networkItem : networkList) { + saveNetwork(svcInstanceId, networkItem, transaction); + } + } + } + svcData.setNetworks(null); + + // Save vnfs / vfModules + GenericResourceApiServicedataServicedataVnfs vnfs = svcData.getVnfs(); + if (vnfs != null) { + List<GenericResourceApiServicedataServicedataVnfsVnf> vnfList = vnfs.getVnf(); + if ((vnfList != null) && !vnfList.isEmpty()) { + for (GenericResourceApiServicedataServicedataVnfsVnf vnfItem : vnfList) { + saveVnf(svcInstanceId, vnfItem, transaction); + } + } + } + svcData.setVnfs(null); + + configService.setSvcData(objectMapper.writeValueAsString(svcData)); + } else { + + } + if (transaction != null) { + transaction.save(configService); + } else { + configServicesRepository.save(configService); + } + } + + public long deleteVfModule(String svcInstanceId, String vnfId, String vfModuleId) { + return(configVfModulesRepository.deleteBySvcInstanceIdAndVnfIdAndVfModuleId(svcInstanceId, vnfId, vfModuleId)); + } + + public long deleteVnf(String svcInstanceId, String vnfId) { + configVfModulesRepository.deleteBySvcInstanceIdAndVnfId(svcInstanceId, vnfId); + return(configVnfsRepository.deleteBySvcInstanceIdAndVnfId(svcInstanceId, vnfId)); + } + + public long deleteNetwork(String svcInstanceId, String networkId) { + return(configNetworksRepository.deleteBySvcInstanceIdAndNetworkId(svcInstanceId, networkId)); + } + + public long deleteService(String svcInstanceId) { + configVfModulesRepository.deleteBySvcInstanceId(svcInstanceId); + configVnfsRepository.deleteBySvcInstanceId(svcInstanceId); + configNetworksRepository.deleteBySvcInstanceId(svcInstanceId); + return(configServicesRepository.deleteBySvcInstanceId(svcInstanceId)); + } + + public boolean hasServiceData(ConfigServices service) { + if ((service.getSvcData() != null) && (service.getSvcData().length() > 0)) { + return(true); + } + + List<ConfigNetworks> networks = configNetworksRepository.findBySvcInstanceId(service.getSvcInstanceId()); + if ((networks != null) && !networks.isEmpty()) { + return(true); + } + + List<ConfigVnfs> vnfs = configVnfsRepository.findBySvcInstanceId(service.getSvcInstanceId()); + if ((vnfs != null) && !vnfs.isEmpty()) { + return(true); + } + + return(false); + + } + public void clearServiceData(ConfigServices service) { + configVfModulesRepository.deleteBySvcInstanceId(service.getSvcInstanceId()); + configVnfsRepository.deleteBySvcInstanceId(service.getSvcInstanceId()); + configNetworksRepository.deleteBySvcInstanceId(service.getSvcInstanceId()); + service.setSvcData(null); + configServicesRepository.save(service); + } + +} diff --git a/ms/generic-resource-api/src/main/java/org/onap/sdnc/apps/ms/gra/data/ConfigServices.java b/ms/generic-resource-api/src/main/java/org/onap/sdnc/apps/ms/gra/data/ConfigServices.java index 2c89ed4..bf5d862 100644 --- a/ms/generic-resource-api/src/main/java/org/onap/sdnc/apps/ms/gra/data/ConfigServices.java +++ b/ms/generic-resource-api/src/main/java/org/onap/sdnc/apps/ms/gra/data/ConfigServices.java @@ -41,11 +41,30 @@ public class ConfigServices { this.svcData = ""; } + public ConfigServices(String svcInstanceId) { + this.svcInstanceId = svcInstanceId; + this.svcData = ""; + } + public ConfigServices(String svcInstanceId, String svcData) { this.svcInstanceId = svcInstanceId; this.svcData = svcData; } + public ConfigServices(String svcInstanceId, GenericResourceApiServicestatusServiceStatus serviceStatus) { + this.svcInstanceId = svcInstanceId; + this.svcData = ""; + + if (serviceStatus != null) { + this.serviceStatusAction = serviceStatus.getAction(); + this.serviceStatusFinalIndicator = serviceStatus.getFinalIndicator(); + this.serviceStatusRequestStatus = serviceStatus.getRequestStatus().toString(); + this.serviceStatusResponseCode = serviceStatus.getResponseCode(); + this.serviceStatusResponseMessage = serviceStatus.getResponseMessage(); + this.serviceStatusResponseTimestamp = serviceStatus.getResponseTimestamp(); + } + } + public ConfigServices(String svcInstanceId, String svcData, GenericResourceApiServicestatusServiceStatus serviceStatus) { this.svcInstanceId = svcInstanceId; this.svcData = svcData; |