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