diff options
author | Ashwin Sridharan <asridharan@research.att.com> | 2017-06-14 23:11:25 -0400 |
---|---|---|
committer | Patrick Brady <pb071s@att.com> | 2017-06-15 12:26:44 -0700 |
commit | 0414b3eaf226c9f96607c7f8cf4196676d2c5ea4 (patch) | |
tree | c88c360b31b7ac543f7452d0aab99628bcd29708 /appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org | |
parent | 7a261fb5644ea718e28fa91da00dfb91be1d8d6e (diff) |
[feature/APPC-6]
Added Ansible Adapter Extension for APP-C
Change-Id: I4580fe5ebec526186fff5520fc7d12bff49f3fa4
Signed-off-by: Ashwin Sridharan <asridharan@research.att.com>
Signed-off-by: Patrick Brady <pb071s@att.com>
Diffstat (limited to 'appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org')
8 files changed, 1644 insertions, 0 deletions
diff --git a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/AnsibleActivator.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/AnsibleActivator.java new file mode 100644 index 000000000..fea817126 --- /dev/null +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/AnsibleActivator.java @@ -0,0 +1,132 @@ +/*- + * ============LICENSE_START======================================================= + * APPC + * ================================================================================ + * Copyright (C) 2017 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========================================================= + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ + +package org.openecomp.appc.adapter.ansible; + +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; + +import org.openecomp.appc.Constants; +import org.openecomp.appc.adapter.ansible.impl.AnsibleAdapterImpl; +import org.openecomp.appc.configuration.Configuration; +import org.openecomp.appc.configuration.ConfigurationFactory; +import org.openecomp.appc.i18n.Msg; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +/** + * This activator is used to initialize and terminate an instance of AnsibleAdapter class + * + * Author : Ashwin Sridharan + * Date : Oct 2016 + */ +public class AnsibleActivator implements BundleActivator { + + /** + * The bundle registration + */ + private ServiceRegistration registration = null; + + /** + * The reference to the actual implementation object that implements the services + */ + private AnsibleAdapter adapter; + + /** + * The logger to be used + */ + private static final EELFLogger logger = EELFManager.getInstance().getLogger(AnsibleActivator.class); + + /** + * The configuration object used to configure this bundle + */ + private Configuration configuration; + + /** + * Called when this bundle is started so the Framework can perform the bundle-specific activities necessary to start + * this bundle. This method can be used to register services or to allocate any resources that this bundle needs. + * <p> + * This method must complete and return to its caller in a timely manner. + * </p> + * + * @param context + * The execution context of the bundle being started. + * @throws java.lang.Exception + * If this method throws an exception, this bundle is marked as stopped and the Framework will remove + * this bundle's listeners, unregister all services registered by this bundle, and release all services + * used by this bundle. + * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) + */ + @Override + public void start(BundleContext context) throws Exception { + + logger.info("Starting bundle " + getName()); + String appName = "APPC: "; + logger.info(Msg.COMPONENT_INITIALIZING, appName, "Ansible Adapter"); + adapter = new AnsibleAdapterImpl(); + + if (registration == null) { + logger.info(Msg.REGISTERING_SERVICE, appName, adapter.getAdapterName(), + AnsibleAdapter.class.getSimpleName()); + registration = context.registerService(AnsibleAdapter.class, adapter, null); + } + + logger.info(Msg.COMPONENT_INITIALIZED, appName, "Ansible adapter"); + } + + /** + * Called when this bundle is stopped so the Framework can perform the bundle-specific activities necessary to stop + * the bundle. In general, this method should undo the work that the BundleActivator.start method started. There + * should be no active threads that were started by this bundle when this bundle returns. A stopped bundle must not + * call any Framework objects. + * <p> + * This method must complete and return to its caller in a timely manner. + * </p> + * + * @param context + * The execution context of the bundle being stopped. + * @throws java.lang.Exception + * If this method throws an exception, the bundle is still marked as stopped, and the Framework will + * remove the bundle's listeners, unregister all services registered by the bundle, and release all + * services used by the bundle. * + * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) + */ + @Override + public void stop(BundleContext context) throws Exception { + logger.info("Stopping bundle " + getName()); + + if (registration != null) { + String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME); + logger.info(Msg.COMPONENT_TERMINATING, appName, "Ansible adapter"); + logger.info(Msg.UNREGISTERING_SERVICE, appName, adapter.getAdapterName()); + registration.unregister(); + registration = null; + logger.info(Msg.COMPONENT_TERMINATED, appName, "Ansible adapter"); + } + } + + public String getName() { + return "APPC Ansible Adapter"; + } + +} diff --git a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/AnsibleAdapter.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/AnsibleAdapter.java new file mode 100644 index 000000000..8f2725da5 --- /dev/null +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/AnsibleAdapter.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * APPC + * ================================================================================ + * Copyright (C) 2017 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========================================================= + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ + +package org.openecomp.appc.adapter.ansible; + +import java.util.Map; + +import org.openecomp.appc.exceptions.APPCException; +import org.openecomp.sdnc.sli.SvcLogicContext; +import org.openecomp.sdnc.sli.SvcLogicJavaPlugin; +import org.openecomp.sdnc.sli.SvcLogicException; + +/** + * This interface defines the operations that the Ansible adapter exposes. + * + */ +public interface AnsibleAdapter extends SvcLogicJavaPlugin { + + + /** + * Returns the symbolic name of the adapter + * + * @return The adapter name + */ + String getAdapterName(); + + + /* Method to post request for execution of Playbook */ + void reqExec(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException; + + /* Method to get result of a playbook execution request */ + void reqExecResult(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException; + + + /* Method to get log of a playbook execution request */ + void reqExecLog(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException; + + +} diff --git a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/impl/AnsibleAdapterImpl.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/impl/AnsibleAdapterImpl.java new file mode 100644 index 000000000..ba35a70de --- /dev/null +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/impl/AnsibleAdapterImpl.java @@ -0,0 +1,516 @@ +/*- + * ============LICENSE_START======================================================= + * APPC + * ================================================================================ + * Copyright (C) 2017 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========================================================= + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ + +package org.openecomp.appc.adapter.ansible.impl; + +import java.net.URI; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.regex.Pattern; +import java.lang.*; + +import org.openecomp.appc.Constants; +import org.openecomp.appc.exceptions.APPCException; + +import org.openecomp.appc.configuration.Configuration; +import org.openecomp.appc.configuration.ConfigurationFactory; +import org.openecomp.appc.exceptions.APPCException; +import org.openecomp.appc.i18n.Msg; +import org.openecomp.appc.pool.Pool; +import org.openecomp.appc.pool.PoolExtensionException; +import org.openecomp.appc.util.StructuredPropertyHelper; +import org.openecomp.appc.util.StructuredPropertyHelper.Node; + +import org.openecomp.sdnc.sli.SvcLogicContext; +import org.openecomp.sdnc.sli.SvcLogicException; + + +import org.slf4j.MDC; + +import org.json.JSONObject; +import org.json.JSONArray; +import org.json.JSONException; + + +import com.google.common.base.Strings; +//import com.google.gson.Gson; +//import com.google.gson.GsonBuilder; + +import org.openecomp.appc.adapter.ansible.AnsibleAdapter; + +import org.openecomp.appc.adapter.ansible.model.AnsibleResult; +import org.openecomp.appc.adapter.ansible.model.AnsibleMessageParser; +import org.openecomp.appc.adapter.ansible.model.AnsibleResultCodes; +import org.openecomp.appc.adapter.ansible.model.AnsibleServerEmulator; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.att.eelf.i18n.EELFResourceManager; +import static com.att.eelf.configuration.Configuration.*; + + +/** + * This class implements the {@link AnsibleAdapter} interface. This interface + * defines the behaviors that our service provides. + * + */ +public class AnsibleAdapterImpl implements AnsibleAdapter { + + /** + * The constant used to define the adapter name in the mapped diagnostic + * context + */ + + + @SuppressWarnings("nls") + public static final String MDC_ADAPTER = "Ansible Adapter"; + + /** + * The constant used to define the service name in the mapped diagnostic + * context + */ + @SuppressWarnings("nls") + public static final String MDC_SERVICE = "service"; + + /** + * The constant for the status code for a failed outcome + */ + @SuppressWarnings("nls") + public static final String OUTCOME_FAILURE = "failure"; + + /** + * The constant for the status code for a successful outcome + */ + @SuppressWarnings("nls") + public static final String OUTCOME_SUCCESS = "success"; + + /** + Adapter Name + **/ + private static final String ADAPTER_NAME = "Ansible Adapter"; + + + /** + * The logger to be used + */ + private static final EELFLogger logger = EELFManager.getInstance().getLogger(AnsibleAdapterImpl.class); + + /** + * A reference to the adapter configuration object. + */ + private Configuration configuration;; + + /** can Specify a X509 certificate file for use if required ... + Must be initialized with setCertFile + **/ + private String certFile = ""; + + + /** + * Connection object + **/ + ConnectionBuilder http_client ; + + /** + * Ansible API Message Handlers + **/ + private AnsibleMessageParser messageProcessor; + + /** + indicator whether in test mode + **/ + private boolean testMode = false; + + /** + server emulator object to be used if in test mode + **/ + private AnsibleServerEmulator testServer; + + /** + * This default constructor is used as a work around because the activator + * wasnt getting called + */ + public AnsibleAdapterImpl() { + initialize(); + } + + + /** + * @param props + * not used + */ + public AnsibleAdapterImpl(Properties props) { + initialize(); + } + + + + /** + Used for jUnit test and testing interface + **/ + public AnsibleAdapterImpl(boolean Mode){ + testMode = Mode; + testServer = new AnsibleServerEmulator(); + messageProcessor = new AnsibleMessageParser(); + } + + /** + * Returns the symbolic name of the adapter + * + * @return The adapter name + * @see org.openecomp.appc.adapter.rest.AnsibleAdapter#getAdapterName() + */ + @Override + public String getAdapterName() { + return ADAPTER_NAME; + } + + + + /** + * @param rc + * Method posts info to Context memory in case of an error + * and throws a SvcLogicException causing SLI to register this as a failure + */ + @SuppressWarnings("static-method") + private void doFailure(SvcLogicContext svcLogic, int code, String message) throws SvcLogicException { + + svcLogic.setStatus(OUTCOME_FAILURE); + svcLogic.setAttribute("org.openecomp.appc.adapter.ansible.result.code",Integer.toString(code)); + svcLogic.setAttribute("org.openecomp.appc.adapter.ansible.message",message); + + throw new SvcLogicException("Ansible Adapter Error = " + message ); + } + + + /** + * initialize the Ansible adapter based on default and over-ride configuration data + */ + private void initialize() { + + configuration = ConfigurationFactory.getConfiguration(); + Properties props = configuration.getProperties(); + + // Create the message processor instance + messageProcessor = new AnsibleMessageParser(); + + // Create the http client instance + // type of client is extracted from the property file parameter + // org.openecomp.appc.adapter.ansible.clientType + // It can be : + // 1. TRUST_ALL (trust all SSL certs). To be used ONLY in dev + // 2. TRUST_CERT (trust only those whose certificates have been stored in the trustStore file) + // 3. DEFAULT (trust only well known certificates). This is standard behaviour to which it will + // revert. To be used in PROD + + try{ + String clientType = props.getProperty("org.openecomp.appc.adapter.ansible.clientType"); + logger.info("Ansible http client type set to " + clientType); + + if (clientType.equals("TRUST_ALL")){ + logger.info("Creating http client to trust ALL ssl certificates. WARNING. This should be done only in dev environments"); + http_client = new ConnectionBuilder(1); + } + else if (clientType.equals("TRUST_CERT")){ + // set path to keystore file + String trustStoreFile = props.getProperty("org.openecomp.appc.adapter.ansible.trustStore"); + String key = props.getProperty("org.openecomp.appc.adapter.ansible.trustStore.trustPasswd"); + char [] trustStorePasswd = key.toCharArray(); + String trustStoreType = "JKS"; + logger.info("Creating http client with trustmanager from " + trustStoreFile); + http_client = new ConnectionBuilder(trustStoreFile, trustStorePasswd); + } + else{ + logger.info("Creating http client with default behaviour"); + http_client = new ConnectionBuilder(0); + } + } + catch (Exception e){ + logger.error("Error Initializing Ansible Adapter due to Unknown Exception: reason = " + e.getMessage()); + } + + logger.info("Intitialized Ansible Adapter"); + + } + + + /** set the certificate file if not a trusted/known CA **/ + private void setCertFile(String CertFile){ + this.certFile = CertFile; + } + + + + // Public Method to post request to execute playbook. Posts the following back + // to Svc context memory + // org.openecomp.appc.adapter.ansible.req.code : 100 if successful + // org.openecomp.appc.adapter.ansible.req.messge : any message + // org.openecomp.appc.adapter.ansible.req.Id : a unique uuid to reference the request + + public void reqExec(Map <String, String> params, SvcLogicContext ctx) throws SvcLogicException { + + String PlaybookName = ""; + String payload = ""; + String AgentUrl = ""; + String User = ""; + String Password = ""; + String Id = ""; + + JSONObject JsonPayload; + + try{ + // create json object to send request + JsonPayload = messageProcessor.ReqMessage(params); + + AgentUrl = (String) JsonPayload.remove("AgentUrl"); + User = (String) JsonPayload.remove("User"); + Password = (String) JsonPayload.remove("Password"); + Id = (String)JsonPayload.getString("Id"); + payload = JsonPayload.toString(); + logger.info("Updated Payload = " + payload); + } + catch(APPCException e){ + doFailure(ctx, AnsibleResultCodes.INVALID_PAYLOAD.getValue(), "Error constructing request for execution of playbook due to missing mandatory parameters. Reason = " + e.getMessage()); + } + catch(JSONException e){ + doFailure(ctx, AnsibleResultCodes.INVALID_PAYLOAD.getValue(), "Error constructing request for execution of playbook due to invalid JSON block. Reason = " + e.getMessage()); + } + catch(NumberFormatException e){ + doFailure(ctx, AnsibleResultCodes.INVALID_PAYLOAD.getValue(), "Error constructing request for execution of playbook due to invalid parameter values. Reason = " + e.getMessage()); + } + + + + int code = -1; + String message = ""; + + try{ + + // post the test request + //--------------------------------------- + logger.info("Posting request = " + payload + " to url = " + AgentUrl ); + AnsibleResult testresult = postExecRequest(AgentUrl, payload, User, Password); + + + // Process if HTTP was successfull + if(testresult.getStatusCode() == 200){ + testresult = messageProcessor.parsePostResponse(testresult.getStatusMessage()); + } + else{ + doFailure(ctx, testresult.getStatusCode(), "Error posting request. Reason = " + testresult.getStatusMessage()); + } + + + code = testresult.getStatusCode(); + message = testresult.getStatusMessage(); + + + // Check status of test request returned by Agent + //----------------------------------------------- + if (code == AnsibleResultCodes.PENDING.getValue()){ + logger.info(String.format("Submission of Test %s successful.", PlaybookName)); + // test request accepted. We are in asynchronous case + } + else{ + doFailure(ctx, code, "Request for execution of playbook rejected. Reason = " + message); + } + } + + catch(APPCException e){ + doFailure(ctx, AnsibleResultCodes.UNKNOWN_EXCEPTION.getValue(), "Exception encountered when posting request for execution of playbook. Reason = " + e.getMessage()); + } + + + ctx.setAttribute("org.openecomp.appc.adapter.ansible.result.code", Integer.toString(code)); + ctx.setAttribute("org.openecomp.appc.adapter.ansible.message", message ); + ctx.setAttribute("org.openecomp.appc.adapter.ansible.Id", Id); + + } + + + // Public method to query status of a specific request + // It blocks till the Ansible Server responds or the session times out + + public void reqExecResult(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException { + + + // Get uri + String ReqUri = ""; + + try{ + ReqUri = messageProcessor.ReqUri_Result(params); + System.out.println("Got uri = " + ReqUri); + } + catch(APPCException e){ + doFailure(ctx, AnsibleResultCodes.INVALID_PAYLOAD.getValue(), "Error constructing request to retreive result due to missing parameters. Reason = " + e.getMessage()); + return; + } + catch(NumberFormatException e){ + doFailure(ctx, AnsibleResultCodes.INVALID_PAYLOAD.getValue(), "Error constructing request to retreive result due to invalid parameters value. Reason = " + e.getMessage()); + return; + } + + int code = -1; + String message = ""; + String results = ""; + + try{ + // Try to retreive the test results (modify the url for that) + AnsibleResult testresult = queryServer(ReqUri, params.get("User"), params.get("Password")); + code = testresult.getStatusCode(); + message = testresult.getStatusMessage(); + + if(code == 200){ + logger.info("Parsing response from Server = " + message); + // Valid HTTP. process the Ansible message + testresult = messageProcessor.parseGetResponse(message); + code = testresult.getStatusCode(); + message = testresult.getStatusMessage(); + results = testresult.getResults(); + + } + + logger.info("Request response = " + message); + + } + catch (APPCException e){ + doFailure(ctx, AnsibleResultCodes.UNKNOWN_EXCEPTION.getValue(), "Exception encountered retreiving result : " + e.getMessage()); + return; + } + + // We were able to get and process the results. Determine if playbook succeeded + + if (code == AnsibleResultCodes.FINAL_SUCCESS.getValue()){ + message = String.format("Ansible Request %s finished with Result = %s, Message = %s", params.get("Id"), OUTCOME_SUCCESS, message); + logger.info(message); + } + else { + logger.info(String.format("Ansible Request %s finished with Result %s, Message = %s", params.get("Id"), OUTCOME_FAILURE, message)); + ctx.setAttribute("org.openecomp.appc.adapter.ansible.results", results); + doFailure(ctx, code, message ); + return; + } + + + ctx.setAttribute("org.openecomp.appc.adapter.ansible.result.code", Integer.toString(400)); + ctx.setAttribute("org.openecomp.appc.adapter.ansible.message",message); + ctx.setAttribute("org.openecomp.appc.adapter.ansible.results", results); + ctx.setStatus(OUTCOME_SUCCESS); + } + + + // Public method to get logs from plyabook execution for a specifcic request + // It blocks till the Ansible Server responds or the session times out + // very similar to reqExecResult + // logs are returned in the DG context variable org.openecomp.appc.adapter.ansible.log + + public void reqExecLog(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException{ + + + // Get uri + String ReqUri = ""; + try{ + ReqUri = messageProcessor.ReqUri_Log(params); + logger.info("Retreiving results from " + ReqUri); + } + catch(Exception e){ + doFailure(ctx, AnsibleResultCodes.INVALID_PAYLOAD.getValue(), e.getMessage()); + } + + int code = -1; + String message = ""; + float Duration = -1; + + try{ + // Try to retreive the test results (modify the url for that) + AnsibleResult testresult = queryServer(ReqUri, params.get("User"), params.get("Password")); + code = testresult.getStatusCode(); + message = testresult.getStatusMessage(); + + logger.info("Request output = " + message); + + } + catch (Exception e){ + doFailure(ctx, AnsibleResultCodes.UNKNOWN_EXCEPTION.getValue(), "Exception encountered retreiving output : " + e.getMessage()); + } + + ctx.setAttribute("org.openecomp.appc.adapter.ansible.log",message); + ctx.setStatus(OUTCOME_SUCCESS); + } + + + + + + /** + * Method that posts the request + **/ + + private AnsibleResult postExecRequest(String AgentUrl, String Payload, String User, String Password) { + + String reqOutput = "UNKNOWN"; + int reqStatus = -1; + + AnsibleResult testresult; + + if (!testMode){ + http_client.setHttpContext(User, Password); + testresult = http_client.Post(AgentUrl, Payload); + } + else{ + testresult = testServer.Post(AgentUrl, Payload); + } + + return testresult; + } + + + /* + Method to query Ansible server + + */ + private AnsibleResult queryServer(String AgentUrl, String User, String Password) { + + String testOutput = "UNKNOWN"; + int testStatus = -1; + AnsibleResult testresult; + + logger.info("Querying url = " + AgentUrl); + + if (!testMode){ + testresult = http_client.Get(AgentUrl); + } + else{ + testresult = testServer.Get(AgentUrl); + } + + return testresult; + + } + + + +} diff --git a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/impl/ConnectionBuilder.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/impl/ConnectionBuilder.java new file mode 100644 index 000000000..5c2801cd4 --- /dev/null +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/impl/ConnectionBuilder.java @@ -0,0 +1,225 @@ +/*- + * ============LICENSE_START======================================================= + * APPC + * ================================================================================ + * Copyright (C) 2017 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========================================================= + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ + +package org.openecomp.appc.adapter.ansible.impl; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.auth.AuthScope; +import org.apache.http.entity.StringEntity; + +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.cert.CertificateException; +import java.security.KeyManagementException; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; +import java.security.NoSuchAlgorithmException; +import javax.net.ssl.SSLException; +import javax.net.ssl.SSLContext; +import javax.net.ssl.X509TrustManager; + +import java.io.FileInputStream; +import java.io.IOException; + + +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.conn.ssl.SSLContexts; +import org.apache.http.conn.ssl.TrustSelfSignedStrategy; + + +import org.openecomp.appc.exceptions.APPCException; +import org.openecomp.appc.adapter.ansible.model.AnsibleResult; +import org.openecomp.appc.adapter.ansible.model.AnsibleResultCodes; + + +/** + * Returns custom http client + - based on options + - can create one with ssl using an X509 certificate that does NOT have a known CA + - create one which trusts ALL SSL certificates + - return default httpclient (which only trusts known CAs from default cacerts file for process) -- this is the default option + +**/ + + +public class ConnectionBuilder { + + + + private CloseableHttpClient http_client = null; + private HttpClientContext http_context = new HttpClientContext(); + + + + + // Various constructors depending on how we want to instantiate the http ConnectionBuilder instance + + + /** + * Constructor that initializes an http client based on certificate + **/ + public ConnectionBuilder(String CertFile) throws KeyStoreException, CertificateException, IOException, KeyManagementException, NoSuchAlgorithmException, APPCException{ + + + /* Point to the certificate */ + FileInputStream fs = new FileInputStream(CertFile); + + /* Generate a certificate from the X509 */ + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + X509Certificate cert = (X509Certificate)cf.generateCertificate(fs); + + /* Create a keystore object and load the certificate there */ + KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); + keystore.load(null, null); + keystore.setCertificateEntry("cacert", cert); + + + SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(keystore).build(); + SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslcontext, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); + + http_client = HttpClients.custom().setSSLSocketFactory(factory).build(); + }; + + + /** + * Constructor which trusts all certificates in a specific java keystore file (assumes a JKS file) + **/ + public ConnectionBuilder(String trustStoreFile, char[] trustStorePasswd) throws KeyStoreException, IOException, KeyManagementException, NoSuchAlgorithmException, CertificateException { + + + /* Load the specified trustStore */ + KeyStore keystore = KeyStore.getInstance("JKS"); + FileInputStream readStream = new FileInputStream(trustStoreFile); + keystore.load(readStream,trustStorePasswd); + + SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(keystore).build(); + SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslcontext, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); + + http_client = HttpClients.custom().setSSLSocketFactory(factory).build(); + }; + + /** + * Constructor that trusts ALL SSl certificates (NOTE : ONLY FOR DEV TESTING) if Mode == 1 + or Default if Mode == 0 + */ + public ConnectionBuilder(int Mode) throws SSLException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException{ + if (Mode == 1){ + SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(); + SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslcontext, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); + + http_client = HttpClients.custom().setSSLSocketFactory(factory).build(); + } + + else{ + http_client = HttpClients.createDefault(); + } + + }; + + + // Use to create an http context with auth headers + public void setHttpContext(String User, String MyPassword){ + + // Are credential provided ? If so, set the context to be used + if (User != null && ! User.isEmpty() && MyPassword != null && ! MyPassword.isEmpty()){ + UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(User, MyPassword); + AuthScope authscope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT); + BasicCredentialsProvider credsprovider = new BasicCredentialsProvider(); + credsprovider.setCredentials(authscope, credentials); + http_context.setCredentialsProvider(credsprovider); + } + + + }; + + + // Method posts to the ansible server and writes out response to + // Ansible result object + public AnsibleResult Post(String AgentUrl, String Payload){ + + AnsibleResult result = new AnsibleResult(); + try{ + + HttpPost postObj = new HttpPost(AgentUrl); + StringEntity bodyParams = new StringEntity(Payload, "UTF-8"); + postObj.setEntity(bodyParams); + postObj.addHeader("Content-type", "application/json"); + + HttpResponse response = http_client.execute(postObj, http_context); + + HttpEntity entity = response.getEntity(); + String responseOutput = entity != null ? EntityUtils.toString(entity) : null; + int responseCode = response.getStatusLine().getStatusCode(); + result.setStatusCode(responseCode); + result.setStatusMessage(responseOutput); + } + + catch(IOException io){ + result.setStatusCode(AnsibleResultCodes.IO_EXCEPTION.getValue()); + result.setStatusMessage(io.getMessage()); + } + + + + return result; + + } + + // Method gets information from an Ansible server and writes out response to + // Ansible result object + + public AnsibleResult Get(String AgentUrl){ + + AnsibleResult result = new AnsibleResult(); + + try{ + HttpGet getObj = new HttpGet(AgentUrl ); + HttpResponse response = http_client.execute(getObj, http_context); + + + HttpEntity entity = response.getEntity(); + String responseOutput = entity != null ? EntityUtils.toString(entity) : null; + int responseCode = response.getStatusLine().getStatusCode(); + result.setStatusCode(responseCode); + result.setStatusMessage(responseOutput); + + } + catch(IOException io){ + result.setStatusCode(AnsibleResultCodes.IO_EXCEPTION.getValue()); + result.setStatusMessage(io.getMessage()); + } + + return result; + }; + +} diff --git a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleMessageParser.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleMessageParser.java new file mode 100644 index 000000000..e9b889930 --- /dev/null +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleMessageParser.java @@ -0,0 +1,362 @@ +/*- + * ============LICENSE_START======================================================= + * APPC + * ================================================================================ + * Copyright (C) 2017 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========================================================= + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ + +package org.openecomp.appc.adapter.ansible.model; + +/** + * This module imples the APP-C/Ansible Server interface + * based on the REST API specifications + */ + +import java.lang.NumberFormatException ; +import java.util.*; +import com.google.common.base.Strings; + +import org.json.JSONObject; +import org.json.JSONArray; +import org.json.JSONException; +import org.openecomp.appc.exceptions.APPCException; +import org.openecomp.appc.adapter.ansible.model.AnsibleResult; + + +/** + * Class that validates and constructs requests sent/received from + * Ansible Server + * + */ +public class AnsibleMessageParser { + + + + + // Accepts a map of strings and + // a) validates if all parameters are appropriate (else, throws an exception) + // and b) if correct returns a JSON object with appropriate key-value + // pairs to send to the server. + public JSONObject ReqMessage(Map <String, String> params) throws APPCException, NumberFormatException, JSONException{ + + // Mandatory parameters, that must be in the supplied information to the Ansible Adapter + // 1. URL to connect to + // 2. credentials for URL (assume username password for now) + // 3. Playbook name + String[] mandatoryTestParams = {"AgentUrl", "PlaybookName", "User", "Password"}; + + // Optional testService parameters that may be provided in the request + String[] optionalTestParams = {"EnvParameters", "NodeList", "LocalParameters", "Timeout", "Version", "FileParameters", "Action"}; + + JSONObject JsonPayload = new JSONObject(); + String payload = ""; + JSONObject paramsJson; + + + // Verify all the mandatory parameters are there + for (String key: mandatoryTestParams){ + if (! params.containsKey(key)){ + throw new APPCException(String.format("Ansible: Mandatory AnsibleAdapter key %s not found in parameters provided by calling agent !", key)); + } + payload = params.get(key); + if (Strings.isNullOrEmpty(payload)){ + throw new APPCException(String.format("Ansible: Mandatory AnsibleAdapter key % value is Null or Emtpy", key)); + } + + JsonPayload.put(key, payload); + } + + // Iterate through optional parameters + // If null or empty omit it + for (String key : optionalTestParams){ + if (params.containsKey(key)){ + payload = params.get(key); + if(!Strings.isNullOrEmpty(payload)){ + + // different cases require different treatment + switch (key){ + case "Timeout": + int Timeout = Integer.parseInt(payload); + if (Timeout < 0){ + throw new NumberFormatException(" : specified negative integer for timeout = " + payload); + } + JsonPayload.put(key, payload); + break; + + case "Version": + JsonPayload.put(key, payload); + break; + + case "LocalParameters": + paramsJson = new JSONObject(payload); + JsonPayload.put(key, paramsJson); + break; + + case "EnvParameters": + paramsJson = new JSONObject(payload); + JsonPayload.put(key, paramsJson); + break; + + case "NodeList": + JSONArray paramsArray = new JSONArray(payload); + JsonPayload.put(key, paramsArray); + break; + + case "FileParameters": + // Files may have strings with newlines. Escape them as appropriate + String formattedPayload = payload.replace("\n", "\\n").replace("\r", "\\r"); + JSONObject fileParams = new JSONObject(formattedPayload); + JsonPayload.put(key, fileParams); + break; + + } + } + } + } + + + // Generate a unique uuid for the test + String ReqId = UUID.randomUUID().toString(); + JsonPayload.put("Id", ReqId); + + return JsonPayload; + + } + + + + // method that validates that the Map has enough information + // to query Ansible server for a result . If so, it + // returns the appropriate url, else an empty string + public String ReqUri_Result(Map <String, String> params) throws APPCException, NumberFormatException{ + + // Mandatory parameters, that must be in the request + String[] mandatoryTestParams = {"AgentUrl", "Id", "User", "Password" }; + + // Verify all the mandatory parameters are there + String payload = ""; + String Uri = ""; + + for (String key: mandatoryTestParams){ + if (! params.containsKey(key)){ + throw new APPCException(String.format("Ansible: Mandatory AnsibleAdapter key %s not found in parameters provided by calling agent !", key)); + } + + payload = params.get(key); + if (Strings.isNullOrEmpty(payload)){ + throw new APPCException(String.format("Ansible: Mandatory AnsibleAdapter key %s not found in parameters provided by calling agent !", key)); + } + + } + + Uri = params.get("AgentUrl") + "?Id=" + params.get("Id") + "&Type=GetResult"; + + return Uri; + + } + + + + // method that validates that the Map has enough information + // to query Ansible server for logs. If so, it populates the appropriate + // returns the appropriate url, else an empty string + public String ReqUri_Output(Map <String, String> params) throws APPCException, NumberFormatException{ + + + // Mandatory parameters, that must be in the request + String[] mandatoryTestParams = {"AgentUrl", "Id", "User", "Password" }; + + // Verify all the mandatory parameters are there + String payload = ""; + String Uri = ""; + + for (String key: mandatoryTestParams){ + if (! params.containsKey(key)){ + throw new APPCException(String.format("Ansible: Mandatory AnsibleAdapter key %s not found in parameters provided by calling agent !", key)); + } + payload = params.get(key); + if (Strings.isNullOrEmpty(payload)){ + throw new APPCException(String.format("Ansible: Mandatory AnsibleAdapter key %s not found in parameters provided by calling agent !", key)); + } + + } + + Uri = params.get("AgentUrl") + "?Id=" + params.get("Id") + "&Type=GetOutput"; + return Uri; + + } + + // method that validates that the Map has enough information + // to query Ansible server for logs. If so, it populates the appropriate + // returns the appropriate url, else an empty string + public String ReqUri_Log(Map <String, String> params) throws APPCException, NumberFormatException{ + + + // Mandatory parameters, that must be in the request + String[] mandatoryTestParams = {"AgentUrl", "Id", "User", "Password" }; + + // Verify all the mandatory parameters are there + String payload = ""; + String Uri = ""; + + for (String key: mandatoryTestParams){ + if (! params.containsKey(key)){ + throw new APPCException(String.format("Ansible: Mandatory AnsibleAdapter key %s not found in parameters provided by calling agent !", key)); + } + payload = params.get(key); + if (Strings.isNullOrEmpty(payload)){ + throw new APPCException(String.format("Ansible: Mandatory AnsibleAdapter key %s not found in parameters provided by calling agent !", key)); + } + + } + + Uri = params.get("AgentUrl") + "?Id=" + params.get("Id") + "&Type=GetLog"; + return Uri; + + } + + + /** + This method parses response from the + Ansible Server when we do a post + and returns an AnsibleResult object + **/ + + public AnsibleResult parsePostResponse(String Input) throws APPCException{ + + AnsibleResult ansibleResult = new AnsibleResult(); + + try{ + //Jsonify it + JSONObject postResponse = new JSONObject(Input); + + // Mandatory keys required are StatusCode and StatusMessage + int Code = postResponse.getInt("StatusCode"); + String Message = postResponse.getString("StatusMessage"); + + + // Status code must must be either 100 (accepted) or 101 (rejected) + boolean valCode = AnsibleResultCodes.CODE.checkValidCode(AnsibleResultCodes.INITRESPONSE.getValue(), Code); + if(!valCode){ + throw new APPCException("Invalid InitResponse code = " + Code + " received. MUST be one of " + AnsibleResultCodes.CODE.getValidCodes(AnsibleResultCodes.INITRESPONSE.getValue()) ); + } + + ansibleResult.setStatusCode(Code); + ansibleResult.setStatusMessage(Message); + + } + catch(JSONException e){ + ansibleResult = new AnsibleResult(600, "Error parsing response = " + Input + ". Error = " + e.getMessage(), ""); + } + + + return ansibleResult; + } + + + /** This method parses response from an Ansible server when we do a GET for a result + and returns an AnsibleResult object + **/ + public AnsibleResult parseGetResponse(String Input) throws APPCException { + + AnsibleResult ansibleResult = new AnsibleResult(); + int FinalCode = AnsibleResultCodes.FINAL_SUCCESS.getValue(); + + + try{ + + //Jsonify it + JSONObject postResponse = new JSONObject(Input); + + // Mandatory keys required are Status and Message + int Code = postResponse.getInt("StatusCode"); + String Message = postResponse.getString("StatusMessage"); + + // Status code must be valid + // Status code must must be either 100 (accepted) or 101 (rejected) + boolean valCode = AnsibleResultCodes.CODE.checkValidCode(AnsibleResultCodes.FINALRESPONSE.getValue(), Code); + + if(!valCode){ + throw new APPCException("Invalid FinalResponse code = " + Code + " received. MUST be one of " + AnsibleResultCodes.CODE.getValidCodes(AnsibleResultCodes.FINALRESPONSE.getValue())); + } + + + ansibleResult.setStatusCode(Code); + ansibleResult.setStatusMessage(Message); + System.out.println("Received response with code = " + Integer.toString(Code) + " Message = " + Message); + + if(! postResponse.isNull("Results")){ + + // Results are available. process them + // Results is a dictionary of the form + // {host :{status:s, group:g, message:m, hostname:h}, ...} + System.out.println("Processing results in response"); + JSONObject results = postResponse.getJSONObject("Results"); + System.out.println("Get JSON dictionary from Results .."); + Iterator<String> hosts = results.keys(); + System.out.println("Iterating through hosts"); + + while(hosts.hasNext()){ + String host = hosts.next(); + System.out.println("Processing host = " + host); + + try{ + JSONObject host_response = results.getJSONObject(host); + int subCode = host_response.getInt("StatusCode"); + String message = host_response.getString("StatusMessage"); + + System.out.println("Code = " + Integer.toString(subCode) + " Message = " + message); + + if(subCode != 200 || ! message.equals("SUCCESS")){ + FinalCode = AnsibleResultCodes.REQ_FAILURE.getValue(); + } + } + catch(JSONException e){ + ansibleResult.setStatusCode(AnsibleResultCodes.INVALID_RESPONSE.getValue()); + ansibleResult.setStatusMessage(String.format("Error processing response message = %s from host %s", results.getString(host), host)); + break; + } + } + + ansibleResult.setStatusCode(FinalCode); + + // We return entire Results object as message + ansibleResult.setResults(results.toString()); + + } + else{ + ansibleResult.setStatusCode(AnsibleResultCodes.INVALID_RESPONSE.getValue()); + ansibleResult.setStatusMessage("Results not found in GET for response"); + } + + + } + catch(JSONException e){ + ansibleResult = new AnsibleResult(AnsibleResultCodes.INVALID_PAYLOAD.getValue(), "Error parsing response = " + Input + ". Error = " + e.getMessage(), ""); + } + + + return ansibleResult; + } + + + +}; + + + diff --git a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleResult.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleResult.java new file mode 100644 index 000000000..3453a845a --- /dev/null +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleResult.java @@ -0,0 +1,85 @@ +/*- + * ============LICENSE_START======================================================= + * APPC + * ================================================================================ + * Copyright (C) 2017 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========================================================= + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ + +package org.openecomp.appc.adapter.ansible.model; + + +/* Simple class to store code and message returned by POST/GET to an Ansible Server */ +public class AnsibleResult{ + private int StatusCode; + private String StatusMessage; + private String Results; + + + public AnsibleResult(){ + StatusCode = -1; + StatusMessage = "UNKNOWN"; + Results = "UNKNOWN"; + + } + + // constructor + public AnsibleResult(int code, String message, String result){ + StatusCode = code; + StatusMessage = message; + Results = result; + } + + //************************************************* + // Various set methods + public void setStatusCode(int code){ + this.StatusCode = code; + } + + public void setStatusMessage(String message){ + this.StatusMessage = message; + } + + public void setResults(String results){ + this.Results = results; + } + + + void set(int code, String message, String results){ + this.StatusCode = code; + this.StatusMessage = message; + this.Results = results; + + } + + //********************************************* + // Various get methods + public int getStatusCode(){ + return this.StatusCode; + } + + public String getStatusMessage(){ + return this.StatusMessage; + } + + public String getResults(){ + return this.Results; + } + + +} + diff --git a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleResultCodes.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleResultCodes.java new file mode 100644 index 000000000..57b679246 --- /dev/null +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleResultCodes.java @@ -0,0 +1,120 @@ +/*- + * ============LICENSE_START======================================================= + * APPC + * ================================================================================ + * Copyright (C) 2017 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========================================================= + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ + +package org.openecomp.appc.adapter.ansible.model; + + +import java.util.*; + +/** + * enum of the various codes that APP-C uses to resolve different + * status of response from Ansible Server + **/ + +public enum AnsibleResultCodes{ + + SUCCESS(400), + KEYSTORE_EXCEPTION(622), + CERTIFICATE_ERROR(610), + IO_EXCEPTION (611), + HOST_UNKNOWN(625), + USER_UNAUTHORIZED(613), + UNKNOWN_EXCEPTION(699), + SSL_EXCEPTION(697), + INVALID_PAYLOAD(698), + INVALID_RESPONSE(601), + PENDING(100), + REJECTED(101), + FINAL_SUCCESS(200), + REQ_FAILURE(401), + MESSAGE(1), + CODE(0), + INITRESPONSE(0), + FINALRESPONSE(1); + + private final Set<Integer> InitCodes = new HashSet<Integer>(Arrays.asList(100, 101)); + private final Set<Integer> FinalCodes = new HashSet<Integer>(Arrays.asList(200, 500)); + private final ArrayList<Set<Integer>>CodeSets = new ArrayList<Set<Integer>>(Arrays.asList(InitCodes, FinalCodes)); + + private final Set<String> MessageSet = new HashSet<String>(Arrays.asList("PENDING", "FINISHED", "TERMINATED")); + + private final int value; + + AnsibleResultCodes(int value){ + this.value = value; + }; + + + public int getValue(){ + return this.value; + } + + + public boolean checkValidCode(int Type, int Code){ + Set<Integer>CodeSet = CodeSets.get(Type); + if (CodeSet.contains(Code)){ + return true; + } + else{ + return false; + } + } + + + public String getValidCodes(int Type){ + Set<Integer>CodeSet = CodeSets.get(Type); + + Iterator iter = CodeSet.iterator(); + String ValidCodes = "[ "; + while(iter.hasNext()){ + ValidCodes = ValidCodes + iter.next().toString() + ","; + } + + ValidCodes = ValidCodes + "]"; + return ValidCodes; + } + + + public boolean checkValidMessage(String Message){ + if (MessageSet.contains(Message)){ + return true; + } + else{ + return false; + } + } + + + + public String getValidMessages(){ + Iterator iter = MessageSet.iterator(); + String ValidMessage = "[ "; + while(iter.hasNext()){ + ValidMessage = ValidMessage + iter.next() + ","; + } + + ValidMessage = ValidMessage + "]"; + return ValidMessage; + } + + +}; diff --git a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleServerEmulator.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleServerEmulator.java new file mode 100644 index 000000000..6ee917c8d --- /dev/null +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleServerEmulator.java @@ -0,0 +1,146 @@ +/*- + * ============LICENSE_START======================================================= + * APPC + * ================================================================================ + * Copyright (C) 2017 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========================================================= + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ + + + + +/* Class to emulate responses from the Ansible Server that is compliant with the APP-C Ansible Server + Interface. Used for jUnit tests to verify code is working. In tests it can be used + as a replacement for methods from ConnectionBuilder class +*/ + +package org.openecomp.appc.adapter.ansible.model; + +import java.util.*; +import java.util.regex.Pattern; +import java.util.regex.Matcher; +import com.google.common.base.Strings; + +import org.json.JSONObject; +import org.json.JSONArray; +import org.json.JSONException; +import org.openecomp.appc.exceptions.APPCException; +import org.openecomp.appc.adapter.ansible.model.AnsibleResult; + +public class AnsibleServerEmulator { + + + private String playbookName = "test_playbook.yaml"; + private String TestId; + + /** + * Method that emulates the response from an Ansible Server + when presented with a request to execute a playbook + Returns an ansible object result. The response code is always the http code 200 (i.e connection successful) + payload is json string as would be sent back by Ansible Server + **/ + + public AnsibleResult Post(String AgentUrl, String payload){ + AnsibleResult result = new AnsibleResult() ; + + try{ + // Request must be a JSON object + + JSONObject message = new JSONObject(payload); + if (message.isNull("Id")){ + RejectRequest(result, "Must provide a valid Id"); + } + else if(message.isNull("PlaybookName")){ + RejectRequest(result, "Must provide a playbook Name"); + } + else if(!message.getString("PlaybookName").equals(playbookName)){ + RejectRequest(result, "Playbook " + message.getString("PlaybookName") + " not found in catalog"); + } + else{ + AcceptRequest(result); + } + } + catch (JSONException e){ + RejectRequest(result, e.getMessage()); + } + + return result; + } + + + /** Method to emulate response from an Ansible + Server when presented with a GET request + Returns an ansibl object result. The response code is always the http code 200 (i.e connection successful) + payload is json string as would be sent back by Ansible Server + + **/ + public AnsibleResult Get(String AgentUrl){ + + // Extract id + Pattern pattern = Pattern.compile(".*?\\?Id=(.*?)&Type.*"); + Matcher matcher = pattern.matcher(AgentUrl); + String Id = ""; + + if (matcher.find()){ + Id = matcher.group(1); + } + + AnsibleResult get_result = new AnsibleResult(); + + JSONObject response = new JSONObject(); + response.put("StatusCode", 200); + response.put("StatusMessage", "FINISHED"); + + JSONObject results = new JSONObject(); + + JSONObject vm_results = new JSONObject(); + vm_results.put("StatusCode", 200); + vm_results.put("StatusMessage", "SUCCESS"); + vm_results.put("Id", Id); + results.put("192.168.1.10", vm_results); + + + response.put("Results", results); + + get_result.setStatusCode(200); + get_result.setStatusMessage(response.toString()); + + return get_result; + + } + + + private void RejectRequest(AnsibleResult result, String Message){ + result.setStatusCode(200); + JSONObject response = new JSONObject(); + response.put("StatusCode", AnsibleResultCodes.REJECTED.getValue()); + response.put("StatusMessage", Message); + result.setStatusMessage(response.toString()); + + } + + private void AcceptRequest(AnsibleResult result){ + result.setStatusCode(200); + JSONObject response = new JSONObject(); + response.put("StatusCode", AnsibleResultCodes.PENDING.getValue()); + response.put("StatusMessage", "PENDING"); + result.setStatusMessage(response.toString()); + + } + +}; + |