Fixed sonar issue in ProviderAdapterImpl
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / main / java / org / onap / appc / adapter / iaas / impl / ProviderAdapterImpl.java
index ba6c8c6..cd14ffa 100644 (file)
@@ -2,10 +2,12 @@
  * ============LICENSE_START=======================================================
  * ONAP : APPC
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * 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
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
  * ============LICENSE_END=========================================================
  */
-
-
 package org.onap.appc.adapter.iaas.impl;
 
 import org.onap.appc.Constants;
@@ -34,6 +33,7 @@ import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
 import org.onap.appc.adapter.iaas.provider.operation.impl.EvacuateServer;
 import org.onap.appc.configuration.Configuration;
 import org.onap.appc.configuration.ConfigurationFactory;
+import org.onap.appc.encryption.EncryptionTool;
 import org.onap.appc.exceptions.APPCException;
 import org.onap.appc.util.StructuredPropertyHelper;
 import org.onap.appc.util.StructuredPropertyHelper.Node;
@@ -47,7 +47,6 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
-
 /**
  * This class implements the {@link ProviderAdapter} interface. This interface defines the behaviors that our service
  * provides.
@@ -61,26 +60,18 @@ public class ProviderAdapterImpl implements ProviderAdapter {
      * The default domain name for authentication
      */
     public static final String DEFAULT_DOMAIN_NAME = "Default";
-
-    /**
-     * The logger to be used
-     */
-    private static final EELFLogger logger = EELFManager.getInstance().getLogger(ProviderAdapterImpl.class);
     /**
      * A reference to the adapter configuration object.
      */
     private Configuration configuration;
-
     /**
      * reference to operation factory
      */
-    ProviderOperationFactory factory = ProviderOperationFactory.getInstance();
-
+    private ProviderOperationFactory factory;
     /**
      * A cache of providers that are predefined.
      */
     private Map<String /* provider name */, ProviderCache> providerCache;
-
     /**
      * The username, password, and domain to use for dynamically created connections
      */
@@ -88,7 +79,6 @@ public class ProviderAdapterImpl implements ProviderAdapter {
     private static String DEFAULT_PASS;
     private static String DEFAULT_DOMAIN;
 
-
     /**
      * This default constructor is used as a work around because the activator wasnt getting called
      */
@@ -97,7 +87,6 @@ public class ProviderAdapterImpl implements ProviderAdapter {
         initialize();
 
     }
-
     /**
      * This constructor is used primarily in the test cases to bypass initialization of the adapter for isolated,
      * disconnected testing
@@ -111,7 +100,6 @@ public class ProviderAdapterImpl implements ProviderAdapter {
             initialize();
         }
     }
-
     /**
      * @param props not used
      */
@@ -119,152 +107,125 @@ public class ProviderAdapterImpl implements ProviderAdapter {
         initialize();
 
     }
-
     @Override
     public Server restartServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
-
         IProviderOperation op = factory.getOperationObject(Operation.RESTART_SERVICE);
         op.setProviderCache(this.providerCache);
-        op.setDefaultPass(DEFAULT_PASS);
+        op.setDefaultPassword(DEFAULT_PASS);
         op.setDefaultUser(DEFAULT_USER);
         op.setDefaultDomain(DEFAULT_DOMAIN);
         return (Server) op.doOperation(params, context);
     }
-
     @Override
     public Server stopServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
-
         IProviderOperation op = factory.getOperationObject(Operation.STOP_SERVICE);
         op.setProviderCache(this.providerCache);
-        op.setDefaultPass(DEFAULT_PASS);
+        op.setDefaultPassword(DEFAULT_PASS);
         op.setDefaultUser(DEFAULT_USER);
         op.setDefaultDomain(DEFAULT_DOMAIN);
         return (Server) op.doOperation(params, context);
     }
-
     @Override
     public Server startServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
-
         IProviderOperation op = factory.getOperationObject(Operation.START_SERVICE);
         op.setProviderCache(this.providerCache);
-        op.setDefaultPass(DEFAULT_PASS);
+        op.setDefaultPassword(DEFAULT_PASS);
         op.setDefaultUser(DEFAULT_USER);
         op.setDefaultDomain(DEFAULT_DOMAIN);
         return (Server) op.doOperation(params, context);
     }
