From 74e0706bab06db09ce25998d3d3a52ec8b0237d3 Mon Sep 17 00:00:00 2001 From: Krishnakumar Jinka Date: Sun, 25 Nov 2018 13:56:36 +0900 Subject: Reduce resp values meth complexity The response values determining method has more complexity reduce it using enums. Fix build fail due to incorrect response value. Modify LCM response code to enum. Fix enum package. Modify status code enum. Add modification copyright notice. Fix review comments. Add serialization key Issue-ID: POLICY-1251 Change-Id: I49909b6dd2810dd864784afab0a328b855131501 Signed-off-by: kris.jinka Signed-off-by: krisjinka --- .../org/onap/policy/appclcm/LcmResponseCode.java | 36 ++++----- .../onap/policy/appclcm/util/StatusCodeEnum.java | 89 +++++++++++++++++++++ .../java/org/onap/policy/sdnr/PciResponseCode.java | 40 ++++------ .../org/onap/policy/sdnr/util/StatusCodeEnum.java | 90 ++++++++++++++++++++++ 4 files changed, 206 insertions(+), 49 deletions(-) create mode 100644 controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/util/StatusCodeEnum.java create mode 100644 controlloop/common/model-impl/sdnr/src/main/java/org/onap/policy/sdnr/util/StatusCodeEnum.java (limited to 'controlloop/common/model-impl') diff --git a/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LcmResponseCode.java b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LcmResponseCode.java index cca69b132..d3c3464ed 100644 --- a/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LcmResponseCode.java +++ b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/LcmResponseCode.java @@ -3,13 +3,14 @@ * appclcm * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd. 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. @@ -20,7 +21,10 @@ package org.onap.policy.appclcm; -public class LcmResponseCode { +import java.io.Serializable; +import org.onap.policy.appclcm.util.StatusCodeEnum; + +public class LcmResponseCode implements Serializable { /* These fields define the key to the response code value. */ public static final String ACCEPTED = "ACCEPTED"; @@ -30,8 +34,9 @@ public class LcmResponseCode { public static final String FAILURE = "FAILURE"; public static final String PARTIAL_SUCCESS = "PARTIAL SUCCESS"; public static final String PARTIAL_FAILURE = "PARTIAL FAILURE"; + private static final long serialVersionUID = 8189456447227022582L; - private Integer code; + private final Integer code; protected LcmResponseCode(final int code) { this.code = code; @@ -48,27 +53,12 @@ public class LcmResponseCode { /** * Translates the code to a string value that represents the meaning of the code. - * - * @param code the numeric value that is returned by APPC based on success, failure, etc. of the - * action requested + * + * @param code the numeric value that is returned by APPC based on success, failure, etc. of the action requested * @return the string value equivalent of the APPC response code */ public static String toResponseValue(int code) { - if (code == 100) { - return ACCEPTED; - } else if (code == 200) { - return ERROR; - } else if (code >= 300 && code <= 313) { - return REJECT; - } else if (code == 400) { - return SUCCESS; - } else if (code == 450 || (code >= 401 && code <= 406)) { - return FAILURE; - } else if (code == 500) { - return PARTIAL_SUCCESS; - } else if (code >= 501 && code <= 599) { - return PARTIAL_FAILURE; - } - return null; + StatusCodeEnum statusCodeEnum = StatusCodeEnum.fromStatusCode(code); + return (statusCodeEnum != null) ? statusCodeEnum.toString() : null; } } diff --git a/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/util/StatusCodeEnum.java b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/util/StatusCodeEnum.java new file mode 100644 index 000000000..d954789e8 --- /dev/null +++ b/controlloop/common/model-impl/appclcm/src/main/java/org/onap/policy/appclcm/util/StatusCodeEnum.java @@ -0,0 +1,89 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Samsung Electronics Co., Ltd. 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.appclcm.util; + +import java.util.HashMap; +import java.util.Map; + +public enum StatusCodeEnum { + ACCEPTED("ACCEPTED"), ERROR("ERROR"), REJECT("REJECT"), SUCCESS("SUCCESS"), FAILURE("FAILURE"), + PARTIAL_SUCCESS("PARTIAL SUCCESS"), PARTIAL_FAILURE("PARTIAL FAILURE"); + + private String name; + + StatusCodeEnum(final String name) { + this.name = name; + } + + public String toString() { + return this.name; + } + + /** + * Determine status enum from the code. + * + * @param statusCode integer code indicating the status + * @return enum representation of status code + */ + public static StatusCodeEnum fromStatusCode(final int statusCode) { + if (statusCode == 100) { + return ACCEPTED; + } + + if (statusCode == 200) { + return ERROR; + } + + if (isRejectStatusCode(statusCode)) { + return REJECT; + } + + if (statusCode == 400) { + return SUCCESS; + } + + if (isFailureStatusCode(statusCode)) { + return FAILURE; + } + + if (statusCode == 500) { + return PARTIAL_SUCCESS; + } + + if (isPartialFailureStatusCode(statusCode)) { + return PARTIAL_FAILURE; + } + + return null; + } + + private static boolean isRejectStatusCode(final int statusCode) { + return statusCode >= 300 && statusCode <= 313; + } + + private static boolean isFailureStatusCode(final int statusCode) { + return statusCode == 450 || (statusCode >= 401 && statusCode <= 406); + } + + private static boolean isPartialFailureStatusCode(final int statusCode) { + return statusCode >= 501 && statusCode <= 599; + } +} \ No newline at end of file diff --git a/controlloop/common/model-impl/sdnr/src/main/java/org/onap/policy/sdnr/PciResponseCode.java b/controlloop/common/model-impl/sdnr/src/main/java/org/onap/policy/sdnr/PciResponseCode.java index eba57366d..430adcb92 100644 --- a/controlloop/common/model-impl/sdnr/src/main/java/org/onap/policy/sdnr/PciResponseCode.java +++ b/controlloop/common/model-impl/sdnr/src/main/java/org/onap/policy/sdnr/PciResponseCode.java @@ -3,13 +3,14 @@ * sdnr * ================================================================================ * Copyright (C) 2018 Wipro Limited Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd. 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. @@ -20,7 +21,10 @@ package org.onap.policy.sdnr; -public class PciResponseCode { +import java.io.Serializable; +import org.onap.policy.sdnr.util.StatusCodeEnum; + +public class PciResponseCode implements Serializable { /* These fields define the key to the response code value. */ public static final String ACCEPTED = "ACCEPTED"; @@ -30,8 +34,9 @@ public class PciResponseCode { public static final String FAILURE = "FAILURE"; public static final String PARTIAL_SUCCESS = "PARTIAL SUCCESS"; public static final String PARTIAL_FAILURE = "PARTIAL FAILURE"; + private static final long serialVersionUID = -5371924429933449763L; - private Integer code; + private final Integer code; protected PciResponseCode(final int code) { this.code = code; @@ -47,30 +52,13 @@ public class PciResponseCode { } /** - * Translates the code to a string value that represents the meaning of the - * code. - * - * @param code - * the numeric value that is returned by SDNR based on success, - * failure, etc. of the action requested + * Translates the code to a string value that represents the meaning of the code. + * + * @param code the numeric value that is returned by SDNR based on success, failure, etc. of the action requested * @return the string value equivalent of the SDNR response code */ public static String toResponseValue(int code) { - if (code == 100) { - return ACCEPTED; - } else if (code == 200) { - return SUCCESS; - } else if (code >= 300 && code <= 313) { - return REJECT; - } else if (code == 400) { - return ERROR; - } else if (code == 450 || (code >= 401 && code <= 406)) { - return FAILURE; - } else if (code == 500) { - return PARTIAL_SUCCESS; - } else if (code >= 501 && code <= 599) { - return PARTIAL_FAILURE; - } - return null; + StatusCodeEnum statusCodeEnum = StatusCodeEnum.fromStatusCode(code); + return (statusCodeEnum != null) ? statusCodeEnum.toString() : null; } } diff --git a/controlloop/common/model-impl/sdnr/src/main/java/org/onap/policy/sdnr/util/StatusCodeEnum.java b/controlloop/common/model-impl/sdnr/src/main/java/org/onap/policy/sdnr/util/StatusCodeEnum.java new file mode 100644 index 000000000..8830545da --- /dev/null +++ b/controlloop/common/model-impl/sdnr/src/main/java/org/onap/policy/sdnr/util/StatusCodeEnum.java @@ -0,0 +1,90 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Samsung Electronics Co., Ltd. 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.sdnr.util; + +import java.util.HashMap; +import java.util.Map; + +public enum StatusCodeEnum { + ACCEPTED("ACCEPTED"), ERROR("ERROR"), REJECT("REJECT"), SUCCESS("SUCCESS"), FAILURE("FAILURE"), + PARTIAL_SUCCESS("PARTIAL SUCCESS"), PARTIAL_FAILURE("PARTIAL FAILURE"); + + private String name; + + StatusCodeEnum(final String name) { + this.name = name; + } + + public String toString() { + return this.name; + } + + /** + * Determine status enum from the code. + * + * @param statusCode integer code indicating the status + * @return enum representation of status code + */ + public static StatusCodeEnum fromStatusCode(final int statusCode) { + if (statusCode == 100) { + return ACCEPTED; + } + + if (statusCode == 200) { + return SUCCESS; + } + + if (isRejectStatusCode(statusCode)) { + return REJECT; + } + + if (statusCode == 400) { + return ERROR; + } + + if (isFailureStatusCode(statusCode)) { + return FAILURE; + } + + if (statusCode == 500) { + return PARTIAL_SUCCESS; + } + + if (isPartialFailureStatusCode(statusCode)) { + return PARTIAL_FAILURE; + } + + return null; + } + + private static boolean isRejectStatusCode(final int statusCode) { + return statusCode >= 300 && statusCode <= 313; + } + + private static boolean isFailureStatusCode(final int statusCode) { + return statusCode == 450 || (statusCode >= 401 && statusCode <= 406); + } + + private static boolean isPartialFailureStatusCode(final int statusCode) { + return statusCode >= 501 && statusCode <= 599; + } + +} -- cgit 1.2.3-korg