From 161df8a94bb3b0c34ed16fd4fdba078bd1eeef9a Mon Sep 17 00:00:00 2001 From: Patrick Brady Date: Wed, 13 Dec 2017 11:14:21 -0800 Subject: Second part of onap rename This is the second commit of the rename. The folder structure is renamed for appc-adapters and appc-config in this commit. Change-Id: Iaa2b8c937ff1ca1b5d1178128961fb115ee65d9b Signed-off-by: Patrick Brady Issue-ID: APPC-13 --- .../appc/adapter/ansible/AnsibleActivator.java | 126 ++++++ .../onap/appc/adapter/ansible/AnsibleAdapter.java | 60 +++ .../adapter/ansible/impl/AnsibleAdapterImpl.java | 501 +++++++++++++++++++++ .../adapter/ansible/impl/ConnectionBuilder.java | 224 +++++++++ .../ansible/model/AnsibleMessageParser.java | 364 +++++++++++++++ .../appc/adapter/ansible/model/AnsibleResult.java | 87 ++++ .../adapter/ansible/model/AnsibleResultCodes.java | 122 +++++ .../ansible/model/AnsibleServerEmulator.java | 148 ++++++ .../appc/adapter/ansible/AnsibleActivator.java | 126 ------ .../appc/adapter/ansible/AnsibleAdapter.java | 60 --- .../adapter/ansible/impl/AnsibleAdapterImpl.java | 501 --------------------- .../adapter/ansible/impl/ConnectionBuilder.java | 224 --------- .../ansible/model/AnsibleMessageParser.java | 364 --------------- .../appc/adapter/ansible/model/AnsibleResult.java | 87 ---- .../adapter/ansible/model/AnsibleResultCodes.java | 122 ----- .../ansible/model/AnsibleServerEmulator.java | 148 ------ 16 files changed, 1632 insertions(+), 1632 deletions(-) create mode 100644 appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/AnsibleActivator.java create mode 100644 appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/AnsibleAdapter.java create mode 100644 appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/impl/AnsibleAdapterImpl.java create mode 100644 appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/impl/ConnectionBuilder.java create mode 100644 appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleMessageParser.java create mode 100644 appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleResult.java create mode 100644 appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleResultCodes.java create mode 100644 appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleServerEmulator.java delete mode 100644 appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/AnsibleActivator.java delete mode 100644 appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/AnsibleAdapter.java delete mode 100644 appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/impl/AnsibleAdapterImpl.java delete mode 100644 appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/impl/ConnectionBuilder.java delete mode 100644 appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleMessageParser.java delete mode 100644 appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleResult.java delete mode 100644 appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleResultCodes.java delete mode 100644 appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleServerEmulator.java (limited to 'appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org') diff --git a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/AnsibleActivator.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/AnsibleActivator.java new file mode 100644 index 000000000..d76cd50f1 --- /dev/null +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/AnsibleActivator.java @@ -0,0 +1,126 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.adapter.ansible; + +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; + +import org.onap.appc.Constants; +import org.onap.appc.adapter.ansible.impl.AnsibleAdapterImpl; +import org.onap.appc.configuration.Configuration; +import org.onap.appc.configuration.ConfigurationFactory; +import org.onap.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 + */ +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 final EELFLogger logger = EELFManager.getInstance().getLogger(AnsibleActivator.class); + + /** + * The configuration object used to configure this bundle + */ + private final Configuration configuration = ConfigurationFactory.getConfiguration(); + + /** + * 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. + *

+ * This method must complete and return to its caller in a timely manner. + *

+ * + * @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. + *

+ * This method must complete and return to its caller in a timely manner. + *

+ * + * @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/onap/appc/adapter/ansible/AnsibleAdapter.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/AnsibleAdapter.java new file mode 100644 index 000000000..7bff184bc --- /dev/null +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/AnsibleAdapter.java @@ -0,0 +1,60 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.adapter.ansible; + +import java.util.Map; + +import org.onap.appc.exceptions.APPCException; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin; +import org.onap.ccsdk.sli.core.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 params, SvcLogicContext ctx) throws SvcLogicException; + + /* Method to get result of a playbook execution request */ + void reqExecResult(Map params, SvcLogicContext ctx) throws SvcLogicException; + + + /* Method to get log of a playbook execution request */ + void reqExecLog(Map params, SvcLogicContext ctx) throws SvcLogicException; + + +} diff --git a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/impl/AnsibleAdapterImpl.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/impl/AnsibleAdapterImpl.java new file mode 100644 index 000000000..0b426bb26 --- /dev/null +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/impl/AnsibleAdapterImpl.java @@ -0,0 +1,501 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.adapter.ansible.impl; + +import java.util.Map; +import java.util.Properties; +import java.lang.*; + + +import org.onap.appc.configuration.Configuration; +import org.onap.appc.configuration.ConfigurationFactory; +import org.onap.appc.exceptions.APPCException; + +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; + + + +import org.json.JSONObject; +import org.json.JSONArray; +import org.json.JSONException; + + + +import org.onap.appc.adapter.ansible.AnsibleAdapter; + +import org.onap.appc.adapter.ansible.model.AnsibleResult; +import org.onap.appc.adapter.ansible.model.AnsibleMessageParser; +import org.onap.appc.adapter.ansible.model.AnsibleResultCodes; +import org.onap.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.onap.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.onap.appc.adapter.ansible.result.code",Integer.toString(code)); + svcLogic.setAttribute("org.onap.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.onap.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.onap.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.onap.appc.adapter.ansible.trustStore"); + String key = props.getProperty("org.onap.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.onap.appc.adapter.ansible.req.code : 100 if successful + // org.onap.appc.adapter.ansible.req.messge : any message + // org.onap.appc.adapter.ansible.req.Id : a unique uuid to reference the request + + public void reqExec(Map 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.onap.appc.adapter.ansible.result.code", Integer.toString(code)); + ctx.setAttribute("org.onap.appc.adapter.ansible.message", message ); + ctx.setAttribute("org.onap.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 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.onap.appc.adapter.ansible.results", results); + doFailure(ctx, code, message ); + return; + } + + + ctx.setAttribute("org.onap.appc.adapter.ansible.result.code", Integer.toString(400)); + ctx.setAttribute("org.onap.appc.adapter.ansible.message",message); + ctx.setAttribute("org.onap.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.onap.appc.adapter.ansible.log + + public void reqExecLog(Map 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.onap.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/onap/appc/adapter/ansible/impl/ConnectionBuilder.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/impl/ConnectionBuilder.java new file mode 100644 index 000000000..e84a85a0c --- /dev/null +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/impl/ConnectionBuilder.java @@ -0,0 +1,224 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.adapter.ansible.impl; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +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 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.onap.appc.exceptions.APPCException; +import org.onap.appc.adapter.ansible.model.AnsibleResult; +import org.onap.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/onap/appc/adapter/ansible/model/AnsibleMessageParser.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleMessageParser.java new file mode 100644 index 000000000..6bebd73fe --- /dev/null +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleMessageParser.java @@ -0,0 +1,364 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.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.onap.appc.exceptions.APPCException; +import org.onap.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 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 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 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 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 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/onap/appc/adapter/ansible/model/AnsibleResult.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleResult.java new file mode 100644 index 000000000..a4bbcbef8 --- /dev/null +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleResult.java @@ -0,0 +1,87 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.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/onap/appc/adapter/ansible/model/AnsibleResultCodes.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleResultCodes.java new file mode 100644 index 000000000..986ad3e6a --- /dev/null +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleResultCodes.java @@ -0,0 +1,122 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.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 InitCodes = new HashSet(Arrays.asList(100, 101)); + private final Set FinalCodes = new HashSet(Arrays.asList(200, 500)); + private final ArrayList>CodeSets = new ArrayList>(Arrays.asList(InitCodes, FinalCodes)); + + private final Set MessageSet = new HashSet(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){ + SetCodeSet = CodeSets.get(Type); + if (CodeSet.contains(Code)){ + return true; + } + else{ + return false; + } + } + + + public String getValidCodes(int Type){ + SetCodeSet = 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/onap/appc/adapter/ansible/model/AnsibleServerEmulator.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleServerEmulator.java new file mode 100644 index 000000000..c430743ad --- /dev/null +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleServerEmulator.java @@ -0,0 +1,148 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + + + + +/* 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.onap.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.onap.appc.exceptions.APPCException; +import org.onap.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()); + + } + +}; + 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 deleted file mode 100644 index d76cd50f1..000000000 --- a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/AnsibleActivator.java +++ /dev/null @@ -1,126 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.adapter.ansible; - -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; - -import org.onap.appc.Constants; -import org.onap.appc.adapter.ansible.impl.AnsibleAdapterImpl; -import org.onap.appc.configuration.Configuration; -import org.onap.appc.configuration.ConfigurationFactory; -import org.onap.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 - */ -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 final EELFLogger logger = EELFManager.getInstance().getLogger(AnsibleActivator.class); - - /** - * The configuration object used to configure this bundle - */ - private final Configuration configuration = ConfigurationFactory.getConfiguration(); - - /** - * 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. - *

