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