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