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