diff options
Diffstat (limited to 'adaptors/chef-adaptor/chef-adaptor-bundle')
28 files changed, 4206 insertions, 0 deletions
diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/pom.xml b/adaptors/chef-adaptor/chef-adaptor-bundle/pom.xml new file mode 100644 index 000000000..6b1b2a1d9 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/pom.xml @@ -0,0 +1,198 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ============LICENSE_START======================================================= + ONAP : APPC + ================================================================================ + Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + ================================================================================ + Copyright (C) 2017 Amdocs + ============================================================================= + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + ============LICENSE_END========================================================= + --> +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.onap.ccsdk.parent</groupId> + <artifactId>binding-parent</artifactId> + <version>2.2.0-SNAPSHOT</version> + <relativePath/> + </parent> + + <groupId>org.onap.ccsdk.sli.adaptors</groupId> + <artifactId>chef-adaptor-bundle</artifactId> + <version>1.3.0-SNAPSHOT</version> + <packaging>bundle</packaging> + + <name>ccsdk-sli-adaptors :: ${project.artifactId}</name> + + <properties> + <cdp.pal.version>1.1.25.8-oss</cdp.pal.version> + </properties> + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.onap.ccsdk.sli.core</groupId> + <artifactId>sli-core-artifacts</artifactId> + <version>${project.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + <dependencies> + <dependency> + <groupId>org.json</groupId> + <artifactId>json</artifactId> + </dependency> + <dependency> + <groupId>commons-codec</groupId> + <artifactId>commons-codec</artifactId> + </dependency> + <dependency> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpcore</artifactId> + </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + </dependency> + <dependency> + <groupId>org.bouncycastle</groupId> + <artifactId>bcpkix-jdk15on</artifactId> + </dependency> + <dependency> + <groupId>org.bouncycastle</groupId> + <artifactId>bcprov-jdk15on</artifactId> + </dependency> + <dependency> + <groupId>javax</groupId> + <artifactId>javaee-api</artifactId> + </dependency> + <dependency> + <groupId>com.att.cdp</groupId> + <artifactId>cdp-pal-common</artifactId> + <version>${cdp.pal.version}</version> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>com.att.cdp</groupId> + <artifactId>cdp-pal-openstack</artifactId> + <version>${cdp.pal.version}</version> + <scope>compile</scope> + <exclusions> + <exclusion> + <groupId>com.att.cdp</groupId> + <artifactId>cdp-pal-common</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>javax.ws.rs</groupId> + <artifactId>javax.ws.rs-api</artifactId> + </dependency> + <dependency> + <groupId>javax.xml.bind</groupId> + <artifactId>jaxb-api</artifactId> + </dependency> + <dependency> + <groupId>org.onap.ccsdk.sli.core</groupId> + <artifactId>sli-provider</artifactId> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>jcl-over-slf4j</artifactId> + </dependency> + <dependency> + <groupId>org.mariadb.jdbc</groupId> + <artifactId>mariadb-java-client</artifactId> + </dependency> + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + </dependency> + <!-- Needed to run test cases --> + <dependency> + <groupId>org.glassfish.jersey.core</groupId> + <artifactId>jersey-common</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.codehaus.jackson</groupId> + <artifactId>jackson-jaxrs</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <!-- Specifically using mockito version 1.10.19 to make sure junit works --> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <version>1.10.19</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-reflect</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-api-mockito2</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <Bundle-SymbolicName>chef-adaptor</Bundle-SymbolicName> + <Export-Package>org.onap.ccsdk.sli.adaptors.chef</Export-Package> + <Import-Package> + org.onap.ccsdk.sli.core.sli.*,org.osgi.framework.*,org.slf4j.*,javax.net.*,org.xml.sax.*,javax.naming.*,javax.security.* + </Import-Package> + <Embed-Dependency> + *;scope=compile|runtime;artifactId=!sli-common|!common-bundle|org.eclipse.osgi|slf4j-api|jcl-over-slf4j|mariadb-java-client|xml-apis + </Embed-Dependency> + <Embed-Transitive>true</Embed-Transitive> + </instructions> + <manifestLocation>${project.basedir}/src/main/resources/META-INF</manifestLocation> + </configuration> + </plugin> + </plugins> + </build> +</project> diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/ChefAdaptor.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/ChefAdaptor.java new file mode 100644 index 000000000..33b71a8a7 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/ChefAdaptor.java @@ -0,0 +1,216 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef; + +import java.util.Map; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin; + +/** + * This interface defines the operations that the provider adaptor exposes. + * <p> + * This interface defines static constant property values that can be used to configure the adaptor. These constants are + * prefixed with the name PROPERTY_ to indicate that they are configuration properties. These properties are read from + * the configuration file for the adaptor and are used to define the providers, identity service URLs, and other + * information needed by the adaptor to interface with an IaaS provider. + * </p> + */ +public interface ChefAdaptor extends SvcLogicJavaPlugin { + + /** + * The type of provider to be accessed to locate and operate on a virtual machine instance. This is used to load the + * correct provider support through the CDP IaaS abstraction layer and can be OpenStackProvider, BareMetalProvider, + * or any other supported provider type. + */ + static final String PROPERTY_PROVIDER_TYPE = "org.onap.appc.provider.type"; + + /** + * The adaptor maintains a cache of providers organized by the name of the provider, not its type. This is + * equivalent to the system or installation name. All regions within the same installation are assumed to be the + * same type. + */ + static final String PROPERTY_PROVIDER_NAME = "org.onap.appc.provider.name"; + + /** + * The fully-qualified URL of the instance to be manipulated as it is known to the provider. + */ + static final String PROPERTY_INSTANCE_URL = "org.onap.appc.instance.url"; + + /** + * The fully-qualified URL of the instance to be manipulated as it is known to the provider. + */ + static final String PROPERTY_IDENTITY_URL = "org.onap.appc.identity.url"; + + /** + * This method is used to restart an existing virtual machine given the fully qualified URL of the machine. + * <p> + * This method is invoked from a directed graph as an <code>Executor</code> node. This means that the parameters + * passed to the method are passed as properties in a map. This method expects the following properties to be + * defined: + * <dl> + * <dt>org.onap.appc.provider.type</dt> + * <dd>The appropriate provider type, such as <code>OpenStackProvider</code>. This is used by the CDP IaaS + * abstraction layer to dynamically load and open a connection to the appropriate provider type. All CDP supported + * provider types are legal.</dd> + * <dt>org.onap.appc.instance.url</dt> + * <dd>The fully qualified URL of the instance to be restarted, as it is known to the provider (i.e., the self-link + * URL of the server)</dd> + * </dl> + * </p> + * + * @param properties + * A map of name-value pairs that supply the parameters needed by this method. The properties needed are + * defined above. + * @param context + * The service logic context of the graph being executed. + * @return The <code>Server</code> object that represents the VM being restarted. The returned server object can be + * inspected for the final state of the server once the restart has been completed. The method does not + * return until the restart has either completed or has failed. + * @throws SvcLogicException + * If the server cannot be restarted for some reason + */ + // Server restartServer(Map<String, String> properties, SvcLogicContext context) throws SvcLogicException; + + /** + * This method is used to stop the indicated server + * <p> + * This method is invoked from a directed graph as an <code>Executor</code> node. This means that the parameters + * passed to the method are passed as properties in a map. This method expects the following properties to be + * defined: + * <dl> + * <dt>org.onap.appc.provider.type</dt> + * <dd>The appropriate provider type, such as <code>OpenStackProvider</code>. This is used by the CDP IaaS + * abstraction layer to dynamically load and open a connection to the appropriate provider type. All CDP supported + * provider types are legal.</dd> + * <dt>org.onap.appc.instance.url</dt> + * <dd>The fully qualified URL of the instance to be stopped, as it is known to the provider (i.e., the self-link + * URL of the server)</dd> + * </dl> + * </p> + * + * @param properties + * A map of name-value pairs that supply the parameters needed by this method. The properties needed are + * defined above. + * @param context + * The service logic context of the graph being executed. + * @return The <code>Server</code> object that represents the VM being stopped. The returned server object can be + * inspected for the final state of the server once the stop has been completed. The method does not return + * until the stop has either completed or has failed. + * @throws SvcLogicException + * If the server cannot be stopped for some reason + */ + //Server stopServer(Map<String, String> properties, SvcLogicContext context) throws SvcLogicException; + + /** + * This method is used to start the indicated server + * <p> + * This method is invoked from a directed graph as an <code>Executor</code> node. This means that the parameters + * passed to the method are passed as properties in a map. This method expects the following properties to be + * defined: + * <dl> + * <dt>org.onap.appc.provider.type</dt> + * <dd>The appropriate provider type, such as <code>OpenStackProvider</code>. This is used by the CDP IaaS + * abstraction layer to dynamically load and open a connection to the appropriate provider type. All CDP supported + * provider types are legal.</dd> + * <dt>org.onap.appc.instance.url</dt> + * <dd>The fully qualified URL of the instance to be started, as it is known to the provider (i.e., the self-link + * URL of the server)</dd> + * </dl> + * </p> + * + * @param properties + * A map of name-value pairs that supply the parameters needed by this method. The properties needed are + * defined above. + * @param context + * The service logic context of the graph being executed. + * @return The <code>Server</code> object that represents the VM being started. The returned server object can be + * inspected for the final state of the server once the start has been completed. The method does not return + * until the start has either completed or has failed. + * @throws SvcLogicException + * If the server cannot be started for some reason + */ + // Server startServer(Map<String, String> properties, SvcLogicContext context) throws SvcLogicException; + + /** + * This method is used to rebuild the indicated server + * <p> + * This method is invoked from a directed graph as an <code>Executor</code> node. This means that the parameters + * passed to the method are passed as properties in a map. This method expects the following properties to be + * defined: + * <dl> + * <dt>org.onap.appc.provider.type</dt> + * <dd>The appropriate provider type, such as <code>OpenStackProvider</code>. This is used by the CDP IaaS + * abstraction layer to dynamically load and open a connection to the appropriate provider type. All CDP supported + * provider types are legal.</dd> + * <dt>org.onap.appc.instance.url</dt> + * <dd>The fully qualified URL of the instance to be rebuilt, as it is known to the provider (i.e., the self-link + * URL of the server)</dd> + * </dl> + * </p> + * + * @param properties A map of name-value pairs that supply the parameters needed by this method. The properties needed are + * defined above. + * @param context The service logic context of the graph being executed. + * + * @return The <code>Server</code> object that represents the VM being rebuilt. The returned server object can be + * inspected for the final state of the server once the rebuild has been completed. The method does not + * return until the rebuild has either completed or has failed. + * + * @throws SvcLogicException If the server cannot be rebuilt for some reason + */ + // Server rebuildServer(Map<String, String> properties, SvcLogicContext context) throws SvcLogicException; + + // Server evacuateServer(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException; + + //Server migrateServer(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException; + void trigger(Map<String, String> params, SvcLogicContext ctx); + + void chefGet(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException; + + void chefPut(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException; + + void chefPost(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException; + + void chefDelete(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException; + + void nodeObejctBuilder(Map<String, String> params, SvcLogicContext ctx); + + void checkPushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException; + + void pushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException; + + void retrieveData(Map<String, String> params, SvcLogicContext ctx); + + void combineStrings(Map<String, String> params, SvcLogicContext ctx); + + void vnfcEnvironment(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException; + + void vnfcNodeobjects(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException; + + void vnfcPushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException; + + void fetchResults(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException; + +} diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/ChefApiClientFactory.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/ChefApiClientFactory.java new file mode 100644 index 000000000..07745ed1c --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/ChefApiClientFactory.java @@ -0,0 +1,72 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.chefclient; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.google.common.collect.ImmutableMap; +import java.io.File; +import java.io.IOException; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; +import org.apache.http.client.HttpClient; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.ssl.SSLContexts; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.api.ChefApiClient; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.ChefApiClientImpl; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.ChefApiHeaderFactory; + +public class ChefApiClientFactory { + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(ChefApiClientFactory.class); + + private HttpClient httpClient = createChefHttpClient(); + private ChefApiHeaderFactory chefApiHeaderFactory = new ChefApiHeaderFactory(); + + private HttpClient createChefHttpClient() { + String trustStoreFileName = "/opt/onap/chef/chefServerSSL.jks"; + char[] trustStoreCreds = "adminadmin".toCharArray(); + try { + SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( + SSLContexts.custom().loadTrustMaterial(new File(trustStoreFileName), trustStoreCreds).build(), + SSLConnectionSocketFactory.getDefaultHostnameVerifier()); + return HttpClients.custom().setSSLSocketFactory(sslsf).build(); + } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException | CertificateException + | IOException e) { + logger.error(e.getMessage(), e); + } + return null; + } + + public ChefApiClient create(String endPoint, String organizations, String userId, String pemPath) { + return new ChefApiClientImpl(httpClient, endPoint, organizations, (methodName, requestPath, body) -> chefApiHeaderFactory + .create(methodName, requestPath, body, userId, organizations, pemPath)); + } + + public ChefApiClient create(String endPoint, String organizations) { + return new ChefApiClientImpl(httpClient, endPoint, organizations, (methodName, requestPath, body) -> ImmutableMap.of()); + } + +} diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/api/ChefApiClient.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/api/ChefApiClient.java new file mode 100644 index 000000000..947888d34 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/api/ChefApiClient.java @@ -0,0 +1,33 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.chefclient.api; + +public interface ChefApiClient { + + org.onap.ccsdk.sli.adaptors.chef.chefclient.api.ChefResponse get(String path); + + org.onap.ccsdk.sli.adaptors.chef.chefclient.api.ChefResponse delete(String path); + + org.onap.ccsdk.sli.adaptors.chef.chefclient.api.ChefResponse post(String path, String body); + + org.onap.ccsdk.sli.adaptors.chef.chefclient.api.ChefResponse put(String path, String body); + +} diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/api/ChefResponse.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/api/ChefResponse.java new file mode 100644 index 000000000..c4615fc77 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/api/ChefResponse.java @@ -0,0 +1,47 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * + * Modifications Copyright © 2018 IBM. + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.chefclient.api; + +public final class ChefResponse { + + private final String body; + private final int status; + + private ChefResponse(int status, String body) { + this.status = status; + this.body = body; + } + + public static ChefResponse create(int status, String body) { + return new ChefResponse(status, body); + } + + public int getStatusCode() { + return status; + } + + public String getBody() { + return body; + } + +} diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/ChefApiClientImpl.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/ChefApiClientImpl.java new file mode 100644 index 000000000..7229b0d10 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/ChefApiClientImpl.java @@ -0,0 +1,117 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Modifications Copyright (C) 2019 IBM + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.chefclient.impl; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import java.io.IOException; +import java.net.URISyntaxException; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.client.HttpClient; +import org.apache.http.util.EntityUtils; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.api.ChefApiClient; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.api.ChefResponse; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.ChefRequestBuilder.OngoingRequestBuilder; + +public class ChefApiClientImpl implements ChefApiClient { + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(ChefApiClientImpl.class); + private final HttpClient httpClient; + private final String endpoint; + private final String organization; + private final org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.HttpHeaderFactory httpHeaderFactory; + + public ChefApiClientImpl(HttpClient httpClient, String endpoint, String organization, + org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.HttpHeaderFactory httpHeaderFactory) { + this.httpClient = httpClient; + this.endpoint = endpoint; + this.organization = organization; + this.httpHeaderFactory = httpHeaderFactory; + } + + @Override + public ChefResponse get(String path) { + OngoingRequestBuilder requestBuilder = org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.ChefRequestBuilder.newRequestTo(endpoint) + .httpGet() + .withPath(getPath(path)) + .withHeaders(httpHeaderFactory.create("GET", path, "")); + return execute(requestBuilder); + } + + @Override + public ChefResponse delete(String path) { + OngoingRequestBuilder requestBuilder = org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.ChefRequestBuilder.newRequestTo(endpoint) + .httpDelete() + .withPath(getPath(path)) + .withHeaders(httpHeaderFactory.create("DELETE", path, "")); + return execute(requestBuilder); + } + + @Override + public ChefResponse post(String path, String body) { + OngoingRequestBuilder requestBuilder = org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.ChefRequestBuilder.newRequestTo(endpoint) + .httpPost(body) + .withPath(getPath(path)) + .withHeaders(httpHeaderFactory.create("POST", path, body)); + return execute(requestBuilder); + } + + @Override + public ChefResponse put(String path, String body) { + OngoingRequestBuilder requestBuilder = org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.ChefRequestBuilder.newRequestTo(endpoint) + .httpPut(body) + .withPath(getPath(path)) + .withHeaders(httpHeaderFactory.create("PUT", path, body)); + logger.info("request: PATH: " + path + " body: " + body); + return execute(requestBuilder); + } + + private ChefResponse execute(OngoingRequestBuilder chefRequest) { + try { + if (httpClient == null) { + return ChefResponse.create(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Could not create http client for chef"); + } + HttpResponse response = httpClient.execute(chefRequest.build()); + int statusCode = response.getStatusLine().getStatusCode(); + HttpEntity httpEntity = response.getEntity(); + String responseBody = EntityUtils.toString(httpEntity); + return ChefResponse.create(statusCode, responseBody); + } catch (IOException ex) { + logger.error("Error occured while reading response", ex.getMessage(), ex); + return ChefResponse.create(HttpStatus.SC_INTERNAL_SERVER_ERROR, ex.getMessage()); + } catch (URISyntaxException ex) { + logger.error("Malformed URL", ex.getMessage(), ex); + return ChefResponse.create(HttpStatus.SC_INTERNAL_SERVER_ERROR, ex.getMessage()); + } + } + + private String getPath(String path) { + return "/organizations/" + organization + path; + } + +} + diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/ChefApiHeaderFactory.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/ChefApiHeaderFactory.java new file mode 100644 index 000000000..14485e554 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/ChefApiHeaderFactory.java @@ -0,0 +1,69 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Modification Copyright (C) 2019 IBM + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.chefclient.impl; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMap.Builder; +import java.util.Date; + +public class ChefApiHeaderFactory { + + private org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.FormattedTimestamp formattedTimestamp = new org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.FormattedTimestamp(); + + public ImmutableMap<String, String> create(String methodName, String path, String body, String userId, + String organizations, String pemPath) { + + String hashedBody = org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.Utils.sha1AndBase64(body); + String timeStamp = formattedTimestamp.format(new Date()); + + Builder<String, String> builder = ImmutableMap.builder(); + builder + .put("Content-type", "application/json") + .put("Accept", "application/json") + .put("X-Ops-Timestamp", timeStamp) + .put("X-Ops-UserId", userId) + .put("X-Chef-Version", "12.4.1") + .put("X-Ops-Content-Hash", hashedBody) + .put("X-Ops-Sign", "version=1.0") + .build(); + + String hashedPath = org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.Utils.sha1AndBase64("/organizations/" + organizations + path); + + StringBuilder sb = new StringBuilder(); + sb.append("Method:").append(methodName).append("\n"); + sb.append("Hashed Path:").append(hashedPath).append("\n"); + sb.append("X-Ops-Content-Hash:").append(hashedBody).append("\n"); + sb.append("X-Ops-Timestamp:").append(timeStamp).append("\n"); + sb.append("X-Ops-UserId:").append(userId); + + String authString = org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.Utils.signWithRSA(sb.toString(), pemPath); + String[] authHeaders = org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.Utils.splitAs60(authString); + + for (int i = 0; i < authHeaders.length; i++) { + builder.put("X-Ops-Authorization-" + (i + 1), authHeaders[i]); + } + + return builder.build(); + } + +} diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/ChefRequestBuilder.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/ChefRequestBuilder.java new file mode 100644 index 000000000..3a79af35f --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/ChefRequestBuilder.java @@ -0,0 +1,115 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.chefclient.impl; + +import com.google.common.collect.ImmutableMap; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Map.Entry; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.StringEntity; + +final class ChefRequestBuilder { + + private ChefRequestBuilder() { + } + + static OngoingRequestBuilder newRequestTo(String endPoint) { + return new OngoingRequestBuilder(endPoint); + } + + static class OngoingRequestBuilder { + + private HttpRequestBase httpRequestBase; + private String endPoint; + private String path; + private ImmutableMap<String, String> headers; + + private OngoingRequestBuilder(String endPoint) { + this.endPoint = endPoint; + } + + public OngoingRequestBuilder withPath(String path) { + this.path = path; + return this; + } + + public OngoingRequestBuilder httpGet() { + httpRequestBase = new HttpGet(); + return this; + } + + public OngoingRequestBuilder httpDelete() { + httpRequestBase = new HttpDelete(); + return this; + } + + public OngoingRequestBuilder httpPost(String body) { + HttpPost httpPost = new HttpPost(); + httpPost.setEntity(toEntity(body)); + httpRequestBase = httpPost; + return this; + } + + public OngoingRequestBuilder httpPut(String body) { + HttpPut httpPut = new HttpPut(); + httpPut.setEntity(toEntity(body)); + httpRequestBase = httpPut; + return this; + } + + private StringEntity toEntity(String body) { + StringEntity stringEntity = new StringEntity(body, "UTF-8"); + stringEntity.setContentType("application/json"); + return stringEntity; + } + + public OngoingRequestBuilder withHeaders(ImmutableMap<String, String> headers) { + this.headers = headers; + return this; + } + + public HttpRequestBase build() throws URISyntaxException { + setRequestUri(); + setRequestHeaders(); + return httpRequestBase; + } + + private void setRequestUri() throws URISyntaxException { + URI fullPath = new URIBuilder(endPoint).setPath(path).build(); + httpRequestBase.setURI(fullPath); + } + + private void setRequestHeaders() { + for (Entry<String, String> entry : headers.entrySet()) { + httpRequestBase.addHeader(entry.getKey(), entry.getValue()); + } + } + + } + +} diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/FormattedTimestamp.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/FormattedTimestamp.java new file mode 100644 index 000000000..0d86b6e64 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/FormattedTimestamp.java @@ -0,0 +1,38 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.chefclient.impl; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +class FormattedTimestamp { + + String format(Date date) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + sdf.setTimeZone(TimeZone.getTimeZone("UTC")); + String timeStamp = sdf.format(date); + timeStamp = timeStamp.replace(" ", "T"); + timeStamp = timeStamp + "Z"; + return timeStamp; + } + +}
\ No newline at end of file diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/HttpHeaderFactory.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/HttpHeaderFactory.java new file mode 100644 index 000000000..2ba5ee834 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/HttpHeaderFactory.java @@ -0,0 +1,30 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.chefclient.impl; + +import com.google.common.collect.ImmutableMap; + +@FunctionalInterface +public interface HttpHeaderFactory { + + ImmutableMap<String, String> create(String methodName, String requestPath, String body); + +} diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/Utils.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/Utils.java new file mode 100644 index 000000000..8f12ab13a --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/Utils.java @@ -0,0 +1,101 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Modifications Copyright (C) 2019 IBM + * ============================================================================= + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.chefclient.impl; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.PrivateKey; +import java.security.Security; +import java.security.Signature; +import java.security.SignatureException; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.openssl.PEMKeyPair; +import org.bouncycastle.openssl.PEMParser; +import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; +import org.bouncycastle.util.encoders.Base64; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Utils { + private static final Logger logger = LoggerFactory.getLogger(Utils.class); + + private Utils() { + } + + public static String sha1AndBase64(String inStr) { + MessageDigest md = null; + byte[] outbty = null; + try { + md = MessageDigest.getInstance("SHA-1"); + byte[] digest = md.digest(inStr.getBytes()); + outbty = Base64.encode(digest); + } catch (NoSuchAlgorithmException nsae) { + logger.error(nsae.getMessage()); + } + return new String(outbty); + } + + public static String signWithRSA(String inStr, String pemPath) { + byte[] outStr = null; + try (BufferedReader br = new BufferedReader(new FileReader(pemPath))) { + Security.addProvider(new BouncyCastleProvider()); + PEMParser pemParser = new PEMParser(br); + JcaPEMKeyConverter converter = new JcaPEMKeyConverter(); + Object object = pemParser.readObject(); + KeyPair kp = converter.getKeyPair((PEMKeyPair) object); + PrivateKey privateKey = kp.getPrivate(); + Signature instance = Signature.getInstance("RSA"); + instance.initSign(privateKey); + instance.update(inStr.getBytes()); + byte[] signature = instance.sign(); + outStr = Base64.encode(signature); + } catch (InvalidKeyException | IOException | SignatureException | NoSuchAlgorithmException e) { + logger.error(e.getMessage()); + } + return new String(outStr); + } + + public static String[] splitAs60(String inStr) { + int count = inStr.length() / 60; + String[] out = new String[count + 1]; + for (int i = 0; i < count; i++) { + String tmp = inStr.substring(i * 60, i * 60 + 60); + out[i] = tmp; + } + if (inStr.length() > count * 60) { + String tmp = inStr.substring(count * 60, inStr.length()); + out[count] = tmp; + } + return out; + } + +} diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorFactory.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorFactory.java new file mode 100644 index 000000000..59851b463 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorFactory.java @@ -0,0 +1,35 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.impl; + +import org.onap.ccsdk.sli.adaptors.chef.ChefAdaptor; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.ChefApiClientFactory; + +public class ChefAdaptorFactory { + + private ChefApiClientFactory chefApiClientFactory = new ChefApiClientFactory(); + private org.onap.ccsdk.sli.adaptors.chef.impl.PrivateKeyChecker privateKeyChecker = new org.onap.ccsdk.sli.adaptors.chef.impl.PrivateKeyChecker(); + + public ChefAdaptor create() { + return new org.onap.ccsdk.sli.adaptors.chef.impl.ChefAdaptorImpl(chefApiClientFactory, privateKeyChecker); + } + +} diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorImpl.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorImpl.java new file mode 100644 index 000000000..1582e518c --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorImpl.java @@ -0,0 +1,674 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Modifications Copyright (C) 2019 IBM + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.impl; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import org.apache.commons.lang.StringUtils; +import org.json.JSONException; +import org.json.JSONObject; +import org.onap.ccsdk.sli.adaptors.chef.ChefAdaptor; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.ChefApiClientFactory; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.api.ChefApiClient; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.api.ChefResponse; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; + +/** + * This class implements the {@link ChefAdaptor} interface. This interface + * defines the behaviors that our service provides. + */ +public class ChefAdaptorImpl implements ChefAdaptor { + + @SuppressWarnings("nls") + public static final String MDC_ADAPTOR = "adaptor"; + @SuppressWarnings("nls") + public static final String MDC_SERVICE = "service"; + @SuppressWarnings("nls") + public static final String OUTCOME_FAILURE = "failure"; + @SuppressWarnings("nls") + public static final String OUTCOME_SUCCESS = "success"; + @SuppressWarnings("nls") + public static final String PROPERTY_PROVIDER = "provider"; + @SuppressWarnings("nls") + public static final String PROPERTY_PROVIDER_IDENTITY = "identity"; + @SuppressWarnings("nls") + public static final String PROPERTY_PROVIDER_NAME = "name"; + @SuppressWarnings("nls") + public static final String PROPERTY_PROVIDER_TENANT = "tenant"; + @SuppressWarnings("nls") + public static final String PROPERTY_PROVIDER_TENANT_NAME = "name"; + @SuppressWarnings("nls") + public static final String PROPERTY_PROVIDER_TENANT_PASSWORD = "password"; // NOSONAR + @SuppressWarnings("nls") + public static final String PROPERTY_PROVIDER_TENANT_USERID = "userid"; + @SuppressWarnings("nls") + public static final String PROPERTY_PROVIDER_TYPE = "type"; + private static final EELFLogger logger = EELFManager.getInstance().getLogger(ChefAdaptorImpl.class); + private static final String CANNOT_FIND_PRIVATE_KEY_STR = "Cannot find the private key in the APPC file system, please load the private key to "; + private static final String POSTING_REQUEST_JSON_ERROR_STR = "Error posting request due to invalid JSON block: "; + private static final String POSTING_REQUEST_ERROR_STR = "Error posting request: "; + private static final String CHEF_CLIENT_RESULT_CODE_STR = "chefClientResult.code"; + private static final String CHEF_SERVER_RESULT_CODE_STR = "chefServerResult.code"; + private static final String CHEF_CLIENT_RESULT_MSG_STR = "chefClientResult.message"; + private static final String CHEF_SERVER_RESULT_MSG_STR = "chefServerResult.message"; + private static final String CHEF_ACTION_STR = "chefAction"; + private static final String NODE_LIST_STR = "NodeList"; + private static final Integer STATUS_OK = 200; + private static final Integer STATUS_PUSHJOBCHECK = 201; + private static final Integer PUSHJOBSTATUS = 202; + private static final Integer KEY_NOTFOUND = 500; + private static final Integer NO_ENVIRONMENT = 404; + private static final Integer APPC_ERRORCODE = 401; + private final ChefApiClientFactory chefApiClientFactory; + private final org.onap.ccsdk.sli.adaptors.chef.impl.PrivateKeyChecker privateKeyChecker; + // chef server Initialize variable + private String username = StringUtils.EMPTY; + private String clientPrivatekey = StringUtils.EMPTY; + private String chefserver = StringUtils.EMPTY; + private String serverAddress = StringUtils.EMPTY; + private String organizations = StringUtils.EMPTY; + + ChefAdaptorImpl(ChefApiClientFactory chefApiClientFactory, org.onap.ccsdk.sli.adaptors.chef.impl.PrivateKeyChecker privateKeyChecker) { + this.chefApiClientFactory = chefApiClientFactory; + this.privateKeyChecker = privateKeyChecker; + logger.info("Initialize Chef Adaptor"); + } + + @SuppressWarnings("nls") + @Override + public void vnfcEnvironment(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException { + int code; + logger.info("environment of VNF-C"); + chefInfo(params, ctx); + String env = params.get("Environment"); + logger.info("Environmnet" + env); + if (env.equals(StringUtils.EMPTY)) { + chefServerResult(ctx, STATUS_OK, "Skip Environment block "); + } else { + String message; + if (privateKeyChecker.doesExist(clientPrivatekey)) { + try { + JSONObject envJ = new JSONObject(env); + String envName = envJ.getString("name"); + // update the details of an environment on the Chef server. + ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, + clientPrivatekey); + ChefResponse chefResponse = chefApiClient.put("/environments/" + envName, env); + code = chefResponse.getStatusCode(); + message = chefResponse.getBody(); + if (code == NO_ENVIRONMENT) { + // need create a new environment + chefResponse = chefApiClient.post("/environments", env); + code = chefResponse.getStatusCode(); + message = chefResponse.getBody(); + logger.info("requestbody {}", chefResponse.getBody()); + } + chefServerResult(ctx, code, message); + } catch (JSONException e) { + code = APPC_ERRORCODE; + logger.error(POSTING_REQUEST_JSON_ERROR_STR, e); + doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + e.getMessage()); + } catch (Exception e) { + code = APPC_ERRORCODE; + logger.error(POSTING_REQUEST_ERROR_STR + "vnfcEnvironment", e); + doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + "vnfcEnvironment" + e.getMessage()); + } + } else { + code = KEY_NOTFOUND; + message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey; + doFailure(ctx, code, message); + } + } + } + + @SuppressWarnings("nls") + @Override + public void vnfcNodeobjects(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException { + logger.info("update the nodeObjects of VNF-C"); + int code; + final String LOG_ERR_METHOD_STR = "vnfcNodeobjects"; + try { + chefInfo(params, ctx); + String nodeListS = params.get(NODE_LIST_STR); + String nodeS = params.get("Node"); + if (StringUtils.isNotBlank(nodeListS) && StringUtils.isNotBlank(nodeS)) { + nodeListS = nodeListS.replace("[", StringUtils.EMPTY); + nodeListS = nodeListS.replace("]", StringUtils.EMPTY); + nodeListS = nodeListS.replace("\"", StringUtils.EMPTY); + nodeListS = nodeListS.replace(" ", StringUtils.EMPTY); + List<String> nodes = Arrays.asList(nodeListS.split("\\s*,\\s*")); + code = STATUS_OK; + String message = null; + if (privateKeyChecker.doesExist(clientPrivatekey)) { + ChefApiClient cac = chefApiClientFactory.create(chefserver, organizations, username, + clientPrivatekey); + + for (String nodeName : nodes) { + JSONObject nodeJ = new JSONObject(nodeS); + nodeJ.remove("name"); + nodeJ.put("name", nodeName); + String nodeObject = nodeJ.toString(); + logger.info(nodeObject); + ChefResponse chefResponse = cac.put("/nodes/" + nodeName, nodeObject); + code = chefResponse.getStatusCode(); + message = chefResponse.getBody(); + if (code != STATUS_OK) { + break; + } + } + } else { + code = KEY_NOTFOUND; + message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey; + doFailure(ctx, code, message); + } + chefServerResult(ctx, code, message); + } else { + throw new SvcLogicException("Missing Mandatory param(s) Node , NodeList "); + } + } catch (JSONException e) { + code = APPC_ERRORCODE; + logger.error(POSTING_REQUEST_JSON_ERROR_STR + LOG_ERR_METHOD_STR, e); + doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + LOG_ERR_METHOD_STR + e.getMessage()); + } catch (Exception e) { + code = APPC_ERRORCODE; + logger.error(POSTING_REQUEST_ERROR_STR + LOG_ERR_METHOD_STR, e); + doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + LOG_ERR_METHOD_STR + e.getMessage()); + } + } + + @Override + public void vnfcPushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException { + int code; + try { + chefInfo(params, ctx); + String nodeList = params.get(NODE_LIST_STR); + if (StringUtils.isNotBlank(nodeList)) { + String isCallback = params.get("CallbackCapable"); + String chefAction = "/pushy/jobs"; + // need work on this + String pushRequest; + if ("true".equals(isCallback)) { + String requestId = params.get("RequestId"); + String callbackUrl = params.get("CallbackUrl"); + pushRequest = "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":" + + nodeList + "," + "\"env\": {\"RequestId\": \"" + requestId + "\", \"CallbackUrl\": \"" + + callbackUrl + "\"}," + "\"capture_output\": true" + "}"; + } else { + pushRequest = "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":" + + nodeList + "," + "\"env\": {}," + "\"capture_output\": true" + "}"; + } + ChefApiClient cac = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey); + ChefResponse chefResponse = cac.post(chefAction, pushRequest); + code = chefResponse.getStatusCode(); + logger.info("pushRequest:" + pushRequest); + logger.info("requestbody: {}", chefResponse.getBody()); + String message = chefResponse.getBody(); + if (code == STATUS_PUSHJOBCHECK) { + int startIndex = message.indexOf("jobs") + 5; + int endIndex = message.length() - 2; + String jobID = message.substring(startIndex, endIndex); + ctx.setAttribute("jobID", jobID); + logger.info(jobID); + } + chefServerResult(ctx, code, message); + } else { + throw new SvcLogicException("Missing Mandatory param(s) NodeList "); + } + } catch (Exception e) { + code = APPC_ERRORCODE; + logger.error(POSTING_REQUEST_ERROR_STR + "vnfcPushJob", e); + doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + "vnfcPushJob" + e.getMessage()); + } + } + + @SuppressWarnings("nls") + @Override + public void fetchResults(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException { + int code = STATUS_OK; + final String LOG_STR = "fetchResults"; + try { + chefInfo(params, ctx); + String nodeListS = params.get(NODE_LIST_STR); + if (StringUtils.isNotBlank(nodeListS)) { + nodeListS = nodeListS.replace("[", StringUtils.EMPTY); + nodeListS = nodeListS.replace("]", StringUtils.EMPTY); + nodeListS = nodeListS.replace("\"", StringUtils.EMPTY); + nodeListS = nodeListS.replace(" ", StringUtils.EMPTY); + List<String> nodes = Arrays.asList(nodeListS.split("\\s*,\\s*")); + JSONObject result = new JSONObject(); + String returnMessage = StringUtils.EMPTY; + + for (String node : nodes) { + String chefAction = "/nodes/" + node; + String message; + if (privateKeyChecker.doesExist(clientPrivatekey)) { + ChefResponse chefResponse = getApiMethod(chefAction); + code = chefResponse.getStatusCode(); + message = chefResponse.getBody(); + } else { + code = KEY_NOTFOUND; + message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey; + doFailure(ctx, code, message); + } + if (code == STATUS_OK) { + JSONObject nodeResult = new JSONObject(); + JSONObject allNodeData = new JSONObject(message); + allNodeData = allNodeData.getJSONObject("normal"); + String attribute = "PushJobOutput"; + + String resultData = allNodeData.optString(attribute, null); + if (resultData == null) { + resultData = Optional.ofNullable(allNodeData.optJSONObject(attribute)) + .map(p -> p.toString()).orElse(null); + if (resultData == null) { + resultData = Optional.ofNullable(allNodeData.optJSONArray(attribute)) + .map(p -> p.toString()).orElse(null); + + if (resultData == null) { + code = KEY_NOTFOUND; + returnMessage = "Cannot find " + attribute; + break; + } + } + } + nodeResult.put(attribute, resultData); + result.put(node, nodeResult); + returnMessage = result.toString(); + } else { + code = KEY_NOTFOUND; + returnMessage = message + " Cannot access: " + node; + doFailure(ctx, code, message); + break; + } + } + + chefServerResult(ctx, code, returnMessage); + } else { + throw new SvcLogicException("Missing Mandatory param(s) NodeList "); + } + } catch (JSONException e) { + code = APPC_ERRORCODE; + logger.error(POSTING_REQUEST_JSON_ERROR_STR + LOG_STR, e); + doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + LOG_STR + e.getMessage()); + } catch (Exception e) { + code = APPC_ERRORCODE; + logger.error(POSTING_REQUEST_ERROR_STR + LOG_STR, e); + doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + LOG_STR + e.getMessage()); + } + } + + private ChefResponse getApiMethod(String chefAction) { + ChefApiClient cac = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey); + return cac.get(chefAction); + } + + /** + * build node object + */ + @SuppressWarnings("nls") + @Override + public void nodeObejctBuilder(Map<String, String> params, SvcLogicContext ctx) { + logger.info("nodeObejctBuilder"); + String name = params.get("nodeobject.name"); + String normal = params.get("nodeobject.normal"); + String overrides = params.get("nodeobject.overrides"); + String defaults = params.get("nodeobject.defaults"); + String runList = params.get("nodeobject.run_list"); + String chefEnvironment = params.get("nodeobject.chef_environment"); + String nodeObject = "{\"json_class\":\"Chef::Node\",\"default\":{" + defaults + + "},\"chef_type\":\"node\",\"run_list\":[" + runList + "],\"override\":{" + overrides + + "},\"normal\": {" + normal + "},\"automatic\":{},\"name\":\"" + name + "\",\"chef_environment\":\"" + + chefEnvironment + "\",}"; + logger.info(nodeObject); + ctx.setAttribute("chef.nodeObject", nodeObject); + } + + /** + * send get request to chef server + */ + private void chefInfo(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException { + + username = params.get("username"); + serverAddress = params.get("serverAddress"); + organizations = params.get("organizations"); + if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(serverAddress) + && StringUtils.isNotBlank(organizations)) { + chefserver = "https://" + serverAddress + "/organizations/" + organizations; + clientPrivatekey = "/opt/onap/appc/chef/" + serverAddress + "/" + organizations + "/" + username + ".pem"; + logger.info(" clientPrivatekey " + clientPrivatekey); + } else { + doFailure(ctx, APPC_ERRORCODE, "Missing mandatory param(s) such as username, serverAddress, organizations"); + } + } + + @SuppressWarnings("nls") + @Override + public void retrieveData(Map<String, String> params, SvcLogicContext ctx) { + String allConfigData = params.get("allConfig"); + String key = params.get("key"); + String dgContext = params.get("dgContext"); + JSONObject jsonConfig = new JSONObject(allConfigData); + String contextData = fetchContextData(key, jsonConfig); + ctx.setAttribute(dgContext, contextData); + } + + private String fetchContextData(String key, JSONObject jsonConfig) { + try { + return jsonConfig.getString(key); + } catch (Exception e) { + logger.error("Failed getting string value corresponding to " + key + ". Trying to fetch nested json object", + e); + try { + return jsonConfig.getJSONObject(key).toString(); + } catch (Exception ex) { + logger.error("Failed getting json object corresponding to " + key + ". Trying to fetch array", ex); + return jsonConfig.getJSONArray(key).toString(); + } + } + } + + @SuppressWarnings("nls") + @Override + public void combineStrings(Map<String, String> params, SvcLogicContext ctx) { + String string1 = params.get("String1"); + String string2 = params.get("String2"); + String dgContext = params.get("dgContext"); + String contextData = string1 + string2; + ctx.setAttribute(dgContext, contextData); + } + + /** + * Send GET request to chef server + */ + @SuppressWarnings("nls") + + @Override + public void chefGet(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException { + logger.info("chef get method"); + chefInfo(params, ctx); + String chefAction = params.get(CHEF_ACTION_STR); + int code; + String message; + if (privateKeyChecker.doesExist(clientPrivatekey)) { + ChefResponse chefResponse = getApiMethod(chefAction); + code = chefResponse.getStatusCode(); + message = chefResponse.getBody(); + } else { + code = KEY_NOTFOUND; + message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey; + } + chefServerResult(ctx, code, message); + } + + /** + * Send PUT request to chef server + */ + @SuppressWarnings("nls") + + @Override + public void chefPut(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException { + chefInfo(params, ctx); + String chefAction = params.get(CHEF_ACTION_STR); + String chefNodeStr = params.get("chefRequestBody"); + int code; + String message; + if (privateKeyChecker.doesExist(clientPrivatekey)) { + ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, + clientPrivatekey); + ChefResponse chefResponse = chefApiClient.put(chefAction, chefNodeStr); + code = chefResponse.getStatusCode(); + message = chefResponse.getBody(); + } else { + code = KEY_NOTFOUND; + message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey; + } + logger.info(code + " " + message); + chefServerResult(ctx, code, message); + } + + /** + * send Post request to chef server + */ + @Override + public void chefPost(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException { + chefInfo(params, ctx); + logger.info("chef Post method"); + logger.info(username + " " + clientPrivatekey + " " + chefserver + " " + organizations); + String chefNodeStr = params.get("chefRequestBody"); + String chefAction = params.get(CHEF_ACTION_STR); + int code; + String message; + // should load pem from somewhere else + if (privateKeyChecker.doesExist(clientPrivatekey)) { + ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, + clientPrivatekey); + // need pass path into it + // "/nodes/testnode" + ChefResponse chefResponse = chefApiClient.post(chefAction, chefNodeStr); + code = chefResponse.getStatusCode(); + message = chefResponse.getBody(); + } else { + code = KEY_NOTFOUND; + message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey; + } + logger.info(code + " " + message); + chefServerResult(ctx, code, message); + } + + /** + * send delete request to chef server + */ + @Override + public void chefDelete(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException { + logger.info("chef delete method"); + chefInfo(params, ctx); + String chefAction = params.get(CHEF_ACTION_STR); + int code; + String message; + if (privateKeyChecker.doesExist(clientPrivatekey)) { + ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, + clientPrivatekey); + ChefResponse chefResponse = chefApiClient.delete(chefAction); + code = chefResponse.getStatusCode(); + message = chefResponse.getBody(); + } else { + code = KEY_NOTFOUND; + message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey; + } + logger.info(code + " " + message); + chefServerResult(ctx, code, message); + } + + /** + * Trigger target vm run chef + */ + @Override + public void trigger(Map<String, String> params, SvcLogicContext svcLogicContext) { + logger.info("Run trigger method"); + String tVmIp = params.get("ip"); + try { + ChefResponse chefResponse = chefApiClientFactory.create(tVmIp, organizations).get(""); + chefClientResult(svcLogicContext, chefResponse.getStatusCode(), chefResponse.getBody()); + svcLogicContext.setAttribute("chefAgent.code", STATUS_OK.toString()); + } catch (Exception e) { + logger.error("An error occurred when executing trigger method", e); + svcLogicContext.setAttribute("chefAgent.code", KEY_NOTFOUND.toString()); + svcLogicContext.setAttribute("chefAgent.message", e.toString()); + } + } + + @SuppressWarnings("nls") + @Override + public void checkPushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException { + int code; + try { + chefInfo(params, ctx); + String jobID = params.get("jobid"); + String retry = params.get("retryTimes"); + String intrva = params.get("retryInterval"); + if (StringUtils.isNotBlank(jobID) && StringUtils.isNotBlank(retry) && StringUtils.isNotBlank(intrva)) { + + int retryTimes = Integer.parseInt(params.get("retryTimes")); + int retryInterval = Integer.parseInt(params.get("retryInterval")); + String chefAction = "/pushy/jobs/" + jobID; + String message = StringUtils.EMPTY; + String status = StringUtils.EMPTY; + for (int i = 0; i < retryTimes; i++) { + sleepFor(retryInterval); + ChefResponse chefResponse = getApiMethod(chefAction); + code = chefResponse.getStatusCode(); + message = chefResponse.getBody(); + JSONObject obj = new JSONObject(message); + status = obj.getString("status"); + if (!"running".equals(status)) { + logger.info(i + " time " + code + " " + status); + break; + } + } + resolveSvcLogicAttributes(ctx, message, status); + } else { + throw new SvcLogicException("Missing Mandatory param(s) retryTimes , retryInterval "); + } + } catch (Exception e) { + code = APPC_ERRORCODE; + logger.error("An error occurred when executing checkPushJob method", e); + doFailure(ctx, code, e.getMessage()); + } + } + + private void resolveSvcLogicAttributes(SvcLogicContext svcLogic, String message, String status) { + if ("complete".equals(status)) { + if (hasFailedNode(message)) { + String finalMessage = "PushJob Status Complete but check failed nodes in the message :" + message; + svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, APPC_ERRORCODE.toString()); + svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, finalMessage); + } else { + svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, STATUS_OK.toString()); + svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, message); + } + } else if ("running".equals(status)) { + svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, PUSHJOBSTATUS.toString()); + svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, "chef client runtime out"); + } else { + svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, KEY_NOTFOUND.toString()); + svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, message); + } + } + + private Boolean hasFailedNode(String message) { + try { + JSONObject messageJson = new JSONObject(message); + JSONObject node = messageJson.getJSONObject("nodes"); + final String failed = "failed"; + if (node == null) { + logger.debug("Status Complete but node details in the message is null : " + message); + return Boolean.TRUE; + } + if (node.has(failed) && !(node.isNull(failed)) && (node.getJSONArray(failed).length() != 0)) { + logger.debug("Status Complete but one or more Failed nodes ....FAILURE " + message); + return Boolean.TRUE; + } + logger.debug("Status Complete and no failed nodes ....SUCCESS " + message); + return Boolean.FALSE; + } catch (JSONException e) { + logger.error("Exception occured in hasFailedNode", e); + throw new JSONException("Exception occured in hasFailedNode" + e.getMessage()); + } + + } + + private void sleepFor(int retryInterval) { + try { + Thread.sleep(retryInterval); // 1000 milliseconds is one second. + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } + + @SuppressWarnings("nls") + @Override + public void pushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException { + int code; + try { + chefInfo(params, ctx); + String pushRequest = params.get("pushRequest"); + String chefAction = "/pushy/jobs"; + ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, + clientPrivatekey); + ChefResponse chefResponse = chefApiClient.post(chefAction, pushRequest); + code = chefResponse.getStatusCode(); + String message = chefResponse.getBody(); + if (code == STATUS_PUSHJOBCHECK) { + int startIndex = message.indexOf("jobs") + 6; + int endIndex = message.length() - 2; + String jobID = message.substring(startIndex, endIndex); + ctx.setAttribute("jobID", jobID); + logger.info(jobID); + } + chefServerResult(ctx, code, message); + } catch (Exception e) { + code = APPC_ERRORCODE; + logger.error("An error occurred when executing pushJob method", e); + doFailure(ctx, code, e.getMessage()); + } + } + + @SuppressWarnings("static-method") + private void chefServerResult(SvcLogicContext svcLogicContext, int code, String message) { + initSvcLogic(svcLogicContext, code, message, "server"); + } + + @SuppressWarnings("static-method") + private void chefClientResult(SvcLogicContext svcLogicContext, int code, String message) { + initSvcLogic(svcLogicContext, code, message, "client"); + } + + private void initSvcLogic(SvcLogicContext svcLogicContext, int code, String message, String target) { + + String codeStr = "server".equals(target) ? CHEF_SERVER_RESULT_CODE_STR : CHEF_CLIENT_RESULT_CODE_STR; + String messageStr = "client".equals(target) ? CHEF_CLIENT_RESULT_MSG_STR : CHEF_SERVER_RESULT_MSG_STR; + svcLogicContext.setStatus(OUTCOME_SUCCESS); + svcLogicContext.setAttribute(codeStr, Integer.toString(code)); + svcLogicContext.setAttribute(messageStr, message); + logger.info(codeStr + ": " + svcLogicContext.getAttribute(codeStr)); + logger.info(messageStr + ": " + svcLogicContext.getAttribute(messageStr)); + } + + @SuppressWarnings("static-method") + private void doFailure(SvcLogicContext svcLogic, int code, String message) throws SvcLogicException { + + String cutMessage = message.contains("\n") ? message.substring(message.indexOf('\n')) : message; + svcLogic.setStatus(OUTCOME_FAILURE); + svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, Integer.toString(code)); + svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, cutMessage); + throw new SvcLogicException("Chef Adaptor error:" + cutMessage); + } + +} diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/impl/PrivateKeyChecker.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/impl/PrivateKeyChecker.java new file mode 100644 index 000000000..d2cafdff6 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/java/org/onap/ccsdk/sli/adaptors/chef/impl/PrivateKeyChecker.java @@ -0,0 +1,42 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.impl; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import java.io.File; + +class PrivateKeyChecker { + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(PrivateKeyChecker.class); + + Boolean doesExist(String clientPrivateKeyPath) { + File f = new File(clientPrivateKeyPath); + if (f.exists()) { + logger.info("Key exists"); + return true; + } else { + logger.info("Key doesn't exists"); + return false; + } + } + +}
\ No newline at end of file diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/resources/chef-adaptor.properties b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/resources/chef-adaptor.properties new file mode 100644 index 000000000..2a8bbe869 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/main/resources/chef-adaptor.properties @@ -0,0 +1,89 @@ +### +# ============LICENSE_START======================================================= +# ONAP : APPC +# ================================================================================ +# Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Copyright (C) 2017 Amdocs +# ============================================================================= +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ============LICENSE_END========================================================= +### +# +# Default properties for the APP-C Provider Adaptor +# +# ------------------------------------------------------------------------------------------------- +# +# Define the name and path of any user-provided configuration (bootstrap) file that can be loaded +# to supply configuration options +org.onap.appc.bootstrap.file=appc.properties +org.onap.appc.bootstrap.path=/opt/onap/appc/data/properties,${user.home},. +appc.application.name=APPC +# +# Define the message resource bundle name to be loaded +org.onap.appc.resources=org/onap/appc/i18n/MessageResources +# +# The name of the adaptor. +org.onap.appc.provider.adaptor.name=org.onap.appc.appc_provider_adaptor +# +# Set up the logging environment +# +org.onap.appc.logging.file=org/onap/appc/logback.xml +org.onap.appc.logging.path=${user.home};etc;../etc +org.onap.appc.logger=org.onap.appc +org.onap.appc.security.logger=org.onap.appc.security +# +# The minimum and maximum provider/tenant context pool sizes. Min=1 means that as soon +# as the provider/tenant is referenced a Context is opened and added to the pool. Max=0 +# means that the upper bound on the pool is unbounded. +org.onap.appc.provider.min.pool=1 +org.onap.appc.provider.max.pool=0 +# +# The following properties are used to configure the retry logic for connection to the +# IaaS provider(s). The retry delay property is the amount of time, in seconds, the +# application waits between retry attempts. The retry limit is the number of retries +# that are allowed before the request is failed. +org.onap.appc.provider.retry.delay=30 +org.onap.appc.provider.retry.limit=10 +# +# The trusted hosts list for SSL access when a certificate is not provided. +# +provider.trusted.hosts=* +# +# The amount of time, in seconds, to wait for a server state change (start->stop, stop->start, etc). +# If the server does not change state to a valid state within the alloted time, the operation +# fails. +org.onap.appc.server.state.change.timeout=300 +# +# The amount of time to wait, in seconds, between subsequent polls to the OpenStack provider +# to refresh the status of a resource we are waiting on. +# +org.onap.appc.openstack.poll.interval=20 +# +# The connection information to connect to the provider we are using. These properties +# are "structured" properties, in that the name is a compound name, where the nodes +# of the name can be ordered (1, 2, 3, ...). All of the properties with the same ordinal +# position are defining the same entity. For example, provider1.type and provider1.name +# are defining the same provider, whereas provider2.name and provider2.type are defining +# the values for a different provider. Any number of providers can be defined in this +# way. +# +# Don't change these 2 right now since they are hard coded in the DG +#provider1.type=appc +#provider1.name=appc +#These you can change +#provider1.identity=appc +#provider1.tenant1.name=appc +#provider1.tenant1.userid=appc +#provider1.tenant1.password=appc diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/ChefApiClientImplTest.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/ChefApiClientImplTest.java new file mode 100644 index 000000000..6f9601c7d --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/ChefApiClientImplTest.java @@ -0,0 +1,231 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.chefclient.impl; + +import com.google.common.collect.ImmutableMap; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.function.Supplier; +import org.apache.http.Header; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.StatusLine; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.entity.StringEntity; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentMatcher; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.ChefApiClientFactory; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.api.ChefApiClient; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.api.ChefResponse; + +import static junit.framework.TestCase.assertEquals; +import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.argThat; +import static org.mockito.Mockito.mock; + +@RunWith(MockitoJUnitRunner.class) +public class ChefApiClientImplTest { + + private static final String END_POINT = "https://chefServer"; + private static final String ORGANIZATIONS_PATH = "onap"; + private static final String USER_ID = "testUser"; + private static final String REQUEST_PATH = "/test/path"; + private static final String BODY = "SOME BODY STRING"; + private static final String PEM_FILEPATH = "path/to/pemFile"; + private static final ImmutableMap<String, String> HEADERS = ImmutableMap.<String, String>builder() + .put("Content-type", "application/json") + .put("Accept", "application/json") + .put("X-Ops-Timestamp", "1970-01-15T06:56:07Z") + .put("X-Ops-UserId", USER_ID) + .put("X-Chef-Version", "12.4.1") + .put("X-Ops-Content-Hash", BODY) + .put("X-Ops-Sign", "version=1.0").build(); + + @Mock + private HttpClient httpClient; + @Mock + private org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.ChefApiHeaderFactory chefHttpHeaderFactory; + + @InjectMocks + private ChefApiClientFactory chefApiClientFactory; + private ChefApiClient chefApiClient; + + @Before + public void setUp() { + chefApiClient = chefApiClientFactory.create( + END_POINT, + ORGANIZATIONS_PATH, + USER_ID, + PEM_FILEPATH); + } + + @Test + public void execute_HttpGet_shouldReturnResponseObject_whenRequestIsSuccessful() throws IOException { + // GIVEN + String methodName = "GET"; + String body = ""; + Supplier<ChefResponse> chefClientApiCall = () -> chefApiClient.get(REQUEST_PATH); + + // WHEN //THEN + assertChefApiClientCall(methodName, body, chefClientApiCall); + } + + @Test + public void execute_HttpDelete_shouldReturnResponseObject_whenRequestIsSuccessful() throws IOException { + // GIVEN + String methodName = "DELETE"; + String body = ""; + Supplier<ChefResponse> chefClientApiCall = () -> chefApiClient.delete(REQUEST_PATH); + + // WHEN //THEN + assertChefApiClientCall(methodName, body, chefClientApiCall); + } + + @Test + public void execute_HttpPost_shouldReturnResponseObject_whenRequestIsSuccessful() throws IOException { + // GIVEN + String methodName = "POST"; + Supplier<ChefResponse> chefClientApiCall = () -> chefApiClient.post(REQUEST_PATH, BODY); + + // WHEN //THEN + assertChefApiClientCall(methodName, BODY, chefClientApiCall); + } + + @Test + public void execute_HttpPut_shouldReturnResponseObject_whenRequestIsSuccessful() throws IOException { + // GIVEN + String methodName = "PUT"; + Supplier<ChefResponse> chefClientApiCall = () -> chefApiClient.put(REQUEST_PATH, BODY); + + // WHEN //THEN + assertChefApiClientCall(methodName, BODY, chefClientApiCall); + } + + private void assertChefApiClientCall(String methodName, String body, Supplier<ChefResponse> httpMethod) + throws IOException { + // GIVEN + given(chefHttpHeaderFactory.create(methodName, REQUEST_PATH, body, USER_ID, ORGANIZATIONS_PATH, PEM_FILEPATH)) + .willReturn(HEADERS); + + StatusLine statusLine = mock(StatusLine.class); + given(statusLine.getStatusCode()).willReturn(HttpStatus.SC_OK); + HttpResponse httpResponse = mock(HttpResponse.class); + given(httpResponse.getStatusLine()).willReturn(statusLine); + given(httpResponse.getEntity()).willReturn(new StringEntity("Successful Response String")); + given(httpClient.execute(argThat(new HttpRequestBaseMatcher(methodName)))) + .willReturn(httpResponse); + + // WHEN + ChefResponse chefResponse = httpMethod.get(); + + // THEN + assertEquals("Successful Response String", chefResponse.getBody()); + assertEquals(HttpStatus.SC_OK, chefResponse.getStatusCode()); + } + + @Test + public void execute_shouldHandleException_whenHttpClientExecutionFails() throws IOException { + + // GIVEN + given(chefHttpHeaderFactory.create("GET", REQUEST_PATH, "", USER_ID, ORGANIZATIONS_PATH, PEM_FILEPATH)) + .willReturn(HEADERS); + + String expectedErrorMsg = "HttpClient call failed"; + given(httpClient.execute(argThat(new HttpRequestBaseMatcher("GET")))) + .willThrow(new IOException(expectedErrorMsg)); + + // WHEN + ChefResponse chefResponse = chefApiClient.get(REQUEST_PATH); + + // THEN + assertEquals(expectedErrorMsg, chefResponse.getBody()); + assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, chefResponse.getStatusCode()); + } + + @Test + public void execute_shouldHandleException_whenEndpointURIisMalformed() { + // GIVEN + String expectedErrorMsg = "Malformed escape pair at index 1: /%#@/"; + + // WHEN + ChefApiClient chefApiClient = chefApiClientFactory.create( + "/%#@/", + ORGANIZATIONS_PATH, + USER_ID, + PEM_FILEPATH); + ChefResponse chefResponse = chefApiClient.get(REQUEST_PATH); + + // THEN + assertEquals(expectedErrorMsg, chefResponse.getBody()); + assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, chefResponse.getStatusCode()); + } + + private class HttpRequestBaseMatcher extends ArgumentMatcher<HttpRequestBase> { + + private final String methodName; + + public HttpRequestBaseMatcher(String methodName) { + this.methodName = methodName; + } + + @Override + public boolean matches(Object arguments) { + HttpRequestBase httpRequestBase = (HttpRequestBase) arguments; + try { + return methodName.equals(httpRequestBase.getMethod()) + && new URI(END_POINT + "/organizations/" + ORGANIZATIONS_PATH + REQUEST_PATH).equals(httpRequestBase.getURI()) + && checkIfBodyMatches(httpRequestBase) + && checkIfHeadersMatch(httpRequestBase); + } catch (URISyntaxException e) { + e.printStackTrace(); + return false; + } + } + + public boolean checkIfBodyMatches(HttpRequestBase httpRequestBase) { + if (httpRequestBase instanceof HttpEntityEnclosingRequestBase) { + HttpEntityEnclosingRequestBase requestBaseWithBody = (HttpEntityEnclosingRequestBase) httpRequestBase; + StringEntity stringEntity = new StringEntity(BODY, "UTF-8"); + stringEntity.setContentType("application/json"); + return stringEntity.toString().equals(requestBaseWithBody.getEntity().toString()); + } + return true; + } + + private boolean checkIfHeadersMatch(HttpRequestBase httpRequestBase) { + Header[] generatedHeaders = httpRequestBase.getAllHeaders(); + return generatedHeaders.length > 0 + && generatedHeaders.length == HEADERS.size() + && HEADERS.entrySet().stream().allMatch(p -> httpRequestBase.getFirstHeader(p.getKey()).getValue().equals(p.getValue())); + } + + } + +} diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/ChefApiHeaderFactoryTest.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/ChefApiHeaderFactoryTest.java new file mode 100644 index 000000000..f890d9249 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/ChefApiHeaderFactoryTest.java @@ -0,0 +1,85 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.chefclient.impl; + +import com.google.common.collect.ImmutableMap; +import java.util.Date; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import static junit.framework.TestCase.assertEquals; +import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.any; + +@RunWith(MockitoJUnitRunner.class) +public class ChefApiHeaderFactoryTest { + + private static final String ORGANIZATIONS_PATH = "onap"; + private static final String USER_ID = "testUser"; + private static final String REQUEST_PATH = "/test/path"; + private static final String EXPECTED_TIMESTAMP = "1970-01-15T06:56:07Z"; + private static final String EMPTY_BODY = ""; + + @Mock + private org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.FormattedTimestamp formattedTimestamp; + + @InjectMocks + private org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.ChefApiHeaderFactory chefApiHeaderFactory; + + @Test + public void create_shouldCreateProperChefHeaders_withHashedAuthorizationString() { + // GIVEN + given(formattedTimestamp.format(any(Date.class))).willReturn(EXPECTED_TIMESTAMP); + String pemFilePath = getClass().getResource("/testclient.pem").getPath(); + + // WHEN + ImmutableMap<String, String> headers = chefApiHeaderFactory + .create("GET", REQUEST_PATH, "", USER_ID, ORGANIZATIONS_PATH, pemFilePath); + + // THEN + assertEquals(headers, createExpectedHeaders()); + } + + private ImmutableMap<String, String> createExpectedHeaders() { + String hashedBody = org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.Utils.sha1AndBase64(EMPTY_BODY); + ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); + builder + .put("Content-type", "application/json") + .put("Accept", "application/json") + .put("X-Ops-Timestamp", EXPECTED_TIMESTAMP) + .put("X-Ops-UserId", USER_ID) + .put("X-Chef-Version", "12.4.1") + .put("X-Ops-Content-Hash", hashedBody) + .put("X-Ops-Sign", "version=1.0") + .put("X-Ops-Authorization-1", "i+HGCso703727yd2ZQWMZIIpGKgTzm41fA31LIExNxEf9mOUMcpesIHjH/Wr") + .put("X-Ops-Authorization-2", "QEvsX/Gy1ay9KsUtqhy9GA6PB8UfDeMNoVUisqR4HQW+S6IOfvqBjW+2afzE") + .put("X-Ops-Authorization-3", "RdRReB/TJIF3s6ZC8vNpbEdY9kHmwiDglhxmS8X2FS+ArSh/DK/i7MqBbjux") + .put("X-Ops-Authorization-4", "49iiOlRVG7aTr/FA115hlBYP9CYCIQWKIBUOK3JyV9fXNdVqc9R0r1XdjxUl") + .put("X-Ops-Authorization-5", "EDGw6tuE8YW8mH5wkgHCjKpXG3WjmWt2X6kUrdIu44qCBK2N3sZziSub2fJA") + .put("X-Ops-Authorization-6", "hPBuOhjiYDZuFUqC99lCryM0Hf5RMw1uTlkYsBEZmA=="); + + return builder.build(); + } + +}
\ No newline at end of file diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/FormattedTimestampTest.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/FormattedTimestampTest.java new file mode 100644 index 000000000..8d5f10f19 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/chefclient/impl/FormattedTimestampTest.java @@ -0,0 +1,42 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.chefclient.impl; + +import java.util.Date; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class FormattedTimestampTest { + + @Test + public void format_shouldFormatGivenDate_withCorrectTimezoneSet() { + // GIVEN + String expectedFormattedDate = "1970-01-15T06:56:07Z"; + + // WHEN + String formattedDateWithTimezone = new org.onap.ccsdk.sli.adaptors.chef.chefclient.impl.FormattedTimestamp().format(new Date(1234567890)); + + // THEN + assertEquals(expectedFormattedDate, formattedDateWithTimezone); + } + +}
\ No newline at end of file diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorImplDataRetrieverTest.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorImplDataRetrieverTest.java new file mode 100644 index 000000000..16f0e0174 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorImplDataRetrieverTest.java @@ -0,0 +1,83 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.impl; + +import com.google.common.collect.ImmutableMap; +import java.util.Map; +import org.assertj.core.api.Assertions; +import org.junit.Test; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; + +public class ChefAdaptorImplDataRetrieverTest { + + private static final String KEY_PARAM = "key"; + private static final String DG_CONTEXT_PARAM = "dgContext"; + private static final String ALL_CONFIG_PARAM = "allConfig"; + private static final String KEY_VALUE = "keyValue"; + private static final String DG_CONTEXT_VALUE = "contextValue"; + + @Test + public void retrieveData_shouldSetContextData_withExtractedJsonString() { + // GIVEN + Map<String, String> params = givenParamMapWithJson("{" + KEY_VALUE + ":testValue}"); + SvcLogicContext svcLogicContext = new SvcLogicContext(); + + // WHEN + new org.onap.ccsdk.sli.adaptors.chef.impl.ChefAdaptorFactory().create().retrieveData(params, svcLogicContext); + + // THEN + Assertions.assertThat(svcLogicContext.getAttribute(DG_CONTEXT_VALUE)).isEqualTo("testValue"); + } + + @Test + public void retrieveData_shouldSetContextData_withExtractedJsonObject() { + // GIVEN + Map<String, String> params = givenParamMapWithJson("{" + KEY_VALUE + ": {param : testValue} }"); + SvcLogicContext svcLogicContext = new SvcLogicContext(); + + // WHEN + new org.onap.ccsdk.sli.adaptors.chef.impl.ChefAdaptorFactory().create().retrieveData(params, svcLogicContext); + + // THEN + Assertions.assertThat(svcLogicContext.getAttribute(DG_CONTEXT_VALUE)).isEqualTo("{\"param\":\"testValue\"}"); + } + + @Test + public void retrieveData_shouldSetContextData_withExtractedJsonArray() { + // GIVEN + Map<String, String> params = givenParamMapWithJson("{" + KEY_VALUE + ": [val1, val2, val3] }"); + SvcLogicContext svcLogicContext = new SvcLogicContext(); + + // WHEN + new org.onap.ccsdk.sli.adaptors.chef.impl.ChefAdaptorFactory().create().retrieveData(params, svcLogicContext); + + // THEN + Assertions.assertThat(svcLogicContext.getAttribute(DG_CONTEXT_VALUE)).isEqualTo("[\"val1\",\"val2\",\"val3\"]"); + } + + private Map<String, String> givenParamMapWithJson(String json) { + return ImmutableMap + .of(KEY_PARAM, KEY_VALUE, + DG_CONTEXT_PARAM, DG_CONTEXT_VALUE, + ALL_CONFIG_PARAM, json); + } + +}
\ No newline at end of file diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorImplHttpMethodTest.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorImplHttpMethodTest.java new file mode 100644 index 000000000..283b51ebb --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorImplHttpMethodTest.java @@ -0,0 +1,185 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.impl; + +import com.google.common.collect.ImmutableMap; +import java.util.Map; +import java.util.function.Consumer; +import java.util.function.Supplier; +import org.apache.http.HttpStatus; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.ccsdk.sli.adaptors.chef.ChefAdaptor; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.ChefApiClientFactory; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.api.ChefApiClient; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.api.ChefResponse; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.verifyZeroInteractions; + +@RunWith(MockitoJUnitRunner.class) +public class ChefAdaptorImplHttpMethodTest { + + private static final String CLIENT_PRIVATE_KEY_PATH = "/opt/onap/appc/chef/localhost/onap/testclient.pem"; + private static final String RESULT_CODE_ATTR_KEY = "chefServerResult.code"; + private static final String RESULT_MESSAGE_ATTR_KEY = "chefServerResult.message"; + private static final String EXPECTED_RESPONSE_MSG = "chefResponseMessage"; + private static final String ACTION_PARAM = "action"; + private static final String REQUEST_BODY_DATA = "requestBodyData"; + private static final Map<String, String> PARAMS = ImmutableMap + .of("username", "testclient", + "serverAddress", "localhost", + "organizations", "onap", + "chefAction", ACTION_PARAM, + "chefRequestBody", REQUEST_BODY_DATA); + @Mock + private org.onap.ccsdk.sli.adaptors.chef.impl.PrivateKeyChecker privateKeyChecker; + @Mock + private ChefApiClientFactory chefApiClientFactory; + @Mock + private ChefApiClient chefApiClient; + + @InjectMocks + private org.onap.ccsdk.sli.adaptors.chef.impl.ChefAdaptorFactory chefAdaptorFactory; + private SvcLogicContext svcLogicContext; + + @Before + public void setUp() { + svcLogicContext = new SvcLogicContext(); + } + + @Test + public void chefGet_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() { + assertSuccessfulChefHttpCallFor(() -> chefApiClient.get(ACTION_PARAM), this :: chefGet); + } + + @Test + public void chefGet_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() { + assertNoChefCallOccurFor(this :: chefGet); + } + + @Test + public void chefDelete_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() { + assertSuccessfulChefHttpCallFor(() -> chefApiClient.delete(ACTION_PARAM), this :: chefDelete); + } + + @Test + public void chefDelete_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() { + assertNoChefCallOccurFor(this :: chefDelete); + } + + @Test + public void chefPut_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() { + assertSuccessfulChefHttpCallFor(() -> chefApiClient.put(ACTION_PARAM, REQUEST_BODY_DATA), this :: chefPut); + } + + @Test + public void chefPut_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() { + assertNoChefCallOccurFor(this :: chefPut); + } + + @Test + public void chefPost_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() { + assertSuccessfulChefHttpCallFor(() -> chefApiClient.post(ACTION_PARAM, REQUEST_BODY_DATA), this :: chefPost); + } + + @Test + public void chefPost_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() { + assertNoChefCallOccurFor(this :: chefPost); + } + + public void assertSuccessfulChefHttpCallFor(Supplier<ChefResponse> responseSupplier, + Consumer<ChefAdaptor> chefAdaptorCall) { + // GIVEN + given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true); + given(chefApiClientFactory.create("https://localhost/organizations/onap", + "onap", + "testclient", + CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient); + given(responseSupplier.get()).willReturn(ChefResponse.create(HttpStatus.SC_OK, EXPECTED_RESPONSE_MSG)); + + // WHEN + chefAdaptorCall.accept(chefAdaptorFactory.create()); + + // THEN + assertTrue(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_OK)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(EXPECTED_RESPONSE_MSG); + } + + public void assertNoChefCallOccurFor(Consumer<ChefAdaptor> chefAdaptorCall) { + // GIVEN + given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(false); + + // WHEN + chefAdaptorCall.accept(chefAdaptorFactory.create()); + + // THEN + verifyZeroInteractions(chefApiClient); + assertTrue(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)) + .isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo( + "Cannot find the private key in the APPC file system, please load the private key to " + + CLIENT_PRIVATE_KEY_PATH); + } + + public void chefGet(ChefAdaptor chefAdaptor) { + try { + chefAdaptor.chefGet(PARAMS, svcLogicContext); + } catch (SvcLogicException e) { + e.printStackTrace(); + } + } + + public void chefDelete(ChefAdaptor chefAdaptor) { + try { + chefAdaptor.chefDelete(PARAMS, svcLogicContext); + } catch (SvcLogicException e) { + e.printStackTrace(); + } + } + + public void chefPost(ChefAdaptor chefAdaptor) { + try { + chefAdaptor.chefPost(PARAMS, svcLogicContext); + } catch (SvcLogicException e) { + e.printStackTrace(); + } + } + + public void chefPut(ChefAdaptor chefAdaptor) { + try { + chefAdaptor.chefPut(PARAMS, svcLogicContext); + } catch (SvcLogicException e) { + e.printStackTrace(); + } + } + +}
\ No newline at end of file diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorImplJobPusherTest.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorImplJobPusherTest.java new file mode 100644 index 000000000..4bf5a2280 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorImplJobPusherTest.java @@ -0,0 +1,269 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.impl; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMap.Builder; +import java.util.Map; +import java.util.Map.Entry; +import org.apache.http.HttpStatus; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.ChefApiClientFactory; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.api.ChefApiClient; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.api.ChefResponse; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; + +import static com.google.common.collect.Maps.immutableEntry; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.BDDMockito.given; + +@RunWith(MockitoJUnitRunner.class) +public class ChefAdaptorImplJobPusherTest { + + private static final String CLIENT_PRIVATE_KEY_PATH = "/opt/onap/appc/chef/localhost/onap/testclient.pem"; + private static final String RESULT_CODE_ATTR_KEY = "chefServerResult.code"; + private static final String RESULT_MESSAGE_ATTR_KEY = "chefServerResult.message"; + private static final String EXPECTED_RESPONSE_MSG = "jobs/{666}/"; + + private static final String USERNAME = "testclient"; + private static final String SERVER_ADDRESS = "localhost"; + private static final String ORGANIZATIONS = "onap"; + private static final String ACTION_PARAM = "/pushy/jobs"; + private static final String REQUEST_BODY_DATA = "requestBodyData"; + private static final String JOB_ID = "jobID"; + + @Mock + private org.onap.ccsdk.sli.adaptors.chef.impl.PrivateKeyChecker privateKeyChecker; + + @Mock + private ChefApiClientFactory chefApiClientFactory; + + @Mock + private ChefApiClient chefApiClient; + + @InjectMocks + private org.onap.ccsdk.sli.adaptors.chef.impl.ChefAdaptorFactory chefAdaptorFactory; + private SvcLogicContext svcLogicContext; + + @Before + public void setUp() { + svcLogicContext = new SvcLogicContext(); + } + + @Test + public void pushJob_shouldSuccessfullyMakePostCall_andUpdateSvcLogicContext_whenReturnedStatusIsDifferentThan_201() + throws SvcLogicException { + assertSuccessfulPostCallForStatus(HttpStatus.SC_OK); + assertThat(svcLogicContext.getAttribute(JOB_ID)).isBlank(); + } + + @Test + public void pushJob_shouldSuccessfullyMakePostCall_andUpdateSvcLogicContext_withReturnedStatusIs_201() + throws SvcLogicException { + assertSuccessfulPostCallForStatus(HttpStatus.SC_CREATED); + assertThat(svcLogicContext.getAttribute(JOB_ID)).isEqualTo("666"); + } + + @SuppressWarnings("unchecked") + public void assertSuccessfulPostCallForStatus(int expectedHttpStatus) throws SvcLogicException { + // GIVEN + Map<String, String> params = givenInputParams( + immutableEntry("chefAction", ACTION_PARAM), + immutableEntry("pushRequest", REQUEST_BODY_DATA)); + given(chefApiClientFactory.create("https://localhost/organizations/onap", ORGANIZATIONS, USERNAME, + CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient); + given(chefApiClient.post(ACTION_PARAM, REQUEST_BODY_DATA)) + .willReturn(ChefResponse.create(expectedHttpStatus, EXPECTED_RESPONSE_MSG)); + + // WHEN + chefAdaptorFactory.create().pushJob(params, svcLogicContext); + + // THEN + assertTrue(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(expectedHttpStatus)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(EXPECTED_RESPONSE_MSG); + } + + @SuppressWarnings("unchecked") + @Test + public void pushJob_shouldHandleAllOccurringExceptions_duringMethodExecution() { + // GIVEN + Map<String, String> params = givenInputParams(); + String expectedErrorMessage = "Something went wrong"; + given(chefApiClientFactory.create("https://localhost/organizations/onap", ORGANIZATIONS, USERNAME, + CLIENT_PRIVATE_KEY_PATH)).willThrow(new NullPointerException(expectedErrorMessage)); + + // WHEN // THEN + assertThatExceptionOfType(SvcLogicException.class) + .isThrownBy(() -> chefAdaptorFactory.create().pushJob(params, svcLogicContext)) + .withMessage("Chef Adaptor error:" + expectedErrorMessage); + + assertFalse(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedErrorMessage); + assertThat(svcLogicContext.getAttribute(JOB_ID)).isBlank(); + } + + @SuppressWarnings("unchecked") + @Test + public void checkPushJob_shouldSetFailStatusAndMsgInContext_andThrowException_whenRetryTimesParamIsMissing() { + // GIVEN + Map<String, String> params = givenInputParams( + immutableEntry("retryInterval", "1"), + immutableEntry("jobid", "666")); + + // WHEN // THEN + assertIfInputParamsAreValidated(params); + } + + @SuppressWarnings("unchecked") + @Test + public void checkPushJob_shouldSetFailStatusAndMsgInContext_andThrowException_whenRetryIntervalParamIsMissing() { + // GIVEN + Map<String, String> params = givenInputParams( + immutableEntry("retryTimes", "4"), + immutableEntry("jobid", "666")); + + // WHEN // THEN + assertIfInputParamsAreValidated(params); + } + + @SuppressWarnings("unchecked") + @Test + public void checkPushJob_shouldSetFailStatusAndMsgInContext_andThrowException_whenJobIdParamIsMissing() { + // GIVEN + Map<String, String> params = givenInputParams( + immutableEntry("retryTimes", "4"), + immutableEntry("retryInterval", "1")); + assertIfInputParamsAreValidated(params); + } + + public void assertIfInputParamsAreValidated(Map<String, String> params) { + // WHEN // THEN + assertThatExceptionOfType(SvcLogicException.class) + .isThrownBy(() -> chefAdaptorFactory.create().checkPushJob(params, svcLogicContext)) + .withMessage("Chef Adaptor error:" + "Missing Mandatory param(s) retryTimes , retryInterval "); + + assertFalse(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo("Missing Mandatory param(s) retryTimes , retryInterval "); + } + + @Test + public void checkPushJob_shouldCheckJobStatusOnlyOnce_withoutAdditionalRetries_whenFirstReturnedJobStatusIs_Complete() + throws SvcLogicException { + String expectedHttpStatus = Integer.toString(HttpStatus.SC_OK); + //String expectedMessage = "{status:complete}"; + String expectedMessage = "{\"nodes\":{\"succeeded\":[\"NODE1.atttest.com\"]},\"id\":\"26d\",\"command\":\"chef-client\",\"status\":\"complete\"} "; + assertCheckJobStatusFor( + expectedHttpStatus, + expectedMessage, + ChefResponse.create(HttpStatus.SC_OK, expectedMessage), + ChefResponse.create(HttpStatus.SC_OK, "{status:running}")); + } + + @Test + public void checkPushJob_withFailedNode_whenFirstReturnedJobStatusIs_Complete() + throws SvcLogicException { + String expectedHttpStatus = "401"; + String message = "{\"nodes\":{\"failed\":[\"NODE1.atttest.com\"]},\"id\":\"26d\",\"command\":\"chef-client\",\"status\":\"complete\"} "; + String expectedMessage = "PushJob Status Complete but check failed nodes in the message :" + message; + + assertCheckJobStatusFor( + expectedHttpStatus, + expectedMessage, + ChefResponse.create(HttpStatus.SC_OK, message)); + } + + @Test + public void checkPushJob_shouldCheckJobStatusExpectedNumberOf_ThreeRetryTimes_whenEachReturnedStatusIs_Running() + throws SvcLogicException { + String expectedHttpStatus = Integer.toString(HttpStatus.SC_ACCEPTED); + String expectedMessage = "chef client runtime out"; + + assertCheckJobStatusFor( + expectedHttpStatus, + expectedMessage, + ChefResponse.create(HttpStatus.SC_OK, "{status:running}"), + ChefResponse.create(HttpStatus.SC_OK, "{status:running}"), + ChefResponse.create(HttpStatus.SC_OK, "{status:running}")); + } + + @Test + public void checkPushJob_shouldCheckJobStatusOnlyOnce_withoutAdditionalRetries_whenFirstReturnedJobStatusIsNot_Running() + throws SvcLogicException { + + String expectedHttpStatus = Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR); + String expectedMessage = "{status:unexpectedStatus}"; + + assertCheckJobStatusFor( + expectedHttpStatus, + expectedMessage, + ChefResponse.create(HttpStatus.SC_OK, "{status:unexpectedStatus}"), + ChefResponse.create(HttpStatus.SC_OK, "{status:running}")); + } + + @SuppressWarnings("unchecked") + public void assertCheckJobStatusFor(String expectedHttpStatus, String expectedMessage, ChefResponse firstResponse, + ChefResponse... nextResponses) throws SvcLogicException { + + // GIVEN + Map<String, String> params = givenInputParams( + immutableEntry("jobid", "666"), + immutableEntry("retryTimes", "3"), + immutableEntry("retryInterval", "1")); + given(chefApiClientFactory.create("https://localhost/organizations/onap", ORGANIZATIONS, USERNAME, + CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient); + given(chefApiClient.get(ACTION_PARAM + "/" + params.get("jobid"))) + .willReturn(firstResponse, nextResponses); + + // WHEN + chefAdaptorFactory.create().checkPushJob(params, svcLogicContext); + + // THEN + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(expectedHttpStatus); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedMessage); + } + + @SuppressWarnings("unchecked") + private Map<String, String> givenInputParams(Entry<String, String>... entries) { + Builder<String, String> paramsBuilder = ImmutableMap.builder(); + paramsBuilder.put("username", USERNAME) + .put("serverAddress", SERVER_ADDRESS) + .put("organizations", ORGANIZATIONS); + + for (Entry<String, String> entry : entries) { + paramsBuilder.put(entry); + } + return paramsBuilder.build(); + } + +} diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorImplTest.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorImplTest.java new file mode 100644 index 000000000..1fa6bc168 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorImplTest.java @@ -0,0 +1,197 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.impl; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import java.util.Collections; +import java.util.Map; +import org.apache.http.HttpStatus; +import org.json.JSONObject; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.ChefApiClientFactory; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.api.ChefResponse; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.mockito.Answers.RETURNS_DEEP_STUBS; +import static org.mockito.BDDMockito.given; + +@RunWith(MockitoJUnitRunner.class) +public class ChefAdaptorImplTest { + + private static final String EXPECTED_NODE_OBJECT_ATTR_NAME = "chef.nodeObject"; + private static final String RESULT_CODE_ATTR_KEY = "chefClientResult.code"; + private static final String RESULT_MESSAGE_ATTR_KEY = "chefClientResult.message"; + private static final String EXPECTED_RESPONSE_MSG = "chefResponseMessage"; + private static final String IP_PARAM = "ip"; + private static final String ENDPOINT_IP = "http://127.0.0.1"; + private static final String CHEF_AGENT_CODE_KEY = "chefAgent.code"; + private static final String CHEF_AGENT_MESSAGE_KEY = "chefAgent.message"; + + @Mock(answer = RETURNS_DEEP_STUBS) + private ChefApiClientFactory chefApiClientFactory; + @Mock + private org.onap.ccsdk.sli.adaptors.chef.impl.PrivateKeyChecker privateKeyChecker; + + @InjectMocks + private org.onap.ccsdk.sli.adaptors.chef.impl.ChefAdaptorFactory chefAdaptorFactory; + + @Test + public void nodeObjectBuilder_shouldBuildJsonNodeObject_forPassedParams_andAddToSvcLogicContext() { + // GIVEN + Map<String, String> params = givenInputParams(); + SvcLogicContext svcLogicContext = new SvcLogicContext(); + + // WHEN + chefAdaptorFactory.create().nodeObejctBuilder(params, svcLogicContext); + + // THEN + assertThat(resultJson(svcLogicContext)).isEqualTo(expectedJson()); + } + + private String resultJson(SvcLogicContext svcLogicContext) { + String resultJsonString = svcLogicContext.getAttribute(EXPECTED_NODE_OBJECT_ATTR_NAME); + return new JSONObject(resultJsonString).toString(); + } + + private Map<String, String> givenInputParams() { + return ImmutableMap.<String, String>builder() + .put("nodeobject.name", "testNodeName") + .put("nodeobject.normal", "val:normal") + .put("nodeobject.overrides", "val:override") + .put("nodeobject.defaults", "val:default") + .put("nodeobject.run_list", "val1,val2,val3") + .put("nodeobject.chef_environment", "testChefEnvVal") + .build(); + } + + private String expectedJson() { + JSONObject expectedJson = new JSONObject(); + expectedJson.put("json_class", "Chef::Node"); + expectedJson.put("chef_type", "node"); + expectedJson.put("automatic", Collections.emptyMap()); + expectedJson.put("name", "testNodeName"); + expectedJson.put("normal", ImmutableMap.of("val", "normal")); + expectedJson.put("override", ImmutableMap.of("val", "override")); + expectedJson.put("default", ImmutableMap.of("val", "default")); + expectedJson.put("run_list", ImmutableList.of("val1", "val2", "val3")); + expectedJson.put("chef_environment", "testChefEnvVal"); + return expectedJson.toString(); + } + + @Test + public void combineStrings_shouldConcatenateTwoParamStrings_andSetThemInSvcContext() { + // GIVEN + Map<String, String> params = ImmutableMap + .of("dgContext", "contextValue", "String1", "paramString1", "String2", "paramString2"); + SvcLogicContext svcLogicContext = new SvcLogicContext(); + + // WHEN + chefAdaptorFactory.create().combineStrings(params, svcLogicContext); + + // THEN + assertThat(svcLogicContext.getAttribute("contextValue")).isEqualTo("paramString1paramString2"); + } + + @Test + public void trigger_shouldTriggerTargetEndpoint_andUpdateSvclogicContext() { + // GIVEN + Map<String, String> params = ImmutableMap.of(IP_PARAM, ENDPOINT_IP); + SvcLogicContext svcLogicContext = new SvcLogicContext(); + given(chefApiClientFactory.create(ENDPOINT_IP, "").get("")) + .willReturn(ChefResponse.create(HttpStatus.SC_OK, EXPECTED_RESPONSE_MSG)); + + // WHEN + chefAdaptorFactory.create().trigger(params, svcLogicContext); + + // THEN + assertTrue(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(CHEF_AGENT_CODE_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_OK)); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_OK)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(EXPECTED_RESPONSE_MSG); + } + + @Test + public void trigger_shouldUpdateSvcLogicContext_withFailStatusAndMsg_whenExceptionOccurs() { + // GIVEN + Map<String, String> params = ImmutableMap.of(IP_PARAM, ENDPOINT_IP); + SvcLogicContext svcLogicContext = new SvcLogicContext(); + given(chefApiClientFactory.create(ENDPOINT_IP, "")).willThrow(new RuntimeException()); + + // WHEN + chefAdaptorFactory.create().trigger(params, svcLogicContext); + + // THEN + assertThat(svcLogicContext.getAttribute(CHEF_AGENT_CODE_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR)); + assertThat(svcLogicContext.getAttribute(CHEF_AGENT_MESSAGE_KEY)).isEqualTo(new RuntimeException().toString()); + } + + @Test + public void chefInfo_shouldUpdateSvcLogicContext_withFailStatusAndMsg_andThrowException_whenUsernameParamIsMissing() { + Map<String, String> params = ImmutableMap.of( + "serverAddress", "http://chefAddress", + "organizations", "onap"); + checkIfInputParamsAreValidated(params); + } + + @Test + public void chefInfo_shouldUpdateSvcLogicContext_withFailStatusAndMsg_andThrowException_whenServerAddressParamIsMissing() { + Map<String, String> params = ImmutableMap.of( + "username", "TestUsername", + "organizations", "onap"); + checkIfInputParamsAreValidated(params); + } + + @Test + public void chefInfo_shouldUpdateSvcLogicContext_withFailStatusAndMsg_andThrowException_whenOrganizationsParamIsMissing() { + Map<String, String> params = ImmutableMap.of( + "username", "TestUsername", + "serverAddress", "http://chefAddress"); + checkIfInputParamsAreValidated(params); + } + + private void checkIfInputParamsAreValidated(Map<String, String> params) { + // GIVEN + String expectedErrorMsg = "Missing mandatory param(s) such as username, serverAddress, organizations"; + SvcLogicContext svcLogicContext = new SvcLogicContext(); + + // WHEN// THEN + assertThatExceptionOfType(SvcLogicException.class) + .isThrownBy(() -> chefAdaptorFactory.create().chefGet(params, svcLogicContext)) + .withMessage("Chef Adaptor error:" + + expectedErrorMsg); + + assertFalse(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute("chefServerResult.code")).isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED)); + assertThat(svcLogicContext.getAttribute("chefServerResult.message")).isEqualTo(expectedErrorMsg); + } + +} diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorImplVNFCOperationsTest.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorImplVNFCOperationsTest.java new file mode 100644 index 000000000..253cfcc9f --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/impl/ChefAdaptorImplVNFCOperationsTest.java @@ -0,0 +1,452 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.impl; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMap.Builder; +import java.util.Map; +import java.util.Map.Entry; +import org.apache.http.HttpStatus; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.ChefApiClientFactory; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.api.ChefApiClient; +import org.onap.ccsdk.sli.adaptors.chef.chefclient.api.ChefResponse; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; + +import static com.google.common.collect.Maps.immutableEntry; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import static org.mockito.BDDMockito.given; + +@RunWith(MockitoJUnitRunner.class) +public class ChefAdaptorImplVNFCOperationsTest { + + private static final String CHEF_END_POINT = "https://localhost/organizations/onap"; + private static final String USERNAME = "testclient"; + private static final String ORGANIZATIONS = "onap"; + private static final String SERVER_ADDRESS = "localhost"; + private static final String CLIENT_PRIVATE_KEY_PATH = "/opt/onap/appc/chef/localhost/onap/testclient.pem"; + private static final String RESULT_CODE_ATTR_KEY = "chefServerResult.code"; + private static final String RESULT_MESSAGE_ATTR_KEY = "chefServerResult.message"; + private static final String FAILURE_STATUS = "failure"; + private static final String SUCCESS_STATUS = "success"; + private static final String CHEF_ADAPTOR_ERROR_PREFIX = "Chef Adaptor error:"; + private static final String ENV_PARAM_KEY = "Environment"; + private static final String ENV_JSON_VALUE = "{name:envName}"; + + @Mock + private org.onap.ccsdk.sli.adaptors.chef.impl.PrivateKeyChecker privateKeyChecker; + + @Mock + private ChefApiClientFactory chefApiClientFactory; + + @Mock + private ChefApiClient chefApiClient; + + @InjectMocks + private org.onap.ccsdk.sli.adaptors.chef.impl.ChefAdaptorFactory chefAdaptorFactory; + private SvcLogicContext svcLogicContext; + + @Before + public void setUp() { + svcLogicContext = new SvcLogicContext(); + } + + @SuppressWarnings("unchecked") + @Test + public void vnfcEnvironment_shouldSkipEnvironmentCreation_whenEnvParamIsEmpty() throws SvcLogicException { + // GIVEN + Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, "")); + + // WHEN + chefAdaptorFactory.create().vnfcEnvironment(params, svcLogicContext); + + // THEN + assertTrue(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_OK)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo("Skip Environment block "); + } + + @SuppressWarnings("unchecked") + @Test + public void vnfcEnvironment_shouldCreateNewEnvironment_forEnvParam_whenRequestedEnvDoesNotExist() + throws SvcLogicException { + // GIVEN + String expectedErrorMessage = "New Environment Created"; + Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, ENV_JSON_VALUE)); + given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true); + given(chefApiClientFactory.create(CHEF_END_POINT, ORGANIZATIONS, USERNAME, + CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient); + given(chefApiClient.put("/environments/" + "envName", ENV_JSON_VALUE)) + .willReturn(ChefResponse.create(HttpStatus.SC_NOT_FOUND, "")); + given(chefApiClient.post("/environments", ENV_JSON_VALUE)) + .willReturn(ChefResponse.create(HttpStatus.SC_CREATED, expectedErrorMessage)); + + // WHEN + chefAdaptorFactory.create().vnfcEnvironment(params, svcLogicContext); + + // THEN + assertTrue(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_CREATED)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedErrorMessage); + } + + @SuppressWarnings("unchecked") + @Test + public void vnfcEnvironment_shouldNotAttemptEnvCreation_andThrowException_whenPrivateKeyCheckFails() { + // GIVEN + String expectedErrorMsg = "Cannot find the private key in the APPC file system, please load the private key to "; + Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, ENV_JSON_VALUE)); + given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(false); + + // WHEN // THEN + assertThatExceptionOfType(SvcLogicException.class) + .isThrownBy(() -> chefAdaptorFactory.create().vnfcEnvironment(params, svcLogicContext)) + .withMessage(CHEF_ADAPTOR_ERROR_PREFIX + expectedErrorMsg + CLIENT_PRIVATE_KEY_PATH); + + assertFalse(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedErrorMsg + CLIENT_PRIVATE_KEY_PATH); + } + + @SuppressWarnings("unchecked") + @Test + public void vnfcEnvironment_shouldNotAttemptEnvCreation_andHandleJSONException_whenJSONParamsAreMalformed() { + // GIVEN + String expectedErrorMessage = "Error posting request due to invalid JSON block: "; + Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, "MALFORMED_JSON")); + given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true); + + // WHEN // THEN + assertThatExceptionOfType(SvcLogicException.class) + .isThrownBy(() -> chefAdaptorFactory.create().vnfcEnvironment(params, svcLogicContext)) + .withMessageStartingWith(CHEF_ADAPTOR_ERROR_PREFIX + expectedErrorMessage); + + assertFalse(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).startsWith(expectedErrorMessage); + } + + @SuppressWarnings("unchecked") + @Test + public void vnfcEnvironment_shouldNotAttemptEnvCreation_andHandleException_whenExceptionOccursDuringExecution() { + // GIVEN + String expectedErrorMessage = "Error posting request: "; + Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, ENV_JSON_VALUE)); + given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true); + given(chefApiClientFactory.create(CHEF_END_POINT, ORGANIZATIONS, USERNAME, + CLIENT_PRIVATE_KEY_PATH)).willThrow(new NullPointerException("Null value encountered")); + + // WHEN // THEN + assertThatExceptionOfType(SvcLogicException.class) + .isThrownBy(() -> chefAdaptorFactory.create().vnfcEnvironment(params, svcLogicContext)) + .withMessage(CHEF_ADAPTOR_ERROR_PREFIX + expectedErrorMessage + "vnfcEnvironmentNull value encountered"); + + assertFalse(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).startsWith(expectedErrorMessage); + } + + @Test + public void vnfcNodeObjects_shouldUpdateNodeObjects_andSetCodeAndMessageFromLastSuccessfulResponseInSvcLogicContext() + throws SvcLogicException { + // GIVEN + ChefResponse firstNodeResponse = ChefResponse.create(HttpStatus.SC_OK, "firstMessage"); + ChefResponse secondNodeResponse = ChefResponse.create(HttpStatus.SC_OK, "secondMessage"); + int expectedHttpStatus = HttpStatus.SC_OK; + String expectedMessage = "secondMessage"; + + assertNodeObjectsAreUpdatedFor(firstNodeResponse, secondNodeResponse, expectedHttpStatus, expectedMessage); + } + + @Test + public void vnfcNodeObjects_shouldStopProcessingNodeObjectUpdates_whenFirstReturnedResponseIsOtherThan_200() + throws SvcLogicException { + ChefResponse firstNodeResponse = ChefResponse.create(HttpStatus.SC_ACCEPTED, "firstMessage"); + ChefResponse secondNodeResponse = ChefResponse.create(HttpStatus.SC_OK, "secondMessage"); + int expectedHttpStatus = HttpStatus.SC_ACCEPTED; + String expectedMessage = "firstMessage"; + + assertNodeObjectsAreUpdatedFor(firstNodeResponse, secondNodeResponse, expectedHttpStatus, expectedMessage); + } + + @SuppressWarnings("unchecked") + public void assertNodeObjectsAreUpdatedFor(ChefResponse firstNodeResponse, ChefResponse secondNodeResponse, + int expectedHttpStatus, String expectedMessage) throws SvcLogicException { + // GIVEN + Map<String, String> params = givenInputParams( + immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]"), + immutableEntry("Node", "{name:nodeName}")); + + given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true); + given(chefApiClientFactory.create(CHEF_END_POINT, ORGANIZATIONS, USERNAME, + CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient); + given(chefApiClient.put("/nodes/" + "test1.vnf_b.onap.com", "{\"name\":\"test1.vnf_b.onap.com\"}")) + .willReturn(firstNodeResponse); + given(chefApiClient.put("/nodes/" + "test2.vnf_b.onap.com", "{\"name\":\"test2.vnf_b.onap.com\"}")) + .willReturn(secondNodeResponse); + + // WHEN + chefAdaptorFactory.create().vnfcNodeobjects(params, svcLogicContext); + + // THEN + assertTrue(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(expectedHttpStatus)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedMessage); + } + + @SuppressWarnings("unchecked") + @Test + public void vnfcNodeObjects_shouldThrowSvcLogicException_whenNodeListParamIsEmpty() { + Map<String, String> params = givenInputParams( + immutableEntry("NodeList", ""), + immutableEntry("Node", "{name:nodeName}")); + checkMissingParamsAreValidated(params); + } + + @SuppressWarnings("unchecked") + @Test + public void vnfcNodeObjects_shouldThrowSvcLogicException_whenNodeParamIsEmpty() { + Map<String, String> params = givenInputParams( + immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]"), + immutableEntry("Node", "")); + checkMissingParamsAreValidated(params); + } + + public void checkMissingParamsAreValidated(Map<String, String> params) { + // GIVEN + String expectedErrorMsg = "vnfcNodeobjectsMissing Mandatory param(s) Node , NodeList "; + + // WHEN // THEN + assertThatExceptionOfType(SvcLogicException.class) + .isThrownBy(() -> chefAdaptorFactory.create().vnfcNodeobjects(params, svcLogicContext)) + .withMessage(CHEF_ADAPTOR_ERROR_PREFIX + "Error posting request: " + expectedErrorMsg); + + assertFalse(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo("Error posting request: " + expectedErrorMsg); + } + + @SuppressWarnings("unchecked") + @Test + public void vnfcNodeObjects_shouldNotUpdateNodes_andHandleJSONException_whenJSONParamsAreMalformed() { + // GIVEN + Map<String, String> params = givenInputParams( + immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]"), + immutableEntry("Node", "MALFORMED_JSON")); + String expectedErrorMessage = "Error posting request due to invalid JSON block: "; + given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true); + + // WHEN // THEN + assertThatExceptionOfType(SvcLogicException.class) + .isThrownBy(() -> chefAdaptorFactory.create().vnfcNodeobjects(params, svcLogicContext)) + .withMessageStartingWith(CHEF_ADAPTOR_ERROR_PREFIX + expectedErrorMessage); + + assertFalse(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).startsWith(expectedErrorMessage); + } + + @SuppressWarnings("unchecked") + @Test + public void vnfcPushJob_shouldUpdateSvcContextWithJobId_whenPushJobWasSuccessfullyCreatedWithCallbackUrl() + throws SvcLogicException { + Map<String, String> params = givenInputParams( + immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]"), + immutableEntry("CallbackCapable", "true"), + immutableEntry("RequestId", "666"), + immutableEntry("CallbackUrl", "someURLForCallback")); + int expectedResponseStatus = HttpStatus.SC_CREATED; + String expectedResponseMessage = "jobs:666-9"; + + assertVnfcPushJobExecutionFor(params, buildJsonRequestWithCallback(), expectedResponseStatus, expectedResponseMessage); + assertThat(svcLogicContext.getAttribute("jobID")).isEqualTo("666"); + } + + private String buildJsonRequestWithCallback() { + return "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":" + + "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]" + "," + "\"env\": {\"RequestId\": \"" + "666" + + "\", \"CallbackUrl\": \"" + + "someURLForCallback" + "\"}," + "\"capture_output\": true" + "}"; + } + + @SuppressWarnings("unchecked") + @Test + public void vnfcPushJob_shouldUpdateSvcContextWithJobId_whenPushJobWasSuccessfullyCreatedWithoutCallbackUrl() + throws SvcLogicException { + Map<String, String> params = givenInputParams( + immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]"), + immutableEntry("RequestId", "666")); + int expectedResponseStatus = HttpStatus.SC_OK; + String expectedResponseMessage = "jobs:666-9"; + + assertVnfcPushJobExecutionFor(params, buildJsonRequestWithoutCallback(), expectedResponseStatus, expectedResponseMessage); + assertThat(svcLogicContext.getAttribute("jobID")).isBlank(); + } + + private String buildJsonRequestWithoutCallback() { + return "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":" + + "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]" + "," + "\"env\": {}," + "\"capture_output\": true" + + "}"; + } + + public void assertVnfcPushJobExecutionFor(Map<String, String> params, String pushRequestWithCallback, + int expectedResponseStatus, String expectedResponseMessage) throws SvcLogicException { + // GIVEN + given(chefApiClientFactory.create(CHEF_END_POINT, ORGANIZATIONS, USERNAME, + CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient); + given(chefApiClient.post("/pushy/jobs", pushRequestWithCallback)) + .willReturn(ChefResponse.create(expectedResponseStatus, expectedResponseMessage)); + + // WHEN + chefAdaptorFactory.create().vnfcPushJob(params, svcLogicContext); + + // THEN + assertTrue(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(expectedResponseStatus)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedResponseMessage); + } + + @SuppressWarnings("unchecked") + @Test + public void vnfcPushJob_shouldNotPushJob_andThrowException_whenNodeListParamIsEmpty() { + // GIVEN + String expectedErrorMessage = "Error posting request: vnfcPushJobMissing Mandatory param(s) NodeList "; + Map<String, String> params = givenInputParams(); + // WHEN // THEN + assertThatExceptionOfType(SvcLogicException.class) + .isThrownBy(() -> chefAdaptorFactory.create().vnfcPushJob(params, svcLogicContext)) + .withMessageStartingWith(CHEF_ADAPTOR_ERROR_PREFIX + expectedErrorMessage); + + assertFalse(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedErrorMessage); + } + + @SuppressWarnings("unchecked") + @Test + public void fetchResults_shouldNotFetchResults_andThrowException_whenNodeListParamIsEmpty() { + // GIVEN + String expectedErrorMessage = "Error posting request: fetchResultsMissing Mandatory param(s) NodeList "; + Map<String, String> params = givenInputParams(); + // WHEN // THEN + assertThatExceptionOfType(SvcLogicException.class) + .isThrownBy(() -> chefAdaptorFactory.create().fetchResults(params, svcLogicContext)) + .withMessageStartingWith(CHEF_ADAPTOR_ERROR_PREFIX + expectedErrorMessage); + + assertFalse(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedErrorMessage); + } + + @SuppressWarnings("unchecked") + @Test + public void fetchResults_shouldNotFetchResults_andThrowException_whenPrivateKeyCheckFails() { + // GIVEN + Map<String, String> params = givenInputParams( + immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]")); + String expectedErrorMessage = + "Error posting request: fetchResults" + + CHEF_ADAPTOR_ERROR_PREFIX + + "Cannot find the private key in the APPC file system, please load the private key to " + + CLIENT_PRIVATE_KEY_PATH; + given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(false); + + // WHEN // THEN + assertThatExceptionOfType(SvcLogicException.class) + .isThrownBy(() -> chefAdaptorFactory.create().fetchResults(params, svcLogicContext)) + .withMessage(CHEF_ADAPTOR_ERROR_PREFIX + expectedErrorMessage); + + assertFalse(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedErrorMessage); + } + + @SuppressWarnings("unchecked") + @Test + public void fetchResults_shouldUpdateSvcLogicContextWithJsonResponse_fromSuccessfulChefServerCall() + throws SvcLogicException { + // GIVEN + String json = "{normal:{PushJobOutput : \"ssh start/running, process 1090\"}}"; + Map<String, String> params = givenInputParams( + immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\"]")); + given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true); + given(chefApiClientFactory.create(CHEF_END_POINT, ORGANIZATIONS, USERNAME, + CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient); + given(chefApiClient.get("/nodes/" + "test1.vnf_b.onap.com")) + .willReturn(ChefResponse.create(HttpStatus.SC_OK, json)); + + // WHEN + chefAdaptorFactory.create().fetchResults(params, svcLogicContext); + + // THEN + assertTrue(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_OK)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)) + .isEqualTo("{\"test1.vnf_b.onap.com\":{\"PushJobOutput\":\"ssh start/running, process 1090\"}}"); + } + + @SuppressWarnings("unchecked") + @Test + public void fetchResults_shouldUpdateSvcLogicContextWithFailedMessage_whenReturnedJSONMessageIsMissingAttribute() + throws SvcLogicException { + // GIVEN + String json = "{normal:{invalidKey : \"ssh start/running, process 1090\"}}"; + Map<String, String> params = givenInputParams( + immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\"]")); + given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true); + given(chefApiClientFactory.create(CHEF_END_POINT, ORGANIZATIONS, USERNAME, + CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient); + given(chefApiClient.get("/nodes/" + "test1.vnf_b.onap.com")) + .willReturn(ChefResponse.create(HttpStatus.SC_OK, json)); + + // WHEN + chefAdaptorFactory.create().fetchResults(params, svcLogicContext); + + // THEN + assertTrue(svcLogicContext.isSuccess()); + assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR)); + assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo("Cannot find PushJobOutput"); + } + + @SuppressWarnings("unchecked") + private Map<String, String> givenInputParams(Entry<String, String>... entries) { + Builder<String, String> paramsBuilder = ImmutableMap.builder(); + paramsBuilder.put("username", USERNAME) + .put("serverAddress", SERVER_ADDRESS) + .put("organizations", ORGANIZATIONS); + + for (Entry<String, String> entry : entries) { + paramsBuilder.put(entry); + } + return paramsBuilder.build(); + } + +} diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/impl/PrivateKeyCheckerTest.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/impl/PrivateKeyCheckerTest.java new file mode 100644 index 000000000..e8d383ab1 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/sli/adaptors/chef/impl/PrivateKeyCheckerTest.java @@ -0,0 +1,41 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.adaptors.chef.impl; + +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class PrivateKeyCheckerTest { + + @Test + public void doesExist_shouldReturnTrue_whenFileExists() { + String pemFilePath = getClass().getResource("/testclient.pem").getPath(); + assertTrue(new org.onap.ccsdk.sli.adaptors.chef.impl.PrivateKeyChecker().doesExist(pemFilePath)); + } + + @Test + public void doesExist_shouldReturnFalse_whenFileDoesNotExist() { + assertFalse(new org.onap.ccsdk.sli.adaptors.chef.impl.PrivateKeyChecker().doesExist("dummyPemFile")); + } + +}
\ No newline at end of file diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/test/ExecutorHarness.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/test/ExecutorHarness.java new file mode 100644 index 000000000..f3878148e --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/test/ExecutorHarness.java @@ -0,0 +1,170 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.test; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin; + +/** + * This class is used as a test harness to wrap the call to an executor node. + */ + +public class ExecutorHarness { + + /** + * The executor to be tested + */ + private SvcLogicJavaPlugin executor; + + /** + * The collection of all exec methods found on the class + */ + private Map<String, Method> methods; + + /** + * The field of the class being tested that contains the reference to the logger to be used. This is modified to + * point to our interception logger for the test. + */ + private Field contextLogger; + + /** + * The interception logger that buffers all messages logged and allows us to look at them as part of the test case. + */ + private org.onap.ccsdk.test.InterceptLogger logger; + + /** + * Create the harness and initialize it + * + * @throws SecurityException If a security manager, s, is present and any of the following conditions is met: + * <ul> + * <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li> + * <li>the caller's class loader is not the same as or an ancestor of the class loader for the current + * class and invocation of s.checkPackageAccess() denies access to the package of this class</li> + * </ul> + * @throws NoSuchFieldException if a field with the specified name is not found. + * @throws IllegalAccessException if this Field object is enforcing Java language access control and the underlying field is either + * inaccessible or final. + * @throws IllegalArgumentException if the specified object is not an instance of the class or interface declaring the underlying field + * (or a subclass or implementor thereof), or if an unwrapping conversion fails. + */ + @SuppressWarnings("nls") + public ExecutorHarness() throws NoSuchFieldException, SecurityException, IllegalArgumentException, + IllegalAccessException { + methods = new HashMap<>(); + new SvcLogicContext(); + + Class<?> contextClass = SvcLogicContext.class; + contextLogger = contextClass.getDeclaredField("LOG"); + contextLogger.setAccessible(true); + logger = new org.onap.ccsdk.test.InterceptLogger(); + contextLogger.set(null, logger); + } + + /** + * Convenience constructor + * + * @param executor The executor to be tested by the harness + * + * @throws SecurityException If a security manager, s, is present and any of the following conditions is met: + * <ul> + * <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li> + * <li>the caller's class loader is not the same as or an ancestor of the class loader for the current + * class and invocation of s.checkPackageAccess() denies access to the package of this class</li> + * </ul> + * @throws NoSuchFieldException if a field with the specified name is not found. + * @throws IllegalAccessException if this Field object is enforcing Java language access control and the underlying field is either + * inaccessible or final. + * @throws IllegalArgumentException if the specified object is not an instance of the class or interface declaring the underlying field + * (or a subclass or implementor thereof), or if an unwrapping conversion fails. + */ + public ExecutorHarness(SvcLogicJavaPlugin executor) throws NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { + this(); + setExecutor(executor); + } + + /** + * @return The java plugin class to be executed + */ + public SvcLogicJavaPlugin getExecutor() { + return executor; + } + + /** + * @param executor The java plugin class to be executed + */ + public void setExecutor(SvcLogicJavaPlugin executor) { + this.executor = executor; + scanExecutor(); + } + + /** + * @return The set of all methods that meet the signature requirements + */ + public List<String> getExecMethodNames() { + List<String> names = new ArrayList<>(); + names.addAll(methods.keySet()); + return names; + } + + /** + * Returns an indication if the named method is a valid executor method that could be called from a DG execute node + * + * @param methodName The method name to be validated + * + * @return True if the method name meets the signature requirements, false if the method either does not exist or + * does not meet the requirements. + */ + public boolean isExecMethod(String methodName) { + return methods.containsKey(methodName); + } + + /** + * This method scans the executor class hierarchy to locate all methods that match the required signature of the + * executor and records these methods in a map. + */ + private void scanExecutor() { + methods.clear(); + Class<?> executorClass = executor.getClass(); + Method[] publicMethods = executorClass.getMethods(); + for (Method method : publicMethods) { + if (method.getReturnType().equals(Void.class)) { + Class<?>[] paramTypes = method.getParameterTypes(); + if (paramTypes.length == 2) { + if (Map.class.isAssignableFrom(paramTypes[0]) + && SvcLogicContext.class.isAssignableFrom(paramTypes[1])) { + methods.put(method.getName(), method); + } + } + } + } + } + +} diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/test/InterceptLogger.java b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/test/InterceptLogger.java new file mode 100644 index 000000000..9f80f6880 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/java/org/onap/ccsdk/test/InterceptLogger.java @@ -0,0 +1,447 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.test; + +import ch.qos.logback.classic.Level; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.List; +import org.slf4j.Marker; + +/** + * This class is used as an intercept logger that can be used in testing to intercept and record all messages that are + * logged, thus allowing a junit test case to examine the log output and make assertions. + */ +public class InterceptLogger implements org.slf4j.Logger { + + /** + * The list of all intercepted log events + */ + private List<LogRecord> events; + + /** + * Create the intercept logger + */ + public InterceptLogger() { + events = new ArrayList<LogRecord>(1000); + } + + /** + * @return Returns all intercepted log events + */ + public List<LogRecord> getLogRecords() { + return events; + } + + /** + * Clears all log events + */ + public void clear() { + events.clear(); + } + + @Override + public void debug(Marker marker, String msg) { + debug(msg); + } + + @Override + public void debug(Marker marker, String format, Object arg) { + debug(MessageFormat.format(format, arg)); + } + + @Override + public void debug(Marker marker, String format, Object... arguments) { + debug(MessageFormat.format(format, arguments)); + } + + @Override + public void debug(Marker marker, String format, Object arg1, Object arg2) { + debug(MessageFormat.format(format, arg1, arg2)); + } + + @Override + public void debug(Marker marker, String msg, Throwable t) { + debug(msg, t); + } + + @Override + public void debug(String msg) { + events.add(new LogRecord(Level.DEBUG, msg)); + } + + @Override + public void debug(String format, Object arg) { + events.add(new LogRecord(Level.DEBUG, MessageFormat.format(format, arg))); + } + + @Override + public void debug(String format, Object... arguments) { + events.add(new LogRecord(Level.DEBUG, MessageFormat.format(format, arguments))); + } + + @Override + public void debug(String format, Object arg1, Object arg2) { + events.add(new LogRecord(Level.DEBUG, MessageFormat.format(format, arg1, arg2))); + } + + @Override + public void debug(String msg, Throwable t) { + events.add(new LogRecord(Level.DEBUG, msg, t)); + } + + @Override + public void error(Marker marker, String msg) { + error(msg); + } + + @Override + public void error(Marker marker, String format, Object arg) { + error(format, arg); + } + + @Override + public void error(Marker marker, String format, Object... arguments) { + error(format, arguments); + } + + @Override + public void error(Marker marker, String format, Object arg1, Object arg2) { + error(format, arg1, arg2); + } + + @Override + public void error(Marker marker, String msg, Throwable t) { + events.add(new LogRecord(Level.ERROR, msg, t)); + } + + @Override + public void error(String msg) { + events.add(new LogRecord(Level.ERROR, msg)); + } + + @Override + public void error(String format, Object arg) { + events.add(new LogRecord(Level.ERROR, MessageFormat.format(format, arg))); + } + + @Override + public void error(String format, Object... arguments) { + events.add(new LogRecord(Level.ERROR, MessageFormat.format(format, arguments))); + } + + @Override + public void error(String format, Object arg1, Object arg2) { + events.add(new LogRecord(Level.ERROR, MessageFormat.format(format, arg1, arg2))); + } + + @Override + public void error(String msg, Throwable t) { + events.add(new LogRecord(Level.ERROR, msg, t)); + } + + @Override + public String getName() { + return null; + } + + @Override + public void info(Marker marker, String msg) { + info(msg); + } + + @Override + public void info(Marker marker, String format, Object arg) { + info(format, arg); + } + + @Override + public void info(Marker marker, String format, Object... arguments) { + info(format, arguments); + } + + @Override + public void info(Marker marker, String format, Object arg1, Object arg2) { + info(format, arg1, arg2); + } + + @Override + public void info(Marker marker, String msg, Throwable t) { + events.add(new LogRecord(Level.INFO, msg, t)); + } + + @Override + public void info(String msg) { + events.add(new LogRecord(Level.INFO, msg)); + } + + @Override + public void info(String format, Object arg) { + events.add(new LogRecord(Level.INFO, MessageFormat.format(format, arg))); + } + + @Override + public void info(String format, Object... arguments) { + events.add(new LogRecord(Level.INFO, MessageFormat.format(format, arguments))); + } + + @Override + public void info(String format, Object arg1, Object arg2) { + events.add(new LogRecord(Level.INFO, MessageFormat.format(format, arg1, arg2))); + } + + @Override + public void info(String msg, Throwable t) { + events.add(new LogRecord(Level.INFO, msg, t)); + } + + @Override + public boolean isDebugEnabled() { + return true; + } + + @Override + public boolean isDebugEnabled(Marker marker) { + return true; + } + + @Override + public boolean isErrorEnabled() { + return true; + } + + @Override + public boolean isErrorEnabled(Marker marker) { + return true; + } + + @Override + public boolean isInfoEnabled() { + return true; + } + + @Override + public boolean isInfoEnabled(Marker marker) { + return true; + } + + @Override + public boolean isTraceEnabled() { + return true; + } + + @Override + public boolean isTraceEnabled(Marker marker) { + return true; + } + + @Override + public boolean isWarnEnabled() { + return true; + } + + @Override + public boolean isWarnEnabled(Marker marker) { + return true; + } + + @Override + public void trace(Marker marker, String msg) { + trace(msg); + } + + @Override + public void trace(Marker marker, String format, Object arg) { + trace(format, arg); + } + + @Override + public void trace(Marker marker, String format, Object... argArray) { + trace(format, argArray); + } + + @Override + public void trace(Marker marker, String format, Object arg1, Object arg2) { + trace(format, arg1, arg2); + } + + @Override + public void trace(Marker marker, String msg, Throwable t) { + trace(msg, t); + } + + @Override + public void trace(String msg) { + events.add(new LogRecord(Level.TRACE, msg)); + } + + @Override + public void trace(String format, Object arg) { + events.add(new LogRecord(Level.TRACE, MessageFormat.format(format, arg))); + } + + @Override + public void trace(String format, Object... arguments) { + events.add(new LogRecord(Level.TRACE, MessageFormat.format(format, arguments))); + } + + @Override + public void trace(String format, Object arg1, Object arg2) { + events.add(new LogRecord(Level.TRACE, MessageFormat.format(format, arg1, arg2))); + } + + @Override + public void trace(String msg, Throwable t) { + events.add(new LogRecord(Level.TRACE, msg, t)); + } + + @Override + public void warn(Marker marker, String msg) { + warn(msg); + } + + @Override + public void warn(Marker marker, String format, Object arg) { + warn(format, arg); + } + + @Override + public void warn(Marker marker, String format, Object... arguments) { + warn(format, arguments); + } + + @Override + public void warn(Marker marker, String format, Object arg1, Object arg2) { + warn(format, arg1, arg2); + } + + @Override + public void warn(Marker marker, String msg, Throwable t) { + events.add(new LogRecord(Level.WARN, msg, t)); + } + + @Override + public void warn(String msg) { + events.add(new LogRecord(Level.WARN, msg)); + } + + @Override + public void warn(String format, Object arg) { + events.add(new LogRecord(Level.WARN, MessageFormat.format(format, arg))); + } + + @Override + public void warn(String format, Object... arguments) { + events.add(new LogRecord(Level.WARN, MessageFormat.format(format, arguments))); + } + + @Override + public void warn(String format, Object arg1, Object arg2) { + events.add(new LogRecord(Level.WARN, MessageFormat.format(format, arg1, arg2))); + } + + @Override + public void warn(String msg, Throwable t) { + events.add(new LogRecord(Level.WARN, msg, t)); + } + + /** + * This inner class represents an intercepted log event + */ + public class LogRecord { + private Level level; + private String message; + private long timestamp; + private Throwable t; + + public LogRecord(Level level, String message) { + setLevel(level); + setTimestamp(System.currentTimeMillis()); + setMessage(message); + } + + public LogRecord(Level level, String message, Throwable t) { + this(level, message); + setThrowable(t); + } + + /** + * @return the value of level + */ + public Level getLevel() { + return level; + } + + /** + * @param level the value for level + */ + public void setLevel(Level level) { + this.level = level; + } + + /** + * @return the value of message + */ + public String getMessage() { + return message; + } + + /** + * @param message the value for message + */ + public void setMessage(String message) { + this.message = message; + } + + /** + * @return the value of timestamp + */ + public long getTimestamp() { + return timestamp; + } + + /** + * @param timestamp the value for timestamp + */ + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } + + /** + * @return the value of t + */ + public Throwable getThrowable() { + return t; + } + + /** + * @param t the value for t + */ + public void setThrowable(Throwable t) { + this.t = t; + } + + } + +} diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/resources/properties/chef-adaptor-test.properties b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/resources/properties/chef-adaptor-test.properties new file mode 100644 index 000000000..4ccbf654c --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/resources/properties/chef-adaptor-test.properties @@ -0,0 +1,101 @@ +### +# ============LICENSE_START======================================================= +# ONAP : APPC +# ================================================================================ +# Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Copyright (C) 2017 Amdocs +# ============================================================================= +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ============LICENSE_END========================================================= +### +# +# Default properties for the APP-C Provider Adaptor +# +# ------------------------------------------------------------------------------------------------- +# +# Define the name and path of any user-provided configuration (bootstrap) file that can be loaded +# to supply configuration options +org.onap.appc.bootstrap.file=appc.properties +org.onap.appc.bootstrap.path=/opt/onap/appc/data/properties,${user.home},. +appc.application.name=APPC +# +# Define the message resource bundle name to be loaded +org.onap.appc.resources=org/onap/appc/i18n/MessageResources +# +# The name of the adaptor. +org.onap.appc.provider.adaptor.name=org.onap.appc.appc_provider_adaptor +# +# Set up the logging environment +# +org.onap.appc.logging.file=org/onap/appc/logback.xml +org.onap.appc.logging.path=${user.home};etc;../etc +org.onap.appc.logger=org.onap.appc +org.onap.appc.security.logger=org.onap.appc.security +# +# The minimum and maximum provider/tenant context pool sizes. Min=1 means that as soon +# as the provider/tenant is referenced a Context is opened and added to the pool. Max=0 +# means that the upper bound on the pool is unbounded. +org.onap.appc.provider.min.pool=1 +org.onap.appc.provider.max.pool=0 +# +# The following properties are used to configure the retry logic for connection to the +# IaaS provider(s). The retry delay property is the amount of time, in seconds, the +# application waits between retry attempts. The retry limit is the number of retries +# that are allowed before the request is failed. +org.onap.appc.provider.retry.delay=30 +org.onap.appc.provider.retry.limit=10 +# +# The trusted hosts list for SSL access when a certificate is not provided. +# +provider.trusted.hosts=* +# +# The amount of time, in seconds, to wait for a server state change (start->stop, stop->start, etc). +# If the server does not change state to a valid state within the alloted time, the operation +# fails. +org.onap.appc.server.state.change.timeout=300 +# +# The amount of time to wait, in seconds, between subsequent polls to the OpenStack provider +# to refresh the status of a resource we are waiting on. +# +org.onap.appc.openstack.poll.interval=20 +# +# The connection information to connect to the provider we are using. These properties +# are "structured" properties, in that the name is a compound name, where the nodes +# of the name can be ordered (1, 2, 3, ...). All of the properties with the same ordinal +# position are defining the same entity. For example, provider1.type and provider1.name +# are defining the same provider, whereas provider2.name and provider2.type are defining +# the values for a different provider. Any number of providers can be defined in this +# way. +# +# Don't change these 2 right now since they are hard coded in the DG +#provider1.type=appc +#provider1.name=appc +#These you can change +#provider1.identity=appc +#provider1.tenant1.name=appc +#provider1.tenant1.userid=appc +#provider1.tenant1.password=appc +# After a change to the provider make sure to recheck these values with an api call to provider1.identity/tokens +test.expected-regions=1 +test.expected-endpoints=1 +#Your OpenStack IP +#test.ip=192.168.1.2 +# Your OpenStack Platform's Keystone Port (default is 5000) +#test.port=5000 +#test.tenantid=abcde12345fghijk6789lmnopq123rst +#test.vmid=abc12345-1234-5678-890a-abcdefg12345 +# Port 8774 below is default port for OpenStack's Nova API Service +#test.url=http://192.168.1.2:8774/v2/abcde12345fghijk6789lmnopq123rst/servers/abc12345-1234-5678-890a-abcdefg12345 + diff --git a/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/resources/testclient.pem b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/resources/testclient.pem new file mode 100644 index 000000000..a12f38118 --- /dev/null +++ b/adaptors/chef-adaptor/chef-adaptor-bundle/src/test/resources/testclient.pem @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEowIBAAKCAQEAqDFyi0Tp4CQN2Q1kp2NXLPl5P4CVqjdXTUgxugupRC9aqJa5 +l7NOOkQWadSaU/CCtdsikp6ymyxEqR0Y3TtxmY3ongFBtLDaU/UnWXZY0pcaJBN/ +ZZKV9++lZF8+USKlZTQo+EqD7XlEjiDKjfwylaMTSwQPKGa0bDZZ6U44K63uuClw +mkvhtlaeS+t4Ah/YMU2nSxrWH3CpPxO4qD2KducrNRs01+hRxoVEaEX7HGXi8zdz +BHSp8mcA8mGl9uJFXN8Gtgphrlknq7DPjVb0iZ9Lw2u6WNhZizFHghEwHRSJYn26 +DNzmOjyD0IaQf5lf1L1sO5DcPkWC2I21MNaE4QIDAQABAoIBAEa/Xe4lE7d7kvOd +BZy/VZkOaykB/nJ2CtvwJTKb2xxaSuklVXXxL1Ok9kSX8D6kqWazgYxpArnw2gTE +v4O3kGZF4fYskyXdSkkMkvu3o08Zzh4ksW7ZRQngnRJmWcEpMKcsVJt0RKAsZWDf +fDRTRDfbO69PSsz0vqnSBunzQ/9i4LbFDOKYnWFDOiGKzM8SXMvVUVpGAzvc773M +lthBAo9KpbNrbO0b3OGUM8pU2o9GRbBAEzIq8j/i2h/vHtswgM9g3IsHNqYbvJPD +uAY8hsYEPZh/RasIBLN1J9HD+Ex1q2OA+Yi3jBtT4s545MJIHpdmo57/B2SHQbug +KuWTsb0CgYEA0ZKIig/jeZEOokvWxDrN6Ok2Hb9TFZcNi/qBLcQ25A1C2fDkvsHc +S6UDxknT2cyQ77R9xK25vrFKKrv5FUwgg7h0ps19w8xSZRG9Wvg0nus69oavXIDi +MOCBfb6pn2+Glhdt84Ku0oSTPtfcTHerACOunGATpk1NW2dlmDbvmTcCgYEAzXQs +9lS31+2Oh64g4gH/6Jfx+Mh5JY+2bUhSMy53tulzFwfI1zLyyu9SLLfVbAnxhmS8 +rSg7Q2mN492ZhnoVScYN3RTpudBJIinqlFoarqu+6E83k+83wGv3m2pP6XPbpHCy +QE6IQm5U+ZSbMRD+8SjG3pbPRYcwww5xRsIj/qcCgYA2kEc6Yu6fzROZT9OH3aOU +u3tafWC9Y0mko0EU0FxWPdmk8qIrxD999mWoL7qXnzoxHrYMCgstSe18eNpeICbr +BJBiiWfwHXdqVxcM40iYA7ijTOfFVs0NWrZ8LbLuDtRkqY738pNfviK0HvF9ez6r +V57zmdQj3UaBwMbjvZHeOwKBgQCA0gsvGMd8+FKZ+DBeBWrz5/GsT+bGCmoT83i2 +5rfhVFb5ZcQkSqm5XH1l1I5ZA5MQ9TDoUYV3K0PwUA3nJ70ZWLlwmaBDBboVVbrj +8esxAjbdam4qr5+BYzEJnYslkaNyY8cgUx1UqeFV7DuydDml9C9deanUqoOEihW0 +jB4NmQKBgD4nwTUcQIbzeWnFoPxbDMQIIheZ3EK+rEbmXiU3blPhmCoye9q58Qet +YaeQKiJEF2mWnBE6VtSg84OiENSAMxUn2VlwopFoTbfLKD4qfGoGWm9tjUdBblHe +3U3ZdQLYKTiSNr+LXrAI0w4MukmL8vzxzo80tcB4+tePyWN/lqoq +-----END RSA PRIVATE KEY----- |