2 * ============LICENSE_START=======================================================
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
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.openecomp.appc.adapter.iaas.impl;
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;
45 import java.util.HashMap;
46 import java.util.List;
48 import java.util.Properties;
51 * This class implements the {@link ProviderAdapter} interface. This interface defines the behaviors that our service
54 @SuppressWarnings("javadoc")
55 public class ProviderAdapterImpl implements ProviderAdapter {
58 * The logger to be used
60 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ProviderAdapterImpl.class);
62 * A reference to the adapter configuration object.
64 private Configuration configuration;
67 * reference to operation factory
69 ProviderOperationFactory factory = ProviderOperationFactory.getInstance();
72 * A cache of providers that are predefined.
74 private Map<String /* provider name */, ProviderCache> providerCache;
77 * The username and password to use for dynamically created connections
79 private static String DEFAULT_USER;
80 private static String DEFAULT_PASS;
84 * This default constructor is used as a work around because the activator wasnt getting called
86 @SuppressWarnings("all")
87 public ProviderAdapterImpl() {
93 * This constructor is used primarily in the test cases to bypass initialization of the adapter for isolated,
94 * disconnected testing
97 * True if the adapter is to be initialized, can false if not
99 @SuppressWarnings("all")
100 public ProviderAdapterImpl(boolean initialize) {
101 configuration = ConfigurationFactory.getConfiguration();
111 public ProviderAdapterImpl(@SuppressWarnings("unused") Properties props) {
117 public Server restartServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
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);
127 public Server stopServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
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);
137 public Server startServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
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);
147 public Server rebuildServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
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);
157 public Server terminateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
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);
167 public Server evacuateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
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);
179 public Server migrateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
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);
189 public Server vmStatuschecker(Map<String, String> params, SvcLogicContext context) throws APPCException {
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);
199 public Stack terminateStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
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);
209 public Stack snapshotStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
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);
219 public Stack restoreStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
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);
229 public Server lookupServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
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);
239 public Image createSnapshot(Map<String, String> params, SvcLogicContext context) throws APPCException {
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);
249 * Returns the symbolic name of the adapter
251 * @return The adapter name
252 * @see org.openecomp.appc.adapter.iaas.ProviderAdapter#getAdapterName()
255 public String getAdapterName() {
256 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
261 * initialize the provider adapter by building the context cache
263 private void initialize() {
264 configuration = ConfigurationFactory.getConfiguration();
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.
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
293 providerCache = new HashMap<>();
294 Properties properties = configuration.getProperties();
295 List<Node> providers = StructuredPropertyHelper.getStructuredProperties(properties, Property.PROVIDER);
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();
315 case Property.PROVIDER_TENANT_USERID:
316 userId = node2.getValue();
317 DEFAULT_USER = node2.getValue();
319 case Property.PROVIDER_TENANT_PASSWORD:
320 password = node2.getValue();
321 DEFAULT_PASS = node2.getValue();
326 cache.addTenant(null, tenantName, userId, password);
331 * Add the provider to the set of providers cached
333 if (cache.getIdentityURL() != null && cache.getProviderType() != null) {
334 providerCache.put(null, cache);
335 providerCache.put(cache.getIdentityURL(), cache);
339 * Now, initialize the cache for the loaded provider