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 --- .../exception/AutomationCompositionException.java | 95 +++++++++++++ .../AutomationCompositionRuntimeException.java | 109 +++++++++++++++ .../common/acm/rest/CoderHttpMesageConverter.java | 74 ++++++++++ .../acm/rest/RequestResponseLoggingFilter.java | 69 ++++++++++ .../acm/startstop/CommonCommandLineArguments.java | 146 ++++++++++++++++++++ .../common/exception/ControlLoopException.java | 94 ------------- .../exception/ControlLoopRuntimeException.java | 107 --------------- .../common/rest/CoderHttpMesageConverter.java | 74 ---------- .../common/rest/RequestResponseLoggingFilter.java | 69 ---------- .../startstop/CommonCommandLineArguments.java | 145 -------------------- .../controlloop/common/utils/CommonUtils.java | 152 --------------------- 11 files changed, 493 insertions(+), 641 deletions(-) create mode 100644 common/src/main/java/org/onap/policy/clamp/common/acm/exception/AutomationCompositionException.java create mode 100644 common/src/main/java/org/onap/policy/clamp/common/acm/exception/AutomationCompositionRuntimeException.java create mode 100644 common/src/main/java/org/onap/policy/clamp/common/acm/rest/CoderHttpMesageConverter.java create mode 100644 common/src/main/java/org/onap/policy/clamp/common/acm/rest/RequestResponseLoggingFilter.java create mode 100644 common/src/main/java/org/onap/policy/clamp/common/acm/startstop/CommonCommandLineArguments.java delete mode 100644 common/src/main/java/org/onap/policy/clamp/controlloop/common/exception/ControlLoopException.java delete mode 100644 common/src/main/java/org/onap/policy/clamp/controlloop/common/exception/ControlLoopRuntimeException.java delete mode 100644 common/src/main/java/org/onap/policy/clamp/controlloop/common/rest/CoderHttpMesageConverter.java delete mode 100644 common/src/main/java/org/onap/policy/clamp/controlloop/common/rest/RequestResponseLoggingFilter.java delete mode 100644 common/src/main/java/org/onap/policy/clamp/controlloop/common/startstop/CommonCommandLineArguments.java delete mode 100644 common/src/main/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtils.java (limited to 'common/src/main/java/org/onap') diff --git a/common/src/main/java/org/onap/policy/clamp/common/acm/exception/AutomationCompositionException.java b/common/src/main/java/org/onap/policy/clamp/common/acm/exception/AutomationCompositionException.java new file mode 100644 index 000000000..c22aa920e --- /dev/null +++ b/common/src/main/java/org/onap/policy/clamp/common/acm/exception/AutomationCompositionException.java @@ -0,0 +1,95 @@ +/*- + * ============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.common.acm.exception; + +import javax.ws.rs.core.Response; +import lombok.Getter; +import lombok.ToString; +import org.onap.policy.models.errors.concepts.ErrorResponse; +import org.onap.policy.models.errors.concepts.ErrorResponseInfo; +import org.onap.policy.models.errors.concepts.ErrorResponseUtils; + +/** + * This class is a base exception from which all automation composition exceptions are sub classes. + */ +@Getter +@ToString +public class AutomationCompositionException extends Exception implements ErrorResponseInfo { + private static final long serialVersionUID = -8507246953751956974L; + + // The error response of the exception + private final ErrorResponse errorResponse = new ErrorResponse(); + + // The object on which the exception was thrown + private final transient Object object; + + /** + * Instantiates a new automation composition exception. + * + * @param statusCode the status code for the response as a HTTP status code + * @param message the message on the exception + */ + public AutomationCompositionException(final Response.Status statusCode, final String message) { + this(statusCode, message, null); + } + + /** + * Instantiates a new automation composition exception. + * + * @param statusCode the return code for the exception + * @param message the message on the exception + * @param object the object that the exception was thrown on + */ + public AutomationCompositionException(final Response.Status statusCode, final String message, final Object object) { + super(message); + errorResponse.setResponseCode(statusCode); + ErrorResponseUtils.getExceptionMessages(errorResponse, this); + this.object = object; + } + + /** + * Instantiates a new automation composition exception. + * + * @param statusCode the return code for the exception + * @param message the message on the exception + * @param exception the exception that caused this exception + */ + public AutomationCompositionException(final Response.Status statusCode, final String message, + final Exception exception) { + this(statusCode, message, exception, null); + } + + /** + * Instantiates a new exception. + * + * @param statusCode the return code for the exception + * @param message the message on the exception + * @param exception the exception that caused this exception + * @param object the object that the exception was thrown on + */ + public AutomationCompositionException(final Response.Status statusCode, final String message, + final Exception exception, final Object object) { + super(message, exception); + errorResponse.setResponseCode(statusCode); + ErrorResponseUtils.getExceptionMessages(errorResponse, this); + this.object = object; + } +} diff --git a/common/src/main/java/org/onap/policy/clamp/common/acm/exception/AutomationCompositionRuntimeException.java b/common/src/main/java/org/onap/policy/clamp/common/acm/exception/AutomationCompositionRuntimeException.java new file mode 100644 index 000000000..2fc427db8 --- /dev/null +++ b/common/src/main/java/org/onap/policy/clamp/common/acm/exception/AutomationCompositionRuntimeException.java @@ -0,0 +1,109 @@ +/*- + * ============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.common.acm.exception; + +import javax.ws.rs.core.Response; +import lombok.Getter; +import lombok.ToString; +import org.onap.policy.models.errors.concepts.ErrorResponse; +import org.onap.policy.models.errors.concepts.ErrorResponseInfo; +import org.onap.policy.models.errors.concepts.ErrorResponseUtils; + +/** + * This class is a base automation composition run time exception from which all automation composition run time + * exceptions are sub classes. + */ +@Getter +@ToString +public class AutomationCompositionRuntimeException extends RuntimeException implements ErrorResponseInfo { + private static final long serialVersionUID = -8507246953751956974L; + + // The error response of the exception + private final ErrorResponse errorResponse = new ErrorResponse(); + + // The object on which the exception was thrown + private final transient Object object; + + /** + * Instantiates a new automation composition runtime exception. + * + * @param statusCode the return code for the exception + * @param message the message on the exception + */ + public AutomationCompositionRuntimeException(final Response.Status statusCode, final String message) { + this(statusCode, message, null); + } + + /** + * Instantiates a new automation composition runtime exception. + * + * @param statusCode the return code for the exception + * @param message the message on the exception + * @param object the object that the exception was thrown on + */ + public AutomationCompositionRuntimeException(final Response.Status statusCode, final String message, + final Object object) { + super(message); + this.object = object; + errorResponse.setResponseCode(statusCode); + ErrorResponseUtils.getExceptionMessages(errorResponse, this); + } + + /** + * Instantiates a new automation composition runtime exception. + * + * @param statusCode the return code for the exception + * @param message the message on the exception + * @param exception the exception that caused this automation composition exception + */ + public AutomationCompositionRuntimeException(final Response.Status statusCode, final String message, + final Exception exception) { + this(statusCode, message, exception, null); + } + + /** + * Instantiates a new model runtime exception from an AutomationCompositionException instance. + * + * @param exception the exception that caused this automation composition exception + */ + public AutomationCompositionRuntimeException(final AutomationCompositionException exception) { + super(exception.getMessage(), exception); + this.object = exception.getObject(); + errorResponse.setResponseCode(exception.getErrorResponse().getResponseCode()); + ErrorResponseUtils.getExceptionMessages(errorResponse, this); + } + + /** + * Instantiates a new automation composition runtime exception. + * + * @param statusCode the return code for the exception + * @param message the message on the exception + * @param exception the exception that caused this automation composition exception + * @param object the object that the exception was thrown on + */ + public AutomationCompositionRuntimeException(final Response.Status statusCode, final String message, + final Exception exception, final Object object) { + super(message, exception); + this.object = object; + errorResponse.setResponseCode(statusCode); + ErrorResponseUtils.getExceptionMessages(errorResponse, this); + } +} diff --git a/common/src/main/java/org/onap/policy/clamp/common/acm/rest/CoderHttpMesageConverter.java b/common/src/main/java/org/onap/policy/clamp/common/acm/rest/CoderHttpMesageConverter.java new file mode 100644 index 000000000..f445364ad --- /dev/null +++ b/common/src/main/java/org/onap/policy/clamp/common/acm/rest/CoderHttpMesageConverter.java @@ -0,0 +1,74 @@ +/*- + * ============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.common.acm.rest; + +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.nio.charset.StandardCharsets; +import javax.ws.rs.core.Response; +import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException; +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.common.utils.coder.StandardYamlCoder; +import org.springframework.http.HttpInputMessage; +import org.springframework.http.HttpOutputMessage; +import org.springframework.http.MediaType; +import org.springframework.http.converter.AbstractHttpMessageConverter; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.http.converter.HttpMessageNotWritableException; + +public class CoderHttpMesageConverter extends AbstractHttpMessageConverter { + + private Coder coder; + + public CoderHttpMesageConverter(String type) { + super(new MediaType("application", type, StandardCharsets.UTF_8)); + this.coder = "json".equals(type) ? new StandardCoder() : new StandardYamlCoder(); + } + + @Override + protected boolean supports(Class clazz) { + return true; + } + + @Override + protected T readInternal(Class clazz, HttpInputMessage inputMessage) + throws IOException, HttpMessageNotReadableException { + try (var is = new InputStreamReader(inputMessage.getBody(), StandardCharsets.UTF_8)) { + return coder.decode(is, clazz); + } catch (CoderException e) { + throw new AutomationCompositionRuntimeException(Response.Status.BAD_REQUEST, e.getMessage(), e); + } + } + + @Override + protected void writeInternal(T t, HttpOutputMessage outputMessage) + throws IOException, HttpMessageNotWritableException { + try (var writer = new OutputStreamWriter(outputMessage.getBody(), StandardCharsets.UTF_8)) { + coder.encode(writer, t); + } catch (CoderException e) { + throw new AutomationCompositionRuntimeException(Response.Status.BAD_REQUEST, e.getMessage(), e); + } + } + +} diff --git a/common/src/main/java/org/onap/policy/clamp/common/acm/rest/RequestResponseLoggingFilter.java b/common/src/main/java/org/onap/policy/clamp/common/acm/rest/RequestResponseLoggingFilter.java new file mode 100644 index 000000000..4b6dce46d --- /dev/null +++ b/common/src/main/java/org/onap/policy/clamp/common/acm/rest/RequestResponseLoggingFilter.java @@ -0,0 +1,69 @@ +/*- + * ============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.common.acm.rest; + +import java.io.IOException; +import java.util.UUID; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +@Component +@Order(2) +public class RequestResponseLoggingFilter implements Filter { + + private static final String VERSION_MINOR_NAME = "X-MinorVersion"; + private static final String VERSION_PATCH_NAME = "X-PatchVersion"; + private static final String VERSION_LATEST_NAME = "X-LatestVersion"; + public static final String API_VERSION = "1.0.0"; + public static final String REQUEST_ID_NAME = "X-ONAP-RequestID"; + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) + throws IOException, ServletException { + + + HttpServletResponse res = (HttpServletResponse) response; + HttpServletRequest req = (HttpServletRequest) request; + + /* + * Disabling sonar because of ONAP requires the request ID to be copied from the request + * to the response. + */ + String requestId = req.getHeader(REQUEST_ID_NAME); + res.addHeader(REQUEST_ID_NAME, requestId != null ? requestId : UUID.randomUUID().toString()); // NOSONAR + + res.addHeader(VERSION_MINOR_NAME, "0"); + res.addHeader(VERSION_PATCH_NAME, "0"); + res.addHeader(VERSION_LATEST_NAME, API_VERSION); + + chain.doFilter(request, response); + } + +} diff --git a/common/src/main/java/org/onap/policy/clamp/common/acm/startstop/CommonCommandLineArguments.java b/common/src/main/java/org/onap/policy/clamp/common/acm/startstop/CommonCommandLineArguments.java new file mode 100644 index 000000000..6f6fb6a4b --- /dev/null +++ b/common/src/main/java/org/onap/policy/clamp/common/acm/startstop/CommonCommandLineArguments.java @@ -0,0 +1,146 @@ +/*- + * ============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.common.acm.startstop; + +import java.io.File; +import java.io.PrintWriter; +import java.io.StringWriter; +import javax.ws.rs.core.Response; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.lang3.StringUtils; +import org.onap.policy.clamp.common.acm.exception.AutomationCompositionException; +import org.onap.policy.common.utils.resources.ResourceUtils; + +/** + * This class reads and handles command line parameters. + * + */ +public class CommonCommandLineArguments { + private static final String FILE_MESSAGE_PREAMBLE = " file \""; + private static final int HELP_LINE_LENGTH = 120; + + /** + * Construct the options for the policy participant. + * + * @param options the options for the command line + */ + public CommonCommandLineArguments(final Options options) { + //@formatter:off + options.addOption(Option.builder("h") + .longOpt("help") + .desc("outputs the usage of this command") + .required(false) + .type(Boolean.class) + .build()); + options.addOption(Option.builder("v") + .longOpt("version") + .desc("outputs the version of policy participant") + .required(false) + .type(Boolean.class) + .build()); + options.addOption(Option.builder("c") + .longOpt("config-file") + .desc("the full path to the configuration file to use, " + + "the configuration file must be a Json file containing the " + + "policy participant parameters") + .hasArg() + .argName("CONFIG_FILE") + .required(false) + .type(String.class) + .build()); + //@formatter:on + } + + /** + * Validate the command line options. + * + * @param configurationFilePath the path to the configuration file + * @throws AutomationCompositionException on command argument validation errors + */ + public void validate(final String configurationFilePath) throws AutomationCompositionException { + validateReadableFile("policy participant configuration", configurationFilePath); + } + + /** + * Print version information for policy participant. + * + * @return the version string + */ + public String version() { + return ResourceUtils.getResourceAsString("version.txt"); + } + + /** + * Print help information for policy participant. + * + * @param mainClassName the main class name + * @param options the options for the command + * @return the help string + */ + public String help(final String mainClassName, final Options options) { + final var helpFormatter = new HelpFormatter(); + final var stringWriter = new StringWriter(); + final var printWriter = new PrintWriter(stringWriter); + + helpFormatter.printHelp(printWriter, HELP_LINE_LENGTH, mainClassName + " [options...]", "options", options, 0, + 0, ""); + + return stringWriter.toString(); + } + + /** + * Validate readable file. + * + * @param fileTag the file tag + * @param fileName the file name + * @throws AutomationCompositionException on the file name passed as a parameter + */ + private void validateReadableFile(final String fileTag, final String fileName) + throws AutomationCompositionException { + if (StringUtils.isEmpty(fileName)) { + throw new AutomationCompositionException(Response.Status.NOT_ACCEPTABLE, + fileTag + " file was not specified as an argument"); + } + + // The file name refers to a resource on the local file system + final var fileUrl = ResourceUtils.getUrl4Resource(fileName); + if (fileUrl == null) { + throw new AutomationCompositionException(Response.Status.NOT_ACCEPTABLE, + fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist"); + } + + final var theFile = new File(fileUrl.getPath()); + if (!theFile.exists()) { + throw new AutomationCompositionException(Response.Status.NOT_ACCEPTABLE, + fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist"); + } + if (!theFile.isFile()) { + throw new AutomationCompositionException(Response.Status.NOT_ACCEPTABLE, + fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is not a normal file"); + } + if (!theFile.canRead()) { + throw new AutomationCompositionException(Response.Status.NOT_ACCEPTABLE, + fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is unreadable"); + } + } +} diff --git a/common/src/main/java/org/onap/policy/clamp/controlloop/common/exception/ControlLoopException.java b/common/src/main/java/org/onap/policy/clamp/controlloop/common/exception/ControlLoopException.java deleted file mode 100644 index 58b5368a9..000000000 --- a/common/src/main/java/org/onap/policy/clamp/controlloop/common/exception/ControlLoopException.java +++ /dev/null @@ -1,94 +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.common.exception; - -import javax.ws.rs.core.Response; -import lombok.Getter; -import lombok.ToString; -import org.onap.policy.models.errors.concepts.ErrorResponse; -import org.onap.policy.models.errors.concepts.ErrorResponseInfo; -import org.onap.policy.models.errors.concepts.ErrorResponseUtils; - -/** - * This class is a base exception from which all control loop exceptions are sub classes. - */ -@Getter -@ToString -public class ControlLoopException extends Exception implements ErrorResponseInfo { - private static final long serialVersionUID = -8507246953751956974L; - - // The error response of the exception - private final ErrorResponse errorResponse = new ErrorResponse(); - - // The object on which the exception was thrown - private final transient Object object; - - /** - * Instantiates a new control loop exception. - * - * @param statusCode the status code for the response as a HTTP status code - * @param message the message on the exception - */ - public ControlLoopException(final Response.Status statusCode, final String message) { - this(statusCode, message, null); - } - - /** - * Instantiates a new control loop exception. - * - * @param statusCode the return code for the exception - * @param message the message on the exception - * @param object the object that the exception was thrown on - */ - public ControlLoopException(final Response.Status statusCode, final String message, final Object object) { - super(message); - errorResponse.setResponseCode(statusCode); - ErrorResponseUtils.getExceptionMessages(errorResponse, this); - this.object = object; - } - - /** - * Instantiates a new control loop exception. - * - * @param statusCode the return code for the exception - * @param message the message on the exception - * @param exception the exception that caused this exception - */ - public ControlLoopException(final Response.Status statusCode, final String message, final Exception exception) { - this(statusCode, message, exception, null); - } - - /** - * Instantiates a new exception. - * - * @param statusCode the return code for the exception - * @param message the message on the exception - * @param exception the exception that caused this exception - * @param object the object that the exception was thrown on - */ - public ControlLoopException(final Response.Status statusCode, final String message, final Exception exception, - final Object object) { - super(message, exception); - errorResponse.setResponseCode(statusCode); - ErrorResponseUtils.getExceptionMessages(errorResponse, this); - this.object = object; - } -} diff --git a/common/src/main/java/org/onap/policy/clamp/controlloop/common/exception/ControlLoopRuntimeException.java b/common/src/main/java/org/onap/policy/clamp/controlloop/common/exception/ControlLoopRuntimeException.java deleted file mode 100644 index b110a4362..000000000 --- a/common/src/main/java/org/onap/policy/clamp/controlloop/common/exception/ControlLoopRuntimeException.java +++ /dev/null @@ -1,107 +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.common.exception; - -import javax.ws.rs.core.Response; -import lombok.Getter; -import lombok.ToString; -import org.onap.policy.models.errors.concepts.ErrorResponse; -import org.onap.policy.models.errors.concepts.ErrorResponseInfo; -import org.onap.policy.models.errors.concepts.ErrorResponseUtils; - -/** - * This class is a base control loop run time exception from which all control loop run time exceptions are sub classes. - */ -@Getter -@ToString -public class ControlLoopRuntimeException extends RuntimeException implements ErrorResponseInfo { - private static final long serialVersionUID = -8507246953751956974L; - - // The error response of the exception - private final ErrorResponse errorResponse = new ErrorResponse(); - - // The object on which the exception was thrown - private final transient Object object; - - /** - * Instantiates a new control loop runtime exception. - * - * @param statusCode the return code for the exception - * @param message the message on the exception - */ - public ControlLoopRuntimeException(final Response.Status statusCode, final String message) { - this(statusCode, message, null); - } - - /** - * Instantiates a new control loop runtime exception. - * - * @param statusCode the return code for the exception - * @param message the message on the exception - * @param object the object that the exception was thrown on - */ - public ControlLoopRuntimeException(final Response.Status statusCode, final String message, final Object object) { - super(message); - this.object = object; - errorResponse.setResponseCode(statusCode); - ErrorResponseUtils.getExceptionMessages(errorResponse, this); - } - - /** - * Instantiates a new control loop runtime exception. - * - * @param statusCode the return code for the exception - * @param message the message on the exception - * @param exception the exception that caused this control loop exception - */ - public ControlLoopRuntimeException(final Response.Status statusCode, final String message, - final Exception exception) { - this(statusCode, message, exception, null); - } - - /** - * Instantiates a new model runtime exception from a ControlLoopException instance. - * - * @param exception the exception that caused this control loop exception - */ - public ControlLoopRuntimeException(final ControlLoopException exception) { - super(exception.getMessage(), exception); - this.object = exception.getObject(); - errorResponse.setResponseCode(exception.getErrorResponse().getResponseCode()); - ErrorResponseUtils.getExceptionMessages(errorResponse, this); - } - - /** - * Instantiates a new control loop runtime exception. - * - * @param statusCode the return code for the exception - * @param message the message on the exception - * @param exception the exception that caused this control loop exception - * @param object the object that the exception was thrown on - */ - public ControlLoopRuntimeException(final Response.Status statusCode, final String message, - final Exception exception, final Object object) { - super(message, exception); - this.object = object; - errorResponse.setResponseCode(statusCode); - ErrorResponseUtils.getExceptionMessages(errorResponse, this); - } -} diff --git a/common/src/main/java/org/onap/policy/clamp/controlloop/common/rest/CoderHttpMesageConverter.java b/common/src/main/java/org/onap/policy/clamp/controlloop/common/rest/CoderHttpMesageConverter.java deleted file mode 100644 index 9eb43fd6f..000000000 --- a/common/src/main/java/org/onap/policy/clamp/controlloop/common/rest/CoderHttpMesageConverter.java +++ /dev/null @@ -1,74 +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.common.rest; - -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.nio.charset.StandardCharsets; -import javax.ws.rs.core.Response; -import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; -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.common.utils.coder.StandardYamlCoder; -import org.springframework.http.HttpInputMessage; -import org.springframework.http.HttpOutputMessage; -import org.springframework.http.MediaType; -import org.springframework.http.converter.AbstractHttpMessageConverter; -import org.springframework.http.converter.HttpMessageNotReadableException; -import org.springframework.http.converter.HttpMessageNotWritableException; - -public class CoderHttpMesageConverter extends AbstractHttpMessageConverter { - - private Coder coder; - - public CoderHttpMesageConverter(String type) { - super(new MediaType("application", type, StandardCharsets.UTF_8)); - this.coder = "json".equals(type) ? new StandardCoder() : new StandardYamlCoder(); - } - - @Override - protected boolean supports(Class clazz) { - return true; - } - - @Override - protected T readInternal(Class clazz, HttpInputMessage inputMessage) - throws IOException, HttpMessageNotReadableException { - try (var is = new InputStreamReader(inputMessage.getBody(), StandardCharsets.UTF_8)) { - return coder.decode(is, clazz); - } catch (CoderException e) { - throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST, e.getMessage(), e); - } - } - - @Override - protected void writeInternal(T t, HttpOutputMessage outputMessage) - throws IOException, HttpMessageNotWritableException { - try (var writer = new OutputStreamWriter(outputMessage.getBody(), StandardCharsets.UTF_8)) { - coder.encode(writer, t); - } catch (CoderException e) { - throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST, e.getMessage(), e); - } - } - -} diff --git a/common/src/main/java/org/onap/policy/clamp/controlloop/common/rest/RequestResponseLoggingFilter.java b/common/src/main/java/org/onap/policy/clamp/controlloop/common/rest/RequestResponseLoggingFilter.java deleted file mode 100644 index 915cdf0b2..000000000 --- a/common/src/main/java/org/onap/policy/clamp/controlloop/common/rest/RequestResponseLoggingFilter.java +++ /dev/null @@ -1,69 +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.common.rest; - -import java.io.IOException; -import java.util.UUID; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import org.springframework.core.annotation.Order; -import org.springframework.stereotype.Component; - -@Component -@Order(2) -public class RequestResponseLoggingFilter implements Filter { - - private static final String VERSION_MINOR_NAME = "X-MinorVersion"; - private static final String VERSION_PATCH_NAME = "X-PatchVersion"; - private static final String VERSION_LATEST_NAME = "X-LatestVersion"; - public static final String API_VERSION = "1.0.0"; - public static final String REQUEST_ID_NAME = "X-ONAP-RequestID"; - - @Override - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) - throws IOException, ServletException { - - - HttpServletResponse res = (HttpServletResponse) response; - HttpServletRequest req = (HttpServletRequest) request; - - /* - * Disabling sonar because of ONAP requires the request ID to be copied from the request - * to the response. - */ - String requestId = req.getHeader(REQUEST_ID_NAME); - res.addHeader(REQUEST_ID_NAME, requestId != null ? requestId : UUID.randomUUID().toString()); // NOSONAR - - res.addHeader(VERSION_MINOR_NAME, "0"); - res.addHeader(VERSION_PATCH_NAME, "0"); - res.addHeader(VERSION_LATEST_NAME, API_VERSION); - - chain.doFilter(request, response); - } - -} diff --git a/common/src/main/java/org/onap/policy/clamp/controlloop/common/startstop/CommonCommandLineArguments.java b/common/src/main/java/org/onap/policy/clamp/controlloop/common/startstop/CommonCommandLineArguments.java deleted file mode 100644 index 525da259f..000000000 --- a/common/src/main/java/org/onap/policy/clamp/controlloop/common/startstop/CommonCommandLineArguments.java +++ /dev/null @@ -1,145 +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.common.startstop; - -import java.io.File; -import java.io.PrintWriter; -import java.io.StringWriter; -import javax.ws.rs.core.Response; -import org.apache.commons.cli.HelpFormatter; -import org.apache.commons.cli.Option; -import org.apache.commons.cli.Options; -import org.apache.commons.lang3.StringUtils; -import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException; -import org.onap.policy.common.utils.resources.ResourceUtils; - -/** - * This class reads and handles command line parameters. - * - */ -public class CommonCommandLineArguments { - private static final String FILE_MESSAGE_PREAMBLE = " file \""; - private static final int HELP_LINE_LENGTH = 120; - - /** - * Construct the options for the policy participant. - * - * @param options the options for the command line - */ - public CommonCommandLineArguments(final Options options) { - //@formatter:off - options.addOption(Option.builder("h") - .longOpt("help") - .desc("outputs the usage of this command") - .required(false) - .type(Boolean.class) - .build()); - options.addOption(Option.builder("v") - .longOpt("version") - .desc("outputs the version of policy participant") - .required(false) - .type(Boolean.class) - .build()); - options.addOption(Option.builder("c") - .longOpt("config-file") - .desc("the full path to the configuration file to use, " - + "the configuration file must be a Json file containing the " - + "policy participant parameters") - .hasArg() - .argName("CONFIG_FILE") - .required(false) - .type(String.class) - .build()); - //@formatter:on - } - - /** - * Validate the command line options. - * - * @param configurationFilePath the path to the configuration file - * @throws ControlLoopException on command argument validation errors - */ - public void validate(final String configurationFilePath) throws ControlLoopException { - validateReadableFile("policy participant configuration", configurationFilePath); - } - - /** - * Print version information for policy participant. - * - * @return the version string - */ - public String version() { - return ResourceUtils.getResourceAsString("version.txt"); - } - - /** - * Print help information for policy participant. - * - * @param mainClassName the main class name - * @param options the options for the command - * @return the help string - */ - public String help(final String mainClassName, final Options options) { - final var helpFormatter = new HelpFormatter(); - final var stringWriter = new StringWriter(); - final var printWriter = new PrintWriter(stringWriter); - - helpFormatter.printHelp(printWriter, HELP_LINE_LENGTH, mainClassName + " [options...]", "options", options, 0, - 0, ""); - - return stringWriter.toString(); - } - - /** - * Validate readable file. - * - * @param fileTag the file tag - * @param fileName the file name - * @throws ControlLoopException on the file name passed as a parameter - */ - private void validateReadableFile(final String fileTag, final String fileName) throws ControlLoopException { - if (StringUtils.isEmpty(fileName)) { - throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, - fileTag + " file was not specified as an argument"); - } - - // The file name refers to a resource on the local file system - final var fileUrl = ResourceUtils.getUrl4Resource(fileName); - if (fileUrl == null) { - throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, - fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist"); - } - - final var theFile = new File(fileUrl.getPath()); - if (!theFile.exists()) { - throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, - fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist"); - } - if (!theFile.isFile()) { - throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, - fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is not a normal file"); - } - if (!theFile.canRead()) { - throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, - fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is unreadable"); - } - } -} diff --git a/common/src/main/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtils.java b/common/src/main/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtils.java deleted file mode 100644 index 4ebd0aaa8..000000000 --- a/common/src/main/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtils.java +++ /dev/null @@ -1,152 +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.common.utils; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUpdates; -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.ToscaNodeType; -import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; -import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; - -/** - * Utility functions used in controlloop-runtime and participants. - * - */ -public class CommonUtils { - - private CommonUtils() { - throw new IllegalStateException("Utility class"); - } - - /** - * Prepare participant updates map. - * - * @param clElement controlloop element - * @param participantUpdates list of participantUpdates - */ - public static void prepareParticipantUpdate(ControlLoopElement clElement, - List participantUpdates) { - if (participantUpdates.isEmpty()) { - participantUpdates.add(getControlLoopElementList(clElement)); - return; - } - - var participantExists = false; - for (ParticipantUpdates participantUpdate : participantUpdates) { - if (participantUpdate.getParticipantId().equals(clElement.getParticipantId())) { - participantUpdate.setControlLoopElementList(List.of(clElement)); - participantExists = true; - } - } - if (!participantExists) { - participantUpdates.add(getControlLoopElementList(clElement)); - } - } - - private static ParticipantUpdates getControlLoopElementList(ControlLoopElement clElement) { - var participantUpdate = new ParticipantUpdates(); - participantUpdate.setParticipantId(clElement.getParticipantId()); - participantUpdate.setControlLoopElementList(List.of(clElement)); - return participantUpdate; - } - - /** - * Set the Policy information in the service template for the controlloopelement. - * - * @param clElement controlloop element - * @param toscaServiceTemplate ToscaServiceTemplate - */ - public static void setServiceTemplatePolicyInfo(ControlLoopElement clElement, - ToscaServiceTemplate toscaServiceTemplate) { - // Pass respective PolicyTypes or Policies as part of toscaServiceTemplateFragment - if (toscaServiceTemplate.getPolicyTypes() == null - && toscaServiceTemplate.getToscaTopologyTemplate().getPolicies() == null) { - return; - } - ToscaServiceTemplate toscaServiceTemplateFragment = new ToscaServiceTemplate(); - toscaServiceTemplateFragment.setPolicyTypes(toscaServiceTemplate.getPolicyTypes()); - ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate(); - toscaTopologyTemplate.setPolicies(toscaServiceTemplate.getToscaTopologyTemplate().getPolicies()); - toscaServiceTemplateFragment.setToscaTopologyTemplate(toscaTopologyTemplate); - toscaServiceTemplateFragment.setDataTypes(toscaServiceTemplate.getDataTypes()); - clElement.setToscaServiceTemplateFragment(toscaServiceTemplateFragment); - } - - /** - * Prepare ParticipantDefinitionUpdate to set in the message. - * - * @param clParticipantType controlloop element - * @param entryKey key for the entry - * @param entryValue value relates to toscaNodeTemplate - * @param participantDefinitionUpdates list of participantDefinitionUpdates - * @param commonPropertiesMap common properties map - */ - public static void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey, - ToscaNodeTemplate entryValue, List participantDefinitionUpdates, - Map commonPropertiesMap) { - - var clDefinition = new ControlLoopElementDefinition(); - clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion())); - clDefinition.setControlLoopElementToscaNodeTemplate(entryValue); - if (commonPropertiesMap != null) { - ToscaNodeType nodeType = commonPropertiesMap.get(entryValue.getType()); - if (nodeType != null) { - clDefinition.setCommonPropertiesMap(nodeType.getProperties()); - } - } - - List controlLoopElementDefinitionList = new ArrayList<>(); - - if (participantDefinitionUpdates.isEmpty()) { - participantDefinitionUpdates - .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList)); - } else { - var participantExists = false; - for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) { - if (participantDefinitionUpdate.getParticipantType().equals(clParticipantType)) { - participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition); - participantExists = true; - } - } - if (!participantExists) { - participantDefinitionUpdates.add( - getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList)); - } - } - } - - private static ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition, - ToscaConceptIdentifier clParticipantType, - List controlLoopElementDefinitionList) { - var participantDefinition = new ParticipantDefinition(); - participantDefinition.setParticipantType(clParticipantType); - controlLoopElementDefinitionList.add(clDefinition); - participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList); - return participantDefinition; - } -} -- cgit 1.2.3-korg