diff options
author | Singal, Kapil (ks220y) <ks220y@att.com> | 2021-05-04 12:22:25 -0400 |
---|---|---|
committer | Singal, Kapil (ks220y) <ks220y@att.com> | 2021-05-04 12:22:25 -0400 |
commit | 557282a4274b7f2849e2433ff3373b43c6a920b3 (patch) | |
tree | b7c481a3ca3b3cfefa049f0e5fbce05cf273e438 /adaptors/rest-adaptor/rest-adaptor-bundle/src/main | |
parent | c6b6d5298f25893c7800fda38dd46f4254b1b47f (diff) |
Moving Rest Adaptor from APPC to CCSDK
Issue-ID: CCSDK-3198
Signed-off-by: Singal, Kapil (ks220y) <ks220y@att.com>
Change-Id: I70868423761ce9b8bf04fba2a46913d6e153a30b
Diffstat (limited to 'adaptors/rest-adaptor/rest-adaptor-bundle/src/main')
7 files changed, 1305 insertions, 0 deletions
diff --git a/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/rest/Constants.java b/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/rest/Constants.java new file mode 100644 index 000000000..4478bd9f1 --- /dev/null +++ b/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/rest/Constants.java @@ -0,0 +1,228 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Modifications Copyright (C) 2018 Samsung + * ================================================================================ + * 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.ccsdk.sli.adaptors.rest; + +/** + * This class contains the definitions of all constant values used in the APPC provider, adapters, and other components. + * These constants define properties, settings, and context variables. The context variables can be referenced from + * within the directed graph(s) to access information placed their by the provider and adapters. + * <p> + * Context properties are set in the graph context by the various adapters and the provider, or by the graph itself. + * These properties may also be accessed by the graph, adapters, or the provider. It is these properties that allow + * communication of state through the directed graph. All context properties have a symbolic name that starts with + * "CONTEXT_". + * </p> + */ + +public final class Constants { + + /** + * The name for the error code attribute to be set in the context + */ + @SuppressWarnings("nls") + public static final String ATTRIBUTE_ERROR_CODE = "error_code"; + + /** + * The name for the error message attribute to be set in the context + */ + @SuppressWarnings("nls") + public static final String ATTRIBUTE_ERROR_MESSAGE = "error-message"; + + /** + * The name for the error message to be set in the context + */ + @SuppressWarnings("nls") + public static final String CONTEXT_ERROR_MESSAGE = "org.onap.rest.result.message"; + + @SuppressWarnings("nls") + public static final String CONTEXT_AGENT_ERROR_MESSAGE = "org.onap.rest.agent.result.message"; + + /** + * The name for the error code to be set in the context + */ + @SuppressWarnings("nls") + public static final String CONTEXT_ERROR_CODE = "org.onap.rest.result.code"; + + @SuppressWarnings("nls") + public static final String CONTEXT_AGENT_ERROR_CODE = "org.onap.rest.agent.result.code"; + + /** + * The name for the success message attribute to be set in the context + */ + @SuppressWarnings("nls") + public static final String ATTRIBUTE_SUCCESS_MESSAGE = "success-message"; + + public static final String DG_ATTRIBUTE_STATUS = "SvcLogic.status"; + public static final String DG_OUTPUT_STATUS_CODE = "output.status.code"; + public static final String DG_OUTPUT_STATUS_MESSAGE = "output.status.message"; + + /** + * The property that defines the name of the DG service logic to be loaded + */ + public static final String PROPERTY_MODULE_NAME = "appc.service.logic.module.name"; + + /** + * The property that defines the topology restart DG version to be used + */ + public static final String PROPERTY_TOPOLOGY_VERSION = "appc.topology.dg.version"; + + /** + * The method name of the DG that is used to perform topology restart operations + */ + public static final String PROPERTY_TOPOLOGY_METHOD = "appc.topology.dg.method"; + + /** + * The property that supplies the application name + */ + public static final String PROPERTY_APPLICATION_NAME = "appc.application.name"; + + /** + * The execution mode for the directed graph + */ + public static final String SYNC_MODE = "sync"; + + /** + * The name of the property that contains the service request enumerated value in the graph's context + */ + public static final String CONTEXT_SERVICE = "org.onap.appc.service"; + + /** + * The name of the property that contains the VM id value in the graph's context + */ + public static final String CONTEXT_VMID = "org.onap.appc.vmid"; + + /** + * The name of the property that contains the VM id value in the graph's context + */ + public static final String CONTEXT_IDENTITY_URL = "org.onap.appc.identity.url"; + + /** + * The name of the property that contains the service request id value in the graph's context + */ + public static final String CONTEXT_REQID = "org.onap.appc.reqid"; + + /** + * The name of the property that indicates which method of the IaaS adapter to call + */ + public static final String CONTEXT_ACTION = "org.onap.appc.action"; + + /** + * The enumerated value for restart of a VM. This is a constant for one possible value of CONTEXT_SERVICE. + */ + public static final String SERVICE_RESTART = "RESTART"; + + /** + * The enumerated value for rebuild of a VM. This is a constant for one possible value of CONTEXT_SERVICE. + */ + public static final String SERVICE_REBUILD = "REBUILD"; + + /** + * The name of the adapter. We get the name from a property file so that it can be changed easily if needed. + */ + public static final String PROPERTY_ADAPTER_NAME = "org.onap.appc.provider.adaptor.name"; + + /** + * The minimum number of contexts to cache in each provider/tenant pool + */ + public static final String PROPERTY_MIN_POOL_SIZE = "org.onap.appc.provider.min.pool"; + + /** + * The maximum number of contexts to cache in each provider/tenant pool + */ + public static final String PROPERTY_MAX_POOL_SIZE = "org.onap.appc.provider.max.pool"; + + /** + * The amount of time, in seconds, that the application waits for a change of state of a server to a known valid + * state before giving up and failing the request. + */ + public static final String PROPERTY_SERVER_STATE_CHANGE_TIMEOUT = "org.onap.appc.server.state.change.timeout"; + + /** + * The amount of time, in seconds, between subsequent polls to the openstack provider to update the state of a + * resource + */ + public static final String PROPERTY_OPENSTACK_POLL_INTERVAL = "org.onap.appc.openstack.poll.interval"; + + /** + * The amount of time, in seconds, to wait between retry attempts when a connection to a provider fails. + */ + public static final String PROPERTY_RETRY_DELAY = "org.onap.appc.provider.retry.delay"; + + /** + * The maximum number of times a connection retry will be attempted before the application fails the request + */ + public static final String PROPERTY_RETRY_LIMIT = "org.onap.appc.provider.retry.limit"; + /** + * The amount of time, in seconds, that the application waits for a change of state of a stacj to a known valid + * state before giving up and failing the request. + */ + public static final String PROPERTY_STACK_STATE_CHANGE_TIMEOUT = "org.onap.appc.stack.state.change.timeout"; + + @SuppressWarnings("nls") + public static final String STATUS_GETTER = "status-getter"; + + @SuppressWarnings("nls") + public static final String VM_FUSION_STATUS_GETTER = "fusion-vm-status-getter"; + + /** + * The name for the status vm attribute to be set in the context when executing a vmstatuscheck. + */ + @SuppressWarnings("nls") + public static final String STATUS_OF_VM = "status-vm"; + + /** + * Yang revision value to be used while generating YANG module + */ + public static final String YANG_REVISION = "2017-03-03"; + /** + * Yang revision format to be used while formatting YANG revision date + */ + public static final String YANG_REVISION_FORMAT = "YYYY-MM-DD"; + + /** + * Base container for yang that is generated to store in MD-SAL datastore + */ + public static final String YANG_BASE_CONTAINER = "vnf-config-repo"; + + /** + * VNF config list for yang that is generated to store in MD-SAL datastore + */ + public static final String YANG_VNF_CONFIG_LIST = "vnf-config-list"; + + /** + * Base container of VNF configuration data for yang that is generated to store in MD-SAL datastore + */ + public static final String YANG_VNF_CONFIG = "vnf-config"; + + /** + * default constructor prevents instantiation + */ + Constants() { + throw new IllegalAccessError("Constants"); + } + +} diff --git a/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/rest/RequestFactory.java b/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/rest/RequestFactory.java new file mode 100644 index 000000000..ba1f7f5d9 --- /dev/null +++ b/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/rest/RequestFactory.java @@ -0,0 +1,60 @@ +/* + * ============LICENSE_START============================================================================================================= + * Copyright (c) 2017 Intel Corp. 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.ccsdk.sli.adaptors.rest; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Supplier; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.client.utils.URIBuilder; + +public class RequestFactory { + static final Map<String, Supplier<HttpRequestBase>> map = new HashMap<>(); + + static { + map.put("GET", HttpGet :: new); + map.put("POST", HttpPost :: new); + map.put("PUT", HttpPut :: new); + map.put("DELETE", HttpDelete :: new); + } + + private final EELFLogger logger = EELFManager.getInstance().getLogger(RequestFactory.class); + + public HttpRequestBase getHttpRequest(String method, String tUrl) { + Supplier<HttpRequestBase> httpRequestSupplier = map.get(method.toUpperCase()); + URI uri = null; + if (httpRequestSupplier != null) { + try { + uri = new URIBuilder(tUrl).build(); + } catch (URISyntaxException ex) { + logger.error("URI Syntax Incorrect: " + tUrl, ex); + } + HttpRequestBase httpRequest = httpRequestSupplier.get(); + httpRequest.setURI(uri); + return httpRequest; + + } + throw new IllegalArgumentException("No method named: " + method.toUpperCase()); + } + +} diff --git a/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/rest/RestAdaptor.java b/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/rest/RestAdaptor.java new file mode 100644 index 000000000..836123698 --- /dev/null +++ b/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/rest/RestAdaptor.java @@ -0,0 +1,204 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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.ccsdk.sli.adaptors.rest; + +import java.util.Map; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin; + +/** + * This interface defines the operations that the provider adaptor exposes. + * <p> + * This interface defines static constant property values that can be used to configure the adaptor. These constants are + * prefixed with the name PROPERTY_ to indicate that they are configuration properties. These properties are read from + * the configuration file for the adaptor and are used to define the providers, identity service URLs, and other + * information needed by the adaptor to interface with an IaaS provider. + * </p> + */ +public interface RestAdaptor extends SvcLogicJavaPlugin { + + /** + * The type of provider to be accessed to locate and operate on a virtual machine instance. This is used to load the + * correct provider support through the CDP IaaS abstraction layer and can be OpenStackProvider, BareMetalProvider, + * or any other supported provider type. + */ + static final String PROPERTY_PROVIDER_TYPE = "org.onap.appc.provider.type"; + + /** + * The adaptor maintains a cache of providers organized by the name of the provider, not its type. This is + * equivalent to the system or installation name. All regions within the same installation are assumed to be the + * same type. + */ + static final String PROPERTY_PROVIDER_NAME = "org.onap.appc.provider.name"; + + /** + * The fully-qualified URL of the instance to be manipulated as it is known to the provider. + */ + static final String PROPERTY_INSTANCE_URL = "org.onap.appc.instance.url"; + + /** + * The fully-qualified URL of the instance to be manipulated as it is known to the provider. + */ + static final String PROPERTY_IDENTITY_URL = "org.onap.appc.identity.url"; + + /** + * This method is used to restart an existing virtual machine given the fully qualified URL of the machine. + * <p> + * This method is invoked from a directed graph as an <code>Executor</code> node. This means that the parameters + * passed to the method are passed as properties in a map. This method expects the following properties to be + * defined: + * <dl> + * <dt>org.onap.appc.provider.type</dt> + * <dd>The appropriate provider type, such as <code>OpenStackProvider</code>. This is used by the CDP IaaS + * abstraction layer to dynamically load and open a connection to the appropriate provider type. All CDP supported + * provider types are legal.</dd> + * <dt>org.onap.appc.instance.url</dt> + * <dd>The fully qualified URL of the instance to be restarted, as it is known to the provider (i.e., the self-link + * URL of the server)</dd> + * </dl> + * </p> + * + * @param properties + * A map of name-value pairs that supply the parameters needed by this method. The properties needed are + * defined above. + * @param context + * The service logic context of the graph being executed. + * @return The <code>Server</code> object that represents the VM being restarted. The returned server object can be + * inspected for the final state of the server once the restart has been completed. The method does not + * return until the restart has either completed or has failed. + * @throws APPCException + * If the server cannot be restarted for some reason + */ + // Server restartServer(Map<String, String> properties, SvcLogicContext context) throws APPCException; + + /** + * This method is used to stop the indicated server + * <p> + * This method is invoked from a directed graph as an <code>Executor</code> node. This means that the parameters + * passed to the method are passed as properties in a map. This method expects the following properties to be + * defined: + * <dl> + * <dt>org.onap.appc.provider.type</dt> + * <dd>The appropriate provider type, such as <code>OpenStackProvider</code>. This is used by the CDP IaaS + * abstraction layer to dynamically load and open a connection to the appropriate provider type. All CDP supported + * provider types are legal.</dd> + * <dt>org.onap.appc.instance.url</dt> + * <dd>The fully qualified URL of the instance to be stopped, as it is known to the provider (i.e., the self-link + * URL of the server)</dd> + * </dl> + * </p> + * + * @param properties + * A map of name-value pairs that supply the parameters needed by this method. The properties needed are + * defined above. + * @param context + * The service logic context of the graph being executed. + * @return The <code>Server</code> object that represents the VM being stopped. The returned server object can be + * inspected for the final state of the server once the stop has been completed. The method does not return + * until the stop has either completed or has failed. + * @throws APPCException + * If the server cannot be stopped for some reason + */ + //Server stopServer(Map<String, String> properties, SvcLogicContext context) throws APPCException; + + /** + * This method is used to start the indicated server + * <p> + * This method is invoked from a directed graph as an <code>Executor</code> node. This means that the parameters + * passed to the method are passed as properties in a map. This method expects the following properties to be + * defined: + * <dl> + * <dt>org.onap.appc.provider.type</dt> + * <dd>The appropriate provider type, such as <code>OpenStackProvider</code>. This is used by the CDP IaaS + * abstraction layer to dynamically load and open a connection to the appropriate provider type. All CDP supported + * provider types are legal.</dd> + * <dt>org.onap.appc.instance.url</dt> + * <dd>The fully qualified URL of the instance to be started, as it is known to the provider (i.e., the self-link + * URL of the server)</dd> + * </dl> + * </p> + * + * @param properties + * A map of name-value pairs that supply the parameters needed by this method. The properties needed are + * defined above. + * @param context + * The service logic context of the graph being executed. + * @return The <code>Server</code> object that represents the VM being started. The returned server object can be + * inspected for the final state of the server once the start has been completed. The method does not return + * until the start has either completed or has failed. + * @throws APPCException + * If the server cannot be started for some reason + */ + // Server startServer(Map<String, String> properties, SvcLogicContext context) throws APPCException; + + /** + * This method is used to rebuild the indicated server + * <p> + * This method is invoked from a directed graph as an <code>Executor</code> node. This means that the parameters + * passed to the method are passed as properties in a map. This method expects the following properties to be + * defined: + * <dl> + * <dt>org.onap.appc.provider.type</dt> + * <dd>The appropriate provider type, such as <code>OpenStackProvider</code>. This is used by the CDP IaaS + * abstraction layer to dynamically load and open a connection to the appropriate provider type. All CDP supported + * provider types are legal.</dd> + * <dt>org.onap.appc.instance.url</dt> + * <dd>The fully qualified URL of the instance to be rebuilt, as it is known to the provider (i.e., the self-link + * URL of the server)</dd> + * </dl> + * </p> + * + * @param properties + * A map of name-value pairs that supply the parameters needed by this method. The properties needed are + * defined above. + * @param context + * The service logic context of the graph being executed. + * @return The <code>Server</code> object that represents the VM being rebuilt. The returned server object can be + * inspected for the final state of the server once the rebuild has been completed. The method does not + * return until the rebuild has either completed or has failed. + * @throws APPCException + * If the server cannot be rebuilt for some reason + */ + // Server rebuildServer(Map<String, String> properties, SvcLogicContext context) throws APPCException; + + /** + * Returns the symbolic name of the adaptor + * + * @return The adaptor name + */ + String getAdaptorName(); + + // Server evacuateServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException; + + //Server migrateServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException; + + void commonGet(Map<String, String> params, SvcLogicContext ctx); + + void commonPost(Map<String, String> params, SvcLogicContext ctx); + + void commonPut(Map<String, String> params, SvcLogicContext ctx); + + void commonDelete(Map<String, String> params, SvcLogicContext ctx); + +} diff --git a/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/rest/impl/RequestContext.java b/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/rest/impl/RequestContext.java new file mode 100644 index 000000000..4f297a23b --- /dev/null +++ b/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/rest/impl/RequestContext.java @@ -0,0 +1,249 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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.ccsdk.sli.adaptors.rest.impl; + +import org.onap.ccsdk.sli.adaptors.rest.Constants; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.utils.configuration.Configuration; +import org.onap.ccsdk.sli.core.utils.configuration.ConfigurationFactory; + +/** + * This class is used to track and maintain recovery and time-to-live information for a request as it is being + * processed. + */ +public class RequestContext { + /** + * The number of seconds of wait time between successive attempts to connect to the provider. This is used to + * recover from provider outages or failures. It is not used to recover from logical errors, such as an invalid + * request, server not found, etc. + */ + private Integer retryDelay; + + /** + * The number of times we will attempt to connect to the provider. This is used to recover from provider outages or + * failures. It is not used to recover from logical errors, such as an invalid request, server not found, etc. + */ + private Integer retryLimit; + + /** + * The total time, in milliseconds, that the provider can have to process this request. If the accumulated time + * exceeds the time to live, then the request is failed with a timeout exception, regardless of the state of the + * provider. Note that the caller may supply this as a value in seconds, in which case it must be converted to + * milliseconds for the request context. + */ + private Long timeToLive; + + /** + * The accumulated time, in milliseconds, that has been used so far to process the request. This is compared to the + * time to live each time it is updated. If the accumulated time exceeds the time to live, then the request is + * failed with a timeout exception, regardless of the state of the provider. + */ + private long accumulatedTime; + + /** + * The total number of retries attempted so far + */ + private int attempt; + + /** + * The time when the stopwatch was started + */ + private long startTime = -1; + + /** + * The service logic (DG) context from the SLI + */ + private SvcLogicContext svcLogicContext; + + /** + * The configuration + */ + private Configuration configuration = ConfigurationFactory.getConfiguration(); + + /** + * Set to true whenever the retry limit has been exceeded, reset to false when reset() is called. + */ + private boolean retryFailed; + + /** + * Creates the request context + * + * @param context The service logic (SLI) context associated with the current DG + */ + public RequestContext(SvcLogicContext context) { + setSvcLogicContext(context); + } + + /** + * @return The retry delay, in seconds. If zero, then no retry is to be performed + */ + public int getRetryDelay() { + if (retryDelay == null) { + int value = configuration.getIntegerProperty(Constants.PROPERTY_RETRY_DELAY); + retryDelay = Integer.valueOf(value); + } + + return retryDelay.intValue(); + } + + /** + * This method is a helper that allows the caller to delay for the retry interval time and not have to handle the + * thread interruption, timer handling, etc. + */ + public void delay() { + long time = getRetryDelay() * 1000L; + long future = System.currentTimeMillis() + time; + if (time != 0) { + while (System.currentTimeMillis() < future && time > 0) { + try { + Thread.sleep(time); + } catch (InterruptedException e) { + /* + * This is rare, but it can happen if another thread interrupts us while we are sleeping. In that + * case, the thread is resumed before the delay time has actually expired, so re-calculate the + * amount of delay time needed and reenter the sleep until we get to the future time. + */ + time = future - System.currentTimeMillis(); + } + } + } + } + + /** + * @return The number of retries that are allowed per connection + */ + public int getRetryLimit() { + if (retryLimit == null) { + int value = configuration.getIntegerProperty(Constants.PROPERTY_RETRY_LIMIT); + retryLimit = Integer.valueOf(value); + } + + return retryLimit.intValue(); + } + + /** + * Check and count the connection attempt. + * + * @return True if the connection should be attempted. False indicates that the number of retries has been exhausted + * and it should NOT be attempted. + */ + public boolean attempt() { + if (retryFailed || attempt >= getRetryLimit()) { + retryFailed = true; + return false; + } + attempt++; + + return true; + } + + /** + * @return The number of retry attempts so far + */ + public int getAttempts() { + return attempt; + } + + /** + * @return True if the retry limit has been exceeded, false otherwise + */ + public boolean isFailed() { + return retryFailed; + } + + /** + * This method both checks the time to live to see if it has been exceeded and accumulates the total time used so + * far. + * <p> + * Each time this method is called it accumulates the total duration since the last time it was called to the total + * time accumulator. It then checks the total time to the time to live and if greater, it returns false. As long as + * the total time used is less than or equal to the time to live limit, the method returns true. It is important to + * call this method at the very beginning of the process so that all parts of the process are tracked. + * </p> + * + * @return True if the total time to live has not been exceeded. False indicates that the total time to live has + * been exceeded and no further processing should be performed. + */ + public boolean isAlive() { + long now = System.currentTimeMillis(); + if (startTime == -1) { + startTime = now; + return true; + } + accumulatedTime += (now - startTime); + startTime = now; + if (accumulatedTime > timeToLive) { + return false; + } + return true; + } + + /** + * @return The total amount of time used, in milliseconds. + */ + public long getTotalDuration() { + return accumulatedTime; + } + + /** + * This method is called to reset the retry counters. It has no effect on the time to live accumulator. + */ + public void reset() { + attempt = 0; + } + + /** + * Sets the time to live to the value, expressed in seconds + * + * @param time The time to live, in seconds + */ + public void setTimeToLiveSeconds(int time) { + setTimeToLiveMS(time * 1000L); + } + + /** + * Sets the time to live to the value, expressed in milliseconds + * + * @param time The time to live, in milliseconds + */ + public void setTimeToLiveMS(long time) { + this.timeToLive = time; + } + + /** + * @return The service logic context associated with this request + */ + public SvcLogicContext getSvcLogicContext() { + return svcLogicContext; + } + + /** + * @param svcLogicContext The service logic context to be associated with this request + */ + public void setSvcLogicContext(SvcLogicContext svcLogicContext) { + this.svcLogicContext = svcLogicContext; + } + +} diff --git a/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/rest/impl/RequestFailedException.java b/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/rest/impl/RequestFailedException.java new file mode 100644 index 000000000..c6253fbbd --- /dev/null +++ b/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/rest/impl/RequestFailedException.java @@ -0,0 +1,225 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Modification Copyright (C) 2019 IBM + * ============================================================================= + * 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.ccsdk.sli.adaptors.rest.impl; + +import com.att.cdp.zones.model.Server; +import org.glassfish.grizzly.http.util.HttpStatus; + +/** + * This class is used to capture the exact cause and point of failure for the processing of a request. It is then used + * to encode the reason for the failure, status code, and anything else that needs to be captured and reported for + * diagnostic purposes. + */ +public class RequestFailedException extends Exception { + + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + * The operation that was being requested or performed at the time of the failure. + */ + private String operation; + + /** + * A message that details the reason for the failure + */ + private String reason; + + /** + * The server that was being operated upon + */ + private Server server; + + /** + * The id of the server being operated upon if the server object is not available (such as the server was not found) + */ + private String serverId; + + /** + * The most appropriate Http Status code that reflects the error + */ + private transient HttpStatus status; + + /** + * + */ + public RequestFailedException() { + // intentionally empty + } + + /** + * @param message The error message + */ + public RequestFailedException(String message) { + super(message); + } + + /** + * Construct the request failed exception with the operation being performed, reason for the failure, http status + * code that is most appropriate, and the server we were processing. + * + * @param operation The operation being performed + * @param reason The reason that the operation was failed + * @param status The http status code that is most appropriate + * @param server The server that we were processing + */ + @SuppressWarnings("nls") + public RequestFailedException(String operation, String reason, HttpStatus status, Server server) { + super(operation + ":" + reason); + this.operation = operation; + this.reason = reason; + this.status = status; + this.server = server; + if (server != null) { + this.serverId = server.getId(); + } + } + + /** + * Construct the request failed exception with the operation being performed, reason for the failure, http status + * code that is most appropriate, and the server we were processing. + * + * @param ex The exception that we are wrapping + * @param operation The operation being performed + * @param reason The reason that the operation was failed + * @param status The http status code that is most appropriate + * @param server The server that we were processing + */ + @SuppressWarnings("nls") + public RequestFailedException(Throwable ex, String operation, String reason, HttpStatus status, Server server) { + super(operation + ":" + reason, ex); + this.operation = operation; + this.reason = reason; + this.status = status; + this.server = server; + if (server != null) { + this.serverId = server.getId(); + } + } + + /** + * @param message The error message + * @param cause A nested exception + */ + public RequestFailedException(String message, Throwable cause) { + super(message, cause); + } + + /** + * @param message The error message + * @param cause A nested exception + * @param enableSuppression whether or not suppression is enabled or disabled + * @param writableStackTrace whether or not the stack trace should be writable + */ + public RequestFailedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + + /** + * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). (A null value is + * permitted, and indicates that the cause is nonexistent or unknown.) + */ + public RequestFailedException(Throwable cause) { + super(cause); + } + + /** + * @return The operation being performed + */ + public String getOperation() { + return operation; + } + + /** + * @param operation The operation being performed + */ + public void setOperation(String operation) { + this.operation = operation; + } + + /** + * @return The reason for the failure + */ + public String getReason() { + return reason; + } + + /** + * @param reason The reason for the failure + */ + public void setReason(String reason) { + this.reason = reason; + } + + /** + * @return The server being operated upon + */ + public Server getServer() { + return server; + } + + /** + * @param server The server being operated upon + */ + public void setServer(Server server) { + this.server = server; + if (server != null) { + setServerId(server.getId()); + } + } + + /** + * @return The id of the server being operated upon + */ + public String getServerId() { + return serverId; + } + + /** + * @param serverId The id of the server being operated upon + */ + public void setServerId(String serverId) { + this.serverId = serverId; + } + + /** + * @return The status code from the operation + */ + public HttpStatus getStatus() { + return status; + } + + /** + * @param status The status of the request + */ + public void setStatus(HttpStatus status) { + this.status = status; + } + +} diff --git a/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/rest/impl/RestAdaptorImpl.java b/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/rest/impl/RestAdaptorImpl.java new file mode 100644 index 000000000..ad06e9584 --- /dev/null +++ b/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/rest/impl/RestAdaptorImpl.java @@ -0,0 +1,244 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Modifications Copyright (C) 2018 Samsung + * ================================================================================ + * 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.ccsdk.sli.adaptors.rest.impl; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import java.util.Iterator; +import java.util.Map; +import java.util.function.Supplier; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.glassfish.grizzly.http.util.HttpStatus; +import org.json.JSONObject; +import org.onap.ccsdk.sli.adaptors.rest.Constants; +import org.onap.ccsdk.sli.adaptors.rest.RequestFactory; +import org.onap.ccsdk.sli.adaptors.rest.RestAdaptor; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.utils.configuration.Configuration; +import org.onap.ccsdk.sli.core.utils.configuration.ConfigurationFactory; + +/** + * This class implements the {@link RestAdaptor} interface. This interface defines the behaviors that our service + * provides. + */ +public class RestAdaptorImpl implements RestAdaptor { + + /** + * The constant for the status code for a failed outcome + */ + @SuppressWarnings("nls") + private static final String OUTCOME_FAILURE = "failure"; + + /** + * The constant for the status code for a successful outcome + */ + @SuppressWarnings("nls") + private static final String OUTCOME_SUCCESS = "success"; + + /** + * The logger to be used + */ + private final EELFLogger logger = EELFManager.getInstance().getLogger(RestAdaptorImpl.class); + + /** + * A reference to the adaptor configuration object. + */ + private Configuration configuration; + + /** + * This default constructor is used as a work around because the activator wasnt getting called + */ + public RestAdaptorImpl() { + initialize(); + } + + /** + * Returns the symbolic name of the adaptor + * + * @return The adaptor name + * + * @see RestAdaptor#getAdaptorName() + */ + @Override + public String getAdaptorName() { + return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME); + } + + @Override + public void commonGet(Map<String, String> params, SvcLogicContext ctx) { + logger.info("Run get method"); + + RequestContext rc = new RequestContext(ctx); + rc.isAlive(); + + HttpGet httpGet = (HttpGet) createHttpRequest("GET", params, rc); + executeHttpRequest(httpGet, rc); + } + + @Override + public void commonDelete(Map<String, String> params, SvcLogicContext ctx) { + logger.info("Run Delete method"); + + RequestContext rc = new RequestContext(ctx); + rc.isAlive(); + + HttpDelete httpDelete = (HttpDelete) createHttpRequest("DELETE", params, rc); + executeHttpRequest(httpDelete, rc); + } + + @Override + public void commonPost(Map<String, String> params, SvcLogicContext ctx) { + logger.info("Run post method"); + + RequestContext rc = new RequestContext(ctx); + rc.isAlive(); + + HttpPost httpPost = (HttpPost) createHttpRequest("POST", params, rc); + executeHttpRequest(httpPost, rc); + } + + @Override + public void commonPut(Map<String, String> params, SvcLogicContext ctx) { + logger.info("Run put method"); + + RequestContext rc = new RequestContext(ctx); + rc.isAlive(); + + HttpPut httpPut = (HttpPut) createHttpRequest("PUT", params, rc); + executeHttpRequest(httpPut, rc); + } + + @SuppressWarnings("static-method") + private void doFailure(RequestContext rc, HttpStatus code, String message) { + SvcLogicContext svcLogic = rc.getSvcLogicContext(); + String msg = (message == null) ? code.getReasonPhrase() : message; + if (msg.contains("\n")) { + msg = msg.substring(msg.indexOf('\n')); + } + + String status; + try { + status = Integer.toString(code.getStatusCode()); + } catch (Exception e) { + logger.error("Exception occurred", e); + status = "500"; + } + svcLogic.setStatus(OUTCOME_FAILURE); + svcLogic.setAttribute(Constants.ATTRIBUTE_ERROR_CODE, status); + svcLogic.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg); + svcLogic.setAttribute(Constants.CONTEXT_ERROR_CODE, status); + svcLogic.setAttribute(Constants.CONTEXT_ERROR_MESSAGE, msg); + } + + /** + * @param rc The request context that manages the state and recovery of the request for the life of its processing. + */ + @SuppressWarnings("static-method") + private void doSuccess(RequestContext rc, int code, String message) { + SvcLogicContext svcLogic = rc.getSvcLogicContext(); + svcLogic.setStatus(OUTCOME_SUCCESS); + svcLogic.setAttribute(Constants.ATTRIBUTE_ERROR_CODE, Integer.toString(HttpStatus.OK_200.getStatusCode())); + svcLogic.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, message); + svcLogic.setAttribute(Constants.CONTEXT_AGENT_ERROR_CODE, Integer.toString(code)); + svcLogic.setAttribute(Constants.CONTEXT_AGENT_ERROR_MESSAGE, message); + svcLogic.setAttribute(Constants.CONTEXT_ERROR_CODE, Integer.toString(HttpStatus.OK_200.getStatusCode())); + } + + private void executeHttpRequest(HttpRequestBase httpRequest, RequestContext rc) { + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + HttpResponse response = httpClient.execute(httpRequest); + int responseCode = response.getStatusLine().getStatusCode(); + HttpEntity entity = response.getEntity(); + String responseOutput = EntityUtils.toString(entity); + if (responseCode == 200) { + doSuccess(rc, responseCode, responseOutput); + } else { + doFailure(rc, HttpStatus.getHttpStatus(responseCode), response.getStatusLine().getReasonPhrase()); + } + } catch (Exception e) { + logger.error("An error occurred when executing http request", e); + doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, e.toString()); + } + } + + public HttpRequestBase createHttpRequest(String method, Map<String, String> params, RequestContext rc) { + HttpRequestBase httpRequest = null; + try { + String tUrl = params.get("org.onap.appc.instance.URI"); + String haveHeader = params.get("org.onap.appc.instance.haveHeader"); + String headers = params.get("org.onap.appc.instance.headers"); + + Supplier<RequestFactory> requestFactory = RequestFactory :: new; + httpRequest = requestFactory.get().getHttpRequest(method, tUrl); + + if ("true".equals(haveHeader)) { + JSONObject jsonHeaders = new JSONObject(headers); + Iterator keys = jsonHeaders.keys(); + while (keys.hasNext()) { + String string1 = (String) keys.next(); + String string2 = jsonHeaders.getString(string1); + httpRequest.addHeader(string1, string2); + } + } + if (params.containsKey("org.onap.appc.instance.requestBody")) { + String body = params.get("org.onap.appc.instance.requestBody"); + StringEntity bodyParams = new StringEntity(body, "UTF-8"); + if ("PUT".equals(method)) { + HttpPut httpPut = (HttpPut) httpRequest; + httpPut.setEntity(bodyParams); + } + if ("POST".equals(method)) { + HttpPost httpPost = (HttpPost) httpRequest; + httpPost.setEntity(bodyParams); + } + } + } catch (Exception e) { + logger.error("An error occurred when creating http request", e); + doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, e.toString()); + } + return httpRequest; + } + + /** + * initialize the provider adaptor by building the context cache + */ + private void initialize() { + configuration = ConfigurationFactory.getConfiguration(); + + logger.info("Rest adaptor has been initialized"); + } + +} diff --git a/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/resources/rest-adaptor.properties b/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/resources/rest-adaptor.properties new file mode 100644 index 000000000..a695c63c1 --- /dev/null +++ b/adaptors/rest-adaptor/rest-adaptor-bundle/src/main/resources/rest-adaptor.properties @@ -0,0 +1,95 @@ +### +# ============LICENSE_START======================================================= +# ONAP : APPC +# ================================================================================ +# Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Copyright (C) 2017 Amdocs +# ============================================================================= +# 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========================================================= +### + +# +# Default properties for the APP-C Provider Adaptor +# +# ------------------------------------------------------------------------------------------------- +# +# Define the name and path of any user-provided configuration (bootstrap) file that can be loaded +# to supply configuration options +org.onap.appc.bootstrap.file=appc.properties +org.onap.appc.bootstrap.path=/opt/onap/appc/data/properties,${user.home},. + +appc.application.name=APPC + +# +# Define the message resource bundle name to be loaded +org.onap.appc.resources=org/onap/appc/i18n/MessageResources +# +# The name of the adaptor. +org.onap.appc.provider.adaptor.name=org.onap.appc.appc_provider_adaptor +# +# Set up the logging environment +# +org.onap.appc.logging.file=org/onap/appc/logback.xml +org.onap.appc.logging.path=${user.home};etc;../etc +org.onap.appc.logger=org.onap.appc +org.onap.appc.security.logger=org.onap.appc.security +# +# The minimum and maximum provider/tenant context pool sizes. Min=1 means that as soon +# as the provider/tenant is referenced a Context is opened and added to the pool. Max=0 +# means that the upper bound on the pool is unbounded. +org.onap.appc.provider.min.pool=1 +org.onap.appc.provider.max.pool=0 + +# +# The following properties are used to configure the retry logic for connection to the +# IaaS provider(s). The retry delay property is the amount of time, in seconds, the +# application waits between retry attempts. The retry limit is the number of retries +# that are allowed before the request is failed. +org.onap.appc.provider.retry.delay = 30 +org.onap.appc.provider.retry.limit = 10 + +# +# The trusted hosts list for SSL access when a certificate is not provided. +# +provider.trusted.hosts=* +# +# The amount of time, in seconds, to wait for a server state change (start->stop, stop->start, etc). +# If the server does not change state to a valid state within the alloted time, the operation +# fails. +org.onap.appc.server.state.change.timeout=300 +# +# The amount of time to wait, in seconds, between subsequent polls to the OpenStack provider +# to refresh the status of a resource we are waiting on. +# +org.onap.appc.openstack.poll.interval=20 +# +# The connection information to connect to the provider we are using. These properties +# are "structured" properties, in that the name is a compound name, where the nodes +# of the name can be ordered (1, 2, 3, ...). All of the properties with the same ordinal +# position are defining the same entity. For example, provider1.type and provider1.name +# are defining the same provider, whereas provider2.name and provider2.type are defining +# the values for a different provider. Any number of providers can be defined in this +# way. +# +# Don't change these 2 right now since they are hard coded in the DG +#provider1.type=appc +#provider1.name=appc + +#These you can change +#provider1.identity=appc +#provider1.tenant1.name=appc +#provider1.tenant1.userid=appc +#provider1.tenant1.password=appc |