ba6c8c67891de7fd6c5493c0541027c406315462
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / main / java / org / openecomp / appc / adapter / iaas / impl / ProviderAdapterImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25
26 package org.onap.appc.adapter.iaas.impl;
27
28 import org.onap.appc.Constants;
29 import org.onap.appc.adapter.iaas.ProviderAdapter;
30 import org.onap.appc.adapter.iaas.provider.operation.api.IProviderOperation;
31 import org.onap.appc.adapter.iaas.provider.operation.api.ProviderOperationFactory;
32 import org.onap.appc.adapter.iaas.provider.operation.common.constants.Property;
33 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
34 import org.onap.appc.adapter.iaas.provider.operation.impl.EvacuateServer;
35 import org.onap.appc.configuration.Configuration;
36 import org.onap.appc.configuration.ConfigurationFactory;
37 import org.onap.appc.exceptions.APPCException;
38 import org.onap.appc.util.StructuredPropertyHelper;
39 import org.onap.appc.util.StructuredPropertyHelper.Node;
40 import com.att.cdp.zones.model.Image;
41 import com.att.cdp.zones.model.Server;
42 import com.att.cdp.zones.model.Stack;
43 import com.att.eelf.configuration.EELFLogger;
44 import com.att.eelf.configuration.EELFManager;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
46 import java.util.HashMap;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.Properties;
50
51 /**
52  * This class implements the {@link ProviderAdapter} interface. This interface defines the behaviors that our service
53  * provides.
54  *
55  * @since Aug 12, 2015
56  * @version $Id$
57  */
58 @SuppressWarnings("javadoc")
59 public class ProviderAdapterImpl implements ProviderAdapter {
60     /**
61      * The default domain name for authentication
62      */
63     public static final String DEFAULT_DOMAIN_NAME = "Default";
64
65     /**
66      * The logger to be used
67      */
68     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ProviderAdapterImpl.class);
69     /**
70      * A reference to the adapter configuration object.
71      */
72     private Configuration configuration;
73
74     /**
75      * reference to operation factory
76      */
77     ProviderOperationFactory factory = ProviderOperationFactory.getInstance();
78
79     /**
80      * A cache of providers that are predefined.
81      */
82     private Map<String /* provider name */, ProviderCache> providerCache;
83
84     /**
85      * The username, password, and domain to use for dynamically created connections
86      */
87     private static String DEFAULT_USER;
88     private static String DEFAULT_PASS;
89     private static String DEFAULT_DOMAIN;
90
91
92     /**
93      * This default constructor is used as a work around because the activator wasnt getting called
94      */
95     @SuppressWarnings("all")
96     public ProviderAdapterImpl() {
97         initialize();
98
99     }
100
101     /**
102      * This constructor is used primarily in the test cases to bypass initialization of the adapter for isolated,
103      * disconnected testing
104      *
105      * @param initialize True if the adapter is to be initialized, can false if not
106      */
107     @SuppressWarnings("all")
108     public ProviderAdapterImpl(boolean initialize) {
109         configuration = ConfigurationFactory.getConfiguration();
110         if (initialize) {
111             initialize();
112         }
113     }
114
115     /**
116      * @param props not used
117      */
118     public ProviderAdapterImpl(@SuppressWarnings("unused") Properties props) {
119         initialize();
120
121     }
122
123     @Override
124     public Server restartServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
125
126         IProviderOperation op = factory.getOperationObject(Operation.RESTART_SERVICE);
127         op.setProviderCache(this.providerCache);
128         op.setDefaultPass(DEFAULT_PASS);
129         op.setDefaultUser(DEFAULT_USER);
130         op.setDefaultDomain(DEFAULT_DOMAIN);
131         return (Server) op.doOperation(params, context);
132     }
133
134     @Override
135     public Server stopServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
136
137         IProviderOperation op = factory.getOperationObject(Operation.STOP_SERVICE);
138         op.setProviderCache(this.providerCache);
139         op.setDefaultPass(DEFAULT_PASS);
140         op.setDefaultUser(DEFAULT_USER);
141         op.setDefaultDomain(DEFAULT_DOMAIN);
142         return (Server) op.doOperation(params, context);
143     }
144
145     @Override
146     public Server startServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
147
148         IProviderOperation op = factory.getOperationObject(Operation.START_SERVICE);
149         op.setProviderCache(this.providerCache);
150         op.setDefaultPass(DEFAULT_PASS);
151         op.setDefaultUser(DEFAULT_USER);
152         op.setDefaultDomain(DEFAULT_DOMAIN);
153         return (Server) op.doOperation(params, context);
154     }
155
156     @Override
157     public Server rebuildServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
158
159         IProviderOperation op = factory.getOperationObject(Operation.REBUILD_SERVICE);
160         op.setProviderCache(this.providerCache);
161         op.setDefaultPass(DEFAULT_PASS);
162         op.setDefaultUser(DEFAULT_USER);
163         op.setDefaultDomain(DEFAULT_DOMAIN);
164         return (Server) op.doOperation(params, context);
165     }
166
167     @Override
168     public Server terminateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
169
170         IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_SERVICE);
171         op.setProviderCache(this.providerCache);
172         op.setDefaultPass(DEFAULT_PASS);
173         op.setDefaultUser(DEFAULT_USER);
174         op.setDefaultDomain(DEFAULT_DOMAIN);
175         return (Server) op.doOperation(params, context);
176     }
177
178     @Override
179     public Server evacuateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
180
181         IProviderOperation op = factory.getOperationObject(Operation.EVACUATE_SERVICE);
182         op.setProviderCache(this.providerCache);
183         op.setDefaultPass(DEFAULT_PASS);
184         op.setDefaultUser(DEFAULT_USER);
185         op.setDefaultDomain(DEFAULT_DOMAIN);
186         // pass this object's reference to EvacuateServer to allow rebuild after evacuate
187         ((EvacuateServer) op).setProvideAdapterRef(this);
188         return (Server) op.doOperation(params, context);
189     }
190
191     @Override
192     public Server migrateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
193
194         IProviderOperation op = factory.getOperationObject(Operation.MIGRATE_SERVICE);
195         op.setProviderCache(this.providerCache);
196         op.setDefaultPass(DEFAULT_PASS);
197         op.setDefaultUser(DEFAULT_USER);
198         op.setDefaultDomain(DEFAULT_DOMAIN);
199         return (Server) op.doOperation(params, context);
200     }
201
202     @Override
203     public Server vmStatuschecker(Map<String, String> params, SvcLogicContext context) throws APPCException {
204
205         IProviderOperation op = factory.getOperationObject(Operation.VMSTATUSCHECK_SERVICE);
206         op.setProviderCache(this.providerCache);
207         op.setDefaultPass(DEFAULT_PASS);
208         op.setDefaultUser(DEFAULT_USER);
209         op.setDefaultDomain(DEFAULT_DOMAIN);
210         return (Server) op.doOperation(params, context);
211     }
212
213     @Override
214     public Stack terminateStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
215
216         IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_STACK);
217         op.setProviderCache(this.providerCache);
218         op.setDefaultPass(DEFAULT_PASS);
219         op.setDefaultUser(DEFAULT_USER);
220         op.setDefaultDomain(DEFAULT_DOMAIN);
221         return (Stack) op.doOperation(params, context);
222     }
223
224     @Override
225     public Stack snapshotStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
226
227         IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_STACK);
228         op.setProviderCache(this.providerCache);
229         op.setDefaultPass(DEFAULT_PASS);
230         op.setDefaultUser(DEFAULT_USER);
231         op.setDefaultDomain(DEFAULT_DOMAIN);
232         return (Stack) op.doOperation(params, context);
233     }
234
235     @Override
236     public Stack restoreStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
237
238         IProviderOperation op = factory.getOperationObject(Operation.RESTORE_STACK);
239         op.setProviderCache(this.providerCache);
240         op.setDefaultPass(DEFAULT_PASS);
241         op.setDefaultUser(DEFAULT_USER);
242         op.setDefaultDomain(DEFAULT_DOMAIN);
243         return (Stack) op.doOperation(params, context);
244     }
245
246     @Override
247     public Server lookupServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
248
249         IProviderOperation op = factory.getOperationObject(Operation.LOOKUP_SERVICE);
250         op.setProviderCache(this.providerCache);
251         op.setDefaultPass(DEFAULT_PASS);
252         op.setDefaultUser(DEFAULT_USER);
253         op.setDefaultDomain(DEFAULT_DOMAIN);
254         return (Server) op.doOperation(params, context);
255     }
256
257     @Override
258     public Image createSnapshot(Map<String, String> params, SvcLogicContext context) throws APPCException {
259
260         IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_SERVICE);
261         op.setProviderCache(this.providerCache);
262         op.setDefaultPass(DEFAULT_PASS);
263         op.setDefaultUser(DEFAULT_USER);
264         op.setDefaultDomain(DEFAULT_DOMAIN);
265         return (Image) op.doOperation(params, context);
266     }
267
268     /**
269      * Returns the symbolic name of the adapter
270      *
271      * @return The adapter name
272      * @see org.onap.appc.adapter.iaas.ProviderAdapter#getAdapterName()
273      */
274     @Override
275     public String getAdapterName() {
276         return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
277     }
278
279
280     /**
281      * initialize the provider adapter by building the context cache
282      */
283     private void initialize() {
284         configuration = ConfigurationFactory.getConfiguration();
285
286         /*
287          * Initialize the provider cache for all defined providers. The definition of the providers uses a structured
288          * property set, where the names form a hierarchical name space (dotted notation, such as one.two.three). Each
289          * name in the name space can also be serialized by appending a sequence number. All nodes at the same level
290          * with the same serial number are grouped together in the namespace hierarchy. This allows a hierarchical
291          * multi-valued property to be defined, which can then be used to setup the provider and tenant caches. <p> For
292          * example, the following definitions show how the namespace hierarchy is defined for two providers, with two
293          * tenants on the first provider and a single tenant for the second provider. <pre>
294          * provider1.type=OpenStackProvider provider1.name=ILAB provider1.identity=http://provider1:5000/v2.0
295          * provider1.tenant1.name=CDP-ONAP-APPC provider1.tenant1.userid=testUser
296          * provider1.tenant1.password=testPassword provider1.tenant2.name=TEST-TENANT provider1.tenant2.userid=testUser
297          * provider1.tenant2.password=testPassword provider2.type=OpenStackProvider provider2.name=PDK1
298          * provider2.identity=http://provider2:5000/v2.0 provider2.tenant1.name=someName
299          * provider2.tenant1.userid=someUser provider2.tenant1.password=somePassword </pre> </p>
300          */
301         providerCache = new HashMap<>();
302         Properties properties = configuration.getProperties();
303         List<Node> providers = StructuredPropertyHelper.getStructuredProperties(properties, Property.PROVIDER);
304
305         for (Node provider : providers) {
306             ProviderCache cache = new ProviderCache();
307             List<Node> providerNodes = provider.getChildren();
308             for (Node node : providerNodes) {
309                 if (node.getName().equals(Property.PROVIDER_TYPE)) {
310                     cache.setProviderType(node.getValue());
311                 } else if (node.getName().equals(Property.PROVIDER_IDENTITY)) {
312                     cache.setIdentityURL(node.getValue());
313                     cache.setProviderName(node.getValue());
314                 } else if (node.getName().startsWith(Property.PROVIDER_TENANT)) {
315                     String tenantName = null;
316                     String userId = null;
317                     String password = null;
318                     // domain is not required so set a default
319                     String domain = DEFAULT_DOMAIN_NAME;
320                     for (Node node2 : node.getChildren()) {
321                         switch (node2.getName()) {
322                             case Property.PROVIDER_TENANT_NAME:
323                                 tenantName = node2.getValue();
324                                 break;
325                             case Property.PROVIDER_TENANT_USERID:
326                                 userId = node2.getValue();
327                                 DEFAULT_USER = node2.getValue();
328                                 break;
329                             case Property.PROVIDER_TENANT_PASSWORD:
330                                 password = node2.getValue();
331                                 DEFAULT_PASS = node2.getValue();
332                                 break;
333                             case Property.PROVIDER_TENANT_DOMAIN:
334                                 domain = node2.getValue();
335                                 DEFAULT_DOMAIN = node2.getValue();
336                                 break;
337                         }
338                     }
339
340                     cache.addTenant(null, tenantName, userId, password, domain);
341                 }
342             }
343
344             /*
345              * Add the provider to the set of providers cached
346              */
347             if (cache.getIdentityURL() != null && cache.getProviderType() != null) {
348                 providerCache.put(null, cache);
349                 providerCache.put(cache.getIdentityURL(), cache);
350             }
351
352             /*
353              * Now, initialize the cache for the loaded provider
354              */
355             cache.initialize();
356         }
357     }
358
359 }