From 43098043c4ef31d9d5dead66568d7d9482a6b165 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Tue, 25 Jan 2022 19:55:43 +0000 Subject: Rename TOSCA Control Loop to ACM This commit renames the TOSCA Control Loop functionality in CLAMP to Automation Composition Management. This review is a direct renaming review and, as everything is renamed together it is large. Issue-ID: POLICY-3939 Change-Id: I28f0a6dd889bf3570a4c1365ae9e71fc58db6d6c Signed-off-by: liamfallon --- .../acm/participant/kubernetes/Application.java | 49 ++++ .../configurations/ParticipantConfig.java | 44 ++++ .../ParticipantIntermediaryConfig.java | 43 ++++ .../kubernetes/configurations/SecurityConfig.java | 45 ++++ .../kubernetes/configurations/SpringFoxConfig.java | 45 ++++ .../kubernetes/controller/ChartController.java | 195 ++++++++++++++ .../kubernetes/exception/ServiceException.java | 32 +++ .../AutomationCompositionElementHandler.java | 197 +++++++++++++++ .../participant/kubernetes/helm/HelmClient.java | 279 +++++++++++++++++++++ .../kubernetes/helm/PodStatusValidator.java | 119 +++++++++ .../participant/kubernetes/models/ChartInfo.java | 44 ++++ .../participant/kubernetes/models/ChartList.java | 31 +++ .../kubernetes/models/HelmRepository.java | 39 +++ .../kubernetes/models/InstallationInfo.java | 29 +++ .../parameters/ParticipantK8sParameters.java | 52 ++++ .../kubernetes/service/ChartService.java | 141 +++++++++++ .../participant/kubernetes/service/ChartStore.java | 219 ++++++++++++++++ .../participant/kubernetes/Application.java | 45 ---- .../configurations/ParticipantConfig.java | 44 ---- .../ParticipantIntermediaryConfig.java | 43 ---- .../kubernetes/configurations/SecurityConfig.java | 45 ---- .../kubernetes/configurations/SpringFoxConfig.java | 45 ---- .../kubernetes/controller/ChartController.java | 195 -------------- .../kubernetes/exception/ServiceException.java | 32 --- .../handler/ControlLoopElementHandler.java | 196 --------------- .../participant/kubernetes/helm/HelmClient.java | 279 --------------------- .../kubernetes/helm/PodStatusValidator.java | 119 --------- .../participant/kubernetes/models/ChartInfo.java | 44 ---- .../participant/kubernetes/models/ChartList.java | 31 --- .../kubernetes/models/HelmRepository.java | 39 --- .../kubernetes/models/InstallationInfo.java | 29 --- .../parameters/ParticipantK8sParameters.java | 52 ---- .../kubernetes/service/ChartService.java | 141 ----------- .../participant/kubernetes/service/ChartStore.java | 219 ---------------- .../src/main/resources/config/application.yaml | 16 +- .../AutomationCompositionElementHandlerTest.java | 182 ++++++++++++++ .../kubernetes/helm/HelmClientTest.java | 158 ++++++++++++ .../kubernetes/helm/PodStatusValidatorTest.java | 107 ++++++++ .../kubernetes/parameters/CommonTestData.java | 161 ++++++++++++ .../parameters/ParticipantK8sParametersTest.java | 89 +++++++ .../kubernetes/rest/ActuatorControllerTest.java | 92 +++++++ .../kubernetes/rest/ChartControllerTest.java | 249 ++++++++++++++++++ .../kubernetes/service/ChartServiceTest.java | 148 +++++++++++ .../kubernetes/service/ChartStoreTest.java | 175 +++++++++++++ .../kubernetes/utils/CommonActuatorController.java | 114 +++++++++ .../participant/kubernetes/utils/TestUtils.java | 47 ++++ .../handler/ControlLoopElementHandlerTest.java | 180 ------------- .../kubernetes/helm/HelmClientTest.java | 158 ------------ .../kubernetes/helm/PodStatusValidatorTest.java | 109 -------- .../kubernetes/parameters/CommonTestData.java | 161 ------------ .../parameters/ParticipantK8sParametersTest.java | 89 ------- .../kubernetes/rest/ActuatorControllerTest.java | 92 ------- .../kubernetes/rest/ChartControllerTest.java | 249 ------------------ .../kubernetes/service/ChartServiceTest.java | 148 ----------- .../kubernetes/service/ChartStoreTest.java | 175 ------------- .../kubernetes/utils/CommonActuatorController.java | 114 --------- .../participant/kubernetes/utils/TestUtils.java | 47 ---- .../src/test/resources/application_test.properties | 22 +- .../resources/servicetemplates/KubernetesHelm.yaml | 58 ++--- 59 files changed, 3173 insertions(+), 3168 deletions(-) create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/Application.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/configurations/ParticipantConfig.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/configurations/ParticipantIntermediaryConfig.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/configurations/SecurityConfig.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/configurations/SpringFoxConfig.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/controller/ChartController.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/exception/ServiceException.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/handler/AutomationCompositionElementHandler.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/HelmClient.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidator.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/ChartInfo.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/ChartList.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/HelmRepository.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/InstallationInfo.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/parameters/ParticipantK8sParameters.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/service/ChartService.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/service/ChartStore.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/Application.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/configurations/ParticipantConfig.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/configurations/ParticipantIntermediaryConfig.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/configurations/SecurityConfig.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/configurations/SpringFoxConfig.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/controller/ChartController.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/exception/ServiceException.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/handler/ControlLoopElementHandler.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/HelmClient.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/PodStatusValidator.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/models/ChartInfo.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/models/ChartList.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/models/HelmRepository.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/models/InstallationInfo.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/parameters/ParticipantK8sParameters.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/service/ChartService.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/service/ChartStore.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/handler/AutomationCompositionElementHandlerTest.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/HelmClientTest.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidatorTest.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/parameters/CommonTestData.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/parameters/ParticipantK8sParametersTest.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/rest/ActuatorControllerTest.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/rest/ChartControllerTest.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/service/ChartServiceTest.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/service/ChartStoreTest.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/utils/CommonActuatorController.java create mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/utils/TestUtils.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/handler/ControlLoopElementHandlerTest.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/HelmClientTest.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/PodStatusValidatorTest.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/parameters/CommonTestData.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/parameters/ParticipantK8sParametersTest.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/rest/ActuatorControllerTest.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/rest/ChartControllerTest.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/service/ChartServiceTest.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/service/ChartStoreTest.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/utils/CommonActuatorController.java delete mode 100644 participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/utils/TestUtils.java (limited to 'participant/participant-impl/participant-impl-kubernetes/src') diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/Application.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/Application.java new file mode 100644 index 000000000..6908b2760 --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/Application.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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.policy.clamp.acm.participant.kubernetes; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; +import org.springframework.context.annotation.ComponentScan; + +/** + * Starter. + * + */ +// @formatter:off +@SpringBootApplication +@ComponentScan({ + "org.onap.policy.clamp.acm.participant.kubernetes", + "org.onap.policy.clamp.acm.participant.intermediary" +}) +@ConfigurationPropertiesScan("org.onap.policy.clamp.acm.participant.kubernetes.parameters") +//@formatter:on +public class Application { + /** + * Main class. + * + * @param args args + */ + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/configurations/ParticipantConfig.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/configurations/ParticipantConfig.java new file mode 100644 index 000000000..428819be9 --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/configurations/ParticipantConfig.java @@ -0,0 +1,44 @@ +/*- + * ========================LICENSE_START================================= + * Copyright (C) 2021 Nordix Foundation. All rights reserved. + * ====================================================================== + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ====================================================================== + * 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. + * ========================LICENSE_END=================================== + */ + +package org.onap.policy.clamp.acm.participant.kubernetes.configurations; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.multipart.MultipartResolver; +import org.springframework.web.multipart.commons.CommonsMultipartResolver; + +/** + * Bean Factory class for helm client. + */ +@Configuration +public class ParticipantConfig { + + /** + * Method to create multipartResolver bean. + * @return MultipartResolver + */ + @Bean(name = "multipartResolver") + public MultipartResolver multipartResolver() { + var multipartResolver = new CommonsMultipartResolver(); + multipartResolver.setMaxUploadSize(100000); + return multipartResolver; + } +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/configurations/ParticipantIntermediaryConfig.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/configurations/ParticipantIntermediaryConfig.java new file mode 100644 index 000000000..878b43f48 --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/configurations/ParticipantIntermediaryConfig.java @@ -0,0 +1,43 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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.policy.clamp.acm.participant.kubernetes.configurations; + +import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.acm.participant.kubernetes.handler.AutomationCompositionElementHandler; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ParticipantIntermediaryConfig { + + /** + * Register AutomationCompositionElementListener. + * + * @param intermediaryApi the ParticipantIntermediaryApi + * @param acElementHandler the AutomationComposition Element Handler + */ + @Autowired + public void registerAutomationCompositionElementListener(ParticipantIntermediaryApi intermediaryApi, + AutomationCompositionElementHandler acElementHandler) { + intermediaryApi.registerAutomationCompositionElementListener(acElementHandler); + acElementHandler.setIntermediaryApi(intermediaryApi); + } +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/configurations/SecurityConfig.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/configurations/SecurityConfig.java new file mode 100644 index 000000000..da5762b43 --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/configurations/SecurityConfig.java @@ -0,0 +1,45 @@ +/*- + * ========================LICENSE_START================================= + * Copyright (C) 2021 Nordix Foundation. All rights reserved. + * ====================================================================== + * 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. + * ========================LICENSE_END=================================== + */ + +package org.onap.policy.clamp.acm.participant.kubernetes.configurations; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +@Configuration +public class SecurityConfig extends WebSecurityConfigurerAdapter { + + @Value("${security.enable-csrf:true}") + private boolean csrfEnabled = true; + + @Override + protected void configure(HttpSecurity http) throws Exception { + // @formatter:off + http.authorizeRequests() + .antMatchers().authenticated() + .anyRequest().authenticated() + .and().httpBasic(); + // @formatter:on + + if (!csrfEnabled) { + http.csrf().disable(); + } + } +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/configurations/SpringFoxConfig.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/configurations/SpringFoxConfig.java new file mode 100644 index 000000000..cfa98bd65 --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/configurations/SpringFoxConfig.java @@ -0,0 +1,45 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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.policy.clamp.acm.participant.kubernetes.configurations; + +import org.onap.policy.clamp.acm.participant.kubernetes.controller.ChartController; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; + +@Configuration +public class SpringFoxConfig { + + /** + * Docket Spring Fox Config. + * + * @return Docket + */ + @Bean + public Docket api() { + return new Docket(DocumentationType.SWAGGER_2).select() + .apis(RequestHandlerSelectors.basePackage(ChartController.class.getPackageName())) + .paths(PathSelectors.any()).build(); + } +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/controller/ChartController.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/controller/ChartController.java new file mode 100644 index 000000000..19ab4bbab --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/controller/ChartController.java @@ -0,0 +1,195 @@ +/*- + * ========================LICENSE_START================================= + * Copyright (C) 2021-2022 Nordix Foundation. All rights reserved. + * ====================================================================== + * 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. + * ========================LICENSE_END=================================== + */ + +package org.onap.policy.clamp.acm.participant.kubernetes.controller; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import java.io.IOException; +import java.util.ArrayList; +import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList; +import org.onap.policy.clamp.acm.participant.kubernetes.models.HelmRepository; +import org.onap.policy.clamp.acm.participant.kubernetes.models.InstallationInfo; +import org.onap.policy.clamp.acm.participant.kubernetes.service.ChartService; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +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; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +@RestController("chartController") +@ConditionalOnExpression("${chart.api.enabled:false}") +@RequestMapping("helm") +@Api(tags = {"k8s-participant"}) +public class ChartController { + + @Autowired + private ChartService chartService; + + private static final StandardCoder CODER = new StandardCoder(); + + /** + * REST endpoint to get all the charts. + * + * @return List of charts installed + */ + @GetMapping(path = "/charts", produces = MediaType.APPLICATION_JSON_VALUE) + @ApiOperation(value = "Return all Charts") + @ApiResponses(value = {@ApiResponse(code = 200, message = "chart List")}) + public ResponseEntity getAllCharts() { + return new ResponseEntity<>(ChartList.builder().charts(new ArrayList<>(chartService.getAllCharts())).build(), + HttpStatus.OK); + } + + /** + * REST endpoint to install a helm chart. + * + * @param info Info of the chart to be installed + * @return Status of the install operation + * @throws ServiceException in case of error + * @throws IOException in case of IO error + */ + @PostMapping(path = "/install", consumes = MediaType.APPLICATION_JSON_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE) + @ApiOperation(value = "Install the chart") + @ApiResponses(value = {@ApiResponse(code = 201, message = "chart Installed")}) + public ResponseEntity installChart(@RequestBody InstallationInfo info) + throws ServiceException, IOException { + ChartInfo chart = chartService.getChart(info.getName(), info.getVersion()); + if (chart == null) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + chartService.installChart(chart); + return new ResponseEntity<>(HttpStatus.CREATED); + } + + /** + * REST endpoint to uninstall a specific chart. + * + * @param name name of the chart + * @param version version of the chart + * @return Status of operation + * @throws ServiceException in case of error. + */ + @DeleteMapping(path = "/uninstall/{name}/{version}", produces = MediaType.APPLICATION_JSON_VALUE) + @ApiOperation(value = "Uninstall the Chart") + @ApiResponses(value = {@ApiResponse(code = 201, message = "chart Uninstalled")}) + public ResponseEntity uninstallChart(@PathVariable("name") String name, + @PathVariable("version") String version) throws ServiceException { + ChartInfo chart = chartService.getChart(name, version); + if (chart == null) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + chartService.uninstallChart(chart); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + + /** + * REST endpoint to onboard a chart. + * + * @param chartFile Multipart file for the helm chart + * @param infoJson AppInfo of the chart + * @param overrideFile the file for overriding the chart + * @return Status of onboard operation + * @throws ServiceException in case of error + * @throws IOException in case of IO error + */ + @PostMapping(path = "/onboard/chart", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE) + @ApiOperation(value = "Onboard the Chart") + @ApiResponses(value = {@ApiResponse(code = 201, message = "Chart Onboarded")}) + public ResponseEntity onboardChart(@RequestPart("chart") MultipartFile chartFile, + @RequestParam(name = "values", required = false) MultipartFile overrideFile, + @RequestParam("info") String infoJson) throws ServiceException, IOException { + + ChartInfo info; + try { + info = CODER.decode(infoJson, ChartInfo.class); + } catch (CoderException e) { + throw new ServiceException("Error parsing the chart information", e); + } + + chartService.saveChart(info, chartFile, overrideFile); + return new ResponseEntity<>(HttpStatus.OK); + } + + /** + * REST endpoint to delete a specific helm chart. + * + * @param name name of the chart + * @param version version of the chart + * @return Status of operation + */ + @DeleteMapping(path = "/chart/{name}/{version}") + @ApiOperation(value = "Delete the chart") + @ApiResponses(value = {@ApiResponse(code = 204, message = "Chart Deleted")}) + public ResponseEntity deleteChart(@PathVariable("name") String name, + @PathVariable("version") String version) { + + ChartInfo chart = chartService.getChart(name, version); + if (chart == null) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + chartService.deleteChart(chart); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + + /** + * REST endpoint to configure a helm Repository. + * + * @param repo Helm repository to be configured + * @return Status of the operation + * @throws ServiceException in case of error + * @throws IOException in case of IO error + */ + @PostMapping(path = "/repo", consumes = MediaType.APPLICATION_JSON_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE) + @ApiOperation(value = "Configure helm repository") + @ApiResponses(value = {@ApiResponse(code = 201, message = "Repository added")}) + public ResponseEntity configureRepo(@RequestBody String repo) + throws ServiceException, IOException { + HelmRepository repository; + try { + repository = CODER.decode(repo, HelmRepository.class); + } catch (CoderException e) { + throw new ServiceException("Error parsing the repository information", e); + } + chartService.configureRepository(repository); + + return new ResponseEntity<>(HttpStatus.CREATED); + } +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/exception/ServiceException.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/exception/ServiceException.java new file mode 100644 index 000000000..6414f2fa9 --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/exception/ServiceException.java @@ -0,0 +1,32 @@ +/*- + * ========================LICENSE_START================================= + * Copyright (C) 2021 Nordix Foundation. All rights reserved. + * ====================================================================== + * 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. + * ========================LICENSE_END=================================== + */ + +package org.onap.policy.clamp.acm.participant.kubernetes.exception; + +public class ServiceException extends Exception { + + private static final long serialVersionUID = 6810785674716590648L; + + public ServiceException(String message) { + super(message); + } + + public ServiceException(String message, Exception originalException) { + super(message, originalException); + } +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/handler/AutomationCompositionElementHandler.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/handler/AutomationCompositionElementHandler.java new file mode 100644 index 000000000..753608686 --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/handler/AutomationCompositionElementHandler.java @@ -0,0 +1,197 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021-2022 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.policy.clamp.acm.participant.kubernetes.handler; + +import java.io.IOException; +import java.lang.invoke.MethodHandles; +import java.time.Instant; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; +import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener; +import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException; +import org.onap.policy.clamp.acm.participant.kubernetes.helm.PodStatusValidator; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo; +import org.onap.policy.clamp.acm.participant.kubernetes.service.ChartService; +import org.onap.policy.clamp.models.acm.concepts.AcElementStatistics; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState; +import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +/** + * This class handles implementation of automationCompositionElement updates. + */ +@Component +public class AutomationCompositionElementHandler implements AutomationCompositionElementListener { + private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + private ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); + + // Map of helm installation and the status of corresponding pods + @Getter + private static Map> podStatusMap = new ConcurrentHashMap<>(); + private static final Coder CODER = new StandardCoder(); + + @Autowired + private ChartService chartService; + + @Setter + private ParticipantIntermediaryApi intermediaryApi; + + // Map of acElement Id and installed Helm charts + @Getter(AccessLevel.PACKAGE) + private final Map chartMap = new HashMap<>(); + + // Default thread config values + private static class ThreadConfig { + private int uninitializedToPassiveTimeout = 60; + private int podStatusCheckInterval = 30; + } + + /** + * Callback method to handle a automation composition element state change. + * + * @param automationCompositionElementId the ID of the automation composition element + * @param currentState the current state of the automation composition element + * @param newState the state to which the automation composition element is changing to + */ + @Override + public synchronized void automationCompositionElementStateChange(ToscaConceptIdentifier automationCompositionId, + UUID automationCompositionElementId, AutomationCompositionState currentState, + AutomationCompositionOrderedState newState) { + switch (newState) { + case UNINITIALISED: + ChartInfo chart = chartMap.get(automationCompositionElementId); + if (chart != null) { + LOGGER.info("Helm deployment to be deleted {} ", chart.getReleaseName()); + try { + chartService.uninstallChart(chart); + intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, + automationCompositionElementId, newState, AutomationCompositionState.UNINITIALISED, + ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE); + chartMap.remove(automationCompositionElementId); + podStatusMap.remove(chart.getReleaseName()); + } catch (ServiceException se) { + LOGGER.warn("Deletion of Helm deployment failed", se); + } + } + break; + case PASSIVE: + intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, + automationCompositionElementId, newState, AutomationCompositionState.PASSIVE, + ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE); + break; + case RUNNING: + intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, + automationCompositionElementId, newState, AutomationCompositionState.RUNNING, + ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE); + break; + default: + LOGGER.warn("Cannot transition from state {} to state {}", currentState, newState); + break; + } + } + + /** + * Callback method to handle an update on a automation composition element. + * + * @param element the information on the automation composition element + * @param nodeTemplate toscaNodeTemplate + * @throws PfModelException in case of an exception + */ + @Override + public synchronized void automationCompositionElementUpdate(ToscaConceptIdentifier automationCompositionId, + AutomationCompositionElement element, ToscaNodeTemplate nodeTemplate) throws PfModelException { + @SuppressWarnings("unchecked") + Map chartData = (Map) nodeTemplate.getProperties().get("chart"); + + LOGGER.info("Installation request received for the Helm Chart {} ", chartData); + try { + var chartInfo = CODER.convert(chartData, ChartInfo.class); + chartService.installChart(chartInfo); + chartMap.put(element.getId(), chartInfo); + + var config = CODER.convert(nodeTemplate.getProperties(), ThreadConfig.class); + checkPodStatus(automationCompositionId, element.getId(), chartInfo, config.uninitializedToPassiveTimeout, + config.podStatusCheckInterval); + + } catch (ServiceException | CoderException | IOException | ExecutionException + | InterruptedException e) { + LOGGER.warn("Installation of Helm chart failed", e); + } + } + + /** + * Invoke a new thread to check the status of deployed pods. + * + * @param chart ChartInfo + */ + public void checkPodStatus(ToscaConceptIdentifier controlLoopId, UUID elementId, + ChartInfo chart, int timeout, int podStatusCheckInterval) throws ExecutionException, InterruptedException { + // Invoke runnable thread to check pod status + Future result = executor.submit(new PodStatusValidator(chart, timeout, + podStatusCheckInterval), "Done"); + if (!result.get().isEmpty()) { + LOGGER.info("Pod Status Validator Completed: {}", result.isDone()); + intermediaryApi.updateAutomationCompositionElementState(controlLoopId, elementId, + AutomationCompositionOrderedState.PASSIVE, AutomationCompositionState.PASSIVE, + ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE); + } + } + + /** + * Overridden method. + * + * @param automationCompositionElementId automationCompositionElement id + * @throws PfModelException in case of error + */ + @Override + public synchronized void handleStatistics(UUID automationCompositionElementId) throws PfModelException { + var acElement = intermediaryApi.getAutomationCompositionElement(automationCompositionElementId); + if (acElement != null) { + var acElementStatistics = new AcElementStatistics(); + acElementStatistics.setState(acElement.getState()); + acElementStatistics.setTimeStamp(Instant.now()); + intermediaryApi.updateAutomationCompositionElementStatistics(automationCompositionElementId, + acElementStatistics); + } + } +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/HelmClient.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/HelmClient.java new file mode 100644 index 000000000..87199688e --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/HelmClient.java @@ -0,0 +1,279 @@ +/*- + * ========================LICENSE_START================================= + * Copyright (C) 2021-2022 Nordix Foundation. All rights reserved. + * ====================================================================== + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ====================================================================== + * 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. + * ========================LICENSE_END=================================== + */ + +package org.onap.policy.clamp.acm.participant.kubernetes.helm; + +import java.io.File; +import java.io.IOException; +import java.lang.invoke.MethodHandles; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.apache.commons.io.IOUtils; +import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo; +import org.onap.policy.clamp.acm.participant.kubernetes.models.HelmRepository; +import org.onap.policy.clamp.acm.participant.kubernetes.service.ChartStore; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +/** + * Client to talk with Helm cli. Supports helm3 + version + */ +@Component +public class HelmClient { + + @Autowired + private ChartStore chartStore; + + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + private static final String PATH_DELIMITER = "/"; + + /** + * Install a chart. + * + * @param chart name and version. + * @throws ServiceException incase of error + */ + public void installChart(ChartInfo chart) throws ServiceException { + if (! checkNamespaceExists(chart.getNamespace())) { + var processBuilder = prepareCreateNamespaceCommand(chart.getNamespace()); + executeCommand(processBuilder); + } + var processBuilder = prepareInstallCommand(chart); + logger.info("Installing helm chart {} from the repository {} ", chart.getChartId().getName(), + chart.getRepository().getRepoName()); + executeCommand(processBuilder); + logger.info("Chart {} installed successfully", chart.getChartId().getName()); + } + + /** + * Add repository if doesn't exist. + * @param repo HelmRepository + * @throws ServiceException incase of error + */ + public void addRepository(HelmRepository repo) throws ServiceException { + String output = executeCommand(prepareVerifyRepoCommand(repo)); + if (output.isEmpty()) { + logger.info("Adding repository to helm client"); + executeCommand(prepareRepoAddCommand(repo)); + logger.debug("Added repository {} to the helm client", repo.getRepoName()); + } else { + logger.info("Repository already exists"); + } + } + + + /** + * Finds helm chart repository for the chart. + * + * @param chart ChartInfo. + * @return the chart repository as a string + * @throws ServiceException in case of error + * @throws IOException in case of IO errors + */ + public String findChartRepository(ChartInfo chart) throws ServiceException, IOException { + if (updateHelmRepo()) { + String repository = verifyConfiguredRepo(chart); + if (repository != null) { + logger.info("Helm chart located in the repository {} ", repository); + return repository; + } + } + var localHelmChartDir = chartStore.getAppPath(chart.getChartId()).toString(); + logger.info("Chart not found in helm repositories, verifying local repo {} ", localHelmChartDir); + if (verifyLocalHelmRepo(new File(localHelmChartDir + PATH_DELIMITER + chart.getChartId().getName()))) { + return localHelmChartDir; + } + return null; + } + + /** + * Verify helm chart in configured repositories. + * @param chart chartInfo + * @return repo name + * @throws IOException incase of error + * @throws ServiceException incase of error + */ + public String verifyConfiguredRepo(ChartInfo chart) throws IOException, ServiceException { + logger.info("Looking for helm chart {} in all the configured helm repositories", chart.getChartId().getName()); + String repository = null; + var builder = helmRepoVerifyCommand(chart.getChartId().getName()); + String output = executeCommand(builder); + repository = verifyOutput(output, chart.getChartId().getName()); + return repository; + } + + /** + * Uninstall a chart. + * + * @param chart name and version. + * @throws ServiceException incase of error + */ + public void uninstallChart(ChartInfo chart) throws ServiceException { + executeCommand(prepareUnInstallCommand(chart)); + } + + + /** + * Execute helm cli bash commands . + * @param processBuilder processbuilder + * @return string output + * @throws ServiceException incase of error. + */ + public static String executeCommand(ProcessBuilder processBuilder) throws ServiceException { + var commandStr = toString(processBuilder); + + try { + var process = processBuilder.start(); + process.waitFor(); + int exitValue = process.exitValue(); + + if (exitValue != 0) { + var error = IOUtils.toString(process.getErrorStream(), StandardCharsets.UTF_8); + if (! error.isEmpty()) { + throw new ServiceException("Command execution failed: " + commandStr + " " + error); + } + } + + var output = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8); + logger.debug("Command <{}> execution, output: {}", commandStr, output); + return output; + + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + throw new ServiceException("Failed to execute the Command: " + commandStr + ", the command was interrupted", + ie); + } catch (Exception exc) { + throw new ServiceException("Failed to execute the Command: " + commandStr, exc); + } + } + + private boolean checkNamespaceExists(String namespace) throws ServiceException { + logger.info("Check if namespace {} exists on the cluster", namespace); + String output = executeCommand(prepareVerifyNamespaceCommand(namespace)); + return !output.isEmpty(); + } + + private String verifyOutput(String output, String value) { + for (var line: output.split("\\R")) { + if (line.contains(value)) { + return line.split("/")[0]; + } + } + return null; + } + + private ProcessBuilder prepareRepoAddCommand(HelmRepository repo) { + var url = repo.getProtocol() + "://" + repo.getAddress(); + if (repo.getPort() != null) { + url = url + ":" + repo.getPort(); + } + // @formatter:off + List helmArguments = new ArrayList<>( + List.of( + "helm", + "repo", + "add", repo.getRepoName(), url + )); + if (repo.getUserName() != null && repo.getPassword() != null) { + helmArguments.addAll(List.of("--username", repo.getUserName(), "--password", repo.getPassword())); + } + return new ProcessBuilder().command(helmArguments); + } + + private ProcessBuilder prepareVerifyRepoCommand(HelmRepository repo) { + List helmArguments = List.of("sh", "-c", "helm repo ls | grep " + repo.getRepoName()); + return new ProcessBuilder().command(helmArguments); + } + + private ProcessBuilder prepareVerifyNamespaceCommand(String namespace) { + List helmArguments = List.of("sh", "-c", "kubectl get ns | grep " + namespace); + return new ProcessBuilder().command(helmArguments); + } + + private ProcessBuilder prepareInstallCommand(ChartInfo chart) { + + // @formatter:off + List helmArguments = new ArrayList<>( + List.of( + "helm", + "install", chart.getReleaseName(), chart.getRepository().getRepoName() + "/" + + chart.getChartId().getName(), + "--version", chart.getChartId().getVersion(), + "--namespace", chart.getNamespace() + )); + // @formatter:on + + // Verify if values.yaml/override parameters available for the chart + var localOverrideYaml = chartStore.getOverrideFile(chart); + + if (verifyLocalHelmRepo(localOverrideYaml)) { + logger.info("Override yaml available for the helm chart"); + helmArguments.addAll(List.of("--values", localOverrideYaml.getPath())); + } + + if (chart.getOverrideParams() != null) { + for (Map.Entry entry : chart.getOverrideParams().entrySet()) { + helmArguments.addAll(List.of("--set", entry.getKey() + "=" + entry.getValue())); + } + } + return new ProcessBuilder().command(helmArguments); + } + + private ProcessBuilder prepareUnInstallCommand(ChartInfo chart) { + return new ProcessBuilder("helm", "delete", chart.getReleaseName(), "--namespace", + chart.getNamespace()); + } + + private ProcessBuilder prepareCreateNamespaceCommand(String namespace) { + return new ProcessBuilder().command("kubectl", "create", "namespace", namespace); + } + + private ProcessBuilder helmRepoVerifyCommand(String chartName) { + return new ProcessBuilder().command("sh", "-c", "helm search repo | grep " + chartName); + } + + + private boolean updateHelmRepo() { + try { + logger.info("Updating local helm repositories before verifying the chart"); + executeCommand(new ProcessBuilder().command("helm", "repo", "update")); + logger.debug("Helm repositories updated successfully"); + } catch (ServiceException e) { + logger.error("Failed to update the helm repo: ", e); + return false; + } + return true; + + + } + + private boolean verifyLocalHelmRepo(File localFile) { + return localFile.exists(); + } + + protected static String toString(ProcessBuilder processBuilder) { + return String.join(" ", processBuilder.command()); + } +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidator.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidator.java new file mode 100644 index 000000000..f8b08a6be --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidator.java @@ -0,0 +1,119 @@ +/*- + * ========================LICENSE_START================================= + * Copyright (C) 2021 Nordix Foundation. All rights reserved. + * ====================================================================== + * 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. + * ========================LICENSE_END=================================== + */ + +package org.onap.policy.clamp.acm.participant.kubernetes.helm; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.lang.invoke.MethodHandles; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; +import lombok.SneakyThrows; +import org.apache.commons.io.IOUtils; +import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException; +import org.onap.policy.clamp.acm.participant.kubernetes.handler.AutomationCompositionElementHandler; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class PodStatusValidator implements Runnable { + + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + private final int statusCheckInterval; + + //Timeout for the thread to exit. + private final int timeout; + + private ChartInfo chart; + + /** + * Constructor for PodStatusValidator. + * @param chart chartInfo + * @param timeout timeout for the thread to exit + * @param statusCheckInterval Interval to check pod status + */ + public PodStatusValidator(ChartInfo chart, int timeout, int statusCheckInterval) { + this.chart = chart; + this.timeout = timeout; + this.statusCheckInterval = statusCheckInterval; + } + + + @SneakyThrows + @Override + public void run() { + logger.info("Polling the status of deployed pods for the chart {}", chart.getChartId().getName()); + Map podStatusMap; + String output = null; + var isVerified = false; + long endTime = System.currentTimeMillis() + (timeout * 1000L); + + while (!isVerified && System.currentTimeMillis() < endTime) { + try { + output = HelmClient.executeCommand(verifyPodStatusCommand(chart)); + podStatusMap = mapPodStatus(output); + isVerified = podStatusMap.values() + .stream() + .allMatch("Running"::equals); + if (! isVerified) { + logger.info("Waiting for the pods to be active for the chart {}", chart.getChartId().getName()); + podStatusMap.forEach((key, value) -> logger.info("Pod: {} , state: {}", key, value)); + AutomationCompositionElementHandler.getPodStatusMap().put(chart.getReleaseName(), podStatusMap); + // Recheck status of pods in specific intervals. + Thread.sleep(statusCheckInterval * 1000L); + } else { + logger.info("All pods are in running state for the helm chart {}", chart.getChartId().getName()); + AutomationCompositionElementHandler.getPodStatusMap().put(chart.getReleaseName(), podStatusMap); + } + } catch (ServiceException | IOException e) { + throw new ServiceException("Error verifying the status of the pod. Exiting", e); + } + } + } + + private ProcessBuilder verifyPodStatusCommand(ChartInfo chart) { + String podName = chart.getReleaseName() + "-" + chart.getChartId().getName(); + String cmd = "kubectl get pods --namespace " + chart.getNamespace() + " | grep " + podName; + return new ProcessBuilder("sh", "-c", cmd); + } + + + private Map mapPodStatus(String output) throws IOException, ServiceException { + Map podStatusMap = new HashMap<>(); + try (var reader = new BufferedReader(new InputStreamReader(IOUtils.toInputStream(output, + StandardCharsets.UTF_8)))) { + var line = reader.readLine(); + while (line != null) { + if (line.contains(chart.getChartId().getName())) { + var result = line.split("\\s+"); + podStatusMap.put(result[0], result[2]); + } + line = reader.readLine(); + } + } + if (!podStatusMap.isEmpty()) { + return podStatusMap; + } else { + throw new ServiceException("Status of Pod is empty"); + } + } +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/ChartInfo.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/ChartInfo.java new file mode 100644 index 000000000..b925e782d --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/ChartInfo.java @@ -0,0 +1,44 @@ +/*- + * ========================LICENSE_START================================= + * Copyright (C) 2021 Nordix Foundation. All rights reserved. + * ====================================================================== + * 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. + * ========================LICENSE_END=================================== + */ + +package org.onap.policy.clamp.acm.participant.kubernetes.models; + +import java.util.Map; +import lombok.Data; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; + +@Data +@RequiredArgsConstructor +public class ChartInfo { + + @NonNull + private String releaseName; + + @NonNull + private ToscaConceptIdentifier chartId; + + @NonNull + private String namespace; + + private HelmRepository repository; + + private Map overrideParams; + +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/ChartList.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/ChartList.java new file mode 100644 index 000000000..951bdaa1b --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/ChartList.java @@ -0,0 +1,31 @@ +/*- + * ========================LICENSE_START================================= + * Copyright (C) 2021 Nordix Foundation. All rights reserved. + * ====================================================================== + * 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. + * ========================LICENSE_END=================================== + */ + +package org.onap.policy.clamp.acm.participant.kubernetes.models; + +import java.util.List; +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@Builder +public class ChartList { + private List charts; +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/HelmRepository.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/HelmRepository.java new file mode 100644 index 000000000..2e25e42a6 --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/HelmRepository.java @@ -0,0 +1,39 @@ +/*- + * ========================LICENSE_START================================= + * Copyright (C) 2021 Nordix Foundation. All rights reserved. + * ====================================================================== + * 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. + * ========================LICENSE_END=================================== + */ + +package org.onap.policy.clamp.acm.participant.kubernetes.models; + +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class HelmRepository { + + private String repoName; + + private String protocol; + + private String address; + + private String port; + + private String userName; + + private String password; +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/InstallationInfo.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/InstallationInfo.java new file mode 100644 index 000000000..6ef6865cb --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/InstallationInfo.java @@ -0,0 +1,29 @@ +/*- + * ========================LICENSE_START================================= + * Copyright (C) 2021 Nordix Foundation. All rights reserved. + * ====================================================================== + * 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. + * ========================LICENSE_END=================================== + */ + +package org.onap.policy.clamp.acm.participant.kubernetes.models; + +import lombok.Getter; +import org.immutables.gson.Gson; + +@Getter +@Gson.TypeAdapters +public class InstallationInfo { + private String name; + private String version; +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/parameters/ParticipantK8sParameters.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/parameters/ParticipantK8sParameters.java new file mode 100644 index 000000000..b95756ec5 --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/parameters/ParticipantK8sParameters.java @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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.policy.clamp.acm.participant.kubernetes.parameters; + +import javax.validation.Valid; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import lombok.Getter; +import lombok.Setter; +import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantIntermediaryParameters; +import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantParameters; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.validation.annotation.Validated; + +/** + * Class to hold all parameters needed for the kubernetes participant. + * + */ +@Validated +@Getter +@Setter +@ConfigurationProperties(prefix = "participant") +public class ParticipantK8sParameters implements ParticipantParameters { + + @NotNull + @Valid + private ParticipantIntermediaryParameters intermediaryParameters; + + @NotBlank + private String localChartDirectory; + + @NotBlank + private String infoFileName; +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/service/ChartService.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/service/ChartService.java new file mode 100644 index 000000000..344d161b7 --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/service/ChartService.java @@ -0,0 +1,141 @@ +/*- + * ========================LICENSE_START================================= + * Copyright (C) 2021 Nordix Foundation. All rights reserved. + * ====================================================================== + * 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. + * ========================LICENSE_END=================================== + */ + +package org.onap.policy.clamp.acm.participant.kubernetes.service; + +import java.io.IOException; +import java.lang.invoke.MethodHandles; +import java.util.Collection; +import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException; +import org.onap.policy.clamp.acm.participant.kubernetes.helm.HelmClient; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo; +import org.onap.policy.clamp.acm.participant.kubernetes.models.HelmRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +@Service +public class ChartService { + private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + @Autowired + private ChartStore chartStore; + + @Autowired + private HelmClient helmClient; + + /** + * Get all the installed charts. + * @return list of charts. + */ + public Collection getAllCharts() { + return chartStore.getAllCharts(); + } + + /** + * Get specific chart info. + * @param name name of the app + * @param version version of the app + * @return chart + */ + public ChartInfo getChart(String name, String version) { + return chartStore.getChart(name, version); + } + + /** + * Save a helm chart. + * @param chartInfo name and version of the app. + * @param chartFile Helm chart file + * @param overrideFile override file + * @return chart details of the helm chart + * @throws IOException in case of IO error + * @throws ServiceException in case of error + */ + public ChartInfo saveChart(ChartInfo chartInfo, MultipartFile chartFile, MultipartFile overrideFile) + throws IOException, ServiceException { + return chartStore.saveChart(chartInfo, chartFile, overrideFile); + } + + /** + * Delete a helm chart. + * @param chart name and version of the chart. + */ + public void deleteChart(ChartInfo chart) { + chartStore.deleteChart(chart); + } + + /** + * Install a helm chart. + * @param chart name and version. + * @throws ServiceException in case of error + * @throws IOException in case of IO errors + */ + public void installChart(ChartInfo chart) throws ServiceException, IOException { + if (chart.getRepository() == null) { + String repoName = findChartRepo(chart); + if (repoName == null) { + logger.error("Chart repository could not be found. Skipping chart Installation " + + "for the chart {} ", chart.getChartId().getName()); + return; + } else { + HelmRepository repo = HelmRepository.builder().repoName(repoName).build(); + chart.setRepository(repo); + } + } else { + // Add remote repository if passed via TOSCA + configureRepository(chart.getRepository()); + } + helmClient.installChart(chart); + } + + + /** + * Configure remote repository. + * @param repo HelmRepository + * @throws ServiceException incase of error + */ + public void configureRepository(HelmRepository repo) throws ServiceException { + if (repo.getAddress() != null) { + helmClient.addRepository(repo); + } + } + + /** + * Finds helm chart repository for a given chart. + * @param chart chartInfo. + * @return the chart repo as a string + * @throws ServiceException in case of error + * @throws IOException in case of IO errors + */ + public String findChartRepo(ChartInfo chart) throws ServiceException, IOException { + logger.info("Fetching helm chart repository for the given chart {} ", chart.getChartId().getName()); + return helmClient.findChartRepository(chart); + } + + /** + * Uninstall a helm chart. + * @param chart name and version + * @throws ServiceException in case of error. + */ + public void uninstallChart(ChartInfo chart) throws ServiceException { + logger.info("Uninstalling helm deployment {}", chart.getReleaseName()); + helmClient.uninstallChart(chart); + } +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/service/ChartStore.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/service/ChartStore.java new file mode 100644 index 000000000..52c1b1f04 --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/service/ChartStore.java @@ -0,0 +1,219 @@ +/*- + * ========================LICENSE_START================================= + * Copyright (C) 2021 Nordix Foundation. All rights reserved. + * ====================================================================== + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ====================================================================== + * 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. + * ========================LICENSE_END=================================== + */ + +package org.onap.policy.clamp.acm.participant.kubernetes.service; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.lang.invoke.MethodHandles; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import lombok.AccessLevel; +import lombok.Getter; +import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo; +import org.onap.policy.clamp.acm.participant.kubernetes.parameters.ParticipantK8sParameters; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; +import org.springframework.util.FileSystemUtils; +import org.springframework.web.multipart.MultipartFile; + +@Component +public class ChartStore { + private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + private static final StandardCoder STANDARD_CODER = new StandardCoder(); + + private final ParticipantK8sParameters participantK8sParameters; + + // ChartStore map contains chart name as key & ChartInfo as value. + @Getter(AccessLevel.PACKAGE) + private Map localChartMap = new ConcurrentHashMap<>(); + + /** + * Constructor method. + */ + public ChartStore(ParticipantK8sParameters participantK8sParameters) { + this.participantK8sParameters = participantK8sParameters; + this.restoreFromLocalFileSystem(); + } + + /** + * Get local helm chart file. + * + * @param chart ChartInfo + * @return the chart file. + */ + public File getHelmChartFile(ChartInfo chart) { + var appPath = getAppPath(chart.getChartId()); + return new File(appPath.toFile(), chart.getChartId().getName()); + } + + /** + * Get the override yaml file. + * + * @param chart ChartInfo + * @return the override yaml file + */ + public File getOverrideFile(ChartInfo chart) { + var appPath = getAppPath(chart.getChartId()); + return new File(appPath.toFile(), "values.yaml"); + } + + + /** + * Saves the helm chart. + * + * @param chartInfo chartInfo + * @param chartFile helm chart file. + * @param overrideFile override file. + * @return chart + * @throws IOException incase of IO error + * @throws ServiceException incase of error. + */ + public synchronized ChartInfo saveChart(ChartInfo chartInfo, MultipartFile chartFile, MultipartFile overrideFile) + throws IOException, ServiceException { + if (localChartMap.containsKey(key(chartInfo))) { + throw new ServiceException("Chart already exist"); + } + var appPath = getAppPath(chartInfo.getChartId()); + Files.createDirectories(appPath); + + chartFile.transferTo(getHelmChartFile(chartInfo)); + if (overrideFile != null) { + overrideFile.transferTo(getOverrideFile(chartInfo)); + } + + localChartMap.put(key(chartInfo), chartInfo); + storeChartInFile(chartInfo); + return chartInfo; + } + + /** + * Get the chart info. + * + * @param name name of the chart + * @param version version of the chart + * @return chart + */ + public synchronized ChartInfo getChart(String name, String version) { + return localChartMap.get(key(name, version)); + } + + /** + * Get all the charts installed. + * + * @return list of charts. + */ + public synchronized List getAllCharts() { + return new ArrayList<>(localChartMap.values()); + } + + /** + * Delete a chart. + * + * @param chart chart info + */ + public synchronized void deleteChart(ChartInfo chart) { + var appPath = getAppPath(chart.getChartId()); + try { + FileSystemUtils.deleteRecursively(appPath); + } catch (IOException exc) { + LOGGER.warn("Could not delete chart from local file system : {}", appPath, exc); + } + + localChartMap.remove(key(chart)); + } + + /** + * Fetch the local chart directory of specific chart. + * + * @param chartId Id of the chart + * @return path + */ + public Path getAppPath(ToscaConceptIdentifier chartId) { + return Path.of(participantK8sParameters.getLocalChartDirectory(), chartId.getName(), chartId.getVersion()); + } + + private void storeChartInFile(ChartInfo chart) { + try (var out = new PrintStream(new FileOutputStream(getFile(chart)))) { + out.print(STANDARD_CODER.encode(chart)); + } catch (Exception exc) { + LOGGER.warn("Could not store chart: {}", chart.getChartId(), exc); + } + } + + private File getFile(ChartInfo chart) { + var appPath = getAppPath(chart.getChartId()).toString(); + return Path.of(appPath, participantK8sParameters.getInfoFileName()).toFile(); + } + + private synchronized void restoreFromLocalFileSystem() { + try { + var localChartDirectoryPath = Paths.get(participantK8sParameters.getLocalChartDirectory()); + Files.createDirectories(localChartDirectoryPath); + restoreFromLocalFileSystem(localChartDirectoryPath); + } catch (Exception ioe) { + LOGGER.warn("Could not restore charts from local file system", ioe); + } + } + + private synchronized void restoreFromLocalFileSystem(Path localChartDirectoryPath) + throws IOException { + + Files.walkFileTree(localChartDirectoryPath, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path localChartFile, BasicFileAttributes attrs) throws IOException { + try { + // Decode only the json file excluding the helm charts + if (localChartFile.endsWith(participantK8sParameters.getInfoFileName())) { + ChartInfo chart = STANDARD_CODER.decode(localChartFile.toFile(), ChartInfo.class); + localChartMap.put(key(chart), chart); + } + return FileVisitResult.CONTINUE; + } catch (CoderException ce) { + throw new IOException("Error decoding chart file", ce); + } + } + }); + } + + private String key(ChartInfo chart) { + return key(chart.getChartId().getName(), chart.getChartId().getVersion()); + } + + private String key(String chartName, String chartVersion) { + return chartName + "_" + chartVersion; + } +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/Application.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/Application.java deleted file mode 100644 index 5d9d203fe..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/Application.java +++ /dev/null @@ -1,45 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 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.policy.clamp.controlloop.participant.kubernetes; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.context.properties.ConfigurationPropertiesScan; -import org.springframework.context.annotation.ComponentScan; - -/** - * Starter. - * - */ -@SpringBootApplication -@ComponentScan({"org.onap.policy.clamp.controlloop.participant.kubernetes", - "org.onap.policy.clamp.controlloop.participant.intermediary"}) -@ConfigurationPropertiesScan("org.onap.policy.clamp.controlloop.participant.kubernetes.parameters") -public class Application { - /** - * Main class. - * - * @param args args - */ - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/configurations/ParticipantConfig.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/configurations/ParticipantConfig.java deleted file mode 100644 index 54627d557..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/configurations/ParticipantConfig.java +++ /dev/null @@ -1,44 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * Copyright (C) 2021 Nordix Foundation. All rights reserved. - * ====================================================================== - * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. - * ====================================================================== - * 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. - * ========================LICENSE_END=================================== - */ - -package org.onap.policy.clamp.controlloop.participant.kubernetes.configurations; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.multipart.MultipartResolver; -import org.springframework.web.multipart.commons.CommonsMultipartResolver; - -/** - * Bean Factory class for helm client. - */ -@Configuration -public class ParticipantConfig { - - /** - * Method to create multipartResolver bean. - * @return MultipartResolver - */ - @Bean(name = "multipartResolver") - public MultipartResolver multipartResolver() { - var multipartResolver = new CommonsMultipartResolver(); - multipartResolver.setMaxUploadSize(100000); - return multipartResolver; - } -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/configurations/ParticipantIntermediaryConfig.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/configurations/ParticipantIntermediaryConfig.java deleted file mode 100644 index bfbed66b7..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/configurations/ParticipantIntermediaryConfig.java +++ /dev/null @@ -1,43 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 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.policy.clamp.controlloop.participant.kubernetes.configurations; - -import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi; -import org.onap.policy.clamp.controlloop.participant.kubernetes.handler.ControlLoopElementHandler; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class ParticipantIntermediaryConfig { - - /** - * Register ControlLoopElementListener. - * - * @param intermediaryApi the ParticipantIntermediaryApi - * @param clElementHandler the ControlLoop Element Handler - */ - @Autowired - public void registerControlLoopElementListener(ParticipantIntermediaryApi intermediaryApi, - ControlLoopElementHandler clElementHandler) { - intermediaryApi.registerControlLoopElementListener(clElementHandler); - clElementHandler.setIntermediaryApi(intermediaryApi); - } -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/configurations/SecurityConfig.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/configurations/SecurityConfig.java deleted file mode 100644 index 6b4fb0a75..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/configurations/SecurityConfig.java +++ /dev/null @@ -1,45 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * Copyright (C) 2021 Nordix Foundation. All rights reserved. - * ====================================================================== - * 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. - * ========================LICENSE_END=================================== - */ - -package org.onap.policy.clamp.controlloop.participant.kubernetes.configurations; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Configuration; -import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; - -@Configuration -public class SecurityConfig extends WebSecurityConfigurerAdapter { - - @Value("${security.enable-csrf:true}") - private boolean csrfEnabled = true; - - @Override - protected void configure(HttpSecurity http) throws Exception { - // @formatter:off - http.authorizeRequests() - .antMatchers().authenticated() - .anyRequest().authenticated() - .and().httpBasic(); - // @formatter:on - - if (!csrfEnabled) { - http.csrf().disable(); - } - } -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/configurations/SpringFoxConfig.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/configurations/SpringFoxConfig.java deleted file mode 100644 index 09a497705..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/configurations/SpringFoxConfig.java +++ /dev/null @@ -1,45 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 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.policy.clamp.controlloop.participant.kubernetes.configurations; - -import org.onap.policy.clamp.controlloop.participant.kubernetes.controller.ChartController; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import springfox.documentation.builders.PathSelectors; -import springfox.documentation.builders.RequestHandlerSelectors; -import springfox.documentation.spi.DocumentationType; -import springfox.documentation.spring.web.plugins.Docket; - -@Configuration -public class SpringFoxConfig { - - /** - * Docket Spring Fox Config. - * - * @return Docket - */ - @Bean - public Docket api() { - return new Docket(DocumentationType.SWAGGER_2).select() - .apis(RequestHandlerSelectors.basePackage(ChartController.class.getPackageName())) - .paths(PathSelectors.any()).build(); - } -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/controller/ChartController.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/controller/ChartController.java deleted file mode 100644 index e2ccda4d5..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/controller/ChartController.java +++ /dev/null @@ -1,195 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * Copyright (C) 2021-2022 Nordix Foundation. All rights reserved. - * ====================================================================== - * 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. - * ========================LICENSE_END=================================== - */ - -package org.onap.policy.clamp.controlloop.participant.kubernetes.controller; - -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiResponse; -import io.swagger.annotations.ApiResponses; -import java.io.IOException; -import java.util.ArrayList; -import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.HelmRepository; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.InstallationInfo; -import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartService; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -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; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RequestPart; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.multipart.MultipartFile; - -@RestController("chartController") -@ConditionalOnExpression("${chart.api.enabled:false}") -@RequestMapping("helm") -@Api(tags = {"k8s-participant"}) -public class ChartController { - - @Autowired - private ChartService chartService; - - private static final StandardCoder CODER = new StandardCoder(); - - /** - * REST endpoint to get all the charts. - * - * @return List of charts installed - */ - @GetMapping(path = "/charts", produces = MediaType.APPLICATION_JSON_VALUE) - @ApiOperation(value = "Return all Charts") - @ApiResponses(value = {@ApiResponse(code = 200, message = "chart List")}) - public ResponseEntity getAllCharts() { - return new ResponseEntity<>(ChartList.builder().charts(new ArrayList<>(chartService.getAllCharts())).build(), - HttpStatus.OK); - } - - /** - * REST endpoint to install a helm chart. - * - * @param info Info of the chart to be installed - * @return Status of the install operation - * @throws ServiceException in case of error - * @throws IOException in case of IO error - */ - @PostMapping(path = "/install", consumes = MediaType.APPLICATION_JSON_VALUE, - produces = MediaType.APPLICATION_JSON_VALUE) - @ApiOperation(value = "Install the chart") - @ApiResponses(value = {@ApiResponse(code = 201, message = "chart Installed")}) - public ResponseEntity installChart(@RequestBody InstallationInfo info) - throws ServiceException, IOException { - ChartInfo chart = chartService.getChart(info.getName(), info.getVersion()); - if (chart == null) { - return new ResponseEntity<>(HttpStatus.NOT_FOUND); - } - - chartService.installChart(chart); - return new ResponseEntity<>(HttpStatus.CREATED); - } - - /** - * REST endpoint to uninstall a specific chart. - * - * @param name name of the chart - * @param version version of the chart - * @return Status of operation - * @throws ServiceException in case of error. - */ - @DeleteMapping(path = "/uninstall/{name}/{version}", produces = MediaType.APPLICATION_JSON_VALUE) - @ApiOperation(value = "Uninstall the Chart") - @ApiResponses(value = {@ApiResponse(code = 201, message = "chart Uninstalled")}) - public ResponseEntity uninstallChart(@PathVariable("name") String name, - @PathVariable("version") String version) throws ServiceException { - ChartInfo chart = chartService.getChart(name, version); - if (chart == null) { - return new ResponseEntity<>(HttpStatus.NOT_FOUND); - } - - chartService.uninstallChart(chart); - return new ResponseEntity<>(HttpStatus.NO_CONTENT); - } - - /** - * REST endpoint to onboard a chart. - * - * @param chartFile Multipart file for the helm chart - * @param infoJson AppInfo of the chart - * @param overrideFile the file for overriding the chart - * @return Status of onboard operation - * @throws ServiceException in case of error - * @throws IOException in case of IO error - */ - @PostMapping(path = "/onboard/chart", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, - produces = MediaType.APPLICATION_JSON_VALUE) - @ApiOperation(value = "Onboard the Chart") - @ApiResponses(value = {@ApiResponse(code = 201, message = "Chart Onboarded")}) - public ResponseEntity onboardChart(@RequestPart("chart") MultipartFile chartFile, - @RequestParam(name = "values", required = false) MultipartFile overrideFile, - @RequestParam("info") String infoJson) throws ServiceException, IOException { - - ChartInfo info; - try { - info = CODER.decode(infoJson, ChartInfo.class); - } catch (CoderException e) { - throw new ServiceException("Error parsing the chart information", e); - } - - chartService.saveChart(info, chartFile, overrideFile); - return new ResponseEntity<>(HttpStatus.OK); - } - - /** - * REST endpoint to delete a specific helm chart. - * - * @param name name of the chart - * @param version version of the chart - * @return Status of operation - */ - @DeleteMapping(path = "/chart/{name}/{version}") - @ApiOperation(value = "Delete the chart") - @ApiResponses(value = {@ApiResponse(code = 204, message = "Chart Deleted")}) - public ResponseEntity deleteChart(@PathVariable("name") String name, - @PathVariable("version") String version) { - - ChartInfo chart = chartService.getChart(name, version); - if (chart == null) { - return new ResponseEntity<>(HttpStatus.NOT_FOUND); - } - - chartService.deleteChart(chart); - return new ResponseEntity<>(HttpStatus.NO_CONTENT); - } - - /** - * REST endpoint to configure a helm Repository. - * - * @param repo Helm repository to be configured - * @return Status of the operation - * @throws ServiceException in case of error - * @throws IOException in case of IO error - */ - @PostMapping(path = "/repo", consumes = MediaType.APPLICATION_JSON_VALUE, - produces = MediaType.APPLICATION_JSON_VALUE) - @ApiOperation(value = "Configure helm repository") - @ApiResponses(value = {@ApiResponse(code = 201, message = "Repository added")}) - public ResponseEntity configureRepo(@RequestBody String repo) - throws ServiceException, IOException { - HelmRepository repository; - try { - repository = CODER.decode(repo, HelmRepository.class); - } catch (CoderException e) { - throw new ServiceException("Error parsing the repository information", e); - } - chartService.configureRepository(repository); - - return new ResponseEntity<>(HttpStatus.CREATED); - } -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/exception/ServiceException.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/exception/ServiceException.java deleted file mode 100644 index 9a825cf75..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/exception/ServiceException.java +++ /dev/null @@ -1,32 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * Copyright (C) 2021 Nordix Foundation. All rights reserved. - * ====================================================================== - * 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. - * ========================LICENSE_END=================================== - */ - -package org.onap.policy.clamp.controlloop.participant.kubernetes.exception; - -public class ServiceException extends Exception { - - private static final long serialVersionUID = 6810785674716590648L; - - public ServiceException(String message) { - super(message); - } - - public ServiceException(String message, Exception originalException) { - super(message, originalException); - } -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/handler/ControlLoopElementHandler.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/handler/ControlLoopElementHandler.java deleted file mode 100644 index a8a746254..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/handler/ControlLoopElementHandler.java +++ /dev/null @@ -1,196 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021-2022 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.policy.clamp.controlloop.participant.kubernetes.handler; - -import java.io.IOException; -import java.lang.invoke.MethodHandles; -import java.time.Instant; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.Setter; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType; -import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener; -import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi; -import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException; -import org.onap.policy.clamp.controlloop.participant.kubernetes.helm.PodStatusValidator; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo; -import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartService; -import org.onap.policy.common.utils.coder.Coder; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; -import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -/** - * This class handles implementation of controlLoopElement updates. - */ -@Component -public class ControlLoopElementHandler implements ControlLoopElementListener { - private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - - private ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); - - // Map of helm installation and the status of corresponding pods - @Getter - private static Map> podStatusMap = new ConcurrentHashMap<>(); - private static final Coder CODER = new StandardCoder(); - - @Autowired - private ChartService chartService; - - @Setter - private ParticipantIntermediaryApi intermediaryApi; - - // Map of CLElement Id and installed Helm charts - @Getter(AccessLevel.PACKAGE) - private final Map chartMap = new HashMap<>(); - - // Default thread config values - private static class ThreadConfig { - private int uninitializedToPassiveTimeout = 60; - private int podStatusCheckInterval = 30; - } - - /** - * Callback method to handle a control loop element state change. - * - * @param controlLoopElementId the ID of the control loop element - * @param currentState the current state of the control loop element - * @param newState the state to which the control loop element is changing to - */ - @Override - public synchronized void controlLoopElementStateChange(ToscaConceptIdentifier controlLoopId, - UUID controlLoopElementId, ControlLoopState currentState, ControlLoopOrderedState newState) { - switch (newState) { - case UNINITIALISED: - ChartInfo chart = chartMap.get(controlLoopElementId); - if (chart != null) { - LOGGER.info("Helm deployment to be deleted {} ", chart.getReleaseName()); - try { - chartService.uninstallChart(chart); - intermediaryApi.updateControlLoopElementState(controlLoopId, - controlLoopElementId, newState, ControlLoopState.UNINITIALISED, - ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE); - chartMap.remove(controlLoopElementId); - podStatusMap.remove(chart.getReleaseName()); - } catch (ServiceException se) { - LOGGER.warn("Deletion of Helm deployment failed", se); - } - } - break; - case PASSIVE: - intermediaryApi.updateControlLoopElementState(controlLoopId, - controlLoopElementId, newState, ControlLoopState.PASSIVE, - ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE); - break; - case RUNNING: - intermediaryApi.updateControlLoopElementState(controlLoopId, - controlLoopElementId, newState, ControlLoopState.RUNNING, - ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE); - break; - default: - LOGGER.warn("Cannot transition from state {} to state {}", currentState, newState); - break; - } - } - - - /** - * Callback method to handle an update on a control loop element. - * - * @param element the information on the control loop element - * @param nodeTemplate toscaNodeTemplate - * @throws PfModelException in case of an exception - */ - @Override - public synchronized void controlLoopElementUpdate(ToscaConceptIdentifier controlLoopId, - ControlLoopElement element, ToscaNodeTemplate nodeTemplate) throws PfModelException { - @SuppressWarnings("unchecked") - Map chartData = - (Map) nodeTemplate.getProperties().get("chart"); - - LOGGER.info("Installation request received for the Helm Chart {} ", chartData); - try { - var chartInfo = CODER.convert(chartData, ChartInfo.class); - chartService.installChart(chartInfo); - chartMap.put(element.getId(), chartInfo); - - var config = CODER.convert(nodeTemplate.getProperties(), ThreadConfig.class); - checkPodStatus(controlLoopId, element.getId(), chartInfo, config.uninitializedToPassiveTimeout, - config.podStatusCheckInterval); - - } catch (ServiceException | CoderException | IOException | ExecutionException - | InterruptedException e) { - LOGGER.warn("Installation of Helm chart failed", e); - } - } - - /** - * Invoke a new thread to check the status of deployed pods. - * @param chart ChartInfo - */ - public void checkPodStatus(ToscaConceptIdentifier controlLoopId, UUID elementId, - ChartInfo chart, int timeout, int podStatusCheckInterval) throws ExecutionException, InterruptedException { - // Invoke runnable thread to check pod status - Future result = executor.submit(new PodStatusValidator(chart, timeout, - podStatusCheckInterval), "Done"); - if (!result.get().isEmpty()) { - LOGGER.info("Pod Status Validator Completed: {}", result.isDone()); - intermediaryApi.updateControlLoopElementState(controlLoopId, elementId, - ControlLoopOrderedState.PASSIVE, ControlLoopState.PASSIVE, - ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE); - } - } - - /** - * Overridden method. - * - * @param controlLoopElementId controlLoopElement id - * @throws PfModelException in case of error - */ - @Override - public synchronized void handleStatistics(UUID controlLoopElementId) throws PfModelException { - var clElement = intermediaryApi.getControlLoopElement(controlLoopElementId); - if (clElement != null) { - var clElementStatistics = new ClElementStatistics(); - clElementStatistics.setControlLoopState(clElement.getState()); - clElementStatistics.setTimeStamp(Instant.now()); - intermediaryApi.updateControlLoopElementStatistics(controlLoopElementId, clElementStatistics); - } - } -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/HelmClient.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/HelmClient.java deleted file mode 100644 index 6133c7ebd..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/HelmClient.java +++ /dev/null @@ -1,279 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * Copyright (C) 2021-2022 Nordix Foundation. All rights reserved. - * ====================================================================== - * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. - * ====================================================================== - * 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. - * ========================LICENSE_END=================================== - */ - -package org.onap.policy.clamp.controlloop.participant.kubernetes.helm; - -import java.io.File; -import java.io.IOException; -import java.lang.invoke.MethodHandles; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import org.apache.commons.io.IOUtils; -import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.HelmRepository; -import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartStore; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -/** - * Client to talk with Helm cli. Supports helm3 + version - */ -@Component -public class HelmClient { - - @Autowired - private ChartStore chartStore; - - private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - private static final String PATH_DELIMITER = "/"; - - /** - * Install a chart. - * - * @param chart name and version. - * @throws ServiceException incase of error - */ - public void installChart(ChartInfo chart) throws ServiceException { - if (! checkNamespaceExists(chart.getNamespace())) { - var processBuilder = prepareCreateNamespaceCommand(chart.getNamespace()); - executeCommand(processBuilder); - } - var processBuilder = prepareInstallCommand(chart); - logger.info("Installing helm chart {} from the repository {} ", chart.getChartId().getName(), - chart.getRepository().getRepoName()); - executeCommand(processBuilder); - logger.info("Chart {} installed successfully", chart.getChartId().getName()); - } - - /** - * Add repository if doesn't exist. - * @param repo HelmRepository - * @throws ServiceException incase of error - */ - public void addRepository(HelmRepository repo) throws ServiceException { - String output = executeCommand(prepareVerifyRepoCommand(repo)); - if (output.isEmpty()) { - logger.info("Adding repository to helm client"); - executeCommand(prepareRepoAddCommand(repo)); - logger.debug("Added repository {} to the helm client", repo.getRepoName()); - } else { - logger.info("Repository already exists"); - } - } - - - /** - * Finds helm chart repository for the chart. - * - * @param chart ChartInfo. - * @return the chart repository as a string - * @throws ServiceException in case of error - * @throws IOException in case of IO errors - */ - public String findChartRepository(ChartInfo chart) throws ServiceException, IOException { - if (updateHelmRepo()) { - String repository = verifyConfiguredRepo(chart); - if (repository != null) { - logger.info("Helm chart located in the repository {} ", repository); - return repository; - } - } - var localHelmChartDir = chartStore.getAppPath(chart.getChartId()).toString(); - logger.info("Chart not found in helm repositories, verifying local repo {} ", localHelmChartDir); - if (verifyLocalHelmRepo(new File(localHelmChartDir + PATH_DELIMITER + chart.getChartId().getName()))) { - return localHelmChartDir; - } - return null; - } - - /** - * Verify helm chart in configured repositories. - * @param chart chartInfo - * @return repo name - * @throws IOException incase of error - * @throws ServiceException incase of error - */ - public String verifyConfiguredRepo(ChartInfo chart) throws IOException, ServiceException { - logger.info("Looking for helm chart {} in all the configured helm repositories", chart.getChartId().getName()); - String repository = null; - var builder = helmRepoVerifyCommand(chart.getChartId().getName()); - String output = executeCommand(builder); - repository = verifyOutput(output, chart.getChartId().getName()); - return repository; - } - - /** - * Uninstall a chart. - * - * @param chart name and version. - * @throws ServiceException incase of error - */ - public void uninstallChart(ChartInfo chart) throws ServiceException { - executeCommand(prepareUnInstallCommand(chart)); - } - - - /** - * Execute helm cli bash commands . - * @param processBuilder processbuilder - * @return string output - * @throws ServiceException incase of error. - */ - public static String executeCommand(ProcessBuilder processBuilder) throws ServiceException { - var commandStr = toString(processBuilder); - - try { - var process = processBuilder.start(); - process.waitFor(); - int exitValue = process.exitValue(); - - if (exitValue != 0) { - var error = IOUtils.toString(process.getErrorStream(), StandardCharsets.UTF_8); - if (! error.isEmpty()) { - throw new ServiceException("Command execution failed: " + commandStr + " " + error); - } - } - - var output = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8); - logger.debug("Command <{}> execution, output: {}", commandStr, output); - return output; - - } catch (InterruptedException ie) { - Thread.currentThread().interrupt(); - throw new ServiceException("Failed to execute the Command: " + commandStr + ", the command was interrupted", - ie); - } catch (Exception exc) { - throw new ServiceException("Failed to execute the Command: " + commandStr, exc); - } - } - - private boolean checkNamespaceExists(String namespace) throws ServiceException { - logger.info("Check if namespace {} exists on the cluster", namespace); - String output = executeCommand(prepareVerifyNamespaceCommand(namespace)); - return !output.isEmpty(); - } - - private String verifyOutput(String output, String value) { - for (var line: output.split("\\R")) { - if (line.contains(value)) { - return line.split("/")[0]; - } - } - return null; - } - - private ProcessBuilder prepareRepoAddCommand(HelmRepository repo) { - var url = repo.getProtocol() + "://" + repo.getAddress(); - if (repo.getPort() != null) { - url = url + ":" + repo.getPort(); - } - // @formatter:off - List helmArguments = new ArrayList<>( - List.of( - "helm", - "repo", - "add", repo.getRepoName(), url - )); - if (repo.getUserName() != null && repo.getPassword() != null) { - helmArguments.addAll(List.of("--username", repo.getUserName(), "--password", repo.getPassword())); - } - return new ProcessBuilder().command(helmArguments); - } - - private ProcessBuilder prepareVerifyRepoCommand(HelmRepository repo) { - List helmArguments = List.of("sh", "-c", "helm repo ls | grep " + repo.getRepoName()); - return new ProcessBuilder().command(helmArguments); - } - - private ProcessBuilder prepareVerifyNamespaceCommand(String namespace) { - List helmArguments = List.of("sh", "-c", "kubectl get ns | grep " + namespace); - return new ProcessBuilder().command(helmArguments); - } - - private ProcessBuilder prepareInstallCommand(ChartInfo chart) { - - // @formatter:off - List helmArguments = new ArrayList<>( - List.of( - "helm", - "install", chart.getReleaseName(), chart.getRepository().getRepoName() + "/" - + chart.getChartId().getName(), - "--version", chart.getChartId().getVersion(), - "--namespace", chart.getNamespace() - )); - // @formatter:on - - // Verify if values.yaml/override parameters available for the chart - var localOverrideYaml = chartStore.getOverrideFile(chart); - - if (verifyLocalHelmRepo(localOverrideYaml)) { - logger.info("Override yaml available for the helm chart"); - helmArguments.addAll(List.of("--values", localOverrideYaml.getPath())); - } - - if (chart.getOverrideParams() != null) { - for (Map.Entry entry : chart.getOverrideParams().entrySet()) { - helmArguments.addAll(List.of("--set", entry.getKey() + "=" + entry.getValue())); - } - } - return new ProcessBuilder().command(helmArguments); - } - - private ProcessBuilder prepareUnInstallCommand(ChartInfo chart) { - return new ProcessBuilder("helm", "delete", chart.getReleaseName(), "--namespace", - chart.getNamespace()); - } - - private ProcessBuilder prepareCreateNamespaceCommand(String namespace) { - return new ProcessBuilder().command("kubectl", "create", "namespace", namespace); - } - - private ProcessBuilder helmRepoVerifyCommand(String chartName) { - return new ProcessBuilder().command("sh", "-c", "helm search repo | grep " + chartName); - } - - - private boolean updateHelmRepo() { - try { - logger.info("Updating local helm repositories before verifying the chart"); - executeCommand(new ProcessBuilder().command("helm", "repo", "update")); - logger.debug("Helm repositories updated successfully"); - } catch (ServiceException e) { - logger.error("Failed to update the helm repo: ", e); - return false; - } - return true; - - - } - - private boolean verifyLocalHelmRepo(File localFile) { - return localFile.exists(); - } - - protected static String toString(ProcessBuilder processBuilder) { - return String.join(" ", processBuilder.command()); - } -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/PodStatusValidator.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/PodStatusValidator.java deleted file mode 100644 index d55fd6658..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/PodStatusValidator.java +++ /dev/null @@ -1,119 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * Copyright (C) 2021 Nordix Foundation. All rights reserved. - * ====================================================================== - * 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. - * ========================LICENSE_END=================================== - */ - -package org.onap.policy.clamp.controlloop.participant.kubernetes.helm; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.lang.invoke.MethodHandles; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Map; -import lombok.SneakyThrows; -import org.apache.commons.io.IOUtils; -import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException; -import org.onap.policy.clamp.controlloop.participant.kubernetes.handler.ControlLoopElementHandler; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -public class PodStatusValidator implements Runnable { - - private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - - private final int statusCheckInterval; - - //Timeout for the thread to exit. - private final int timeout; - - private ChartInfo chart; - - /** - * Constructor for PodStatusValidator. - * @param chart chartInfo - * @param timeout timeout for the thread to exit - * @param statusCheckInterval Interval to check pod status - */ - public PodStatusValidator(ChartInfo chart, int timeout, int statusCheckInterval) { - this.chart = chart; - this.timeout = timeout; - this.statusCheckInterval = statusCheckInterval; - } - - - @SneakyThrows - @Override - public void run() { - logger.info("Polling the status of deployed pods for the chart {}", chart.getChartId().getName()); - Map podStatusMap; - String output = null; - var isVerified = false; - long endTime = System.currentTimeMillis() + (timeout * 1000L); - - while (!isVerified && System.currentTimeMillis() < endTime) { - try { - output = HelmClient.executeCommand(verifyPodStatusCommand(chart)); - podStatusMap = mapPodStatus(output); - isVerified = podStatusMap.values() - .stream() - .allMatch("Running"::equals); - if (! isVerified) { - logger.info("Waiting for the pods to be active for the chart {}", chart.getChartId().getName()); - podStatusMap.forEach((key, value) -> logger.info("Pod: {} , state: {}", key, value)); - ControlLoopElementHandler.getPodStatusMap().put(chart.getReleaseName(), podStatusMap); - // Recheck status of pods in specific intervals. - Thread.sleep(statusCheckInterval * 1000L); - } else { - logger.info("All pods are in running state for the helm chart {}", chart.getChartId().getName()); - ControlLoopElementHandler.getPodStatusMap().put(chart.getReleaseName(), podStatusMap); - } - } catch (ServiceException | IOException e) { - throw new ServiceException("Error verifying the status of the pod. Exiting", e); - } - } - } - - private ProcessBuilder verifyPodStatusCommand(ChartInfo chart) { - String podName = chart.getReleaseName() + "-" + chart.getChartId().getName(); - String cmd = "kubectl get pods --namespace " + chart.getNamespace() + " | grep " + podName; - return new ProcessBuilder("sh", "-c", cmd); - } - - - private Map mapPodStatus(String output) throws IOException, ServiceException { - Map podStatusMap = new HashMap<>(); - try (var reader = new BufferedReader(new InputStreamReader(IOUtils.toInputStream(output, - StandardCharsets.UTF_8)))) { - var line = reader.readLine(); - while (line != null) { - if (line.contains(chart.getChartId().getName())) { - var result = line.split("\\s+"); - podStatusMap.put(result[0], result[2]); - } - line = reader.readLine(); - } - } - if (!podStatusMap.isEmpty()) { - return podStatusMap; - } else { - throw new ServiceException("Status of Pod is empty"); - } - } -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/models/ChartInfo.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/models/ChartInfo.java deleted file mode 100644 index 5cbc203ec..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/models/ChartInfo.java +++ /dev/null @@ -1,44 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * Copyright (C) 2021 Nordix Foundation. All rights reserved. - * ====================================================================== - * 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. - * ========================LICENSE_END=================================== - */ - -package org.onap.policy.clamp.controlloop.participant.kubernetes.models; - -import java.util.Map; -import lombok.Data; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; -import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; - -@Data -@RequiredArgsConstructor -public class ChartInfo { - - @NonNull - private String releaseName; - - @NonNull - private ToscaConceptIdentifier chartId; - - @NonNull - private String namespace; - - private HelmRepository repository; - - private Map overrideParams; - -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/models/ChartList.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/models/ChartList.java deleted file mode 100644 index 7f46bbde5..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/models/ChartList.java +++ /dev/null @@ -1,31 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * Copyright (C) 2021 Nordix Foundation. All rights reserved. - * ====================================================================== - * 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. - * ========================LICENSE_END=================================== - */ - -package org.onap.policy.clamp.controlloop.participant.kubernetes.models; - -import java.util.List; -import lombok.Builder; -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -@Builder -public class ChartList { - private List charts; -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/models/HelmRepository.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/models/HelmRepository.java deleted file mode 100644 index a495c7b5c..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/models/HelmRepository.java +++ /dev/null @@ -1,39 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * Copyright (C) 2021 Nordix Foundation. All rights reserved. - * ====================================================================== - * 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. - * ========================LICENSE_END=================================== - */ - -package org.onap.policy.clamp.controlloop.participant.kubernetes.models; - -import lombok.Builder; -import lombok.Data; - -@Data -@Builder -public class HelmRepository { - - private String repoName; - - private String protocol; - - private String address; - - private String port; - - private String userName; - - private String password; -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/models/InstallationInfo.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/models/InstallationInfo.java deleted file mode 100644 index b21e93a01..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/models/InstallationInfo.java +++ /dev/null @@ -1,29 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * Copyright (C) 2021 Nordix Foundation. All rights reserved. - * ====================================================================== - * 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. - * ========================LICENSE_END=================================== - */ - -package org.onap.policy.clamp.controlloop.participant.kubernetes.models; - -import lombok.Getter; -import org.immutables.gson.Gson; - -@Getter -@Gson.TypeAdapters -public class InstallationInfo { - private String name; - private String version; -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/parameters/ParticipantK8sParameters.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/parameters/ParticipantK8sParameters.java deleted file mode 100644 index 3b2b3732b..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/parameters/ParticipantK8sParameters.java +++ /dev/null @@ -1,52 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 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.policy.clamp.controlloop.participant.kubernetes.parameters; - -import javax.validation.Valid; -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.NotNull; -import lombok.Getter; -import lombok.Setter; -import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters; -import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.validation.annotation.Validated; - -/** - * Class to hold all parameters needed for the kubernetes participant. - * - */ -@Validated -@Getter -@Setter -@ConfigurationProperties(prefix = "participant") -public class ParticipantK8sParameters implements ParticipantParameters { - - @NotNull - @Valid - private ParticipantIntermediaryParameters intermediaryParameters; - - @NotBlank - private String localChartDirectory; - - @NotBlank - private String infoFileName; -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/service/ChartService.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/service/ChartService.java deleted file mode 100644 index a07fca596..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/service/ChartService.java +++ /dev/null @@ -1,141 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * Copyright (C) 2021 Nordix Foundation. All rights reserved. - * ====================================================================== - * 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. - * ========================LICENSE_END=================================== - */ - -package org.onap.policy.clamp.controlloop.participant.kubernetes.service; - -import java.io.IOException; -import java.lang.invoke.MethodHandles; -import java.util.Collection; -import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException; -import org.onap.policy.clamp.controlloop.participant.kubernetes.helm.HelmClient; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.HelmRepository; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; - -@Service -public class ChartService { - private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - - @Autowired - private ChartStore chartStore; - - @Autowired - private HelmClient helmClient; - - /** - * Get all the installed charts. - * @return list of charts. - */ - public Collection getAllCharts() { - return chartStore.getAllCharts(); - } - - /** - * Get specific chart info. - * @param name name of the app - * @param version version of the app - * @return chart - */ - public ChartInfo getChart(String name, String version) { - return chartStore.getChart(name, version); - } - - /** - * Save a helm chart. - * @param chartInfo name and version of the app. - * @param chartFile Helm chart file - * @param overrideFile override file - * @return chart details of the helm chart - * @throws IOException in case of IO error - * @throws ServiceException in case of error - */ - public ChartInfo saveChart(ChartInfo chartInfo, MultipartFile chartFile, MultipartFile overrideFile) - throws IOException, ServiceException { - return chartStore.saveChart(chartInfo, chartFile, overrideFile); - } - - /** - * Delete a helm chart. - * @param chart name and version of the chart. - */ - public void deleteChart(ChartInfo chart) { - chartStore.deleteChart(chart); - } - - /** - * Install a helm chart. - * @param chart name and version. - * @throws ServiceException in case of error - * @throws IOException in case of IO errors - */ - public void installChart(ChartInfo chart) throws ServiceException, IOException { - if (chart.getRepository() == null) { - String repoName = findChartRepo(chart); - if (repoName == null) { - logger.error("Chart repository could not be found. Skipping chart Installation " - + "for the chart {} ", chart.getChartId().getName()); - return; - } else { - HelmRepository repo = HelmRepository.builder().repoName(repoName).build(); - chart.setRepository(repo); - } - } else { - // Add remote repository if passed via TOSCA - configureRepository(chart.getRepository()); - } - helmClient.installChart(chart); - } - - - /** - * Configure remote repository. - * @param repo HelmRepository - * @throws ServiceException incase of error - */ - public void configureRepository(HelmRepository repo) throws ServiceException { - if (repo.getAddress() != null) { - helmClient.addRepository(repo); - } - } - - /** - * Finds helm chart repository for a given chart. - * @param chart chartInfo. - * @return the chart repo as a string - * @throws ServiceException in case of error - * @throws IOException in case of IO errors - */ - public String findChartRepo(ChartInfo chart) throws ServiceException, IOException { - logger.info("Fetching helm chart repository for the given chart {} ", chart.getChartId().getName()); - return helmClient.findChartRepository(chart); - } - - /** - * Uninstall a helm chart. - * @param chart name and version - * @throws ServiceException in case of error. - */ - public void uninstallChart(ChartInfo chart) throws ServiceException { - logger.info("Uninstalling helm deployment {}", chart.getReleaseName()); - helmClient.uninstallChart(chart); - } -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/service/ChartStore.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/service/ChartStore.java deleted file mode 100644 index ed53d0352..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/service/ChartStore.java +++ /dev/null @@ -1,219 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * Copyright (C) 2021 Nordix Foundation. All rights reserved. - * ====================================================================== - * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. - * ====================================================================== - * 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. - * ========================LICENSE_END=================================== - */ - -package org.onap.policy.clamp.controlloop.participant.kubernetes.service; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.PrintStream; -import java.lang.invoke.MethodHandles; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import lombok.AccessLevel; -import lombok.Getter; -import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo; -import org.onap.policy.clamp.controlloop.participant.kubernetes.parameters.ParticipantK8sParameters; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; -import org.springframework.util.FileSystemUtils; -import org.springframework.web.multipart.MultipartFile; - -@Component -public class ChartStore { - private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - - private static final StandardCoder STANDARD_CODER = new StandardCoder(); - - private final ParticipantK8sParameters participantK8sParameters; - - // ChartStore map contains chart name as key & ChartInfo as value. - @Getter(AccessLevel.PACKAGE) - private Map localChartMap = new ConcurrentHashMap<>(); - - /** - * Constructor method. - */ - public ChartStore(ParticipantK8sParameters participantK8sParameters) { - this.participantK8sParameters = participantK8sParameters; - this.restoreFromLocalFileSystem(); - } - - /** - * Get local helm chart file. - * - * @param chart ChartInfo - * @return the chart file. - */ - public File getHelmChartFile(ChartInfo chart) { - var appPath = getAppPath(chart.getChartId()); - return new File(appPath.toFile(), chart.getChartId().getName()); - } - - /** - * Get the override yaml file. - * - * @param chart ChartInfo - * @return the override yaml file - */ - public File getOverrideFile(ChartInfo chart) { - var appPath = getAppPath(chart.getChartId()); - return new File(appPath.toFile(), "values.yaml"); - } - - - /** - * Saves the helm chart. - * - * @param chartInfo chartInfo - * @param chartFile helm chart file. - * @param overrideFile override file. - * @return chart - * @throws IOException incase of IO error - * @throws ServiceException incase of error. - */ - public synchronized ChartInfo saveChart(ChartInfo chartInfo, MultipartFile chartFile, MultipartFile overrideFile) - throws IOException, ServiceException { - if (localChartMap.containsKey(key(chartInfo))) { - throw new ServiceException("Chart already exist"); - } - var appPath = getAppPath(chartInfo.getChartId()); - Files.createDirectories(appPath); - - chartFile.transferTo(getHelmChartFile(chartInfo)); - if (overrideFile != null) { - overrideFile.transferTo(getOverrideFile(chartInfo)); - } - - localChartMap.put(key(chartInfo), chartInfo); - storeChartInFile(chartInfo); - return chartInfo; - } - - /** - * Get the chart info. - * - * @param name name of the chart - * @param version version of the chart - * @return chart - */ - public synchronized ChartInfo getChart(String name, String version) { - return localChartMap.get(key(name, version)); - } - - /** - * Get all the charts installed. - * - * @return list of charts. - */ - public synchronized List getAllCharts() { - return new ArrayList<>(localChartMap.values()); - } - - /** - * Delete a chart. - * - * @param chart chart info - */ - public synchronized void deleteChart(ChartInfo chart) { - var appPath = getAppPath(chart.getChartId()); - try { - FileSystemUtils.deleteRecursively(appPath); - } catch (IOException exc) { - LOGGER.warn("Could not delete chart from local file system : {}", appPath, exc); - } - - localChartMap.remove(key(chart)); - } - - /** - * Fetch the local chart directory of specific chart. - * - * @param chartId Id of the chart - * @return path - */ - public Path getAppPath(ToscaConceptIdentifier chartId) { - return Path.of(participantK8sParameters.getLocalChartDirectory(), chartId.getName(), chartId.getVersion()); - } - - private void storeChartInFile(ChartInfo chart) { - try (var out = new PrintStream(new FileOutputStream(getFile(chart)))) { - out.print(STANDARD_CODER.encode(chart)); - } catch (Exception exc) { - LOGGER.warn("Could not store chart: {}", chart.getChartId(), exc); - } - } - - private File getFile(ChartInfo chart) { - var appPath = getAppPath(chart.getChartId()).toString(); - return Path.of(appPath, participantK8sParameters.getInfoFileName()).toFile(); - } - - private synchronized void restoreFromLocalFileSystem() { - try { - var localChartDirectoryPath = Paths.get(participantK8sParameters.getLocalChartDirectory()); - Files.createDirectories(localChartDirectoryPath); - restoreFromLocalFileSystem(localChartDirectoryPath); - } catch (Exception ioe) { - LOGGER.warn("Could not restore charts from local file system", ioe); - } - } - - private synchronized void restoreFromLocalFileSystem(Path localChartDirectoryPath) - throws IOException { - - Files.walkFileTree(localChartDirectoryPath, new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path localChartFile, BasicFileAttributes attrs) throws IOException { - try { - // Decode only the json file excluding the helm charts - if (localChartFile.endsWith(participantK8sParameters.getInfoFileName())) { - ChartInfo chart = STANDARD_CODER.decode(localChartFile.toFile(), ChartInfo.class); - localChartMap.put(key(chart), chart); - } - return FileVisitResult.CONTINUE; - } catch (CoderException ce) { - throw new IOException("Error decoding chart file", ce); - } - } - }); - } - - private String key(ChartInfo chart) { - return key(chart.getChartId().getName(), chart.getChartId().getVersion()); - } - - private String key(String chartName, String chartVersion) { - return chartName + "_" + chartVersion; - } -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/resources/config/application.yaml b/participant/participant-impl/participant-impl-kubernetes/src/main/resources/config/application.yaml index b266fe337..3be0fb2c0 100644 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/resources/config/application.yaml +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/resources/config/application.yaml @@ -13,22 +13,22 @@ participant: reportingTimeIntervalMs: 120000 description: Participant Description participantId: - name: K8sParticipant0 - version: 1.0.0 + name: org.onap.policy.clamp.acm.KubernetesParticipant + version: 2.3.4 participantType: - name: org.onap.k8s.controlloop.K8sControlLoopParticipant + name: org.onap.k8s.acm.K8SAutomationCompositionParticipant version: 2.3.4 - clampControlLoopTopics: + clampAutomationCompositionTopics: topicSources: - - topic: POLICY-CLRUNTIME-PARTICIPANT + topic: POLICY-ACRUNTIME-PARTICIPANT servers: - ${topicServer:localhost} topicCommInfrastructure: dmaap fetchTimeout: 15000 topicSinks: - - topic: POLICY-CLRUNTIME-PARTICIPANT + topic: POLICY-ACRUNTIME-PARTICIPANT servers: - ${topicServer:localhost} topicCommInfrastructure: dmaap @@ -41,7 +41,7 @@ management: server: port: 8083 servlet: - context-path: /onap/k8sparticipant + context-path: /onap/policy/clamp/acm/k8sparticipant logging: # Configuration of logging @@ -50,7 +50,7 @@ logging: org.springframework: ERROR org.springframework.data: ERROR org.springframework.web.reactive.function.client.ExchangeFunctions: ERROR - org.onap.policy.clamp.controlloop.participant.kubernetes: INFO + org.onap.policy.clamp.acm.participant.kubernetes: INFO file: name: /var/log/onap/policy/clamp/application.log diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/handler/AutomationCompositionElementHandlerTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/handler/AutomationCompositionElementHandlerTest.java new file mode 100644 index 000000000..dc74afc1a --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/handler/AutomationCompositionElementHandlerTest.java @@ -0,0 +1,182 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.policy.clamp.acm.participant.kubernetes.handler; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.when; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.Spy; +import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList; +import org.onap.policy.clamp.acm.participant.kubernetes.parameters.CommonTestData; +import org.onap.policy.clamp.acm.participant.kubernetes.service.ChartService; +import org.onap.policy.clamp.acm.participant.kubernetes.utils.TestUtils; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +class AutomationCompositionElementHandlerTest { + + private static final Coder CODER = new StandardCoder(); + private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json"; + private static final String KEY_NAME = + "org.onap.domain.database.HelloWorld_K8SMicroserviceAutomationCompositionElement"; + private static List charts; + private static ToscaServiceTemplate toscaServiceTemplate; + private static final String K8S_AUTOMATION_COMPOSITION_ELEMENT = + "org.onap.domain.database.PMSH_K8SMicroserviceAutomationCompositionElement"; + private CommonTestData commonTestData = new CommonTestData(); + + @InjectMocks + @Spy + private AutomationCompositionElementHandler automationCompositionElementHandler = + new AutomationCompositionElementHandler(); + + @Mock + private ChartService chartService; + + @Mock + private ParticipantIntermediaryApi participantIntermediaryApi; + + @Mock + private ExecutorService executor; + @Mock + private Future result; + + @BeforeAll + static void init() throws CoderException { + charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts(); + toscaServiceTemplate = TestUtils.testAutomationCompositionRead(); + } + + @Test + void test_AutomationCompositionElementStateChange() throws ServiceException { + UUID automationCompositionElementId1 = UUID.randomUUID(); + UUID automationCompositionElementId2 = UUID.randomUUID(); + + automationCompositionElementHandler.getChartMap().put(automationCompositionElementId1, charts.get(0)); + automationCompositionElementHandler.getChartMap().put(automationCompositionElementId2, charts.get(1)); + + doNothing().when(chartService).uninstallChart(charts.get(0)); + + automationCompositionElementHandler.automationCompositionElementStateChange( + commonTestData.getAutomationCompositionId(), automationCompositionElementId1, + AutomationCompositionState.PASSIVE, AutomationCompositionOrderedState.UNINITIALISED); + + doThrow(new ServiceException("Error uninstalling the chart")).when(chartService).uninstallChart(charts.get(0)); + + assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange( + commonTestData.getAutomationCompositionId(), automationCompositionElementId1, + AutomationCompositionState.PASSIVE, AutomationCompositionOrderedState.PASSIVE)); + + assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange( + commonTestData.getAutomationCompositionId(), automationCompositionElementId1, + AutomationCompositionState.PASSIVE, AutomationCompositionOrderedState.UNINITIALISED)); + + assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange( + commonTestData.getAutomationCompositionId(), automationCompositionElementId1, + AutomationCompositionState.PASSIVE, AutomationCompositionOrderedState.RUNNING)); + + } + + @Test + void test_AutomationCompositionElementUpdate() throws PfModelException, IOException, ServiceException, + ExecutionException, InterruptedException { + doNothing().when(automationCompositionElementHandler).checkPodStatus(any(), any(), any(), anyInt(), anyInt()); + UUID elementId1 = UUID.randomUUID(); + AutomationCompositionElement element = new AutomationCompositionElement(); + element.setId(elementId1); + element.setDefinition(new ToscaConceptIdentifier(KEY_NAME, "1.0.1")); + element.setOrderedState(AutomationCompositionOrderedState.PASSIVE); + + Map nodeTemplatesMap = + toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates(); + automationCompositionElementHandler.automationCompositionElementUpdate( + commonTestData.getAutomationCompositionId(), element, + nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT)); + + assertThat(automationCompositionElementHandler.getChartMap()).hasSize(1).containsKey(elementId1); + + doThrow(new ServiceException("Error installing the chart")).when(chartService).installChart(Mockito.any()); + + UUID elementId2 = UUID.randomUUID(); + element.setId(elementId2); + automationCompositionElementHandler.automationCompositionElementUpdate( + commonTestData.getAutomationCompositionId(), element, + nodeTemplatesMap.get(K8S_AUTOMATION_COMPOSITION_ELEMENT)); + + assertThat(automationCompositionElementHandler.getChartMap().containsKey(elementId2)).isFalse(); + } + + @Test + void test_handleStatistics() throws PfModelException { + UUID elementId1 = UUID.randomUUID(); + automationCompositionElementHandler.getChartMap().put(elementId1, charts.get(0)); + when(participantIntermediaryApi.getAutomationCompositionElement(elementId1)) + .thenReturn(new AutomationCompositionElement()); + assertDoesNotThrow(() -> automationCompositionElementHandler.handleStatistics(elementId1)); + } + + @Test + void test_checkPodStatus() throws ExecutionException, InterruptedException { + doReturn(result).when(executor).submit(any(Runnable.class), any()); + doReturn("Done").when(result).get(); + doReturn(true).when(result).isDone(); + var chartInfo = charts.get(0); + ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier(); + AutomationCompositionElement element = new AutomationCompositionElement(); + assertDoesNotThrow( + () -> automationCompositionElementHandler.checkPodStatus(controlLoopId, element.getId(), chartInfo, + 1, 1)); + } +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/HelmClientTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/HelmClientTest.java new file mode 100644 index 000000000..7f1943c97 --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/HelmClientTest.java @@ -0,0 +1,158 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.policy.clamp.acm.participant.kubernetes.helm; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.when; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.Spy; +import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList; +import org.onap.policy.clamp.acm.participant.kubernetes.models.HelmRepository; +import org.onap.policy.clamp.acm.participant.kubernetes.service.ChartStore; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.util.FileSystemUtils; + + +@ExtendWith(SpringExtension.class) +class HelmClientTest { + + private static final Coder CODER = new StandardCoder(); + private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json"; + private static List charts; + + @InjectMocks + @Spy + private HelmClient helmClient = new HelmClient(); + + @Mock + ChartStore chartStore; + + @Mock + HelmRepository repo; + + private static MockedStatic mockedClient; + + @BeforeAll + static void init() throws CoderException { + charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts(); + //Mock static method for bash command execution + mockedClient = mockStatic(HelmClient.class); + } + + @AfterAll + public static void close() throws IOException { + mockedClient.close(); + FileSystemUtils.deleteRecursively(Path.of("target/tmp")); + } + + @Test + void test_installChart() throws IOException { + mockedClient.when(() -> HelmClient.executeCommand(any())) + .thenReturn("success"); + doReturn(new File("/target/tmp/override.yaml")).when(chartStore) + .getOverrideFile(any()); + var chartinfo = charts.get(0); + + assertDoesNotThrow(() -> helmClient.installChart(chartinfo)); + chartinfo.setNamespace(""); + assertDoesNotThrow(() -> helmClient.installChart(chartinfo)); + + mockedClient.when(() -> HelmClient.executeCommand(any())) + .thenReturn(new String()); + assertDoesNotThrow(() -> helmClient.installChart(chartinfo)); + + } + + @Test + void test_addRepository() throws IOException { + mockedClient.when(() -> HelmClient.executeCommand(any())) + .thenReturn(new String()); + when(repo.getRepoName()).thenReturn("RepoName"); + assertDoesNotThrow(() -> helmClient.addRepository(repo)); + + mockedClient.when(() -> HelmClient.executeCommand(any())) + .thenReturn("failed"); + assertDoesNotThrow(() -> helmClient.addRepository(repo)); + } + + @Test + void test_findChartRepository() throws IOException, ServiceException { + String tmpPath = "target/tmp/dummyChart/1.0/"; + mockedClient.when(() -> HelmClient.executeCommand(Mockito.any())) + .thenReturn("nginx-stable/nginx-ingress\t0.9.3\t1.11.3" + + " \tNGINX Ingress Controller"); + String configuredRepo = helmClient.findChartRepository(charts.get(1)); + assertThat(configuredRepo).isEqualTo("nginx-stable"); + + File tmpFile = new File(tmpPath + charts.get(1).getChartId().getName()); + tmpFile.mkdirs(); + doReturn(Path.of(tmpPath)).when(chartStore).getAppPath(charts.get(1).getChartId()); + + doReturn(null).when(helmClient).verifyConfiguredRepo(charts.get(1)); + + String localRepoName = helmClient.findChartRepository(charts.get(1)); + assertNotNull(localRepoName); + assertThat(localRepoName).endsWith(charts.get(0).getChartId().getVersion()); + } + + @Test + void test_uninstallChart() throws ServiceException { + helmClient.uninstallChart(charts.get(0)); + mockedClient.when(() -> HelmClient.executeCommand(any())).thenThrow(new ServiceException("error in execution")); + + assertThatThrownBy(() -> helmClient.uninstallChart(charts.get(0))) + .isInstanceOf(ServiceException.class); + } + + @Test + void test_verifyConfiguredRepoForInvalidChart() throws IOException, ServiceException { + mockedClient.when(() -> HelmClient.executeCommand(Mockito.any())) + .thenReturn(""); + String configuredRepo = helmClient.verifyConfiguredRepo(charts.get(1)); + assertNull(configuredRepo); + } + +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidatorTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidatorTest.java new file mode 100644 index 000000000..89b077044 --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidatorTest.java @@ -0,0 +1,107 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.policy.clamp.acm.participant.kubernetes.helm; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mockStatic; + +import java.io.File; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.MockedStatic; +import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException; +import org.onap.policy.clamp.acm.participant.kubernetes.handler.AutomationCompositionElementHandler; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +class PodStatusValidatorTest { + + + private static final Coder CODER = new StandardCoder(); + private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json"; + private static List charts; + private static int timeout = 60; + private static int statusCheckInterval = 30; + + + @InjectMocks + private static PodStatusValidator podStatusValidator; + + private static MockedStatic mockedClient; + + + @BeforeAll + static void init() throws CoderException { + charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts(); + mockedClient = mockStatic(HelmClient.class); + podStatusValidator = new PodStatusValidator(charts.get(0), timeout, statusCheckInterval); + } + + @AfterEach + void clearPodStatusMap() { + AutomationCompositionElementHandler.getPodStatusMap().clear(); + } + + @AfterAll + public static void close() { + mockedClient.close(); + } + + + @Test + void test_RunningPodState() { + String runningPod = "NAME\tREADY\tSTATUS\tRESTARTS\tAGE\r\nHelloWorld-54777df9f8-qpzqr\t1/1\tRunning\t0\t9h"; + mockedClient.when(() -> HelmClient.executeCommand(any())) + .thenReturn(runningPod); + assertDoesNotThrow(() -> podStatusValidator.run()); + assertThat(AutomationCompositionElementHandler.getPodStatusMap()).hasSize(1); + assertThat(AutomationCompositionElementHandler.getPodStatusMap()).containsKey(charts.get(0).getReleaseName()); + assertThat(AutomationCompositionElementHandler.getPodStatusMap()) + .containsValue(Map.of("HelloWorld-54777df9f8-qpzqr", "Running")); + } + + + @Test + void test_InvalidPodState() { + String invalidPod = "NAME\tREADY\tSTATUS\tRESTARTS\tAGE\nhellofromdocker-54777df9f8-qpzqr\t1/1\tInit\t0\t9h"; + mockedClient.when(() -> HelmClient.executeCommand(any())) + .thenReturn(invalidPod); + assertThatThrownBy(() -> podStatusValidator.run()) + .isInstanceOf(ServiceException.class).hasMessage("Error verifying the status of the pod. Exiting"); + assertThat(AutomationCompositionElementHandler.getPodStatusMap()).isEmpty(); + } + +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/parameters/CommonTestData.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/parameters/CommonTestData.java new file mode 100644 index 000000000..13f8edc15 --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/parameters/CommonTestData.java @@ -0,0 +1,161 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.policy.clamp.acm.participant.kubernetes.parameters; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import org.onap.policy.common.endpoints.parameters.TopicParameters; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; + +public class CommonTestData { + + public static final String PARTICIPANT_GROUP_NAME = "AutomationCompositionParticipantGroup"; + public static final String DESCRIPTION = "Participant description"; + public static final long TIME_INTERVAL = 2000; + public static final List TOPIC_PARAMS = Arrays.asList(getTopicParams()); + public static final Coder CODER = new StandardCoder(); + + + /** + * Get ParticipantK8sParameters. + * + * @return ParticipantK8sParameters + */ + public ParticipantK8sParameters getParticipantK8sParameters() { + try { + return CODER.convert(getParticipantK8sParametersMap(PARTICIPANT_GROUP_NAME), + ParticipantK8sParameters.class); + } catch (final CoderException e) { + throw new RuntimeException("cannot create ParticipantK8sParameters from map", e); + } + } + + /** + * Returns a property map for a ParticipantK8sParameters map for test cases. + * + * @param name name of the parameters + * + * @return a property map suitable for constructing an object + */ + public Map getParticipantK8sParametersMap(final String name) { + final Map map = new TreeMap<>(); + + map.put("name", name); + map.put("intermediaryParameters", getIntermediaryParametersMap(false)); + map.put("localChartDirectory", getLocalChartDir()); + map.put("infoFileName", getInfoFileName()); + return map; + } + + + /** + * Returns string value of local chart Directory. + * @return a string value + */ + public String getLocalChartDir() { + return "/var/helm-manager/local-charts"; + } + + /** + * Returns string value of Info file name. + * @return string value + */ + public String getInfoFileName() { + return "CHART-INFO.json"; + } + + + + /** + * Returns a property map for a intermediaryParameters map for test cases. + * + * @param isEmpty boolean value to represent that object created should be empty or not + * @return a property map suitable for constructing an object + */ + public Map getIntermediaryParametersMap(final boolean isEmpty) { + final Map map = new TreeMap<>(); + if (!isEmpty) { + map.put("name", "Participant parameters"); + map.put("reportingTimeIntervalMs", TIME_INTERVAL); + map.put("description", DESCRIPTION); + map.put("participantId", getParticipantId()); + map.put("participantType", getParticipantId()); + map.put("clampAutomationCompositionTopics", getTopicParametersMap(false)); + } + + return map; + } + + /** + * Returns participantId for test cases. + * + * @return participant Id + */ + public static ToscaConceptIdentifier getParticipantId() { + final ToscaConceptIdentifier participantId = new ToscaConceptIdentifier(); + participantId.setName("K8sParticipant0"); + participantId.setVersion("1.0.0"); + return participantId; + } + + + /** + * Returns a property map for a TopicParameters map for test cases. + * + * @param isEmpty boolean value to represent that object created should be empty or not + * @return a property map suitable for constructing an object + */ + public Map getTopicParametersMap(final boolean isEmpty) { + final Map map = new TreeMap<>(); + if (!isEmpty) { + map.put("topicSources", TOPIC_PARAMS); + map.put("topicSinks", TOPIC_PARAMS); + } + return map; + } + + /** + * Returns topic parameters for test cases. + * + * @return topic parameters + */ + public static TopicParameters getTopicParams() { + final TopicParameters topicParams = new TopicParameters(); + topicParams.setTopic("POLICY-ACRUNTIME-PARTICIPANT"); + topicParams.setTopicCommInfrastructure("dmaap"); + topicParams.setServers(Arrays.asList("localhost")); + return topicParams; + } + + /** + * Get automation composition id. + * @return ToscaConceptIdentifier automationCompositionId + */ + public ToscaConceptIdentifier getAutomationCompositionId() { + return new ToscaConceptIdentifier("PMSHInstance0", "1.0.0"); + } +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/parameters/ParticipantK8sParametersTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/parameters/ParticipantK8sParametersTest.java new file mode 100644 index 000000000..09ea74afe --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/parameters/ParticipantK8sParametersTest.java @@ -0,0 +1,89 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.policy.clamp.acm.participant.kubernetes.parameters; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Set; +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.ValidatorFactory; +import org.junit.jupiter.api.Test; + +class ParticipantK8sParametersTest { + + private CommonTestData commonTestData = new CommonTestData(); + private ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); + + @Test + void testParticipantPolicyParameters() { + final ParticipantK8sParameters participantParameters = commonTestData.getParticipantK8sParameters(); + assertThat(validatorFactory.getValidator().validate(participantParameters)).isNullOrEmpty(); + } + + @Test + void testParticipantK8sParameters_NullTopicSinks() { + final ParticipantK8sParameters participantParameters = commonTestData.getParticipantK8sParameters(); + participantParameters.getIntermediaryParameters().getClampAutomationCompositionTopics().setTopicSinks(null); + assertThat(validatorFactory.getValidator().validate(participantParameters)).isNotEmpty(); + } + + @Test + void testParticipantK8sParameters_NullTopicSources() { + final ParticipantK8sParameters participantParameters = commonTestData.getParticipantK8sParameters(); + participantParameters.getIntermediaryParameters().getClampAutomationCompositionTopics().setTopicSources(null); + assertThat(validatorFactory.getValidator().validate(participantParameters)).isNotEmpty(); + } + + @Test + void testParticipantK8sParameters_BlankLocalChartDirParameter() { + final ParticipantK8sParameters participantParameters = commonTestData.getParticipantK8sParameters(); + participantParameters.setLocalChartDirectory(" "); + Set> violations = validatorFactory.getValidator() + .validate(participantParameters); + assertThat(violations.size()).isEqualTo(1); + } + + @Test + void testParticipantK8sParameters_BlankInfoFileParameter() { + final ParticipantK8sParameters participantParameters = commonTestData.getParticipantK8sParameters(); + participantParameters.setInfoFileName(""); + Set> violations = validatorFactory.getValidator() + .validate(participantParameters); + assertThat(violations.size()).isEqualTo(1); + } + + @Test + void testNoIntermediaryParameters() { + final ParticipantK8sParameters participantParameters = commonTestData.getParticipantK8sParameters(); + participantParameters.setIntermediaryParameters(null); + assertThat(validatorFactory.getValidator().validate(participantParameters)).isNotEmpty(); + } + + @Test + void testNoParticipantId() { + final ParticipantK8sParameters participantParameters = commonTestData.getParticipantK8sParameters(); + participantParameters.getIntermediaryParameters().setParticipantId(null); + assertThat(validatorFactory.getValidator().validate(participantParameters)).isNotEmpty(); + } + +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/rest/ActuatorControllerTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/rest/ActuatorControllerTest.java new file mode 100644 index 000000000..8d05d2bf6 --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/rest/ActuatorControllerTest.java @@ -0,0 +1,92 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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.policy.clamp.acm.participant.kubernetes.rest; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import javax.ws.rs.client.Invocation; +import javax.ws.rs.core.Response; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.onap.policy.clamp.acm.participant.kubernetes.utils.CommonActuatorController; +import org.springframework.boot.test.autoconfigure.actuate.metrics.AutoConfigureMetrics; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@AutoConfigureMetrics +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@TestPropertySource(locations = {"classpath:application_test.properties"}) +class ActuatorControllerTest extends CommonActuatorController { + + private static final String HEALTH_ENDPOINT = "health"; + private static final String METRICS_ENDPOINT = "metrics"; + private static final String PROMETHEUS_ENDPOINT = "prometheus"; + + @LocalServerPort + private int randomServerPort; + + @BeforeEach + public void setUpPort() { + super.setHttpPrefix(randomServerPort); + } + + @Test + void testGetHealth_Unauthorized() throws Exception { + assertUnauthorizedActGet(HEALTH_ENDPOINT); + } + + @Test + void testGetMetrics_Unauthorized() throws Exception { + assertUnauthorizedActGet(METRICS_ENDPOINT); + } + + @Test + void testGetPrometheus_Unauthorized() throws Exception { + assertUnauthorizedActGet(PROMETHEUS_ENDPOINT); + } + + @Test + void testGetHealth() throws Exception { + Invocation.Builder invocationBuilder = super.sendActRequest(HEALTH_ENDPOINT); + Response rawresp = invocationBuilder.buildGet().invoke(); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + } + + @Test + void testGetMetrics() throws Exception { + Invocation.Builder invocationBuilder = super.sendActRequest(METRICS_ENDPOINT); + Response rawresp = invocationBuilder.buildGet().invoke(); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + } + + @Test + void testGePrometheus() throws Exception { + Invocation.Builder invocationBuilder = super.sendActRequest(PROMETHEUS_ENDPOINT); + Response rawresp = invocationBuilder.buildGet().invoke(); + assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); + } + +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/rest/ChartControllerTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/rest/ChartControllerTest.java new file mode 100644 index 000000000..73c5c98a1 --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/rest/ChartControllerTest.java @@ -0,0 +1,249 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021-2022 Nordix Foundation. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.policy.clamp.acm.participant.kubernetes.rest; + +import static org.hamcrest.CoreMatchers.is; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.List; +import org.apache.commons.io.FileUtils; +import org.json.JSONObject; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.onap.policy.clamp.acm.participant.kubernetes.controller.ChartController; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList; +import org.onap.policy.clamp.acm.participant.kubernetes.parameters.ParticipantK8sParameters; +import org.onap.policy.clamp.acm.participant.kubernetes.service.ChartService; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.RequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + + +@ExtendWith(SpringExtension.class) +@WebMvcTest(value = ChartController.class, properties = "chart.api.enabled=true") +@EnableConfigurationProperties(value = ParticipantK8sParameters.class) +class ChartControllerTest { + + private static final Coder CODER = new StandardCoder(); + private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json"; + private static List charts; + private static String RETRIEVE_CHART_URL = "/helm/charts"; + private static String INSTALL_CHART_URL = "/helm/install"; + private static String UNINSTALL_CHART_URL = "/helm/uninstall/"; + private static String ONBOARD_CHART_URL = "/helm/onboard/chart"; + private static String DELETE_CHART_URL = "/helm/chart"; + private static String CONFIGURE_REPO_URL = "/helm/repo"; + + @Autowired + private MockMvc mockMvc; + + @MockBean + private ChartService chartService; + + @Autowired + private WebApplicationContext context; + + /** + * Read input chart info json. + * @throws Exception incase of error. + */ + @BeforeAll + static void setupParams() throws CoderException { + charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts(); + } + + /** + * Mock service layer in Controller. + * @throws Exception incase of error. + */ + @BeforeEach + void mockServiceClass() { + when(chartService.getAllCharts()).thenReturn(charts); + when(chartService.getChart(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion())) + .thenReturn(charts.get(0)); + + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build(); + } + + /** + * Test endpoint for retrieving all charts. + * @throws Exception incase of error. + */ + @Test + void retrieveAllCharts() throws Exception { + RequestBuilder requestBuilder; + requestBuilder = MockMvcRequestBuilders.get(RETRIEVE_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE); + + mockMvc.perform(requestBuilder).andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.charts.[0].chartId.name", is("HelloWorld"))); + } + + /** + * Test endpoint for installing a chart. + * @throws Exception incase of error. + */ + @Test + void installChart() throws Exception { + RequestBuilder requestBuilder; + + //Mocking successful installation for void install method + doNothing().when(chartService).installChart(charts.get(0)); + + requestBuilder = MockMvcRequestBuilders.post(INSTALL_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE) + .content(getInstallationJson(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion())) + .contentType(MediaType.APPLICATION_JSON_VALUE); + + mockMvc.perform(requestBuilder).andExpect(status().isCreated()); + + //Install Invalid chart, expects HTTP status NOT_FOUND + requestBuilder = MockMvcRequestBuilders.post(INSTALL_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE) + .content(getInstallationJson("invalidName", "invalidVersion")) + .contentType(MediaType.APPLICATION_JSON_VALUE); + + mockMvc.perform(requestBuilder).andExpect(status().isNotFound()); + } + + /** + * Test endpoint for uninstalling a chart. + * @throws Exception incase of error. + */ + @Test + void uninstallChart() throws Exception { + RequestBuilder requestBuilder; + + //Mocking successful scenario for void uninstall method + doNothing().when(chartService).uninstallChart(charts.get(0)); + + requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + charts.get(0) + .getChartId().getName() + "/" + charts.get(0).getChartId().getVersion()) + .accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE); + + mockMvc.perform(requestBuilder).andExpect(status().isNoContent()); + + //Invalid chart + requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + "invalidName" + + "/" + "invalidVersion").accept(MediaType.APPLICATION_JSON_VALUE) + .contentType(MediaType.APPLICATION_JSON_VALUE); + + mockMvc.perform(requestBuilder).andExpect(status().isNotFound()); + } + + /** + * Test endpoint for chart onboarding. + * @throws Exception incase of error. + */ + @Test + void onboardChart() throws Exception { + RequestBuilder requestBuilder; + MockMultipartFile chartFile = new MockMultipartFile("chart", "hello.tgz", + MediaType.TEXT_PLAIN_VALUE, "Dummy data".getBytes()); + + MockMultipartFile overrideFile = new MockMultipartFile("values", "values.yaml", + MediaType.TEXT_PLAIN_VALUE, "Dummy data".getBytes()); + + //Mocking successful scenario for void uninstall method + when(chartService.saveChart(charts.get(0), chartFile, null)).thenReturn(charts.get(0)); + + requestBuilder = MockMvcRequestBuilders.multipart(ONBOARD_CHART_URL) + .file(chartFile).file(overrideFile).param("info", getChartInfoJson()); + + mockMvc.perform(requestBuilder).andExpect(status().isOk()); + } + + /** + * Test endpoint for deleting a chart. + * @throws Exception incase of error. + */ + @Test + void deleteChart() throws Exception { + RequestBuilder requestBuilder; + + //Mocking successful scenario for void uninstall method + doNothing().when(chartService).deleteChart(charts.get(0)); + + requestBuilder = MockMvcRequestBuilders.delete(DELETE_CHART_URL + "/" + charts.get(0) + .getChartId().getName() + "/" + charts.get(0).getChartId().getVersion()) + .accept(MediaType.APPLICATION_JSON_VALUE) + .contentType(MediaType.APPLICATION_JSON_VALUE); + + mockMvc.perform(requestBuilder).andExpect(status().isNoContent()); + //Invalid chart + requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + "invalidName" + + "/" + "invalidVersion").accept(MediaType.APPLICATION_JSON_VALUE) + .contentType(MediaType.APPLICATION_JSON_VALUE); + + mockMvc.perform(requestBuilder).andExpect(status().isNotFound()); + + } + + /** + * Test endpoint for configuring a helm repository. + * @throws Exception in case of error. + */ + @Test + void testConfigureRepo() throws Exception { + RequestBuilder requestBuilder; + + requestBuilder = MockMvcRequestBuilders.post(CONFIGURE_REPO_URL).accept(MediaType.APPLICATION_JSON_VALUE) + .content(getInstallationJson(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion())) + .contentType(MediaType.APPLICATION_JSON_VALUE); + + mockMvc.perform(requestBuilder).andExpect(status().isCreated()); + + } + + + private String getInstallationJson(String name, String version) { + JSONObject jsonObj = new JSONObject(); + jsonObj.put("name", name); + jsonObj.put("version", version); + return jsonObj.toString(); + } + + private String getChartInfoJson() throws IOException { + return FileUtils.readFileToString(new File(CHART_INFO_YAML), StandardCharsets.UTF_8); + } + +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/service/ChartServiceTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/service/ChartServiceTest.java new file mode 100644 index 000000000..f5b6093d3 --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/service/ChartServiceTest.java @@ -0,0 +1,148 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.policy.clamp.acm.participant.kubernetes.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; + +import java.io.File; +import java.io.IOException; +import java.util.Collection; +import java.util.List; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException; +import org.onap.policy.clamp.acm.participant.kubernetes.helm.HelmClient; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +class ChartServiceTest { + + private static final Coder CODER = new StandardCoder(); + private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json"; + private static List charts; + + @InjectMocks + @Spy + private ChartService chartService = new ChartService(); + + @Mock + private ChartStore chartStore; + + @Mock + private HelmClient helmClient; + + @BeforeAll + static void init() throws CoderException { + charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts(); + } + + @Test + void test_getAllCharts() { + assertThat(chartService.getAllCharts()).isEmpty(); + + doReturn(charts).when(chartStore).getAllCharts(); + Collection result = chartService.getAllCharts(); + assertNotNull(result); + assertThat(result).containsAll(charts); + } + + @Test + void test_getChart() { + assertNull(chartService.getChart("dummyName", "dummyversion")); + + doReturn(charts.get(0)).when(chartStore).getChart(any(), any()); + ChartInfo chart = chartService.getChart(charts.get(0).getChartId().getName(), + charts.get(0).getChartId().getVersion()); + assertNotNull(chart); + assertThat(chart.getNamespace()).isEqualTo(charts.get(0).getNamespace()); + } + + @Test + void test_saveChart() throws IOException, ServiceException { + doThrow(IOException.class).when(chartStore).saveChart(charts.get(0), null, null); + assertThatThrownBy(() -> chartService.saveChart(charts.get(0), null, null)) + .isInstanceOf(IOException.class); + + MockMultipartFile mockChartFile = new MockMultipartFile("chart", "dummy".getBytes()); + MockMultipartFile mockOverrideFile = new MockMultipartFile("override", "dummy".getBytes()); + + doReturn(charts.get(0)).when(chartStore).saveChart(any(), any(), any()); + + ChartInfo chart = chartService.saveChart(charts.get(0), mockChartFile, mockOverrideFile); + assertNotNull(chart); + assertThat(chart.getChartId().getName()).isEqualTo(charts.get(0).getChartId().getName()); + + } + + @Test + void test_installChart() throws IOException, ServiceException { + assertDoesNotThrow(() -> chartService.installChart(charts.get(0))); + doThrow(ServiceException.class).when(helmClient).installChart(any()); + assertThatThrownBy(() -> chartService.installChart(charts.get(0))).isInstanceOf(ServiceException.class); + + doReturn("dummyRepoName").when(chartService).findChartRepo(any()); + doNothing().when(helmClient).installChart(any()); + chartService.installChart(charts.get(1)); + assertEquals("dummyRepoName", charts.get(1).getRepository().getRepoName()); + + ChartInfo testChart = charts.get(1); + testChart.setRepository(null); + doReturn(null).when(chartService).findChartRepo(any()); + chartService.installChart(charts.get(1)); + } + + @Test + void test_UninstallChart() throws ServiceException { + assertDoesNotThrow(() -> chartService.uninstallChart(charts.get(0))); + doThrow(ServiceException.class).when(helmClient).uninstallChart(any()); + assertThatThrownBy(() -> chartService.uninstallChart(charts.get(0))).isInstanceOf(ServiceException.class); + } + + @Test + void test_findChartRepo() throws IOException, ServiceException { + assertDoesNotThrow(() -> chartService.findChartRepo(charts.get(0))); + doReturn("dummyRepoName").when(helmClient).findChartRepository(any()); + assertEquals("dummyRepoName", chartService.findChartRepo(charts.get(1))); + + doThrow(ServiceException.class).when(helmClient).findChartRepository(any()); + assertThatThrownBy(() -> chartService.findChartRepo(charts.get(0))).isInstanceOf(ServiceException.class); + } +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/service/ChartStoreTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/service/ChartStoreTest.java new file mode 100644 index 000000000..180861bae --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/service/ChartStoreTest.java @@ -0,0 +1,175 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.policy.clamp.acm.participant.kubernetes.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; +import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo; +import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList; +import org.onap.policy.clamp.acm.participant.kubernetes.parameters.ParticipantK8sParameters; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.util.FileSystemUtils; + + +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +class ChartStoreTest { + + private static final Coder CODER = new StandardCoder(); + private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json"; + private static List charts; + + @Mock + private ParticipantK8sParameters parameters; + + private ChartStore chartStore; + + + @BeforeAll + static void init() throws CoderException { + charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts(); + } + + //Overriding the local chart dir parameter to a temp folder under target for testing java FILE IO operations. + @BeforeEach + void setup() { + Mockito.doReturn("target/tmp/").when(parameters).getLocalChartDirectory(); + Mockito.doReturn("info.json").when(parameters).getInfoFileName(); + chartStore = new ChartStore(parameters); + } + + //Clean up the 'tmp' dir after each test case. + @AfterEach + void cleanUp() throws IOException { + FileSystemUtils.deleteRecursively(Path.of(parameters.getLocalChartDirectory())); + chartStore.getLocalChartMap().clear(); + } + + @Test + void test_getHelmChartFile() { + File file = chartStore.getHelmChartFile(charts.get(0)); + assertNotNull(file); + assertThat(file.getPath()).endsWith(charts.get(0).getChartId().getName()); + } + + @Test + void test_getOverrideFile() { + File file = chartStore.getOverrideFile(charts.get(0)); + assertNotNull(file); + assertThat(file.getPath()).endsWith("values.yaml"); + } + + @Test + void test_saveChart() throws IOException, ServiceException { + MockMultipartFile mockChartFile = new MockMultipartFile("chart", "dummy".getBytes()); + MockMultipartFile mockOverrideFile = new MockMultipartFile("override", "dummy".getBytes()); + ChartInfo testChart = charts.get(0); + testChart.setChartId(new ToscaConceptIdentifier("testChart", "1.0.0")); + ChartInfo result = chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile); + + assertThat(result.getChartId().getName()).isEqualTo("testChart"); + assertThat(chartStore.getLocalChartMap()).hasSize(1); + + assertThatThrownBy(() -> chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile)) + .isInstanceOf(ServiceException.class); + } + + + @Test + void test_getChart() { + assertNull(chartStore.getChart(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion())); + chartStore.getLocalChartMap().put(charts.get(0).getChartId().getName() + "_" + charts.get(0).getChartId() + .getVersion(), charts.get(0)); + ChartInfo chart = chartStore.getChart(charts.get(0).getChartId().getName(), + charts.get(0).getChartId().getVersion()); + assertThat(chart.getChartId().getName()).isEqualTo(charts.get(0).getChartId().getName()); + } + + @Test + void test_getAllChart() { + // When the chart store is empty before adding any charts + assertThat(chartStore.getAllCharts()).isEmpty(); + + for (ChartInfo chart : charts) { + chartStore.getLocalChartMap().put(chart.getChartId().getName() + "_" + chart.getChartId().getVersion(), + chart); + } + List retrievedChartList = chartStore.getAllCharts(); + assertThat(retrievedChartList).isNotEmpty(); + assertThat(retrievedChartList.size()).isEqualTo(charts.size()); + } + + @Test + void test_deleteChart() { + chartStore.getLocalChartMap().put(charts.get(0).getChartId().getName() + "_" + charts.get(0).getChartId() + .getVersion(), charts.get(0)); + assertThat(chartStore.getLocalChartMap()).hasSize(1); + chartStore.deleteChart(charts.get(0)); + assertThat(chartStore.getLocalChartMap()).isEmpty(); + } + + @Test + void test_getAppPath() { + Path path = chartStore.getAppPath(charts.get(0).getChartId()); + assertNotNull(path); + assertThat(path.toString()).endsWith(charts.get(0).getChartId().getVersion()); + assertThat(path.toString()).startsWith("target"); + } + + @Test + void test_chartSoreInstantiationWithExistingChartFiles() throws IOException, ServiceException { + MockMultipartFile mockChartFile = new MockMultipartFile("HelmChartFile", "dummyData".getBytes()); + MockMultipartFile mockOverrideFile = new MockMultipartFile("overrideFile.yaml", "dummyData".getBytes()); + ChartInfo testChart = charts.get(0); + testChart.setChartId(new ToscaConceptIdentifier("dummyChart", "1.0.0")); + + //Creating a dummy chart in local dir. + chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile); + + //Instantiating a new chartStore object with pre available chart in local. + ChartStore chartStore2 = new ChartStore(parameters); + assertThat(chartStore2.getLocalChartMap()).hasSize(1).containsKey("dummyChart_" + charts.get(0).getChartId() + .getVersion()); + } +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/utils/CommonActuatorController.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/utils/CommonActuatorController.java new file mode 100644 index 000000000..e5a5be9fa --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/utils/CommonActuatorController.java @@ -0,0 +1,114 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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.policy.clamp.acm.participant.kubernetes.utils; + +import static org.junit.Assert.assertEquals; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.glassfish.jersey.client.ClientProperties; +import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; +import org.onap.policy.common.gson.GsonMessageBodyHandler; +import org.onap.policy.common.utils.network.NetworkUtil; + +/** + * Class to perform Rest unit tests. + * + */ +public class CommonActuatorController { + + public static final String SELF = NetworkUtil.getHostname(); + public static final String CONTEXT_PATH = "onap/policy/clamp/acm/k8sparticipant"; + public static final String ACTUATOR_ENDPOINT = CONTEXT_PATH + "/actuator/"; + + private static String httpPrefix; + + /** + * Sends a request to an actuator endpoint. + * + * @param endpoint the target endpoint + * @return a request builder + * @throws Exception if an error occurs + */ + protected Invocation.Builder sendActRequest(final String endpoint) throws Exception { + return sendFqeRequest(httpPrefix + ACTUATOR_ENDPOINT + endpoint, true); + } + + /** + * Sends a request to an actuator endpoint, without any authorization header. + * + * @param endpoint the target endpoint + * @return a request builder + * @throws Exception if an error occurs + */ + protected Invocation.Builder sendNoAuthActRequest(final String endpoint) throws Exception { + return sendFqeRequest(httpPrefix + ACTUATOR_ENDPOINT + endpoint, false); + } + + /** + * Sends a request to a fully qualified endpoint. + * + * @param fullyQualifiedEndpoint the fully qualified target endpoint + * @param includeAuth if authorization header should be included + * @return a request builder + * @throws Exception if an error occurs + */ + protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth) + throws Exception { + final Client client = ClientBuilder.newBuilder().build(); + + client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); + client.register(GsonMessageBodyHandler.class); + + if (includeAuth) { + client.register(HttpAuthenticationFeature.basic("participantUser", "zb!XztG34")); + } + + final WebTarget webTarget = client.target(fullyQualifiedEndpoint); + + return webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN); + } + + /** + * Assert that GET call to actuator endpoint is Unauthorized. + * + * @param endPoint the endpoint + * @throws Exception if an error occurs + */ + protected void assertUnauthorizedActGet(final String endPoint) throws Exception { + Response rawresp = sendNoAuthActRequest(endPoint).buildGet().invoke(); + assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus()); + } + + /** + * Set Up httpPrefix. + * + * @param port the port + */ + protected void setHttpPrefix(int port) { + httpPrefix = "http://" + SELF + ":" + port + "/"; + } + +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/utils/TestUtils.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/utils/TestUtils.java new file mode 100644 index 000000000..8f4969a9b --- /dev/null +++ b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/acm/participant/kubernetes/utils/TestUtils.java @@ -0,0 +1,47 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.policy.clamp.acm.participant.kubernetes.utils; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.onap.policy.common.utils.coder.YamlJsonTranslator; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class TestUtils { + + private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); + private static final String TOSCA_TEMPLATE_YAML = "src/test/resources/servicetemplates/KubernetesHelm.yaml"; + + public static ToscaServiceTemplate testAutomationCompositionRead() { + return testAutomationCompositionYamlSerialization(TOSCA_TEMPLATE_YAML); + } + + private static ToscaServiceTemplate testAutomationCompositionYamlSerialization( + String automationCompositionFilePath) { + String automationCompositionString = ResourceUtils.getResourceAsString(automationCompositionFilePath); + ToscaServiceTemplate serviceTemplate = + yamlTranslator.fromYaml(automationCompositionString, ToscaServiceTemplate.class); + return serviceTemplate; + } +} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/handler/ControlLoopElementHandlerTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/handler/ControlLoopElementHandlerTest.java deleted file mode 100644 index 805404b90..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/handler/ControlLoopElementHandlerTest.java +++ /dev/null @@ -1,180 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021-2022 Nordix Foundation. - * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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.policy.clamp.controlloop.participant.kubernetes.handler; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.when; - -import java.io.File; -import java.io.IOException; -import java.util.List; -import java.util.Map; -import java.util.UUID; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Future; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.Spy; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; -import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi; -import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList; -import org.onap.policy.clamp.controlloop.participant.kubernetes.parameters.CommonTestData; -import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartService; -import org.onap.policy.clamp.controlloop.participant.kubernetes.utils.TestUtils; -import org.onap.policy.common.utils.coder.Coder; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; -import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; -import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; -import org.springframework.test.context.junit.jupiter.SpringExtension; - - -@ExtendWith(SpringExtension.class) -class ControlLoopElementHandlerTest { - - private static final Coder CODER = new StandardCoder(); - private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json"; - private static final String KEY_NAME = "org.onap.domain.database.HelloWorld_K8SMicroserviceControlLoopElement"; - private static List charts; - private static ToscaServiceTemplate toscaServiceTemplate; - private static final String K8S_CONTROL_LOOP_ELEMENT = - "org.onap.domain.database.PMSH_K8SMicroserviceControlLoopElement"; - private CommonTestData commonTestData = new CommonTestData(); - - @InjectMocks - @Spy - private ControlLoopElementHandler controlLoopElementHandler = new ControlLoopElementHandler(); - - @Mock - private ChartService chartService; - - @Mock - private ParticipantIntermediaryApi participantIntermediaryApi; - - @Mock - private ExecutorService executor; - - @Mock - private Future result; - - @BeforeAll - static void init() throws CoderException { - charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts(); - toscaServiceTemplate = TestUtils.testControlLoopRead(); - } - - - @Test - void test_ControlLoopElementStateChange() throws ServiceException { - UUID controlLoopElementId1 = UUID.randomUUID(); - UUID controlLoopElementId2 = UUID.randomUUID(); - - controlLoopElementHandler.getChartMap().put(controlLoopElementId1, charts.get(0)); - controlLoopElementHandler.getChartMap().put(controlLoopElementId2, charts.get(1)); - - doNothing().when(chartService).uninstallChart(charts.get(0)); - - controlLoopElementHandler.controlLoopElementStateChange(commonTestData.getControlLoopId(), - controlLoopElementId1, ControlLoopState.PASSIVE, ControlLoopOrderedState.UNINITIALISED); - - doThrow(new ServiceException("Error uninstalling the chart")).when(chartService) - .uninstallChart(charts.get(0)); - - assertDoesNotThrow(() -> controlLoopElementHandler - .controlLoopElementStateChange(commonTestData.getControlLoopId(), controlLoopElementId1, - ControlLoopState.PASSIVE, ControlLoopOrderedState.PASSIVE)); - - assertDoesNotThrow(() -> controlLoopElementHandler - .controlLoopElementStateChange(commonTestData.getControlLoopId(), controlLoopElementId1, - ControlLoopState.PASSIVE, ControlLoopOrderedState.UNINITIALISED)); - - assertDoesNotThrow(() -> controlLoopElementHandler - .controlLoopElementStateChange(commonTestData.getControlLoopId(), controlLoopElementId1, - ControlLoopState.PASSIVE, ControlLoopOrderedState.RUNNING)); - - } - - @Test - void test_ControlLoopElementUpdate() throws PfModelException, IOException, ServiceException, - ExecutionException, InterruptedException { - doNothing().when(controlLoopElementHandler).checkPodStatus(any(), any(), any(), anyInt(), anyInt()); - UUID elementId1 = UUID.randomUUID(); - ControlLoopElement element = new ControlLoopElement(); - element.setId(elementId1); - element.setDefinition(new ToscaConceptIdentifier(KEY_NAME, "1.0.1")); - element.setOrderedState(ControlLoopOrderedState.PASSIVE); - - Map nodeTemplatesMap = - toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates(); - controlLoopElementHandler.controlLoopElementUpdate(commonTestData.getControlLoopId(), element, - nodeTemplatesMap.get(K8S_CONTROL_LOOP_ELEMENT)); - - assertThat(controlLoopElementHandler.getChartMap()).hasSize(1).containsKey(elementId1); - - doThrow(new ServiceException("Error installing the chart")).when(chartService) - .installChart(Mockito.any()); - - UUID elementId2 = UUID.randomUUID(); - element.setId(elementId2); - controlLoopElementHandler.controlLoopElementUpdate(commonTestData.getControlLoopId(), element, - nodeTemplatesMap.get(K8S_CONTROL_LOOP_ELEMENT)); - - assertThat(controlLoopElementHandler.getChartMap().containsKey(elementId2)).isFalse(); - } - - @Test - void test_handleStatistics() throws PfModelException { - UUID elementId1 = UUID.randomUUID(); - controlLoopElementHandler.getChartMap().put(elementId1, charts.get(0)); - when(participantIntermediaryApi.getControlLoopElement(elementId1)).thenReturn(new ControlLoopElement()); - assertDoesNotThrow(() -> controlLoopElementHandler.handleStatistics(elementId1)); - } - - @Test - void test_checkPodStatus() throws ExecutionException, InterruptedException { - doReturn(result).when(executor).submit(any(Runnable.class), any()); - doReturn("Done").when(result).get(); - doReturn(true).when(result).isDone(); - var chartInfo = charts.get(0); - ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier(); - ControlLoopElement element = new ControlLoopElement(); - assertDoesNotThrow(() -> controlLoopElementHandler.checkPodStatus(controlLoopId, element.getId(), chartInfo, - 1, 1)); - } -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/HelmClientTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/HelmClientTest.java deleted file mode 100644 index 335dbcb21..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/HelmClientTest.java +++ /dev/null @@ -1,158 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. - * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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.policy.clamp.controlloop.participant.kubernetes.helm; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertNull; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mockStatic; -import static org.mockito.Mockito.when; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; -import java.util.List; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockedStatic; -import org.mockito.Mockito; -import org.mockito.Spy; -import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.HelmRepository; -import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartStore; -import org.onap.policy.common.utils.coder.Coder; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.springframework.util.FileSystemUtils; - - -@ExtendWith(SpringExtension.class) -class HelmClientTest { - - private static final Coder CODER = new StandardCoder(); - private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json"; - private static List charts; - - @InjectMocks - @Spy - private HelmClient helmClient = new HelmClient(); - - @Mock - ChartStore chartStore; - - @Mock - HelmRepository repo; - - private static MockedStatic mockedClient; - - @BeforeAll - static void init() throws CoderException { - charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts(); - //Mock static method for bash command execution - mockedClient = mockStatic(HelmClient.class); - } - - @AfterAll - public static void close() throws IOException { - mockedClient.close(); - FileSystemUtils.deleteRecursively(Path.of("target/tmp")); - } - - @Test - void test_installChart() throws IOException { - mockedClient.when(() -> HelmClient.executeCommand(any())) - .thenReturn("success"); - doReturn(new File("/target/tmp/override.yaml")).when(chartStore) - .getOverrideFile(any()); - var chartinfo = charts.get(0); - - assertDoesNotThrow(() -> helmClient.installChart(chartinfo)); - chartinfo.setNamespace(""); - assertDoesNotThrow(() -> helmClient.installChart(chartinfo)); - - mockedClient.when(() -> HelmClient.executeCommand(any())) - .thenReturn(new String()); - assertDoesNotThrow(() -> helmClient.installChart(chartinfo)); - - } - - @Test - void test_addRepository() throws IOException { - mockedClient.when(() -> HelmClient.executeCommand(any())) - .thenReturn(new String()); - when(repo.getRepoName()).thenReturn("RepoName"); - assertDoesNotThrow(() -> helmClient.addRepository(repo)); - - mockedClient.when(() -> HelmClient.executeCommand(any())) - .thenReturn("failed"); - assertDoesNotThrow(() -> helmClient.addRepository(repo)); - } - - @Test - void test_findChartRepository() throws IOException, ServiceException { - String tmpPath = "target/tmp/dummyChart/1.0/"; - mockedClient.when(() -> HelmClient.executeCommand(Mockito.any())) - .thenReturn("nginx-stable/nginx-ingress\t0.9.3\t1.11.3" - + " \tNGINX Ingress Controller"); - String configuredRepo = helmClient.findChartRepository(charts.get(1)); - assertThat(configuredRepo).isEqualTo("nginx-stable"); - - File tmpFile = new File(tmpPath + charts.get(1).getChartId().getName()); - tmpFile.mkdirs(); - doReturn(Path.of(tmpPath)).when(chartStore).getAppPath(charts.get(1).getChartId()); - - doReturn(null).when(helmClient).verifyConfiguredRepo(charts.get(1)); - - String localRepoName = helmClient.findChartRepository(charts.get(1)); - assertNotNull(localRepoName); - assertThat(localRepoName).endsWith(charts.get(0).getChartId().getVersion()); - } - - @Test - void test_uninstallChart() throws ServiceException { - helmClient.uninstallChart(charts.get(0)); - mockedClient.when(() -> HelmClient.executeCommand(any())).thenThrow(new ServiceException("error in execution")); - - assertThatThrownBy(() -> helmClient.uninstallChart(charts.get(0))) - .isInstanceOf(ServiceException.class); - } - - @Test - void test_verifyConfiguredRepoForInvalidChart() throws IOException, ServiceException { - mockedClient.when(() -> HelmClient.executeCommand(Mockito.any())) - .thenReturn(""); - String configuredRepo = helmClient.verifyConfiguredRepo(charts.get(1)); - assertNull(configuredRepo); - } - -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/PodStatusValidatorTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/PodStatusValidatorTest.java deleted file mode 100644 index 18c32d474..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/PodStatusValidatorTest.java +++ /dev/null @@ -1,109 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. - * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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.policy.clamp.controlloop.participant.kubernetes.helm; - - - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mockStatic; - -import java.io.File; -import java.util.List; -import java.util.Map; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.MockedStatic; -import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException; -import org.onap.policy.clamp.controlloop.participant.kubernetes.handler.ControlLoopElementHandler; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList; -import org.onap.policy.common.utils.coder.Coder; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.springframework.test.context.junit.jupiter.SpringExtension; - -@ExtendWith(SpringExtension.class) -class PodStatusValidatorTest { - - - private static final Coder CODER = new StandardCoder(); - private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json"; - private static List charts; - private static int timeout = 60; - private static int statusCheckInterval = 30; - - - @InjectMocks - private static PodStatusValidator podStatusValidator; - - private static MockedStatic mockedClient; - - - @BeforeAll - static void init() throws CoderException { - charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts(); - mockedClient = mockStatic(HelmClient.class); - podStatusValidator = new PodStatusValidator(charts.get(0), timeout, statusCheckInterval); - } - - @AfterEach - void clearPodStatusMap() { - ControlLoopElementHandler.getPodStatusMap().clear(); - } - - @AfterAll - public static void close() { - mockedClient.close(); - } - - - @Test - void test_RunningPodState() { - String runningPod = "NAME\tREADY\tSTATUS\tRESTARTS\tAGE\r\nHelloWorld-54777df9f8-qpzqr\t1/1\tRunning\t0\t9h"; - mockedClient.when(() -> HelmClient.executeCommand(any())) - .thenReturn(runningPod); - assertDoesNotThrow(() -> podStatusValidator.run()); - assertThat(ControlLoopElementHandler.getPodStatusMap()).hasSize(1); - assertThat(ControlLoopElementHandler.getPodStatusMap()).containsKey(charts.get(0).getReleaseName()); - assertThat(ControlLoopElementHandler.getPodStatusMap()) - .containsValue(Map.of("HelloWorld-54777df9f8-qpzqr", "Running")); - } - - - @Test - void test_InvalidPodState() { - String invalidPod = "NAME\tREADY\tSTATUS\tRESTARTS\tAGE\nhellofromdocker-54777df9f8-qpzqr\t1/1\tInit\t0\t9h"; - mockedClient.when(() -> HelmClient.executeCommand(any())) - .thenReturn(invalidPod); - assertThatThrownBy(() -> podStatusValidator.run()) - .isInstanceOf(ServiceException.class).hasMessage("Error verifying the status of the pod. Exiting"); - assertThat(ControlLoopElementHandler.getPodStatusMap()).isEmpty(); - } - -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/parameters/CommonTestData.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/parameters/CommonTestData.java deleted file mode 100644 index f6ec401b8..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/parameters/CommonTestData.java +++ /dev/null @@ -1,161 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. - * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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.policy.clamp.controlloop.participant.kubernetes.parameters; - -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; -import org.onap.policy.common.endpoints.parameters.TopicParameters; -import org.onap.policy.common.utils.coder.Coder; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; - -public class CommonTestData { - - public static final String PARTICIPANT_GROUP_NAME = "ControlLoopParticipantGroup"; - public static final String DESCRIPTION = "Participant description"; - public static final long TIME_INTERVAL = 2000; - public static final List TOPIC_PARAMS = Arrays.asList(getTopicParams()); - public static final Coder CODER = new StandardCoder(); - - - /** - * Get ParticipantK8sParameters. - * - * @return ParticipantK8sParameters - */ - public ParticipantK8sParameters getParticipantK8sParameters() { - try { - return CODER.convert(getParticipantK8sParametersMap(PARTICIPANT_GROUP_NAME), - ParticipantK8sParameters.class); - } catch (final CoderException e) { - throw new RuntimeException("cannot create ParticipantK8sParameters from map", e); - } - } - - /** - * Returns a property map for a ParticipantK8sParameters map for test cases. - * - * @param name name of the parameters - * - * @return a property map suitable for constructing an object - */ - public Map getParticipantK8sParametersMap(final String name) { - final Map map = new TreeMap<>(); - - map.put("name", name); - map.put("intermediaryParameters", getIntermediaryParametersMap(false)); - map.put("localChartDirectory", getLocalChartDir()); - map.put("infoFileName", getInfoFileName()); - return map; - } - - - /** - * Returns string value of local chart Directory. - * @return a string value - */ - public String getLocalChartDir() { - return "/var/helm-manager/local-charts"; - } - - /** - * Returns string value of Info file name. - * @return string value - */ - public String getInfoFileName() { - return "CHART-INFO.json"; - } - - - - /** - * Returns a property map for a intermediaryParameters map for test cases. - * - * @param isEmpty boolean value to represent that object created should be empty or not - * @return a property map suitable for constructing an object - */ - public Map getIntermediaryParametersMap(final boolean isEmpty) { - final Map map = new TreeMap<>(); - if (!isEmpty) { - map.put("name", "Participant parameters"); - map.put("reportingTimeIntervalMs", TIME_INTERVAL); - map.put("description", DESCRIPTION); - map.put("participantId", getParticipantId()); - map.put("participantType", getParticipantId()); - map.put("clampControlLoopTopics", getTopicParametersMap(false)); - } - - return map; - } - - /** - * Returns participantId for test cases. - * - * @return participant Id - */ - public static ToscaConceptIdentifier getParticipantId() { - final ToscaConceptIdentifier participantId = new ToscaConceptIdentifier(); - participantId.setName("K8sParticipant0"); - participantId.setVersion("1.0.0"); - return participantId; - } - - - /** - * Returns a property map for a TopicParameters map for test cases. - * - * @param isEmpty boolean value to represent that object created should be empty or not - * @return a property map suitable for constructing an object - */ - public Map getTopicParametersMap(final boolean isEmpty) { - final Map map = new TreeMap<>(); - if (!isEmpty) { - map.put("topicSources", TOPIC_PARAMS); - map.put("topicSinks", TOPIC_PARAMS); - } - return map; - } - - /** - * Returns topic parameters for test cases. - * - * @return topic parameters - */ - public static TopicParameters getTopicParams() { - final TopicParameters topicParams = new TopicParameters(); - topicParams.setTopic("POLICY-CLRUNTIME-PARTICIPANT"); - topicParams.setTopicCommInfrastructure("dmaap"); - topicParams.setServers(Arrays.asList("localhost")); - return topicParams; - } - - /** - * Get controlloop id. - * @return ToscaConceptIdentifier controlLoopId - */ - public ToscaConceptIdentifier getControlLoopId() { - return new ToscaConceptIdentifier("PMSHInstance0", "1.0.0"); - } -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/parameters/ParticipantK8sParametersTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/parameters/ParticipantK8sParametersTest.java deleted file mode 100644 index f22fc711e..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/parameters/ParticipantK8sParametersTest.java +++ /dev/null @@ -1,89 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. - * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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.policy.clamp.controlloop.participant.kubernetes.parameters; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.Set; -import javax.validation.ConstraintViolation; -import javax.validation.Validation; -import javax.validation.ValidatorFactory; -import org.junit.jupiter.api.Test; - -class ParticipantK8sParametersTest { - - private CommonTestData commonTestData = new CommonTestData(); - private ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); - - @Test - void testParticipantPolicyParameters() { - final ParticipantK8sParameters participantParameters = commonTestData.getParticipantK8sParameters(); - assertThat(validatorFactory.getValidator().validate(participantParameters)).isNullOrEmpty(); - } - - @Test - void testParticipantK8sParameters_NullTopicSinks() { - final ParticipantK8sParameters participantParameters = commonTestData.getParticipantK8sParameters(); - participantParameters.getIntermediaryParameters().getClampControlLoopTopics().setTopicSinks(null); - assertThat(validatorFactory.getValidator().validate(participantParameters)).isNotEmpty(); - } - - @Test - void testParticipantK8sParameters_NullTopicSources() { - final ParticipantK8sParameters participantParameters = commonTestData.getParticipantK8sParameters(); - participantParameters.getIntermediaryParameters().getClampControlLoopTopics().setTopicSources(null); - assertThat(validatorFactory.getValidator().validate(participantParameters)).isNotEmpty(); - } - - @Test - void testParticipantK8sParameters_BlankLocalChartDirParameter() { - final ParticipantK8sParameters participantParameters = commonTestData.getParticipantK8sParameters(); - participantParameters.setLocalChartDirectory(" "); - Set> violations = validatorFactory.getValidator() - .validate(participantParameters); - assertThat(violations.size()).isEqualTo(1); - } - - @Test - void testParticipantK8sParameters_BlankInfoFileParameter() { - final ParticipantK8sParameters participantParameters = commonTestData.getParticipantK8sParameters(); - participantParameters.setInfoFileName(""); - Set> violations = validatorFactory.getValidator() - .validate(participantParameters); - assertThat(violations.size()).isEqualTo(1); - } - - @Test - void testNoIntermediaryParameters() { - final ParticipantK8sParameters participantParameters = commonTestData.getParticipantK8sParameters(); - participantParameters.setIntermediaryParameters(null); - assertThat(validatorFactory.getValidator().validate(participantParameters)).isNotEmpty(); - } - - @Test - void testNoParticipantId() { - final ParticipantK8sParameters participantParameters = commonTestData.getParticipantK8sParameters(); - participantParameters.getIntermediaryParameters().setParticipantId(null); - assertThat(validatorFactory.getValidator().validate(participantParameters)).isNotEmpty(); - } - -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/rest/ActuatorControllerTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/rest/ActuatorControllerTest.java deleted file mode 100644 index 1442e9f1f..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/rest/ActuatorControllerTest.java +++ /dev/null @@ -1,92 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 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.policy.clamp.controlloop.participant.kubernetes.rest; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import javax.ws.rs.client.Invocation; -import javax.ws.rs.core.Response; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.onap.policy.clamp.controlloop.participant.kubernetes.utils.CommonActuatorController; -import org.springframework.boot.test.autoconfigure.actuate.metrics.AutoConfigureMetrics; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.web.server.LocalServerPort; -import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit.jupiter.SpringExtension; - -@AutoConfigureMetrics -@ExtendWith(SpringExtension.class) -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -@TestPropertySource(locations = {"classpath:application_test.properties"}) -class ActuatorControllerTest extends CommonActuatorController { - - private static final String HEALTH_ENDPOINT = "health"; - private static final String METRICS_ENDPOINT = "metrics"; - private static final String PROMETHEUS_ENDPOINT = "prometheus"; - - @LocalServerPort - private int randomServerPort; - - @BeforeEach - public void setUpPort() { - super.setHttpPrefix(randomServerPort); - } - - @Test - void testGetHealth_Unauthorized() throws Exception { - assertUnauthorizedActGet(HEALTH_ENDPOINT); - } - - @Test - void testGetMetrics_Unauthorized() throws Exception { - assertUnauthorizedActGet(METRICS_ENDPOINT); - } - - @Test - void testGetPrometheus_Unauthorized() throws Exception { - assertUnauthorizedActGet(PROMETHEUS_ENDPOINT); - } - - @Test - void testGetHealth() throws Exception { - Invocation.Builder invocationBuilder = super.sendActRequest(HEALTH_ENDPOINT); - Response rawresp = invocationBuilder.buildGet().invoke(); - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } - - @Test - void testGetMetrics() throws Exception { - Invocation.Builder invocationBuilder = super.sendActRequest(METRICS_ENDPOINT); - Response rawresp = invocationBuilder.buildGet().invoke(); - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } - - @Test - void testGePrometheus() throws Exception { - Invocation.Builder invocationBuilder = super.sendActRequest(PROMETHEUS_ENDPOINT); - Response rawresp = invocationBuilder.buildGet().invoke(); - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - } - -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/rest/ChartControllerTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/rest/ChartControllerTest.java deleted file mode 100644 index 8048b19ce..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/rest/ChartControllerTest.java +++ /dev/null @@ -1,249 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021-2022 Nordix Foundation. - * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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.policy.clamp.controlloop.participant.kubernetes.rest; - -import static org.hamcrest.CoreMatchers.is; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.when; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -import java.io.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.List; -import org.apache.commons.io.FileUtils; -import org.json.JSONObject; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.onap.policy.clamp.controlloop.participant.kubernetes.controller.ChartController; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList; -import org.onap.policy.clamp.controlloop.participant.kubernetes.parameters.ParticipantK8sParameters; -import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartService; -import org.onap.policy.common.utils.coder.Coder; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.http.MediaType; -import org.springframework.mock.web.MockMultipartFile; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.RequestBuilder; -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.web.context.WebApplicationContext; - - -@ExtendWith(SpringExtension.class) -@WebMvcTest(value = ChartController.class, properties = "chart.api.enabled=true") -@EnableConfigurationProperties(value = ParticipantK8sParameters.class) -class ChartControllerTest { - - private static final Coder CODER = new StandardCoder(); - private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json"; - private static List charts; - private static String RETRIEVE_CHART_URL = "/helm/charts"; - private static String INSTALL_CHART_URL = "/helm/install"; - private static String UNINSTALL_CHART_URL = "/helm/uninstall/"; - private static String ONBOARD_CHART_URL = "/helm/onboard/chart"; - private static String DELETE_CHART_URL = "/helm/chart"; - private static String CONFIGURE_REPO_URL = "/helm/repo"; - - @Autowired - private MockMvc mockMvc; - - @MockBean - private ChartService chartService; - - @Autowired - private WebApplicationContext context; - - /** - * Read input chart info json. - * @throws Exception incase of error. - */ - @BeforeAll - static void setupParams() throws CoderException { - charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts(); - } - - /** - * Mock service layer in Controller. - * @throws Exception incase of error. - */ - @BeforeEach - void mockServiceClass() { - when(chartService.getAllCharts()).thenReturn(charts); - when(chartService.getChart(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion())) - .thenReturn(charts.get(0)); - - this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build(); - } - - /** - * Test endpoint for retrieving all charts. - * @throws Exception incase of error. - */ - @Test - void retrieveAllCharts() throws Exception { - RequestBuilder requestBuilder; - requestBuilder = MockMvcRequestBuilders.get(RETRIEVE_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE); - - mockMvc.perform(requestBuilder).andExpect(status().isOk()) - .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.charts.[0].chartId.name", is("HelloWorld"))); - } - - /** - * Test endpoint for installing a chart. - * @throws Exception incase of error. - */ - @Test - void installChart() throws Exception { - RequestBuilder requestBuilder; - - //Mocking successful installation for void install method - doNothing().when(chartService).installChart(charts.get(0)); - - requestBuilder = MockMvcRequestBuilders.post(INSTALL_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE) - .content(getInstallationJson(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion())) - .contentType(MediaType.APPLICATION_JSON_VALUE); - - mockMvc.perform(requestBuilder).andExpect(status().isCreated()); - - //Install Invalid chart, expects HTTP status NOT_FOUND - requestBuilder = MockMvcRequestBuilders.post(INSTALL_CHART_URL).accept(MediaType.APPLICATION_JSON_VALUE) - .content(getInstallationJson("invalidName", "invalidVersion")) - .contentType(MediaType.APPLICATION_JSON_VALUE); - - mockMvc.perform(requestBuilder).andExpect(status().isNotFound()); - } - - /** - * Test endpoint for uninstalling a chart. - * @throws Exception incase of error. - */ - @Test - void uninstallChart() throws Exception { - RequestBuilder requestBuilder; - - //Mocking successful scenario for void uninstall method - doNothing().when(chartService).uninstallChart(charts.get(0)); - - requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + charts.get(0) - .getChartId().getName() + "/" + charts.get(0).getChartId().getVersion()) - .accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE); - - mockMvc.perform(requestBuilder).andExpect(status().isNoContent()); - - //Invalid chart - requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + "invalidName" - + "/" + "invalidVersion").accept(MediaType.APPLICATION_JSON_VALUE) - .contentType(MediaType.APPLICATION_JSON_VALUE); - - mockMvc.perform(requestBuilder).andExpect(status().isNotFound()); - } - - /** - * Test endpoint for chart onboarding. - * @throws Exception incase of error. - */ - @Test - void onboardChart() throws Exception { - RequestBuilder requestBuilder; - MockMultipartFile chartFile = new MockMultipartFile("chart", "hello.tgz", - MediaType.TEXT_PLAIN_VALUE, "Dummy data".getBytes()); - - MockMultipartFile overrideFile = new MockMultipartFile("values", "values.yaml", - MediaType.TEXT_PLAIN_VALUE, "Dummy data".getBytes()); - - //Mocking successful scenario for void uninstall method - when(chartService.saveChart(charts.get(0), chartFile, null)).thenReturn(charts.get(0)); - - requestBuilder = MockMvcRequestBuilders.multipart(ONBOARD_CHART_URL) - .file(chartFile).file(overrideFile).param("info", getChartInfoJson()); - - mockMvc.perform(requestBuilder).andExpect(status().isOk()); - } - - /** - * Test endpoint for deleting a chart. - * @throws Exception incase of error. - */ - @Test - void deleteChart() throws Exception { - RequestBuilder requestBuilder; - - //Mocking successful scenario for void uninstall method - doNothing().when(chartService).deleteChart(charts.get(0)); - - requestBuilder = MockMvcRequestBuilders.delete(DELETE_CHART_URL + "/" + charts.get(0) - .getChartId().getName() + "/" + charts.get(0).getChartId().getVersion()) - .accept(MediaType.APPLICATION_JSON_VALUE) - .contentType(MediaType.APPLICATION_JSON_VALUE); - - mockMvc.perform(requestBuilder).andExpect(status().isNoContent()); - //Invalid chart - requestBuilder = MockMvcRequestBuilders.delete(UNINSTALL_CHART_URL + "invalidName" - + "/" + "invalidVersion").accept(MediaType.APPLICATION_JSON_VALUE) - .contentType(MediaType.APPLICATION_JSON_VALUE); - - mockMvc.perform(requestBuilder).andExpect(status().isNotFound()); - - } - - /** - * Test endpoint for configuring a helm repository. - * @throws Exception in case of error. - */ - @Test - void testConfigureRepo() throws Exception { - RequestBuilder requestBuilder; - - requestBuilder = MockMvcRequestBuilders.post(CONFIGURE_REPO_URL).accept(MediaType.APPLICATION_JSON_VALUE) - .content(getInstallationJson(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion())) - .contentType(MediaType.APPLICATION_JSON_VALUE); - - mockMvc.perform(requestBuilder).andExpect(status().isCreated()); - - } - - - private String getInstallationJson(String name, String version) { - JSONObject jsonObj = new JSONObject(); - jsonObj.put("name", name); - jsonObj.put("version", version); - return jsonObj.toString(); - } - - private String getChartInfoJson() throws IOException { - return FileUtils.readFileToString(new File(CHART_INFO_YAML), StandardCharsets.UTF_8); - } - -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/service/ChartServiceTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/service/ChartServiceTest.java deleted file mode 100644 index f1c8d19df..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/service/ChartServiceTest.java +++ /dev/null @@ -1,148 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. - * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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.policy.clamp.controlloop.participant.kubernetes.service; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; - -import java.io.File; -import java.io.IOException; -import java.util.Collection; -import java.util.List; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Spy; -import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException; -import org.onap.policy.clamp.controlloop.participant.kubernetes.helm.HelmClient; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList; -import org.onap.policy.common.utils.coder.Coder; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.springframework.mock.web.MockMultipartFile; -import org.springframework.test.context.junit.jupiter.SpringExtension; - -@ExtendWith(SpringExtension.class) -class ChartServiceTest { - - private static final Coder CODER = new StandardCoder(); - private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json"; - private static List charts; - - @InjectMocks - @Spy - private ChartService chartService = new ChartService(); - - @Mock - private ChartStore chartStore; - - @Mock - private HelmClient helmClient; - - @BeforeAll - static void init() throws CoderException { - charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts(); - } - - @Test - void test_getAllCharts() { - assertThat(chartService.getAllCharts()).isEmpty(); - - doReturn(charts).when(chartStore).getAllCharts(); - Collection result = chartService.getAllCharts(); - assertNotNull(result); - assertThat(result).containsAll(charts); - } - - @Test - void test_getChart() { - assertNull(chartService.getChart("dummyName", "dummyversion")); - - doReturn(charts.get(0)).when(chartStore).getChart(any(), any()); - ChartInfo chart = chartService.getChart(charts.get(0).getChartId().getName(), - charts.get(0).getChartId().getVersion()); - assertNotNull(chart); - assertThat(chart.getNamespace()).isEqualTo(charts.get(0).getNamespace()); - } - - @Test - void test_saveChart() throws IOException, ServiceException { - doThrow(IOException.class).when(chartStore).saveChart(charts.get(0), null, null); - assertThatThrownBy(() -> chartService.saveChart(charts.get(0), null, null)) - .isInstanceOf(IOException.class); - - MockMultipartFile mockChartFile = new MockMultipartFile("chart", "dummy".getBytes()); - MockMultipartFile mockOverrideFile = new MockMultipartFile("override", "dummy".getBytes()); - - doReturn(charts.get(0)).when(chartStore).saveChart(any(), any(), any()); - - ChartInfo chart = chartService.saveChart(charts.get(0), mockChartFile, mockOverrideFile); - assertNotNull(chart); - assertThat(chart.getChartId().getName()).isEqualTo(charts.get(0).getChartId().getName()); - - } - - @Test - void test_installChart() throws IOException, ServiceException { - assertDoesNotThrow(() -> chartService.installChart(charts.get(0))); - doThrow(ServiceException.class).when(helmClient).installChart(any()); - assertThatThrownBy(() -> chartService.installChart(charts.get(0))).isInstanceOf(ServiceException.class); - - doReturn("dummyRepoName").when(chartService).findChartRepo(any()); - doNothing().when(helmClient).installChart(any()); - chartService.installChart(charts.get(1)); - assertEquals("dummyRepoName", charts.get(1).getRepository().getRepoName()); - - ChartInfo testChart = charts.get(1); - testChart.setRepository(null); - doReturn(null).when(chartService).findChartRepo(any()); - chartService.installChart(charts.get(1)); - } - - @Test - void test_UninstallChart() throws ServiceException { - assertDoesNotThrow(() -> chartService.uninstallChart(charts.get(0))); - doThrow(ServiceException.class).when(helmClient).uninstallChart(any()); - assertThatThrownBy(() -> chartService.uninstallChart(charts.get(0))).isInstanceOf(ServiceException.class); - } - - @Test - void test_findChartRepo() throws IOException, ServiceException { - assertDoesNotThrow(() -> chartService.findChartRepo(charts.get(0))); - doReturn("dummyRepoName").when(helmClient).findChartRepository(any()); - assertEquals("dummyRepoName", chartService.findChartRepo(charts.get(1))); - - doThrow(ServiceException.class).when(helmClient).findChartRepository(any()); - assertThatThrownBy(() -> chartService.findChartRepo(charts.get(0))).isInstanceOf(ServiceException.class); - } -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/service/ChartStoreTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/service/ChartStoreTest.java deleted file mode 100644 index 54f1cc528..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/service/ChartStoreTest.java +++ /dev/null @@ -1,175 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. - * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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.policy.clamp.controlloop.participant.kubernetes.service; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertNull; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; -import java.util.List; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.jupiter.MockitoExtension; -import org.mockito.junit.jupiter.MockitoSettings; -import org.mockito.quality.Strictness; -import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo; -import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList; -import org.onap.policy.clamp.controlloop.participant.kubernetes.parameters.ParticipantK8sParameters; -import org.onap.policy.common.utils.coder.Coder; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; -import org.springframework.mock.web.MockMultipartFile; -import org.springframework.util.FileSystemUtils; - - -@ExtendWith(MockitoExtension.class) -@MockitoSettings(strictness = Strictness.LENIENT) -class ChartStoreTest { - - private static final Coder CODER = new StandardCoder(); - private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json"; - private static List charts; - - @Mock - private ParticipantK8sParameters parameters; - - private ChartStore chartStore; - - - @BeforeAll - static void init() throws CoderException { - charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts(); - } - - //Overriding the local chart dir parameter to a temp folder under target for testing java FILE IO operations. - @BeforeEach - void setup() { - Mockito.doReturn("target/tmp/").when(parameters).getLocalChartDirectory(); - Mockito.doReturn("info.json").when(parameters).getInfoFileName(); - chartStore = new ChartStore(parameters); - } - - //Clean up the 'tmp' dir after each test case. - @AfterEach - void cleanUp() throws IOException { - FileSystemUtils.deleteRecursively(Path.of(parameters.getLocalChartDirectory())); - chartStore.getLocalChartMap().clear(); - } - - @Test - void test_getHelmChartFile() { - File file = chartStore.getHelmChartFile(charts.get(0)); - assertNotNull(file); - assertThat(file.getPath()).endsWith(charts.get(0).getChartId().getName()); - } - - @Test - void test_getOverrideFile() { - File file = chartStore.getOverrideFile(charts.get(0)); - assertNotNull(file); - assertThat(file.getPath()).endsWith("values.yaml"); - } - - @Test - void test_saveChart() throws IOException, ServiceException { - MockMultipartFile mockChartFile = new MockMultipartFile("chart", "dummy".getBytes()); - MockMultipartFile mockOverrideFile = new MockMultipartFile("override", "dummy".getBytes()); - ChartInfo testChart = charts.get(0); - testChart.setChartId(new ToscaConceptIdentifier("testChart", "1.0.0")); - ChartInfo result = chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile); - - assertThat(result.getChartId().getName()).isEqualTo("testChart"); - assertThat(chartStore.getLocalChartMap()).hasSize(1); - - assertThatThrownBy(() -> chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile)) - .isInstanceOf(ServiceException.class); - } - - - @Test - void test_getChart() { - assertNull(chartStore.getChart(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion())); - chartStore.getLocalChartMap().put(charts.get(0).getChartId().getName() + "_" + charts.get(0).getChartId() - .getVersion(), charts.get(0)); - ChartInfo chart = chartStore.getChart(charts.get(0).getChartId().getName(), - charts.get(0).getChartId().getVersion()); - assertThat(chart.getChartId().getName()).isEqualTo(charts.get(0).getChartId().getName()); - } - - @Test - void test_getAllChart() { - // When the chart store is empty before adding any charts - assertThat(chartStore.getAllCharts()).isEmpty(); - - for (ChartInfo chart : charts) { - chartStore.getLocalChartMap().put(chart.getChartId().getName() + "_" + chart.getChartId().getVersion(), - chart); - } - List retrievedChartList = chartStore.getAllCharts(); - assertThat(retrievedChartList).isNotEmpty(); - assertThat(retrievedChartList.size()).isEqualTo(charts.size()); - } - - @Test - void test_deleteChart() { - chartStore.getLocalChartMap().put(charts.get(0).getChartId().getName() + "_" + charts.get(0).getChartId() - .getVersion(), charts.get(0)); - assertThat(chartStore.getLocalChartMap()).hasSize(1); - chartStore.deleteChart(charts.get(0)); - assertThat(chartStore.getLocalChartMap()).isEmpty(); - } - - @Test - void test_getAppPath() { - Path path = chartStore.getAppPath(charts.get(0).getChartId()); - assertNotNull(path); - assertThat(path.toString()).endsWith(charts.get(0).getChartId().getVersion()); - assertThat(path.toString()).startsWith("target"); - } - - @Test - void test_chartSoreInstantiationWithExistingChartFiles() throws IOException, ServiceException { - MockMultipartFile mockChartFile = new MockMultipartFile("HelmChartFile", "dummyData".getBytes()); - MockMultipartFile mockOverrideFile = new MockMultipartFile("overrideFile.yaml", "dummyData".getBytes()); - ChartInfo testChart = charts.get(0); - testChart.setChartId(new ToscaConceptIdentifier("dummyChart", "1.0.0")); - - //Creating a dummy chart in local dir. - chartStore.saveChart(charts.get(0), mockChartFile, mockOverrideFile); - - //Instantiating a new chartStore object with pre available chart in local. - ChartStore chartStore2 = new ChartStore(parameters); - assertThat(chartStore2.getLocalChartMap()).hasSize(1).containsKey("dummyChart_" + charts.get(0).getChartId() - .getVersion()); - } -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/utils/CommonActuatorController.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/utils/CommonActuatorController.java deleted file mode 100644 index 35ffbb5e9..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/utils/CommonActuatorController.java +++ /dev/null @@ -1,114 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 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.policy.clamp.controlloop.participant.kubernetes.utils; - -import static org.junit.Assert.assertEquals; - -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; -import javax.ws.rs.client.Invocation; -import javax.ws.rs.client.WebTarget; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import org.glassfish.jersey.client.ClientProperties; -import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; -import org.onap.policy.common.gson.GsonMessageBodyHandler; -import org.onap.policy.common.utils.network.NetworkUtil; - -/** - * Class to perform Rest unit tests. - * - */ -public class CommonActuatorController { - - public static final String SELF = NetworkUtil.getHostname(); - public static final String CONTEXT_PATH = "onap/k8sparticipant"; - public static final String ACTUATOR_ENDPOINT = CONTEXT_PATH + "/actuator/"; - - private static String httpPrefix; - - /** - * Sends a request to an actuator endpoint. - * - * @param endpoint the target endpoint - * @return a request builder - * @throws Exception if an error occurs - */ - protected Invocation.Builder sendActRequest(final String endpoint) throws Exception { - return sendFqeRequest(httpPrefix + ACTUATOR_ENDPOINT + endpoint, true); - } - - /** - * Sends a request to an actuator endpoint, without any authorization header. - * - * @param endpoint the target endpoint - * @return a request builder - * @throws Exception if an error occurs - */ - protected Invocation.Builder sendNoAuthActRequest(final String endpoint) throws Exception { - return sendFqeRequest(httpPrefix + ACTUATOR_ENDPOINT + endpoint, false); - } - - /** - * Sends a request to a fully qualified endpoint. - * - * @param fullyQualifiedEndpoint the fully qualified target endpoint - * @param includeAuth if authorization header should be included - * @return a request builder - * @throws Exception if an error occurs - */ - protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth) - throws Exception { - final Client client = ClientBuilder.newBuilder().build(); - - client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); - client.register(GsonMessageBodyHandler.class); - - if (includeAuth) { - client.register(HttpAuthenticationFeature.basic("participantUser", "zb!XztG34")); - } - - final WebTarget webTarget = client.target(fullyQualifiedEndpoint); - - return webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN); - } - - /** - * Assert that GET call to actuator endpoint is Unauthorized. - * - * @param endPoint the endpoint - * @throws Exception if an error occurs - */ - protected void assertUnauthorizedActGet(final String endPoint) throws Exception { - Response rawresp = sendNoAuthActRequest(endPoint).buildGet().invoke(); - assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus()); - } - - /** - * Set Up httpPrefix. - * - * @param port the port - */ - protected void setHttpPrefix(int port) { - httpPrefix = "http://" + SELF + ":" + port + "/"; - } - -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/utils/TestUtils.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/utils/TestUtils.java deleted file mode 100644 index af514f8aa..000000000 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/utils/TestUtils.java +++ /dev/null @@ -1,47 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. - * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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.policy.clamp.controlloop.participant.kubernetes.utils; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import org.onap.policy.common.utils.coder.YamlJsonTranslator; -import org.onap.policy.common.utils.resources.ResourceUtils; -import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; - -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class TestUtils { - - private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); - private static final String TOSCA_TEMPLATE_YAML = "src/test/resources/servicetemplates/KubernetesHelm.yaml"; - - - public static ToscaServiceTemplate testControlLoopRead() { - return testControlLoopYamlSerialization(TOSCA_TEMPLATE_YAML); - } - - - private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) { - String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath); - ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class); - return serviceTemplate; - } -} diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/resources/application_test.properties b/participant/participant-impl/participant-impl-kubernetes/src/test/resources/application_test.properties index b5b209fd1..243512166 100644 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/resources/application_test.properties +++ b/participant/participant-impl/participant-impl-kubernetes/src/test/resources/application_test.properties @@ -1,24 +1,24 @@ spring.security.user.name=participantUser spring.security.user.password=zb!XztG34 -server.servlet.context-path=/onap/k8sparticipant +server.servlet.context-path=/onap/policy/clamp/acm/k8sparticipant server.error.path=/error server.http-port=8083 -participant.name=ControlLoopParticipant Kubernetes Test +participant.name=AutomationCompositionParticipant Kubernetes Test participant.intermediaryParameters.name=Participant parameters participant.intermediaryParameters.reportingTimeInterval=120000 participant.intermediaryParameters.description=Participant Description participant.intermediaryParameters.participantId.name=K8sParticipant0 participant.intermediaryParameters.participantId.version=1.0.0 -participant.intermediaryParameters.participantType.name=org.onap.k8s.controlloop.K8SControlLoopParticipant +participant.intermediaryParameters.participantType.name=org.onap.k8s.acm.K8SAutomationCompositionParticipant participant.intermediaryParameters.participantType.version=2.3.4 -participant.intermediaryParameters.clampControlLoopTopics.name=ControlLoop Topics -participant.intermediaryParameters.clampControlLoopTopics.topicSources[0].topic=POLICY-CLRUNTIME-PARTICIPANT -participant.intermediaryParameters.clampControlLoopTopics.topicSources[0].servers[0]=localhost -participant.intermediaryParameters.clampControlLoopTopics.topicSources[0].topicCommInfrastructure=dmaap -participant.intermediaryParameters.clampControlLoopTopics.topicSources[0].fetchTimeout=15000 -participant.intermediaryParameters.clampControlLoopTopics.topicSinks[0].topic=POLICY-CLRUNTIME-PARTICIPANT -participant.intermediaryParameters.clampControlLoopTopics.topicSinks[0].servers[0]=localhost -participant.intermediaryParameters.clampControlLoopTopics.topicSinks[0].topicCommInfrastructure=dmaap +participant.intermediaryParameters.clampAutomationCompositionTopics.name=AutomationComposition Topics +participant.intermediaryParameters.clampAutomationCompositionTopics.topicSources[0].topic=POLICY-ACRUNTIME-PARTICIPANT +participant.intermediaryParameters.clampAutomationCompositionTopics.topicSources[0].servers[0]=localhost +participant.intermediaryParameters.clampAutomationCompositionTopics.topicSources[0].topicCommInfrastructure=dmaap +participant.intermediaryParameters.clampAutomationCompositionTopics.topicSources[0].fetchTimeout=15000 +participant.intermediaryParameters.clampAutomationCompositionTopics.topicSinks[0].topic=POLICY-ACRUNTIME-PARTICIPANT +participant.intermediaryParameters.clampAutomationCompositionTopics.topicSinks[0].servers[0]=localhost +participant.intermediaryParameters.clampAutomationCompositionTopics.topicSinks[0].topicCommInfrastructure=dmaap management.endpoints.web.exposure.include=health,metrics,prometheus diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/resources/servicetemplates/KubernetesHelm.yaml b/participant/participant-impl/participant-impl-kubernetes/src/test/resources/servicetemplates/KubernetesHelm.yaml index 7d594019a..f5eb6233f 100644 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/resources/servicetemplates/KubernetesHelm.yaml +++ b/participant/participant-impl/participant-impl-kubernetes/src/test/resources/servicetemplates/KubernetesHelm.yaml @@ -27,14 +27,14 @@ data_types: type: string required: true node_types: - org.onap.policy.clamp.controlloop.Participant: + org.onap.policy.clamp.acm.Participant: version: 1.0.1 derived_from: tosca.nodetypes.Root properties: provider: type: string requred: false - org.onap.policy.clamp.controlloop.ControlLoopElement: + org.onap.policy.clamp.acm.AutomationCompositionElement: version: 1.0.1 derived_from: tosca.nodetypes.Root properties: @@ -51,11 +51,11 @@ node_types: - greater-or-equal: 0 metadata: common: true - description: A value indicating the start phase in which this control loop element will be started, the - first start phase is zero. Control Loop Elements are started in their start_phase order and stopped - in reverse start phase order. Control Loop Elements with the same start phase are started and - stopped simultaneously - org.onap.policy.clamp.controlloop.ControlLoop: + description: A value indicating the start phase in which this automation composition element will be started, + the first start phase is zero. Automation Composition Elements are started in their start_phase + order and stopped in reverse start phase order. Automation Composition Elements with the same start + phase are started and stopped simultaneously + org.onap.policy.clamp.acm.AutomationComposition: version: 1.0.1 derived_from: tosca.nodetypes.Root properties: @@ -67,9 +67,9 @@ node_types: required: true entry_schema: type: onap.datatypes.ToscaConceptIdentifier - org.onap.policy.clamp.controlloop.K8SMicroserviceControlLoopElement: + org.onap.policy.clamp.acm.K8SMicroserviceAutomationCompositionElement: version: 1.0.1 - derived_from: org.onap.policy.clamp.controlloop.ControlLoopElement + derived_from: org.onap.policy.clamp.acm.AutomationCompositionElement properties: chart: type: string @@ -89,24 +89,24 @@ node_types: requred: true topology_template: node_templates: - org.onap.k8s.controlloop.K8SControlLoopParticipant: + org.onap.k8s.acm.K8SAutomationCompositionParticipant: version: 2.3.4 - type: org.onap.policy.clamp.controlloop.Participant + type: org.onap.policy.clamp.acm.Participant type_version: 1.0.1 description: Participant for K8S properties: provider: ONAP - org.onap.domain.database.HelloWorld_K8SMicroserviceControlLoopElement: + org.onap.domain.database.HelloWorld_K8SMicroserviceAutomationCompositionElement: # Chart from any chart repository configured on helm client. version: 1.2.3 - type: org.onap.policy.clamp.controlloop.K8SMicroserviceControlLoopElement + type: org.onap.policy.clamp.acm.K8SMicroserviceAutomationCompositionElement type_version: 1.0.0 - description: Control loop element for the K8S microservice for Hello World + description: Automation composition element for the K8S microservice for Hello World properties: provider: ONAP participantType: - name: org.onap.k8s.controlloop.K8SControlLoopParticipant + name: org.onap.k8s.acm.K8SAutomationCompositionParticipant version: 2.3.4 startPhase: 2 uninitializedToPassiveTimeout: 180 @@ -119,16 +119,16 @@ topology_template: namespace: onap repository: chartMuseum - org.onap.domain.database.PMSH_K8SMicroserviceControlLoopElement: + org.onap.domain.database.PMSH_K8SMicroserviceAutomationCompositionElement: # Chart from local file system version: 1.2.3 - type: org.onap.policy.clamp.controlloop.K8SMicroserviceControlLoopElement + type: org.onap.policy.clamp.acm.K8SMicroserviceAutomationCompositionElement type_version: 1.0.0 - description: Control loop element for the K8S microservice for PMSH + description: Automation composition element for the K8S microservice for PMSH properties: provider: ONAP participantType: - name: org.onap.k8s.controlloop.K8SControlLoopParticipant + name: org.onap.k8s.acm.K8SAutomationCompositionParticipant version: 2.3.4 startPhase: 2 uninitializedToPassiveTimeout: 180 @@ -142,16 +142,16 @@ topology_template: overrideParams: global.masterPassword: test - org.onap.domain.database.Local_K8SMicroserviceControlLoopElement: + org.onap.domain.database.Local_K8SMicroserviceAutomationCompositionElement: # Chart installation without passing repository name version: 1.2.3 - type: org.onap.policy.clamp.controlloop.K8SMicroserviceControlLoopElement + type: org.onap.policy.clamp.acm.K8SMicroserviceAutomationCompositionElement type_version: 1.0.0 - description: Control loop element for the K8S microservice for local chart + description: Automation composition element for the K8S microservice for local chart properties: provider: ONAP participantType: - name: org.onap.k8s.controlloop.K8SControlLoopParticipant + name: org.onap.k8s.acm.K8SAutomationCompositionParticipant version: 2.3.4 startPhase: 2 uninitializedToPassiveTimeout: 180 @@ -163,17 +163,17 @@ topology_template: releaseName: nginxms namespace: onap - org.onap.domain.sample.GenericK8s_ControlLoopDefinition: + org.onap.domain.sample.GenericK8s_AutomationCompositionDefinition: version: 1.2.3 - type: org.onap.policy.clamp.controlloop.ControlLoop + type: org.onap.policy.clamp.acm.AutomationComposition type_version: 1.0.0 - description: Control loop for Hello World + description: Automation composition for Hello World properties: provider: ONAP elements: - - name: org.onap.domain.database.HelloWorld_K8SMicroserviceControlLoopElement + - name: org.onap.domain.database.HelloWorld_K8SMicroserviceAutomationCompositionElement version: 1.2.3 - - name: org.onap.domain.database.PMSH_K8SMicroserviceControlLoopElement + - name: org.onap.domain.database.PMSH_K8SMicroserviceAutomationCompositionElement version: 1.2.3 - - name: org.onap.domain.database.Local_K8SMicroserviceControlLoopElement + - name: org.onap.domain.database.Local_K8SMicroserviceAutomationCompositionElement version: 1.2.3 -- cgit 1.2.3-korg