2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Copyright (C) 2017 Amdocs
 
   8  * =============================================================================
 
   9  * Licensed under the Apache License, Version 2.0 (the "License");
 
  10  * you may not use this file except in compliance with the License.
 
  11  * You may obtain a copy of the License at
 
  13  *      http://www.apache.org/licenses/LICENSE-2.0
 
  15  * Unless required by applicable law or agreed to in writing, software
 
  16  * distributed under the License is distributed on an "AS IS" BASIS,
 
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  18  * See the License for the specific language governing permissions and
 
  19  * limitations under the License.
 
  21  * ============LICENSE_END=========================================================
 
  23 package org.onap.appc.adapter.iaas.impl;
 
  25 import org.onap.appc.Constants;
 
  26 import org.onap.appc.adapter.iaas.ProviderAdapter;
 
  27 import org.onap.appc.adapter.iaas.provider.operation.api.IProviderOperation;
 
  28 import org.onap.appc.adapter.iaas.provider.operation.api.ProviderOperationFactory;
 
  29 import org.onap.appc.adapter.iaas.provider.operation.common.constants.Property;
 
  30 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
 
  31 import org.onap.appc.adapter.iaas.provider.operation.impl.EvacuateServer;
 
  32 import org.onap.appc.configuration.Configuration;
 
  33 import org.onap.appc.configuration.ConfigurationFactory;
 
  34 import org.onap.appc.exceptions.APPCException;
 
  35 import org.onap.appc.util.StructuredPropertyHelper;
 
  36 import org.onap.appc.util.StructuredPropertyHelper.Node;
 
  37 import com.att.cdp.zones.model.Image;
 
  38 import com.att.cdp.zones.model.Server;
 
  39 import com.att.cdp.zones.model.Stack;
 
  40 import com.att.eelf.configuration.EELFLogger;
 
  41 import com.att.eelf.configuration.EELFManager;
 
  42 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  43 import java.util.HashMap;
 
  44 import java.util.List;
 
  46 import java.util.Properties;
 
  48  * This class implements the {@link ProviderAdapter} interface. This interface defines the behaviors that our service
 
  54 @SuppressWarnings("javadoc")
 
  55 public class ProviderAdapterImpl implements ProviderAdapter {
 
  57      * The default domain name for authentication
 
  59     public static final String DEFAULT_DOMAIN_NAME = "Default";
 
  61      * The logger to be used
 
  63     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ProviderAdapterImpl.class);
 
  65      * A reference to the adapter configuration object.
 
  67     private Configuration configuration;
 
  69      * reference to operation factory
 
  71     ProviderOperationFactory factory = ProviderOperationFactory.getInstance();
 
  73      * A cache of providers that are predefined.
 
  75     private Map<String /* provider name */, ProviderCache> providerCache;
 
  77      * The username, password, and domain to use for dynamically created connections
 
  79     private static String DEFAULT_USER;
 
  80     private static String DEFAULT_PASS;
 
  81     private static String DEFAULT_DOMAIN;
 
  84      * This default constructor is used as a work around because the activator wasnt getting called
 
  86     @SuppressWarnings("all")
 
  87     public ProviderAdapterImpl() {
 
  92      * This constructor is used primarily in the test cases to bypass initialization of the adapter for isolated,
 
  93      * disconnected testing
 
  95      * @param initialize True if the adapter is to be initialized, can false if not
 
  97     @SuppressWarnings("all")
 
  98     public ProviderAdapterImpl(boolean initialize) {
 
  99         configuration = ConfigurationFactory.getConfiguration();
 
 105      * @param props not used
 
 107     public ProviderAdapterImpl(@SuppressWarnings("unused") Properties props) {
 
 112     public Server restartServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
 
 113         IProviderOperation op = factory.getOperationObject(Operation.RESTART_SERVICE);
 
 114         op.setProviderCache(this.providerCache);
 
 115         op.setDefaultPassword(DEFAULT_PASS);
 
 116         op.setDefaultUser(DEFAULT_USER);
 
 117         op.setDefaultDomain(DEFAULT_DOMAIN);
 
 118         return (Server) op.doOperation(params, context);
 
 121     public Server stopServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
 
 122         IProviderOperation op = factory.getOperationObject(Operation.STOP_SERVICE);
 
 123         op.setProviderCache(this.providerCache);
 
 124         op.setDefaultPassword(DEFAULT_PASS);
 
 125         op.setDefaultUser(DEFAULT_USER);
 
 126         op.setDefaultDomain(DEFAULT_DOMAIN);
 
 127         return (Server) op.doOperation(params, context);
 
 130     public Server startServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
 
 131         IProviderOperation op = factory.getOperationObject(Operation.START_SERVICE);
 
 132         op.setProviderCache(this.providerCache);
 
 133         op.setDefaultPassword(DEFAULT_PASS);
 
 134         op.setDefaultUser(DEFAULT_USER);
 
 135         op.setDefaultDomain(DEFAULT_DOMAIN);
 
 136         return (Server) op.doOperation(params, context);
 
 139     public Server rebuildServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
 
 140         IProviderOperation op = factory.getOperationObject(Operation.REBUILD_SERVICE);
 
 141         op.setProviderCache(this.providerCache);
 
 142         op.setDefaultPassword(DEFAULT_PASS);
 
 143         op.setDefaultUser(DEFAULT_USER);
 
 144         op.setDefaultDomain(DEFAULT_DOMAIN);
 
 145         return (Server) op.doOperation(params, context);
 
 148     public Server terminateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
 
 149         IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_SERVICE);
 
 150         op.setProviderCache(this.providerCache);
 
 151         op.setDefaultPassword(DEFAULT_PASS);
 
 152         op.setDefaultUser(DEFAULT_USER);
 
 153         op.setDefaultDomain(DEFAULT_DOMAIN);
 
 154         return (Server) op.doOperation(params, context);
 
 157     public Server evacuateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
 
 158         IProviderOperation op = factory.getOperationObject(Operation.EVACUATE_SERVICE);
 
 159         op.setProviderCache(this.providerCache);
 
 160         op.setDefaultPassword(DEFAULT_PASS);
 
 161         op.setDefaultUser(DEFAULT_USER);
 
 162         op.setDefaultDomain(DEFAULT_DOMAIN);
 
 163         // pass this object's reference to EvacuateServer to allow rebuild after evacuate
 
 164         ((EvacuateServer) op).setProvideAdapterRef(this);
 
 165         return (Server) op.doOperation(params, context);
 
 168     public Server migrateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
 
 169         IProviderOperation op = factory.getOperationObject(Operation.MIGRATE_SERVICE);
 
 170         op.setProviderCache(this.providerCache);
 
 171         op.setDefaultPassword(DEFAULT_PASS);
 
 172         op.setDefaultUser(DEFAULT_USER);
 
 173         op.setDefaultDomain(DEFAULT_DOMAIN);
 
 174         return (Server) op.doOperation(params, context);
 
 177     public Server vmStatuschecker(Map<String, String> params, SvcLogicContext context) throws APPCException {
 
 178         IProviderOperation op = factory.getOperationObject(Operation.VMSTATUSCHECK_SERVICE);
 
 179         op.setProviderCache(this.providerCache);
 
 180         op.setDefaultPassword(DEFAULT_PASS);
 
 181         op.setDefaultUser(DEFAULT_USER);
 
 182         op.setDefaultDomain(DEFAULT_DOMAIN);
 
 183         return (Server) op.doOperation(params, context);
 
 186     public Stack terminateStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
 
 187         IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_STACK);
 
 188         op.setProviderCache(this.providerCache);
 
 189         op.setDefaultPassword(DEFAULT_PASS);
 
 190         op.setDefaultUser(DEFAULT_USER);
 
 191         op.setDefaultDomain(DEFAULT_DOMAIN);
 
 192         return (Stack) op.doOperation(params, context);
 
 195     public Stack snapshotStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
 
 196         IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_STACK);
 
 197         op.setProviderCache(this.providerCache);
 
 198         op.setDefaultPassword(DEFAULT_PASS);
 
 199         op.setDefaultUser(DEFAULT_USER);
 
 200         op.setDefaultDomain(DEFAULT_DOMAIN);
 
 201         return (Stack) op.doOperation(params, context);
 
 204     public Stack restoreStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
 
 205         IProviderOperation op = factory.getOperationObject(Operation.RESTORE_STACK);
 
 206         op.setProviderCache(this.providerCache);
 
 207         op.setDefaultPassword(DEFAULT_PASS);
 
 208         op.setDefaultUser(DEFAULT_USER);
 
 209         op.setDefaultDomain(DEFAULT_DOMAIN);
 
 210         return (Stack) op.doOperation(params, context);
 
 213     public Server lookupServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
 
 214         IProviderOperation op = factory.getOperationObject(Operation.LOOKUP_SERVICE);
 
 215         op.setProviderCache(this.providerCache);
 
 216         op.setDefaultPassword(DEFAULT_PASS);
 
 217         op.setDefaultUser(DEFAULT_USER);
 
 218         op.setDefaultDomain(DEFAULT_DOMAIN);
 
 219         return (Server) op.doOperation(params, context);
 
 222     public Image createSnapshot(Map<String, String> params, SvcLogicContext context) throws APPCException {
 
 223         IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_SERVICE);
 
 224         op.setProviderCache(this.providerCache);
 
 225         op.setDefaultPassword(DEFAULT_PASS);
 
 226         op.setDefaultUser(DEFAULT_USER);
 
 227         op.setDefaultDomain(DEFAULT_DOMAIN);
 
 228         return (Image) op.doOperation(params, context);
 
 231      * Returns the symbolic name of the adapter
 
 233      * @return The adapter name
 
 234      * @see org.onap.appc.adapter.iaas.ProviderAdapter#getAdapterName()
 
 237     public String getAdapterName() {
 
 238         return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
 
 241      * initialize the provider adapter by building the context cache
 
 243     private void initialize() {
 
 244         configuration = ConfigurationFactory.getConfiguration();
 
 246          * Initialize the provider cache for all defined providers. The definition of the providers uses a structured
 
 247          * property set, where the names form a hierarchical name space (dotted notation, such as one.two.three). Each
 
 248          * name in the name space can also be serialized by appending a sequence number. All nodes at the same level
 
 249          * with the same serial number are grouped together in the namespace hierarchy. This allows a hierarchical
 
 250          * multi-valued property to be defined, which can then be used to setup the provider and tenant caches. <p> For
 
 251          * example, the following definitions show how the namespace hierarchy is defined for two providers, with two
 
 252          * tenants on the first provider and a single tenant for the second provider. <pre>
 
 253          * provider1.type=OpenStackProvider provider1.name=ILAB provider1.identity=http://provider1:5000/v2.0
 
 254          * provider1.tenant1.name=CDP-ONAP-APPC provider1.tenant1.userid=testUser
 
 255          * provider1.tenant1.password=testPassword provider1.tenant2.name=TEST-TENANT provider1.tenant2.userid=testUser
 
 256          * provider1.tenant2.password=testPassword provider2.type=OpenStackProvider provider2.name=PDK1
 
 257          * provider2.identity=http://provider2:5000/v2.0 provider2.tenant1.name=someName
 
 258          * provider2.tenant1.userid=someUser provider2.tenant1.password=somePassword </pre> </p>
 
 260         providerCache = new HashMap<>();
 
 261         Properties properties = configuration.getProperties();
 
 262         List<Node> providers = StructuredPropertyHelper.getStructuredProperties(properties, Property.PROVIDER);
 
 263         for (Node provider : providers) {
 
 264             ProviderCache cache = new ProviderCache();
 
 265             List<Node> providerNodes = provider.getChildren();
 
 266             for (Node node : providerNodes) {
 
 267                 if (node.getName().equals(Property.PROVIDER_TYPE)) {
 
 268                     cache.setProviderType(node.getValue());
 
 269                 } else if (node.getName().equals(Property.PROVIDER_IDENTITY)) {
 
 270                     cache.setIdentityURL(node.getValue());
 
 271                     cache.setProviderName(node.getValue());
 
 272                 } else if (node.getName().startsWith(Property.PROVIDER_TENANT)) {
 
 273                     String tenantName = null;
 
 274                     String userId = null;
 
 275                     String password = null;
 
 276                     // domain is not required so set a default
 
 277                     String domain = DEFAULT_DOMAIN_NAME;
 
 278                     for (Node node2 : node.getChildren()) {
 
 279                         switch (node2.getName()) {
 
 280                             case Property.PROVIDER_TENANT_NAME:
 
 281                                 tenantName = node2.getValue();
 
 283                             case Property.PROVIDER_TENANT_USERID:
 
 284                                 userId = node2.getValue();
 
 285                                 DEFAULT_USER = node2.getValue();
 
 287                             case Property.PROVIDER_TENANT_PASSWORD:
 
 288                                 password = node2.getValue();
 
 289                                 DEFAULT_PASS = node2.getValue();
 
 291                             case Property.PROVIDER_TENANT_DOMAIN:
 
 292                                 domain = node2.getValue();
 
 293                                 DEFAULT_DOMAIN = node2.getValue();
 
 297                     cache.addTenant(null, tenantName, userId, password, domain);
 
 301              * Add the provider to the set of providers cached
 
 303             if (cache.getIdentityURL() != null && cache.getProviderType() != null) {
 
 304                 providerCache.put(null, cache);
 
 305                 providerCache.put(cache.getIdentityURL(), cache);
 
 308              * Now, initialize the cache for the loaded provider
 
 314     public Server attachVolume(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
 
 315         // TODO Auto-generated method stub
 
 316           IProviderOperation op = factory.getOperationObject(Operation.ATTACHVOLUME_SERVICE);
 
 317             op.setProviderCache(this.providerCache);
 
 318             op.setDefaultPassword(DEFAULT_PASS);
 
 319             op.setDefaultUser(DEFAULT_USER);
 
 320             return (Server) op.doOperation(params, ctx);
 
 323     public Server dettachVolume(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
 
 324          IProviderOperation op = factory.getOperationObject(Operation.DETACHVOLUME_SERVICE);
 
 325             op.setProviderCache(this.providerCache);
 
 326             op.setDefaultPassword(DEFAULT_PASS);
 
 327             op.setDefaultUser(DEFAULT_USER);
 
 328             return (Server) op.doOperation(params, ctx);