-
     @Override
     public Server rebuildServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
-
         IProviderOperation op = factory.getOperationObject(Operation.REBUILD_SERVICE);
         op.setProviderCache(this.providerCache);
-        op.setDefaultPass(DEFAULT_PASS);
+        op.setDefaultPassword(DEFAULT_PASS);
         op.setDefaultUser(DEFAULT_USER);
         op.setDefaultDomain(DEFAULT_DOMAIN);
         return (Server) op.doOperation(params, context);
     }
-
     @Override
     public Server terminateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
-
         IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_SERVICE);
         op.setProviderCache(this.providerCache);
-        op.setDefaultPass(DEFAULT_PASS);
+        op.setDefaultPassword(DEFAULT_PASS);
         op.setDefaultUser(DEFAULT_USER);
         op.setDefaultDomain(DEFAULT_DOMAIN);
         return (Server) op.doOperation(params, context);
     }
-
     @Override
     public Server evacuateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
-
         IProviderOperation op = factory.getOperationObject(Operation.EVACUATE_SERVICE);
         op.setProviderCache(this.providerCache);
-        op.setDefaultPass(DEFAULT_PASS);
+        op.setDefaultPassword(DEFAULT_PASS);
         op.setDefaultUser(DEFAULT_USER);
         op.setDefaultDomain(DEFAULT_DOMAIN);
         // pass this object's reference to EvacuateServer to allow rebuild after evacuate
         ((EvacuateServer) op).setProvideAdapterRef(this);
         return (Server) op.doOperation(params, context);
     }
-
     @Override
     public Server migrateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
-
         IProviderOperation op = factory.getOperationObject(Operation.MIGRATE_SERVICE);
         op.setProviderCache(this.providerCache);
-        op.setDefaultPass(DEFAULT_PASS);
+        op.setDefaultPassword(DEFAULT_PASS);
         op.setDefaultUser(DEFAULT_USER);
         op.setDefaultDomain(DEFAULT_DOMAIN);
         return (Server) op.doOperation(params, context);
     }
-
     @Override
     public Server vmStatuschecker(Map<String, String> params, SvcLogicContext context) throws APPCException {
-
         IProviderOperation op = factory.getOperationObject(Operation.VMSTATUSCHECK_SERVICE);
         op.setProviderCache(this.providerCache);
-        op.setDefaultPass(DEFAULT_PASS);
+        op.setDefaultPassword(DEFAULT_PASS);
         op.setDefaultUser(DEFAULT_USER);
         op.setDefaultDomain(DEFAULT_DOMAIN);
         return (Server) op.doOperation(params, context);
     }
-
     @Override
     public Stack terminateStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
-
         IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_STACK);
         op.setProviderCache(this.providerCache);
-        op.setDefaultPass(DEFAULT_PASS);
+        op.setDefaultPassword(DEFAULT_PASS);
         op.setDefaultUser(DEFAULT_USER);
         op.setDefaultDomain(DEFAULT_DOMAIN);
         return (Stack) op.doOperation(params, context);
     }
-
     @Override
     public Stack snapshotStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
-
         IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_STACK);
         op.setProviderCache(this.providerCache);
-        op.setDefaultPass(DEFAULT_PASS);
+        op.setDefaultPassword(DEFAULT_PASS);
         op.setDefaultUser(DEFAULT_USER);
         op.setDefaultDomain(DEFAULT_DOMAIN);
         return (Stack) op.doOperation(params, context);
     }
-
     @Override
     public Stack restoreStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
-
         IProviderOperation op = factory.getOperationObject(Operation.RESTORE_STACK);
         op.setProviderCache(this.providerCache);
-        op.setDefaultPass(DEFAULT_PASS);
+        op.setDefaultPassword(DEFAULT_PASS);
         op.setDefaultUser(DEFAULT_USER);
         op.setDefaultDomain(DEFAULT_DOMAIN);
         return (Stack) op.doOperation(params, context);
     }
-
     @Override
     public Server lookupServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
-
         IProviderOperation op = factory.getOperationObject(Operation.LOOKUP_SERVICE);
         op.setProviderCache(this.providerCache);
