aboutsummaryrefslogtreecommitdiffstats
path: root/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/impl/ProviderAdapterImpl.java
blob: 1d00769132981d2ef533e15e41f93e87716bfd3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*-
 * ============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.appc.adapter.iaas.impl;

import org.onap.appc.Constants;
import org.onap.appc.adapter.iaas.ProviderAdapter;
import org.onap.appc.adapter.iaas.provider.operation.api.IProviderOperation;
import org.onap.appc.adapter.iaas.provider.operation.api.ProviderOperationFactory;
import org.onap.appc.adapter.iaas.provider.operation.common.constants.Property;
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.exceptions.APPCException;
import org.onap.appc.util.StructuredPropertyHelper;
import org.onap.appc.util.StructuredPropertyHelper.Node;
import com.att.cdp.zones.model.Image;
import com.att.cdp.zones.model.Server;
import com.att.cdp.zones.model.Stack;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
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.
 *
 * @since Aug 12, 2015
 * @version $Id$
 */
@SuppressWarnings("javadoc")
public class ProviderAdapterImpl implements ProviderAdapter {
    /**
     * The default domain name for authentication
     */
    public static final String DEFAULT_DOMAIN_NAME = "Default";
    /**
     * A reference to the adapter configuration object.
     */
    private Configuration configuration;
    /**
     * reference to operation factory
     */
    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
     */
    private static String DEFAULT_USER;
    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
     */
    @SuppressWarnings("all")
    public ProviderAdapterImpl() {
        initialize();

    }
    /**
     * This constructor is used primarily in the test cases to bypass initialization of the adapter for isolated,
     * disconnected testing
     *
     * @param initialize True if the adapter is to be initialized, can false if not
     */
    @SuppressWarnings("all")
    public ProviderAdapterImpl(boolean initialize) {
        configuration = ConfigurationFactory.getConfiguration();
        if (initialize) {
            initialize();
        }
    }
    /**
     * @param props not used
     */
    public ProviderAdapterImpl(@SuppressWarnings("unused") Properties props) {
        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.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.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.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.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.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.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.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.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.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.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.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.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.setDefaultPassword(DEFAULT_PASS);
        op.setDefaultUser(DEFAULT_USER);
        op.setDefaultDomain(DEFAULT_DOMAIN);
        return (Image) op.doOperation(params, context);
    }
    /**
     * Returns the symbolic name of the adapter
     *
     * @return The adapter name
     * @see org.onap.appc.adapter.iaas.ProviderAdapter#getAdapterName()
     */
    @Override
    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
         * name in the name space can also be serialized by appending a sequence number. All nodes at the same level
         * with the same serial number are grouped together in the namespace hierarchy. This allows a hierarchical
         * multi-valued property to be defined, which can then be used to setup the provider and tenant caches. <p> For
         * example, the following definitions show how the namespace hierarchy is defined for two providers, with two
         * tenants on the first provider and a single tenant for the second provider. <pre>
         * provider1.type=OpenStackProvider provider1.name=ILAB provider1.identity=http://provider1:5000/v2.0
         * provider1.tenant1.name=CDP-ONAP-APPC provider1.tenant1.userid=testUser
         * provider1.tenant1.password=testPassword provider1.tenant2.name=TEST-TENANT provider1.tenant2.userid=testUser
         * provider1.tenant2.password=testPassword provider2.type=OpenStackProvider provider2.name=PDK1
         * 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();
            for (Node node : providerNodes) {
                if (node.getName().equals(Property.PROVIDER_TYPE)) {
                    cache.setProviderType(node.getValue());
                } else if (node.getName().equals(Property.PROVIDER_IDENTITY)) {
                    cache.setIdentityURL(node.getValue());
                    cache.setProviderName(node.getValue());
                } else if (node.getName().startsWith(Property.PROVIDER_TENANT)) {
                    String tenantName = null;
                    String userId = null;
                    String password = null;
                    // domain is not required so set a default
                    String domain = ProviderAdapterImpl.DEFAULT_DOMAIN_NAME;
                    for (Node node2 : node.getChildren()) {
                        switch (node2.getName()) {
                            case Property.PROVIDER_TENANT_NAME:
                                tenantName = node2.getValue();
                                break;
                            case Property.PROVIDER_TENANT_USERID:
                                userId = node2.getValue();
                                ProviderAdapterImpl.DEFAULT_USER = node2.getValue();
                                break;
                            case Property.PROVIDER_TENANT_PASSWORD:
                                //The passwords are automatically decrpyted by the appc Configuration
                                //class and require no additional method calls to decrypt.
                                password = node2.getValue();
                                ProviderAdapterImpl.DEFAULT_PASS = node2.getValue();
                                break;
                            case Property.PROVIDER_TENANT_DOMAIN:
                                domain = node2.getValue();
                                ProviderAdapterImpl.DEFAULT_DOMAIN = node2.getValue();
                                break;
                            default:
                                break;
                        }
                    }
                    cache.addTenant(null, tenantName, userId, password, domain);
                }
            }
            /*
             * Add the provider to the set of providers cached
             */
            if (cache.getIdentityURL() != null && cache.getProviderType() != null) {
                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);
    }

}