diff options
author | 2023-01-23 14:59:18 +0000 | |
---|---|---|
committer | 2023-01-23 14:59:29 +0000 | |
commit | 9783df1b5660ee0e0460cfd8dc5477a7edcb1d94 (patch) | |
tree | e3ffe3209ff38f839cd1af60a4f0dffd341dd1ee /so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src | |
parent | bd591af78a4ab97af4f65d6b7094d9c60f7879c4 (diff) |
Adding Create AS support
Change-Id: I4b1d417c7f20d67d5fb0d05718819186f8dd3bea
Issue-ID: SO-4068
Signed-off-by: waqas.ikram <waqas.ikram@est.tech>
Diffstat (limited to 'so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src')
10 files changed, 724 insertions, 0 deletions
diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/Constants.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/Constants.java new file mode 100644 index 0000000..d7237c9 --- /dev/null +++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/Constants.java @@ -0,0 +1,35 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.cnfm.lcm; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +public class Constants { + + public static final String SERVICE_NAME = "so-cnfm"; + public static final String SERVICE_VERSION = "v1"; + public static final String BASE_URL = "/so/" + SERVICE_NAME + "/" + SERVICE_VERSION + "/api"; + public static final String AS_LIFE_CYCLE_MANAGEMENT_BASE_URL = BASE_URL + "/aslcm/v1"; + + private Constants() {} + +} diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/GsonSerializerConfiguration.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/GsonSerializerConfiguration.java new file mode 100644 index 0000000..f979f99 --- /dev/null +++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/GsonSerializerConfiguration.java @@ -0,0 +1,42 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.cnfm.lcm; + +import java.time.LocalDateTime; +import org.onap.so.cnfm.lcm.bpmn.flows.utils.LocalDateTimeTypeAdapter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import com.google.gson.GsonBuilder; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@Configuration +public class GsonSerializerConfiguration { + + @Bean + public GsonBuilder gsonBuilder() { + return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeTypeAdapter()); + } + +} + + diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/SoCnfmAsLcmManagerUrlProvider.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/SoCnfmAsLcmManagerUrlProvider.java new file mode 100644 index 0000000..2e8aa05 --- /dev/null +++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/SoCnfmAsLcmManagerUrlProvider.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.cnfm.lcm; + +import java.net.URI; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; + + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@Configuration +public class SoCnfmAsLcmManagerUrlProvider { + + private final String soCnfmLcmManagerEndpoint; + + @Autowired + public SoCnfmAsLcmManagerUrlProvider( + @Value("${so-cnfm-lcm.endpoint:http://so-cnfm-lcm.onap:9888}") final String soCnfmLcmManagerEndpoint) { + this.soCnfmLcmManagerEndpoint = soCnfmLcmManagerEndpoint; + } + + public URI getCreatedAsResourceUri(final String asInstanceId) { + return URI.create(soCnfmLcmManagerEndpoint + Constants.AS_LIFE_CYCLE_MANAGEMENT_BASE_URL + "/as_instances/" + + asInstanceId); + } + + public URI getAsLcmOpOccUri(final String asLcmOpOccId) { + return URI.create(soCnfmLcmManagerEndpoint + Constants.AS_LIFE_CYCLE_MANAGEMENT_BASE_URL + "/as_lcm_op_occs/" + + asLcmOpOccId); + } + +} diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/lifecycle/AsLcmOperationOccurrenceManager.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/lifecycle/AsLcmOperationOccurrenceManager.java new file mode 100644 index 0000000..8bb1c6b --- /dev/null +++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/lifecycle/AsLcmOperationOccurrenceManager.java @@ -0,0 +1,125 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.cnfm.lcm.lifecycle; + +import static org.slf4j.LoggerFactory.getLogger; +import java.util.Optional; +import org.onap.so.cnfm.lcm.SoCnfmAsLcmManagerUrlProvider; +import org.onap.so.cnfm.lcm.database.service.DatabaseServiceProvider; +import org.onap.so.cnfm.lcm.model.AsInstanceLinksSelf; +import org.onap.so.cnfm.lcm.model.AsLcmOpOcc; +import org.onap.so.cnfm.lcm.model.AsLcmOpOcc.CancelModeEnum; +import org.onap.so.cnfm.lcm.model.AsLcmOpOcc.OperationEnum; +import org.onap.so.cnfm.lcm.model.AsLcmOpOcc.OperationStateEnum; +import org.onap.so.cnfm.lcm.model.AsLcmOpOccLinks; +import org.slf4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@Service +public class AsLcmOperationOccurrenceManager { + private static final Logger logger = getLogger(AsLcmOperationOccurrenceManager.class); + private final DatabaseServiceProvider databaseServiceProvider; + private final SoCnfmAsLcmManagerUrlProvider cnfmAsLcmManagerUrlProvider; + + @Autowired + public AsLcmOperationOccurrenceManager(final DatabaseServiceProvider databaseServiceProvider, + final SoCnfmAsLcmManagerUrlProvider cnfmAsLcmManagerUrlProvider) { + this.databaseServiceProvider = databaseServiceProvider; + this.cnfmAsLcmManagerUrlProvider = cnfmAsLcmManagerUrlProvider; + } + + public Optional<AsLcmOpOcc> getAsLcmOperationOccurrence(final String asLcmOpOccId) { + logger.info("Getting AS LCM Operation Occurrence Operation for id: {}", asLcmOpOccId); + + final Optional<org.onap.so.cnfm.lcm.database.beans.AsLcmOpOcc> optionalDatabaseEntry = + databaseServiceProvider.getAsLcmOpOcc(asLcmOpOccId); + + if (optionalDatabaseEntry.isEmpty()) { + logger.info("No AS LCM Operation Occurrence found for id: {}", asLcmOpOccId); + return Optional.empty(); + } + logger.info("Found AS LCM Operation Occurrence for id: {}", asLcmOpOccId); + final org.onap.so.cnfm.lcm.database.beans.AsLcmOpOcc asLcmOpOccDatabaseEntry = optionalDatabaseEntry.get(); + final AsLcmOpOcc asLcmOpOcc = convertToAsLcmOpOccsAsLcmOpOcc(asLcmOpOccDatabaseEntry); + return Optional.of(asLcmOpOcc); + } + + private AsLcmOpOcc convertToAsLcmOpOccsAsLcmOpOcc( + final org.onap.so.cnfm.lcm.database.beans.AsLcmOpOcc databaseEntry) { + + final AsLcmOpOcc asLcmOpOcc = new AsLcmOpOcc().id(databaseEntry.getId()) + .stateEnteredTime(databaseEntry.getStateEnteredTime()).startTime(databaseEntry.getStartTime()) + .isAutomaticInvocation(databaseEntry.getIsAutoInvocation()) + .isCancelPending(databaseEntry.getIsCancelPending()); + + if (databaseEntry.getAsInst() != null) { + asLcmOpOcc.setAsInstanceId(databaseEntry.getAsInst().getAsInstId()); + } + + if (databaseEntry.getOperationState() != null) { + asLcmOpOcc.setOperationState(OperationStateEnum.fromValue(databaseEntry.getOperationState().toString())); + } + + if (databaseEntry.getOperation() != null) { + asLcmOpOcc.setOperation(OperationEnum.fromValue(databaseEntry.getOperation().toString())); + } + + if (databaseEntry.getOperationParams() != null) { + asLcmOpOcc.setOperationParams(databaseEntry.getOperationParams()); + } + + if (databaseEntry.getCancelMode() != null) { + asLcmOpOcc.setCancelMode(CancelModeEnum.fromValue(databaseEntry.getCancelMode().toString())); + } + + asLcmOpOcc.setLinks(generateLinks(databaseEntry)); + + logger.info("Database AsLcmOpOcc converted to API AsLcmOpOcc successfully... {}", asLcmOpOcc); + + return asLcmOpOcc; + } + + private AsLcmOpOccLinks generateLinks(final org.onap.so.cnfm.lcm.database.beans.AsLcmOpOcc databaseEntry) { + logger.info("Generating links..."); + final String asLcmOpOccId = databaseEntry.getId(); + + final AsInstanceLinksSelf asLcmOpOccLinksSelf = + new AsInstanceLinksSelf().href(cnfmAsLcmManagerUrlProvider.getAsLcmOpOccUri(asLcmOpOccId).toString()); + + final AsLcmOpOccLinks links = new AsLcmOpOccLinks().self(asLcmOpOccLinksSelf); + + if (databaseEntry.getAsInst() != null) { + final String asInstId = databaseEntry.getAsInst().getAsInstId(); + final AsInstanceLinksSelf asInstanceLinksSelf = new AsInstanceLinksSelf() + .href(cnfmAsLcmManagerUrlProvider.getCreatedAsResourceUri(asInstId).toString()); + links.setAsInstance(asInstanceLinksSelf); + } + + return links; + + } + +} diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/lifecycle/AsLifeCycleManager.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/lifecycle/AsLifeCycleManager.java new file mode 100644 index 0000000..7ba8f8b --- /dev/null +++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/lifecycle/AsLifeCycleManager.java @@ -0,0 +1,86 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.cnfm.lcm.lifecycle; + +import static org.slf4j.LoggerFactory.getLogger; +import java.net.URI; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.onap.so.cnfm.lcm.SoCnfmAsLcmManagerUrlProvider; +import org.onap.so.cnfm.lcm.bpmn.flows.service.JobExecutorService; +import org.onap.so.cnfm.lcm.model.AsInstance; +import org.onap.so.cnfm.lcm.model.CreateAsRequest; +import org.onap.so.cnfm.lcm.model.InstantiateAsRequest; +import org.onap.so.cnfm.lcm.model.TerminateAsRequest; +import org.slf4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@Service +public class AsLifeCycleManager { + private static final Logger logger = getLogger(AsLifeCycleManager.class); + + private final JobExecutorService jobExecutorService; + + private final SoCnfmAsLcmManagerUrlProvider cnfmAsLcmManagerUrlProvider; + + @Autowired + public AsLifeCycleManager(final JobExecutorService jobExecutorService, + final SoCnfmAsLcmManagerUrlProvider cnfmAsLcmManagerUrlProvider) { + this.jobExecutorService = jobExecutorService; + this.cnfmAsLcmManagerUrlProvider = cnfmAsLcmManagerUrlProvider; + } + + public ImmutablePair<URI, AsInstance> createAs(final CreateAsRequest createAsRequest) { + logger.info("Will execute Create AS for CreateAsRequest: {}", createAsRequest); + + final AsInstance nsInstanceResponse = jobExecutorService.runCreateAsJob(createAsRequest); + + return ImmutablePair.of( + cnfmAsLcmManagerUrlProvider.getCreatedAsResourceUri(nsInstanceResponse.getAsInstanceid()), + nsInstanceResponse); + } + + public URI instantiateAs(final String asInstanceId, final InstantiateAsRequest instantiateAsRequest) { + logger.info("Will execute Instantiate AS for InstantiateAsRequest: {} and asInstanceId: {}", + instantiateAsRequest, asInstanceId); + + final String asLcmOpOccId = jobExecutorService.runInstantiateAsJob(asInstanceId, instantiateAsRequest); + return cnfmAsLcmManagerUrlProvider.getAsLcmOpOccUri(asLcmOpOccId); + + } + + public URI terminateAs(final String asInstanceId, final TerminateAsRequest terminateAsRequest) { + logger.info("Will execute Terminate AS for TerminateAsRequest: {} and asInstanceId: {}", terminateAsRequest, + asInstanceId); + + final String asLcmOpOccId = jobExecutorService.runTerminateAsJob(asInstanceId, terminateAsRequest); + return cnfmAsLcmManagerUrlProvider.getAsLcmOpOccUri(asLcmOpOccId); + } + + public void deleteAs(final String asInstanceId) { + logger.info("Will execute Delete AS for asInstanceId: {}", asInstanceId); + jobExecutorService.runDeleteAsJob(asInstanceId); + } + +} diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/rest/AsLcmOperationOccurrencesController.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/rest/AsLcmOperationOccurrencesController.java new file mode 100644 index 0000000..8c25d20 --- /dev/null +++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/rest/AsLcmOperationOccurrencesController.java @@ -0,0 +1,71 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.cnfm.lcm.rest; + +import static org.onap.so.cnfm.lcm.Constants.AS_LIFE_CYCLE_MANAGEMENT_BASE_URL; +import static org.slf4j.LoggerFactory.getLogger; +import java.util.Optional; +import javax.ws.rs.core.MediaType; +import org.onap.so.cnfm.lcm.lifecycle.AsLcmOperationOccurrenceManager; +import org.onap.so.cnfm.lcm.model.AsLcmOpOcc; +import org.onap.so.cnfm.lcm.model.ErrorDetails; +import org.slf4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; + +/** + * + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@Controller +@RequestMapping(value = AS_LIFE_CYCLE_MANAGEMENT_BASE_URL) +public class AsLcmOperationOccurrencesController { + private static final Logger logger = getLogger(AsLcmOperationOccurrencesController.class); + private final AsLcmOperationOccurrenceManager asLcmOperationOccurrenceManager; + + @Autowired + public AsLcmOperationOccurrencesController(final AsLcmOperationOccurrenceManager asLcmOperationOccurrenceManager) { + this.asLcmOperationOccurrenceManager = asLcmOperationOccurrenceManager; + } + + @GetMapping(value = "/as_lcm_op_occs/{asLcmOpOccId}", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getOperationStatus(@PathVariable("asLcmOpOccId") final String asLcmOpOccId) { + logger.info("Received request to retrieve operation status for asLcmOpOccId: {}", asLcmOpOccId); + final Optional<AsLcmOpOcc> optionalAsLcmOpOccs = + asLcmOperationOccurrenceManager.getAsLcmOperationOccurrence(asLcmOpOccId); + + if (optionalAsLcmOpOccs.isPresent()) { + final AsLcmOpOcc asLcmOpOcc = optionalAsLcmOpOccs.get(); + logger.info("Sending back AsLcmOpOcc: {}", asLcmOpOcc); + return ResponseEntity.ok().body(asLcmOpOcc); + } + + final String errorMessage = "Unable to retrieve operation occurrence status for asLcmOpOccId: " + asLcmOpOccId; + logger.error(errorMessage); + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorDetails().detail(errorMessage)); + } +} diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/rest/AsLifecycleManagementController.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/rest/AsLifecycleManagementController.java new file mode 100644 index 0000000..698ef7c --- /dev/null +++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/rest/AsLifecycleManagementController.java @@ -0,0 +1,107 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.cnfm.lcm.rest; + +import static org.onap.so.cnfm.lcm.Constants.AS_LIFE_CYCLE_MANAGEMENT_BASE_URL; +import static org.slf4j.LoggerFactory.getLogger; +import java.net.URI; +import javax.ws.rs.core.MediaType; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.onap.so.cnfm.lcm.lifecycle.AsLifeCycleManager; +import org.onap.so.cnfm.lcm.model.AsInstance; +import org.onap.so.cnfm.lcm.model.CreateAsRequest; +import org.onap.so.cnfm.lcm.model.InstantiateAsRequest; +import org.onap.so.cnfm.lcm.model.TerminateAsRequest; +import org.slf4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@Controller +@RequestMapping(value = AS_LIFE_CYCLE_MANAGEMENT_BASE_URL) +public class AsLifecycleManagementController { + private static final Logger logger = getLogger(AsLifecycleManagementController.class); + + private final AsLifeCycleManager asLifeCycleManager; + + @Autowired + public AsLifecycleManagementController(final AsLifeCycleManager asLifeCycleManager) { + this.asLifeCycleManager = asLifeCycleManager; + } + + @PostMapping(value = "/as_instances", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<AsInstance> createAs(@RequestBody final CreateAsRequest createAsRequest) { + logger.info("Received Create AS Request: {}n", createAsRequest); + + final ImmutablePair<URI, AsInstance> nsInstance = asLifeCycleManager.createAs(createAsRequest); + + final URI resourceUri = nsInstance.getLeft(); + final AsInstance createdAsresponse = nsInstance.getRight(); + + logger.info("AS resource created successfully. Resource location: {}, response: {}", resourceUri, + createdAsresponse); + + return ResponseEntity.created(resourceUri).body(createdAsresponse); + } + + @PostMapping(value = "/as_instances/{asInstanceId}/instantiate", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<Void> instantiateAs(@PathVariable("asInstanceId") final String asInstanceId, + @RequestBody final InstantiateAsRequest instantiateAsRequest) { + logger.debug("Received instantiate AS request: {}\n with asInstanceId: {}", instantiateAsRequest, asInstanceId); + final URI resourceUri = asLifeCycleManager.instantiateAs(asInstanceId, instantiateAsRequest); + logger.info("{} AS Instantiation started successfully. Resource Operation Occurrence uri: {}", asInstanceId, + resourceUri); + return ResponseEntity.accepted().location(resourceUri).build(); + } + + @PostMapping(value = "/as_instances/{asInstanceId}/terminate", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<Void> terminateAs(@PathVariable("asInstanceId") final String asInstanceId, + @RequestBody(required = false) final TerminateAsRequest terminateAsRequest) { + logger.debug("Received terminate AS request: {}\n with asInstanceId: {}", terminateAsRequest, asInstanceId); + final URI resourceUri = asLifeCycleManager.terminateAs(asInstanceId, terminateAsRequest); + logger.info("{} As Terminate started successfully. Resource Operation Occurrence uri: {}", asInstanceId, + resourceUri); + return ResponseEntity.accepted().location(resourceUri).build(); + } + + @DeleteMapping(value = "/as_instances/{asInstanceId}", + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<Void> deleteAs(@PathVariable("asInstanceId") final String asInstanceId) { + logger.debug("Received delete AS request for asInstanceId: {}", asInstanceId); + asLifeCycleManager.deleteAs(asInstanceId); + logger.info("Successfully deleted AS for asInstanceId: {}", asInstanceId); + return ResponseEntity.noContent().build(); + } + +} diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/test/java/org/onap/so/cnfm/lcm/TestApplication.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/test/java/org/onap/so/cnfm/lcm/TestApplication.java new file mode 100644 index 0000000..c2775fb --- /dev/null +++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/test/java/org/onap/so/cnfm/lcm/TestApplication.java @@ -0,0 +1,37 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.cnfm.lcm; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@SpringBootApplication(scanBasePackages = {"org.onap.so"}) +@EnableAutoConfiguration(exclude = {JacksonAutoConfiguration.class}) +public class TestApplication { + public static void main(final String[] args) { + new SpringApplication(TestApplication.class).run(args); + } +} diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/test/java/org/onap/so/cnfm/lcm/rest/AsLcmOperationOccurrencesControllerTest.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/test/java/org/onap/so/cnfm/lcm/rest/AsLcmOperationOccurrencesControllerTest.java new file mode 100644 index 0000000..c3dd4d0 --- /dev/null +++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/test/java/org/onap/so/cnfm/lcm/rest/AsLcmOperationOccurrencesControllerTest.java @@ -0,0 +1,125 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.cnfm.lcm.rest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import java.time.LocalDateTime; +import java.util.UUID; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.so.cnfm.lcm.Constants; +import org.onap.so.cnfm.lcm.TestApplication; +import org.onap.so.cnfm.lcm.bpmn.flows.GsonProvider; +import org.onap.so.cnfm.lcm.database.beans.AsInst; +import org.onap.so.cnfm.lcm.database.beans.AsLcmOpType; +import org.onap.so.cnfm.lcm.database.beans.OperationStateEnum; +import org.onap.so.cnfm.lcm.database.beans.State; +import org.onap.so.cnfm.lcm.database.service.DatabaseServiceProvider; +import org.onap.so.cnfm.lcm.model.AsLcmOpOcc; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.json.GsonHttpMessageConverter; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +import com.google.gson.Gson; + +/** + * + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@RunWith(SpringRunner.class) +@SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ActiveProfiles("test") +public class AsLcmOperationOccurrencesControllerTest { + + private static final String AS_LCM_OP_OCCS = "/as_lcm_op_occs/"; + + @LocalServerPort + private int port; + + @Autowired + private DatabaseServiceProvider databaseServiceProvider; + + @Autowired + private GsonProvider gsonProvider; + + private TestRestTemplate testRestTemplate; + + @Before + public void setUp() { + final Gson gson = gsonProvider.getGson(); + testRestTemplate = new TestRestTemplate( + new RestTemplateBuilder().additionalMessageConverters(new GsonHttpMessageConverter(gson))); + } + + @Test + public void testGetOperationStatus_validAsLcmOpOccId_returnsAsLcmOpOcc() { + final String asLcmOpOccId = addDummyAsLcmOpOccToDatabase(); + final String baseUrl = getAsLcmBaseUrl() + AS_LCM_OP_OCCS + asLcmOpOccId; + final HttpEntity<?> request = new HttpEntity<>(new HttpHeaders()); + final ResponseEntity<AsLcmOpOcc> responseEntity = + testRestTemplate.exchange(baseUrl, HttpMethod.GET, request, AsLcmOpOcc.class); + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + assertTrue(responseEntity.hasBody()); + assertNotNull(responseEntity.getBody()); + } + + + private String getAsLcmBaseUrl() { + return "http://localhost:" + port + Constants.AS_LIFE_CYCLE_MANAGEMENT_BASE_URL; + } + + + private String addDummyAsLcmOpOccToDatabase() { + final LocalDateTime now = LocalDateTime.now(); + final AsInst asInst = new AsInst().name("name").asdId(UUID.randomUUID().toString()) + .status(State.NOT_INSTANTIATED).asdInvariantId(UUID.randomUUID().toString()).statusUpdatedTime(now) + .asApplicationName("asApplicationName").asApplicationVersion("asApplicationVersion") + .asProvider("asProvider").serviceInstanceId(UUID.randomUUID().toString()) + .serviceInstanceName("serviceInstanceName").cloudOwner("cloudOwner").cloudRegion("cloudRegion") + .tenantId("tenantId"); + + databaseServiceProvider.saveAsInst(asInst); + + final org.onap.so.cnfm.lcm.database.beans.AsLcmOpOcc databaseEntry = + new org.onap.so.cnfm.lcm.database.beans.AsLcmOpOcc(); + + databaseEntry.asInst(asInst).operationState(OperationStateEnum.PROCESSING).isCancelPending(false) + .isAutoInvocation(false).operation(AsLcmOpType.INSTANTIATE).startTime(now).stateEnteredTime(now) + .operationParams(""); + + databaseServiceProvider.addAsLcmOpOcc(databaseEntry); + + return databaseEntry.getId(); + } + +} diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/test/resources/application.yaml b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/test/resources/application.yaml new file mode 100644 index 0000000..2b590b2 --- /dev/null +++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/test/resources/application.yaml @@ -0,0 +1,43 @@ +# Copyright © 2023 Nordix Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +spring: + main: + allow-bean-definition-overriding: true + datasource: + hikari: + camunda: + jdbcUrl: jdbc:h2:mem:example-simple;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE + driver-class-name: org.h2.Driver + pool-name: cnfm-lcm-bpmn-pool + registerMbeans: true + cnfm: + jdbcUrl: jdbc:h2:mem:nfvo;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;INIT=CREATE SCHEMA IF NOT EXISTS cnfm;MODE=MYSQL;DATABASE_TO_LOWER=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE + driver-class-name: org.h2.Driver + pool-name: cnfm-lcm-bpmn-pool + registerMbeans: true + jpa: + generate-ddl: true + hibernate: + ddl-auto: create +hibernate: + dialect: org.hibernate.dialect.H2Dialect + hbm2ddl: + auto: create +logging: + level: + org.reflections.Reflections: ERROR +cnfm: + kube-configs-dir: ${java.io.tmpdir}/kube-configs + csar: + dir: ${java.io.tmpdir}
\ No newline at end of file |