diff options
Diffstat (limited to 'plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils')
8 files changed, 658 insertions, 0 deletions
diff --git a/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/CacheName.java b/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/CacheName.java new file mode 100755 index 00000000..f38adcca --- /dev/null +++ b/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/CacheName.java @@ -0,0 +1,48 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aaisimulator.utils; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +public enum CacheName { + + CUSTOMER_CACHE("customer-cache"), + PROJECT_CACHE("project-cache"), + NODES_CACHE("nodes-cache"), + GENERIC_VNF_CACHE("generic-vnf-cache"), + PNF_CACHE("pnf-cache"), + OWNING_ENTITY_CACHE("owning-entity-cache"), + PLATFORM_CACHE("platform-cache"), + LINES_OF_BUSINESS_CACHE("lines-of-business-cache"), + CLOUD_REGION_CACHE("cloud-region-cache"), + ESR_VNFM_CACHE("esr-vnfm-cache"); + + private String name; + + private CacheName(final String name) { + this.name = name; + } + + public String getName() { + return name; + } +} diff --git a/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/Constants.java b/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/Constants.java new file mode 100755 index 00000000..230dc135 --- /dev/null +++ b/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/Constants.java @@ -0,0 +1,151 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aaisimulator.utils; + +/** + * @author waqas.ikram@ericsson.com + * + */ +public class Constants { + + public static final String BASE_URL = "/aai/v{version:\\d+}"; + + public static final String NODES_URL = BASE_URL + "/nodes"; + + public static final String BUSINESS_URL = BASE_URL + "/business"; + + public static final String CLOUD_INFRASTRUCTURE_URL = BASE_URL + "/cloud-infrastructure"; + + public static final String CLOUD_REGIONS = CLOUD_INFRASTRUCTURE_URL + "/cloud-regions/cloud-region/"; + + public static final String CUSTOMER_URL = BUSINESS_URL + "/customers/customer/"; + + public static final String PROJECT_URL = BUSINESS_URL + "/projects/project/"; + + public static final String OWNING_ENTITY_URL = BUSINESS_URL + "/owning-entities/owning-entity/"; + + public static final String PLATFORMS_URL = BUSINESS_URL + "/platforms/platform/"; + + public static final String EXTERNAL_SYSTEM_ESR_VNFM_LIST_URL = BASE_URL + "/external-system/esr-vnfm-list"; + + public static final String NETWORK_URL = BASE_URL + "/network"; + + public static final String GENERIC_VNFS_URL = NETWORK_URL + "/generic-vnfs"; + + public static final String PNFS_URL = NETWORK_URL+ "/pnfs"; + + public static final String RELATIONSHIP_LIST_RELATIONSHIP_URL = "/relationship-list/relationship"; + + public static final String BI_DIRECTIONAL_RELATIONSHIP_LIST_URL = + RELATIONSHIP_LIST_RELATIONSHIP_URL + "/bi-directional"; + + public static final String LINES_OF_BUSINESS_URL = BUSINESS_URL + "/lines-of-business/line-of-business/"; + + public static final String HEALTHY = "healthy"; + + public static final String PROJECT = "project"; + + public static final String PROJECT_PROJECT_NAME = "project.project-name"; + + public static final String OWNING_ENTITY = "owning-entity"; + + public static final String OWNING_ENTITY_OWNING_ENTITY_ID = "owning-entity.owning-entity-id"; + + public static final String X_HTTP_METHOD_OVERRIDE = "X-HTTP-Method-Override"; + + public static final String APPLICATION_MERGE_PATCH_JSON = "application/merge-patch+json"; + + public static final String SERVICE_RESOURCE_TYPE = "service-instance"; + + public static final String RESOURCE_LINK = "resource-link"; + + public static final String RESOURCE_TYPE = "resource-type"; + + public static final String GENERIC_VNF_VNF_NAME = "generic-vnf.vnf-name"; + + public static final String GENERIC_VNF_VNF_ID = "generic-vnf.vnf-id"; + + public static final String SERVICE_INSTANCE_SERVICE_INSTANCE_ID = "service-instance.service-instance-id"; + + public static final String SERVICE_SUBSCRIPTION_SERVICE_TYPE = "service-subscription.service-type"; + + public static final String CUSTOMER_GLOBAL_CUSTOMER_ID = "customer.global-customer-id"; + + public static final String COMPOSED_OF = "org.onap.relationships.inventory.ComposedOf"; + + public static final String GENERIC_VNF = "generic-vnf"; + + public static final String PNF = "pnf"; + + public static final String PLATFORM = "platform"; + + public static final String USES = "org.onap.relationships.inventory.Uses"; + + public static final String PLATFORM_PLATFORM_NAME = "platform.platform-name"; + + public static final String LINE_OF_BUSINESS_LINE_OF_BUSINESS_NAME = "line-of-business.line-of-business-name"; + + public static final String LINE_OF_BUSINESS = "line-of-business"; + + public static final String SERVICE_SUBSCRIPTION = "service-subscription"; + + public static final String CUSTOMER_TYPE = "Customer"; + + public static final String SERVICE_INSTANCE_SERVICE_INSTANCE_NAME = "service-instance.service-instance-name"; + + public static final String CLOUD_REGION_OWNER_DEFINED_TYPE = "cloud-region.owner-defined-type"; + + public static final String CLOUD_REGION_CLOUD_REGION_ID = "cloud-region.cloud-region-id"; + + public static final String CLOUD_REGION_CLOUD_OWNER = "cloud-region.cloud-owner"; + + public static final String LOCATED_IN = "org.onap.relationships.inventory.LocatedIn"; + + public static final String CLOUD_REGION = "cloud-region"; + + public static final String TENANT_TENANT_NAME = "tenant.tenant-name"; + + public static final String TENANT_TENANT_ID = "tenant.tenant-id"; + + public static final String BELONGS_TO = "org.onap.relationships.inventory.BelongsTo"; + + public static final String TENANT = "tenant"; + + public static final String ESR_VNFM = "esr-vnfm"; + + public static final String ESR_SYSTEM_INFO = "esr-system-info"; + + public static final String ESR_SYSTEM_INFO_LIST = "esr-system-info-list"; + + public static final String ESR_VNFM_VNFM_ID = "esr-vnfm.vnfm-id"; + + public static final String DEPENDS_ON = "tosca.relationships.DependsOn"; + + public static final String VSERVER_VSERVER_NAME = "vserver.vserver-name"; + + public static final String VSERVER_VSERVER_ID = "vserver.vserver-id"; + + public static final String HOSTED_ON = "tosca.relationships.HostedOn"; + + public static final String VSERVER = "vserver"; + + private Constants() {} + +} diff --git a/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/HttpServiceUtils.java b/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/HttpServiceUtils.java new file mode 100755 index 00000000..81781025 --- /dev/null +++ b/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/HttpServiceUtils.java @@ -0,0 +1,107 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aaisimulator.utils; + +import static org.onap.so.aaisimulator.utils.Constants.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL; +import static org.onap.so.aaisimulator.utils.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL; +import static org.springframework.http.MediaType.APPLICATION_XML; +import java.net.URI; +import java.util.Arrays; +import java.util.Enumeration; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.servlet.http.HttpServletRequest; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.web.util.UriComponentsBuilder; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +public class HttpServiceUtils { + + private static final String START_WITH_FORWARD_SLASH = "(^/.*?)"; + private static final String ALPHANUMERIC = "((?:v+[a-z0-9]*)/)"; + private static final String REGEX = START_WITH_FORWARD_SLASH + ALPHANUMERIC; + private static final Pattern PATTERN = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE | Pattern.DOTALL); + + private HttpServiceUtils() {} + + public static URI getBaseUrl(final HttpServletRequest request) { + final StringBuffer url = request.getRequestURL(); + final String uri = request.getRequestURI(); + return UriComponentsBuilder.fromHttpUrl(url.substring(0, url.indexOf(uri))).path(getBaseUrl(uri)).build() + .toUri(); + } + + private static String getBaseUrl(final String uri) { + final Matcher matcher = PATTERN.matcher(uri); + if (matcher.find()) { + final StringBuilder builder = new StringBuilder(); + for (int index = 0; index < matcher.groupCount() - 1; index++) { + builder.append(matcher.group(index)); + } + return builder.toString(); + } + return uri; + } + + public static URI getBaseUrl(final StringBuffer requestUrl, final String requestUri) { + return UriComponentsBuilder.fromHttpUrl(requestUrl.substring(0, requestUrl.indexOf(requestUri))).build() + .toUri(); + } + + public static String getBaseServiceInstanceUrl(final HttpServletRequest request, final String relatedLink) { + return UriComponentsBuilder.fromUri(getBaseUrl(request)).path(relatedLink).toUriString(); + } + + public static HttpHeaders getHeaders(final HttpServletRequest request) { + return getHeaders(request, APPLICATION_XML); + } + + public static HttpHeaders getHeaders(final HttpServletRequest request, final MediaType mediaType) { + final HttpHeaders headers = new HttpHeaders(); + for (final Enumeration<String> enumeration = request.getHeaderNames(); enumeration.hasMoreElements();) { + final String headerName = enumeration.nextElement(); + headers.add(headerName, request.getHeader(headerName)); + } + headers.setContentType(mediaType); + headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML)); + return headers; + } + + public static String getTargetUrl(final String targetBaseUrl, final String relatedLink) { + return UriComponentsBuilder.fromUriString(targetBaseUrl).path(relatedLink) + .path(BI_DIRECTIONAL_RELATIONSHIP_LIST_URL).toUriString(); + } + + public static String getRelationShipListRelatedLink(final String requestUriString) { + return requestUriString != null ? requestUriString.replaceFirst(RELATIONSHIP_LIST_RELATIONSHIP_URL, "") + : requestUriString; + } + + public static String getBiDirectionalRelationShipListRelatedLink(final String requestUriString) { + return requestUriString != null ? requestUriString.replaceFirst(BI_DIRECTIONAL_RELATIONSHIP_LIST_URL, "") + : requestUriString; + } + + +} diff --git a/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/RequestError.java b/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/RequestError.java new file mode 100755 index 00000000..782625f7 --- /dev/null +++ b/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/RequestError.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aaisimulator.utils; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@XmlRootElement(name = "requestError") +@XmlAccessorType(XmlAccessType.FIELD) +public class RequestError { + + @XmlElement(name = "serviceException") + private ServiceException serviceException; + + /** + * @return the serviceException + */ + public ServiceException getServiceException() { + return serviceException; + } + + /** + * @param serviceException the serviceException to set + */ + public void setServiceException(final ServiceException serviceException) { + this.serviceException = serviceException; + } + + @Override + public String toString() { + return "RequestError [serviceException=" + serviceException + "]"; + } + + + +} diff --git a/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/RequestErrorBuilder.java b/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/RequestErrorBuilder.java new file mode 100755 index 00000000..f3742220 --- /dev/null +++ b/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/RequestErrorBuilder.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aaisimulator.utils; + +import java.util.Arrays; +import java.util.List; + +/** + * @author waqas.ikram@ericsson.com + * + */ +public class RequestErrorBuilder { + + private final ServiceException serviceException = new ServiceException(); + + public RequestErrorBuilder messageId(final String messageId) { + this.serviceException.setMessageId(messageId); + return this; + } + + public RequestErrorBuilder text(final String text) { + this.serviceException.setText(text); + return this; + } + + public RequestErrorBuilder variables(final List<String> variables) { + this.serviceException.setVariables(variables); + return this; + } + + public RequestErrorBuilder variables(final String... variables) { + this.serviceException.setVariables(Arrays.asList(variables)); + return this; + } + + public RequestError build() { + final RequestError requestError = new RequestError(); + requestError.setServiceException(serviceException); + return requestError; + } + +} diff --git a/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/RequestErrorResponseUtils.java b/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/RequestErrorResponseUtils.java new file mode 100755 index 00000000..755d3a70 --- /dev/null +++ b/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/RequestErrorResponseUtils.java @@ -0,0 +1,57 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aaisimulator.utils; + +import javax.servlet.http.HttpServletRequest; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +/** + * @author waqas.ikram@ericsson.com + * + */ +public class RequestErrorResponseUtils { + + public static final String ERROR_MESSAGE_ID = "SVC3001"; + + public static final String ERROR_MESSAGE = "Resource not found for %1 using id %2 (msg=%3) (ec=%4)"; + + private static final String EMPTY_STRING = ""; + + public static final String getResourceVersion() { + return System.currentTimeMillis() + EMPTY_STRING; + } + + public static ResponseEntity<?> getRequestErrorResponseEntity(final HttpServletRequest request, + final String nodeType) { + return new ResponseEntity<>(new RequestErrorBuilder().messageId(ERROR_MESSAGE_ID).text(ERROR_MESSAGE) + .variables(request.getMethod(), request.getRequestURI(), + "Node Not Found:No Node of " + nodeType + " found at: " + request.getRequestURI(), + "ERR.5.4.6114") + .build(), HttpStatus.NOT_FOUND); + } + + public static ResponseEntity<?> getRequestErrorResponseEntity(final HttpServletRequest request) { + return getRequestErrorResponseEntity(request, Constants.SERVICE_RESOURCE_TYPE); + } + + private RequestErrorResponseUtils() {} + +} diff --git a/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/ServiceException.java b/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/ServiceException.java new file mode 100755 index 00000000..7ba47fa7 --- /dev/null +++ b/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/ServiceException.java @@ -0,0 +1,88 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aaisimulator.utils; + +import java.util.List; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@XmlRootElement(name = "serviceException") +@XmlAccessorType(XmlAccessType.FIELD) +public class ServiceException { + + private String messageId; + + private String text; + + private List<String> variables; + + /** + * @return the messageId + */ + public String getMessageId() { + return messageId; + } + + /** + * @param messageId the messageId to set + */ + public void setMessageId(final String messageId) { + this.messageId = messageId; + } + + /** + * @return the text + */ + public String getText() { + return text; + } + + /** + * @param text the text to set + */ + public void setText(final String text) { + this.text = text; + } + + /** + * @return the variables + */ + public List<String> getVariables() { + return variables; + } + + /** + * @param variables the variables to set + */ + public void setVariables(final List<String> variables) { + this.variables = variables; + } + + @Override + public String toString() { + return "ServiceException [messageId=" + messageId + ", text=" + text + ", variables=" + variables + "]"; + } + +} diff --git a/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/ShallowBeanCopy.java b/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/ShallowBeanCopy.java new file mode 100755 index 00000000..3285e013 --- /dev/null +++ b/plans/usecases-pnf-sw-upgrade/pnf-sw-upgrade/sorch/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/ShallowBeanCopy.java @@ -0,0 +1,89 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aaisimulator.utils; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +public class ShallowBeanCopy { + private static final Logger LOGGER = LoggerFactory.getLogger(ShallowBeanCopy.class); + + private ShallowBeanCopy() {} + + public static void copy(final Object from, final Object to) throws Exception { + final Map<String, Method> fromMethods = getMethods(from); + final Map<String, Method> toMethods = getMethods(to); + + for (final Entry<String, Method> entry : fromMethods.entrySet()) { + final String methodName = entry.getKey(); + final Method fromMethod = entry.getValue(); + + final Optional<Method> optional = getSetMethod(to, fromMethod); + if (optional.isPresent()) { + final Method toGetMethod = toMethods.get(methodName); + final Method toMethod = optional.get(); + final Object toValue = fromMethod.invoke(from); + + final Object fromValue = toGetMethod.invoke(to); + if (toValue != null && !toValue.equals(fromValue)) { + LOGGER.info("Changing {} value from: {} to: {}", methodName, fromValue, toValue); + toMethod.invoke(to, toValue); + } + } + } + } + + + private static Optional<Method> getSetMethod(final Object to, final Method fromMethod) { + final String name = fromMethod.getName().replaceFirst("get|is", "set"); + final Class<?> returnType = fromMethod.getReturnType(); + try { + return Optional.of(to.getClass().getMethod(name, returnType)); + } catch (final NoSuchMethodException noSuchMethodException) { + } + return Optional.empty(); + } + + private static Map<String, Method> getMethods(final Object object) { + final Map<String, Method> methodsFound = new HashMap<>(); + final Method[] methods = object.getClass().getMethods(); + + for (final Method method : methods) { + if (method.getName().startsWith("get") || method.getName().startsWith("is")) { + final String name = method.getName().replaceFirst("get|is", ""); + + methodsFound.put(name, method); + } + } + + return methodsFound; + + } + +} |