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