- * This method must complete and return to its caller in a timely manner. - *

- * - * @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. - *

- * This method must complete and return to its caller in a timely manner. - *

- * - * @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 deleted file mode 100644 index 7bff184bc..000000000 --- a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/AnsibleAdapter.java +++ /dev/null @@ -1,60 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.adapter.ansible; - -import java.util.Map; - -import org.onap.appc.exceptions.APPCException; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; -import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin; -import org.onap.ccsdk.sli.core.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 params, SvcLogicContext ctx) throws SvcLogicException; - - /* Method to get result of a playbook execution request */ - void reqExecResult(Map params, SvcLogicContext ctx) throws SvcLogicException; - - - /* Method to get log of a playbook execution request */ - void reqExecLog(Map 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 deleted file mode 100644 index 0b426bb26..000000000 --- a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/impl/AnsibleAdapterImpl.java +++ /dev/null @@ -1,501 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.adapter.ansible.impl; - -import java.util.Map; -import java.util.Properties; -import java.lang.*; - - -import org.onap.appc.configuration.Configuration; -import org.onap.appc.configuration.ConfigurationFactory; -import org.onap.appc.exceptions.APPCException; - -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; - - - -import org.json.JSONObject; -import org.json.JSONArray; -import org.json.JSONException; - - - -import org.onap.appc.adapter.ansible.AnsibleAdapter; - -import org.onap.appc.adapter.ansible.model.AnsibleResult; -import org.onap.appc.adapter.ansible.model.AnsibleMessageParser; -import org.onap.appc.adapter.ansible.model.AnsibleResultCodes; -import org.onap.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.onap.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.onap.appc.adapter.ansible.result.code",Integer.toString(code)); - svcLogic.setAttribute("org.onap.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.onap.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.onap.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.onap.appc.adapter.ansible.trustStore"); - String key = props.getProperty("org.onap.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.onap.appc.adapter.ansible.req.code : 100 if successful - // org.onap.appc.adapter.ansible.req.messge : any message - // org.onap.appc.adapter.ansible.req.Id : a unique uuid to reference the request - - public void reqExec(Map 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.onap.appc.adapter.ansible.result.code", Integer.toString(code)); - ctx.setAttribute("org.onap.appc.adapter.ansible.message", message ); - ctx.setAttribute("org.onap.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 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.onap.appc.adapter.ansible.results", results); - doFailure(ctx, code, message ); - return; - } - - - ctx.setAttribute("org.onap.appc.adapter.ansible.result.code", Integer.toString(400)); - ctx.setAttribute("org.onap.appc.adapter.ansible.message",message); - ctx.setAttribute("org.onap.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.onap.appc.adapter.ansible.log - - public void reqExecLog(Map 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.onap.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 deleted file mode 100644 index e84a85a0c..000000000 --- a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/impl/ConnectionBuilder.java +++ /dev/null @@ -1,224 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.adapter.ansible.impl; - -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -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 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.onap.appc.exceptions.APPCException; -import org.onap.appc.adapter.ansible.model.AnsibleResult; -import org.onap.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 deleted file mode 100644 index 6bebd73fe..000000000 --- a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleMessageParser.java +++ /dev/null @@ -1,364 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.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.onap.appc.exceptions.APPCException; -import org.onap.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 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 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 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 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 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 deleted file mode 100644 index a4bbcbef8..000000000 --- a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleResult.java +++ /dev/null @@ -1,87 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.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 deleted file mode 100644 index 986ad3e6a..000000000 --- a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleResultCodes.java +++ /dev/null @@ -1,122 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.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 InitCodes = new HashSet(Arrays.asList(100, 101)); - private final Set FinalCodes = new HashSet(Arrays.asList(200, 500)); - private final ArrayList>CodeSets = new ArrayList>(Arrays.asList(InitCodes, FinalCodes)); - - private final Set MessageSet = new HashSet(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){ - SetCodeSet = CodeSets.get(Type); - if (CodeSet.contains(Code)){ - return true; - } - else{ - return false; - } - } - - - public String getValidCodes(int Type){ - SetCodeSet = 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 deleted file mode 100644 index c430743ad..000000000 --- a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/openecomp/appc/adapter/ansible/model/AnsibleServerEmulator.java +++ /dev/null @@ -1,148 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - - - - -/* 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.onap.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.onap.appc.exceptions.APPCException; -import org.onap.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()); - - } - -}; - -- cgit 1.2.3-korg