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