2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.openecomp.appc.adapter.iaas.impl;
25 import org.openecomp.appc.Constants;
26 import org.openecomp.appc.adapter.iaas.ProviderAdapter;
27 import org.openecomp.appc.adapter.iaas.provider.operation.api.IProviderOperation;
28 import org.openecomp.appc.adapter.iaas.provider.operation.api.ProviderOperationFactory;
29 import org.openecomp.appc.adapter.iaas.provider.operation.common.constants.Property;
30 import org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation;
31 import org.openecomp.appc.adapter.iaas.provider.operation.impl.EvacuateServer;
32 import org.openecomp.appc.configuration.Configuration;
33 import org.openecomp.appc.configuration.ConfigurationFactory;
34 import org.openecomp.appc.exceptions.APPCException;
35 import org.openecomp.appc.util.StructuredPropertyHelper;
36 import org.openecomp.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.openecomp.sdnc.sli.SvcLogicContext;
44 import java.util.HashMap;
45 import java.util.List;
47 import java.util.Properties;
50 * This class implements the {@link ProviderAdapter} interface. This interface defines the behaviors that our service
53 @SuppressWarnings("javadoc")
54 public class ProviderAdapterImpl implements ProviderAdapter {
57 * The logger to be used
59 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ProviderAdapterImpl.class);
61 * A reference to the adapter configuration object.
63 private Configuration configuration;
66 * reference to operation factory
68 ProviderOperationFactory factory = ProviderOperationFactory.getInstance();
71 * A cache of providers that are predefined.
73 private Map<String /* provider name */, ProviderCache> providerCache;
76 * The username and password to use for dynamically created connections
78 private static String DEFAULT_USER;
79 private static String DEFAULT_PASS;
83 * This default constructor is used as a work around because the activator wasnt getting called
85 @SuppressWarnings("all")
86 public ProviderAdapterImpl() {
92 * This constructor is used primarily in the test cases to bypass initialization of the adapter for isolated,
93 * disconnected testing
96 * True if the adapter is to be initialized, can false if not
98 @SuppressWarnings("all")
99 public ProviderAdapterImpl(boolean initialize) {
100 configuration = ConfigurationFactory.getConfiguration();
110 public ProviderAdapterImpl(@SuppressWarnings("unused") Properties props) {
116 public Server restartServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
118 IProviderOperation op = factory.getOperationObject(Operation.RESTART_SERVICE);
119 op.setProviderCache(this.providerCache);
120 op.setDefaultPass(DEFAULT_PASS);
121 op.setDefaultUser(DEFAULT_USER);
122 return (Server) op.doOperation(params, context);
126 public Server stopServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
128 IProviderOperation op = factory.getOperationObject(Operation.STOP_SERVICE);
129 op.setProviderCache(this.providerCache);
130 op.setDefaultPass(DEFAULT_PASS);
131 op.setDefaultUser(DEFAULT_USER);
132 return (Server) op.doOperation(params, context);
136 public Server startServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
138 IProviderOperation op = factory.getOperationObject(Operation.START_SERVICE);
139 op.setProviderCache(this.providerCache);
140 op.setDefaultPass(DEFAULT_PASS);
141 op.setDefaultUser(DEFAULT_USER);
142 return (Server) op.doOperation(params, context);
146 public Server rebuildServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
148 IProviderOperation op = factory.getOperationObject(Operation.REBUILD_SERVICE);
149 op.setProviderCache(this.providerCache);
150 op.setDefaultPass(DEFAULT_PASS);
151 op.setDefaultUser(DEFAULT_USER);
152 return (Server) op.doOperation(params, context);
156 public Server terminateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
158 IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_SERVICE);
159 op.setProviderCache(this.providerCache);
160 op.setDefaultPass(DEFAULT_PASS);
161 op.setDefaultUser(DEFAULT_USER);
162 return (Server) op.doOperation(params, context);
166 public Server evacuateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
168 IProviderOperation op = factory.getOperationObject(Operation.EVACUATE_SERVICE);
169 op.setProviderCache(this.providerCache);
170 op.setDefaultPass(DEFAULT_PASS);
171 op.setDefaultUser(DEFAULT_USER);
172 // pass this object's reference to EvacuateServer to allow rebuild after evacuate
173 ((EvacuateServer) op).setProvideAdapterRef(this);
174 return (Server) op.doOperation(params, context);
178 public Server migrateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
180 IProviderOperation op = factory.getOperationObject(Operation.MIGRATE_SERVICE);
181 op.setProviderCache(this.providerCache);
182 op.setDefaultPass(DEFAULT_PASS);
183 op.setDefaultUser(DEFAULT_USER);
184 return (Server) op.doOperation(params, context);
188 public Server vmStatuschecker(Map<String, String> params, SvcLogicContext context) throws APPCException {
190 IProviderOperation op = factory.getOperationObject(Operation.VMSTATUSCHECK_SERVICE);
191 op.setProviderCache(this.providerCache);
192 op.setDefaultPass(DEFAULT_PASS);
193 op.setDefaultUser(DEFAULT_USER);
194 return (Server) op.doOperation(params, context);
198 public Stack terminateStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
200 IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_STACK);
201 op.setProviderCache(this.providerCache);
202 op.setDefaultPass(DEFAULT_PASS);
203 op.setDefaultUser(DEFAULT_USER);
204 return (Stack) op.doOperation(params, context);
208 public Stack snapshotStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
210 IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_STACK);
211 op.setProviderCache(this.providerCache);
212 op.setDefaultPass(DEFAULT_PASS);
213 op.setDefaultUser(DEFAULT_USER);
214 return (Stack) op.doOperation(params, context);
218 public Stack restoreStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
220 IProviderOperation op = factory.getOperationObject(Operation.RESTORE_STACK);
221 op.setProviderCache(this.providerCache);
222 op.setDefaultPass(DEFAULT_PASS);
223 op.setDefaultUser(DEFAULT_USER);
224 return (Stack) op.doOperation(params, context);
228 public Server lookupServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
230 IProviderOperation op = factory.getOperationObject(Operation.LOOKUP_SERVICE);
231 op.setProviderCache(this.providerCache);
232 op.setDefaultPass(DEFAULT_PASS);
233 op.setDefaultUser(DEFAULT_USER);
234 return (Server) op.doOperation(params, context);
238 public Image createSnapshot(Map<String, String> params, SvcLogicContext context) throws APPCException {
240 IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_SERVICE);
241 op.setProviderCache(this.providerCache);
242 op.setDefaultPass(DEFAULT_PASS);
243 op.setDefaultUser(DEFAULT_USER);
244 return (Image) op.doOperation(params, context);
248 * Returns the symbolic name of the adapter
250 * @return The adapter name
251 * @see org.openecomp.appc.adapter.iaas.ProviderAdapter#getAdapterName()
254 public String getAdapterName() {
255 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
260 * initialize the provider adapter by building the context cache
262 private void initialize() {
263 configuration = ConfigurationFactory.getConfiguration();
266 * Initialize the provider cache for all defined providers. The definition of the providers uses a structured
267 * property set, where the names form a hierarchical name space (dotted notation, such as one.two.three). Each
268 * name in the name space can also be serialized by appending a sequence number. All nodes at the same level
269 * with the same serial number are grouped together in the namespace hierarchy. This allows a hierarchical
270 * multi-valued property to be defined, which can then be used to setup the provider and tenant caches.
272 * For example, the following definitions show how the namespace hierarchy is defined for two providers, with
273 * two tenants on the first provider and a single tenant for the second provider. <pre>
274 * provider1.type=OpenStackProvider
275 * provider1.name=ILAB
276 * provider1.identity=http://provider1:5000/v2.0
277 * provider1.tenant1.name=CDP-ONAP-APPC
278 * provider1.tenant1.userid=cdpdev
279 * provider1.tenant1.password=cdpdev@123
280 * provider1.tenant2.name=TEST-TENANT
281 * provider1.tenant2.userid=testUser
282 * provider1.tenant2.password=testPassword
283 * provider2.type=OpenStackProvider
284 * provider2.name=PDK1
285 * provider2.identity=http://provider2:5000/v2.0
286 * provider2.tenant1.name=someName
287 * provider2.tenant1.userid=someUser
288 * provider2.tenant1.password=somePassword
292 providerCache = new HashMap<>();
293 Properties properties = configuration.getProperties();
294 List<Node> providers = StructuredPropertyHelper.getStructuredProperties(properties, Property.PROVIDER);
296 for (Node provider : providers) {
297 ProviderCache cache = new ProviderCache();
298 List<Node> providerNodes = provider.getChildren();
299 for (Node node : providerNodes) {
300 if (node.getName().equals(Property.PROVIDER_TYPE)) {
301 cache.setProviderType(node.getValue());
302 } else if (node.getName().equals(Property.PROVIDER_IDENTITY)) {
303 cache.setIdentityURL(node.getValue());
304 cache.setProviderName(node.getValue());
305 } else if (node.getName().startsWith(Property.PROVIDER_TENANT)) {
306 String tenantName = null;
307 String userId = null;
308 String password = null;
309 for (Node node2 : node.getChildren()) {
310 switch (node2.getName()) {
311 case Property.PROVIDER_TENANT_NAME:
312 tenantName = node2.getValue();
314 case Property.PROVIDER_TENANT_USERID:
315 userId = node2.getValue();
316 DEFAULT_USER = node2.getValue();
318 case Property.PROVIDER_TENANT_PASSWORD:
319 password = node2.getValue();
320 DEFAULT_PASS = node2.getValue();
325 cache.addTenant(null, tenantName, userId, password);
330 * Add the provider to the set of providers cached
332 if (cache.getIdentityURL() != null && cache.getProviderType() != null) {
333 providerCache.put(null, cache);
334 providerCache.put(cache.getIdentityURL(), cache);
338 * Now, initialize the cache for the loaded provider