add support for new openstack rebootaction VM levl
[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  * 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  * ============LICENSE_END=========================================================
22  */
23 package org.onap.appc.adapter.iaas.impl;
24
25 import org.onap.appc.Constants;
26 import org.onap.appc.adapter.iaas.ProviderAdapter;
27 import org.onap.appc.adapter.iaas.provider.operation.api.IProviderOperation;
28 import org.onap.appc.adapter.iaas.provider.operation.api.ProviderOperationFactory;
29 import org.onap.appc.adapter.iaas.provider.operation.common.constants.Property;
30 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
31 import org.onap.appc.adapter.iaas.provider.operation.impl.EvacuateServer;
32 import org.onap.appc.configuration.Configuration;
33 import org.onap.appc.configuration.ConfigurationFactory;
34 import org.onap.appc.exceptions.APPCException;
35 import org.onap.appc.util.StructuredPropertyHelper;
36 import org.onap.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.onap.ccsdk.sli.core.sli.SvcLogicContext;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Map;
46 import java.util.Properties;
47 /**
48  * This class implements the {@link ProviderAdapter} interface. This interface defines the behaviors that our service
49  * provides.
50  *
51  * @since Aug 12, 2015
52  * @version $Id$
53  */
54 @SuppressWarnings("javadoc")
55 public class ProviderAdapterImpl implements ProviderAdapter {
56     /**
57      * The default domain name for authentication
58      */
59     public static final String DEFAULT_DOMAIN_NAME = "Default";
60     /**
61      * The logger to be used
62      */
63     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ProviderAdapterImpl.class);
64     /**
65      * A reference to the adapter configuration object.
66      */
67     private Configuration configuration;
68     /**
69      * reference to operation factory
70      */
71     private ProviderOperationFactory factory;
72     /**
73      * A cache of providers that are predefined.
74      */
75     private Map<String /* provider name */, ProviderCache> providerCache;
76     /**
77      * The username, password, and domain to use for dynamically created connections
78      */
79     private static String DEFAULT_USER;
80     private static String DEFAULT_PASS;
81     private static String DEFAULT_DOMAIN;
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      * This constructor is used primarily in the test cases to bypass initialization of the adapter for isolated,
93      * disconnected testing
94      *
95      * @param initialize True if the adapter is to be initialized, can false if not
96      */
97     @SuppressWarnings("all")
98     public ProviderAdapterImpl(boolean initialize) {
99         configuration = ConfigurationFactory.getConfiguration();
100         if (initialize) {
101             initialize();
102         }
103     }
104     /**
105      * @param props not used
106      */
107     public ProviderAdapterImpl(@SuppressWarnings("unused") Properties props) {
108         initialize();
109
110     }
111     @Override
112     public Server restartServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
113         IProviderOperation op = factory.getOperationObject(Operation.RESTART_SERVICE);
114         op.setProviderCache(this.providerCache);
115         op.setDefaultPassword(DEFAULT_PASS);
116         op.setDefaultUser(DEFAULT_USER);
117         op.setDefaultDomain(DEFAULT_DOMAIN);
118         return (Server) op.doOperation(params, context);
119     }
120     @Override
121     public Server stopServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
122         IProviderOperation op = factory.getOperationObject(Operation.STOP_SERVICE);
123         op.setProviderCache(this.providerCache);
124         op.setDefaultPassword(DEFAULT_PASS);
125         op.setDefaultUser(DEFAULT_USER);
126         op.setDefaultDomain(DEFAULT_DOMAIN);
127         return (Server) op.doOperation(params, context);
128     }
129     @Override
130     public Server startServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
131         IProviderOperation op = factory.getOperationObject(Operation.START_SERVICE);
132         op.setProviderCache(this.providerCache);
133         op.setDefaultPassword(DEFAULT_PASS);
134         op.setDefaultUser(DEFAULT_USER);
135         op.setDefaultDomain(DEFAULT_DOMAIN);
136         return (Server) op.doOperation(params, context);
137     }
138     @Override
139     public Server rebuildServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
140         IProviderOperation op = factory.getOperationObject(Operation.REBUILD_SERVICE);
141         op.setProviderCache(this.providerCache);
142         op.setDefaultPassword(DEFAULT_PASS);
143         op.setDefaultUser(DEFAULT_USER);
144         op.setDefaultDomain(DEFAULT_DOMAIN);
145         return (Server) op.doOperation(params, context);
146     }
147     @Override
148     public Server terminateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
149         IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_SERVICE);
150         op.setProviderCache(this.providerCache);
151         op.setDefaultPassword(DEFAULT_PASS);
152         op.setDefaultUser(DEFAULT_USER);
153         op.setDefaultDomain(DEFAULT_DOMAIN);
154         return (Server) op.doOperation(params, context);
155     }
156     @Override
157     public Server evacuateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
158         IProviderOperation op = factory.getOperationObject(Operation.EVACUATE_SERVICE);
159         op.setProviderCache(this.providerCache);
160         op.setDefaultPassword(DEFAULT_PASS);
161         op.setDefaultUser(DEFAULT_USER);
162         op.setDefaultDomain(DEFAULT_DOMAIN);
163         // pass this object's reference to EvacuateServer to allow rebuild after evacuate
164         ((EvacuateServer) op).setProvideAdapterRef(this);
165         return (Server) op.doOperation(params, context);
166     }
167     @Override
168     public Server migrateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
169         IProviderOperation op = factory.getOperationObject(Operation.MIGRATE_SERVICE);
170         op.setProviderCache(this.providerCache);
171         op.setDefaultPassword(DEFAULT_PASS);
172         op.setDefaultUser(DEFAULT_USER);
173         op.setDefaultDomain(DEFAULT_DOMAIN);
174         return (Server) op.doOperation(params, context);
175     }
176     @Override
177     public Server vmStatuschecker(Map<String, String> params, SvcLogicContext context) throws APPCException {
178         IProviderOperation op = factory.getOperationObject(Operation.VMSTATUSCHECK_SERVICE);
179         op.setProviderCache(this.providerCache);
180         op.setDefaultPassword(DEFAULT_PASS);
181         op.setDefaultUser(DEFAULT_USER);
182         op.setDefaultDomain(DEFAULT_DOMAIN);
183         return (Server) op.doOperation(params, context);
184     }
185     @Override
186     public Stack terminateStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
187         IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_STACK);
188         op.setProviderCache(this.providerCache);
189         op.setDefaultPassword(DEFAULT_PASS);
190         op.setDefaultUser(DEFAULT_USER);
191         op.setDefaultDomain(DEFAULT_DOMAIN);
192         return (Stack) op.doOperation(params, context);
193     }
194     @Override
195     public Stack snapshotStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
196         IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_STACK);
197         op.setProviderCache(this.providerCache);
198         op.setDefaultPassword(DEFAULT_PASS);
199         op.setDefaultUser(DEFAULT_USER);
200         op.setDefaultDomain(DEFAULT_DOMAIN);
201         return (Stack) op.doOperation(params, context);
202     }
203     @Override
204     public Stack restoreStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
205         IProviderOperation op = factory.getOperationObject(Operation.RESTORE_STACK);
206         op.setProviderCache(this.providerCache);
207         op.setDefaultPassword(DEFAULT_PASS);
208         op.setDefaultUser(DEFAULT_USER);
209         op.setDefaultDomain(DEFAULT_DOMAIN);
210         return (Stack) op.doOperation(params, context);
211     }
212     @Override
213     public Server lookupServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
214         IProviderOperation op = factory.getOperationObject(Operation.LOOKUP_SERVICE);
215         op.setProviderCache(this.providerCache);
216         op.setDefaultPassword(DEFAULT_PASS);
217         op.setDefaultUser(DEFAULT_USER);
218         op.setDefaultDomain(DEFAULT_DOMAIN);
219         return (Server) op.doOperation(params, context);
220     }
221     @Override
222     public Image createSnapshot(Map<String, String> params, SvcLogicContext context) throws APPCException {
223         IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_SERVICE);
224         op.setProviderCache(this.providerCache);
225         op.setDefaultPassword(DEFAULT_PASS);
226         op.setDefaultUser(DEFAULT_USER);
227         op.setDefaultDomain(DEFAULT_DOMAIN);
228         return (Image) op.doOperation(params, context);
229     }
230     /**
231      * Returns the symbolic name of the adapter
232      *
233      * @return The adapter name
234      * @see org.onap.appc.adapter.iaas.ProviderAdapter#getAdapterName()
235      */
236     @Override
237     public String getAdapterName() {
238         return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
239     }
240     /**
241      * initialize the provider adapter by building the context cache
242      */
243     private void initialize() {
244         configuration = ConfigurationFactory.getConfiguration();
245         /*
246          * Initialize the provider cache for all defined providers. The definition of the providers uses a structured
247          * property set, where the names form a hierarchical name space (dotted notation, such as one.two.three). Each
248          * name in the name space can also be serialized by appending a sequence number. All nodes at the same level
249          * with the same serial number are grouped together in the namespace hierarchy. This allows a hierarchical
250          * multi-valued property to be defined, which can then be used to setup the provider and tenant caches. <p> For
251          * example, the following definitions show how the namespace hierarchy is defined for two providers, with two
252          * tenants on the first provider and a single tenant for the second provider. <pre>
253          * provider1.type=OpenStackProvider provider1.name=ILAB provider1.identity=http://provider1:5000/v2.0
254          * provider1.tenant1.name=CDP-ONAP-APPC provider1.tenant1.userid=testUser
255          * provider1.tenant1.password=testPassword provider1.tenant2.name=TEST-TENANT provider1.tenant2.userid=testUser
256          * provider1.tenant2.password=testPassword provider2.type=OpenStackProvider provider2.name=PDK1
257          * provider2.identity=http://provider2:5000/v2.0 provider2.tenant1.name=someName
258          * provider2.tenant1.userid=someUser provider2.tenant1.password=somePassword </pre> </p>
259          */
260         factory = ProviderOperationFactory.getInstance();
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.setDefaultPassword(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.setDefaultPassword(DEFAULT_PASS);
328             op.setDefaultUser(DEFAULT_USER);
329             return (Server) op.doOperation(params, ctx);
330     }
331     @Override
332     public Server rebootServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
333         IProviderOperation op = factory.getOperationObject(Operation.REBOOT_SERVICE);
334         op.setProviderCache(this.providerCache);
335         op.setDefaultPassword(DEFAULT_PASS);
336         op.setDefaultUser(DEFAULT_USER);
337         op.setDefaultDomain(DEFAULT_DOMAIN);
338         return (Server) op.doOperation(params, context);
339     }
340
341 }