-        op.setDefaultPass(DEFAULT_PASS);
+        op.setDefaultPassword(DEFAULT_PASS);
         op.setDefaultUser(DEFAULT_USER);
         op.setDefaultDomain(DEFAULT_DOMAIN);
         return (Server) op.doOperation(params, context);
     }
-
     @Override
     public Image createSnapshot(Map<String, String> params, SvcLogicContext context) throws APPCException {
-
         IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_SERVICE);
         op.setProviderCache(this.providerCache);
-        op.setDefaultPass(DEFAULT_PASS);
+        op.setDefaultPassword(DEFAULT_PASS);
         op.setDefaultUser(DEFAULT_USER);
         op.setDefaultDomain(DEFAULT_DOMAIN);
         return (Image) op.doOperation(params, context);
     }
-
     /**
      * Returns the symbolic name of the adapter
      *
@@ -275,14 +236,11 @@ public class ProviderAdapterImpl implements ProviderAdapter {
     public String getAdapterName() {
         return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
     }
-
-
     /**
      * initialize the provider adapter by building the context cache
      */
     private void initialize() {
         configuration = ConfigurationFactory.getConfiguration();
-
         /*
          * Initialize the provider cache for all defined providers. The definition of the providers uses a structured
          * property set, where the names form a hierarchical name space (dotted notation, such as one.two.three). Each
@@ -298,10 +256,10 @@ public class ProviderAdapterImpl implements ProviderAdapter {
          * provider2.identity=http://provider2:5000/v2.0 provider2.tenant1.name=someName
          * provider2.tenant1.userid=someUser provider2.tenant1.password=somePassword </pre> </p>
          */
+        factory = ProviderOperationFactory.getInstance();
         providerCache = new HashMap<>();
         Properties properties = configuration.getProperties();
         List<Node> providers = StructuredPropertyHelper.getStructuredProperties(properties, Property.PROVIDER);
-
         for (Node provider : providers) {
             ProviderCache cache = new ProviderCache();
             List<Node> providerNodes = provider.getChildren();
@@ -327,7 +285,8 @@ public class ProviderAdapterImpl implements ProviderAdapter {
                                 DEFAULT_USER = node2.getValue();
                                 break;
                             case Property.PROVIDER_TENANT_PASSWORD:
-                                password = node2.getValue();
+                                // convert password from Base64 encrypted string prefixed with 'enc:'
+                                password = EncryptionTool.getInstance().encrypt(node2.getValue());
                                 DEFAULT_PASS = node2.getValue();
                                 break;
                             case Property.PROVIDER_TENANT_DOMAIN:
@@ -336,11 +295,9 @@ public class ProviderAdapterImpl implements ProviderAdapter {
                                 break;
                         }
                     }
-
                     cache.addTenant(null, tenantName, userId, password, domain);
                 }
             }
-
             /*
              * Add the provider to the set of providers cached
              */
@@ -348,12 +305,36 @@ public class ProviderAdapterImpl implements ProviderAdapter {
                 providerCache.put(null, cache);
                 providerCache.put(cache.getIdentityURL(), cache);
             }
-
             /*
              * Now, initialize the cache for the loaded provider
              */
             cache.initialize();
         }
     }
+    @Override
+    public Server attachVolume(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
+          IProviderOperation op = factory.getOperationObject(Operation.ATTACHVOLUME_SERVICE);
+            op.setProviderCache(this.providerCache);
+            op.setDefaultPassword(DEFAULT_PASS);
+            op.setDefaultUser(DEFAULT_USER);
+            return (Server) op.doOperation(params, ctx);
+    }
+    @Override
+    public Server dettachVolume(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
+         IProviderOperation op = factory.getOperationObject(Operation.DETACHVOLUME_SERVICE);
+            op.setProviderCache(this.providerCache);
+            op.setDefaultPassword(DEFAULT_PASS);
+            op.setDefaultUser(DEFAULT_USER);
+            return (Server) op.doOperation(params, ctx);
+    }
+    @Override
+    public Server rebootServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
+        IProviderOperation op = factory.getOperationObject(Operation.REBOOT_SERVICE);
+        op.setProviderCache(this.providerCache);
+        op.setDefaultPassword(DEFAULT_PASS);
+        op.setDefaultUser(DEFAULT_USER);
+        op.setDefaultDomain(DEFAULT_DOMAIN);
+        return (Server) op.doOperation(params, context);
+    }
 
 }