2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
26 package org.openecomp.appc.adapter.iaas.impl;
28 import org.openecomp.appc.Constants;
29 import org.openecomp.appc.adapter.iaas.ProviderAdapter;
30 import org.openecomp.appc.adapter.iaas.provider.operation.api.IProviderOperation;
31 import org.openecomp.appc.adapter.iaas.provider.operation.api.ProviderOperationFactory;
32 import org.openecomp.appc.adapter.iaas.provider.operation.common.constants.Property;
33 import org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation;
34 import org.openecomp.appc.adapter.iaas.provider.operation.impl.EvacuateServer;
35 import org.openecomp.appc.configuration.Configuration;
36 import org.openecomp.appc.configuration.ConfigurationFactory;
37 import org.openecomp.appc.exceptions.APPCException;
38 import org.openecomp.appc.util.StructuredPropertyHelper;
39 import org.openecomp.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.openecomp.sdnc.sli.SvcLogicContext;
47 import java.util.HashMap;
48 import java.util.List;
50 import java.util.Properties;
53 * This class implements the {@link ProviderAdapter} interface. This interface defines the behaviors that our service
59 @SuppressWarnings("javadoc")
60 public class ProviderAdapterImpl implements ProviderAdapter {
63 * The logger to be used
65 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ProviderAdapterImpl.class);
67 * A reference to the adapter configuration object.
69 private Configuration configuration;
72 * reference to operation factory
74 ProviderOperationFactory factory = ProviderOperationFactory.getInstance();
77 * A cache of providers that are predefined.
79 private Map<String /* provider name */, ProviderCache> providerCache;
82 * The username and password to use for dynamically created connections
84 private static String DEFAULT_USER;
85 private static String DEFAULT_PASS;
89 * This default constructor is used as a work around because the activator wasnt getting called
91 @SuppressWarnings("all")
92 public ProviderAdapterImpl() {
98 * This constructor is used primarily in the test cases to bypass initialization of the adapter for isolated,
99 * disconnected testing
102 * True if the adapter is to be initialized, can false if not
104 @SuppressWarnings("all")
105 public ProviderAdapterImpl(boolean initialize) {
106 configuration = ConfigurationFactory.getConfiguration();
116 public ProviderAdapterImpl(@SuppressWarnings("unused") Properties props) {
122 public Server restartServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
124 IProviderOperation op = factory.getOperationObject(Operation.RESTART_SERVICE);
125 op.setProviderCache(this.providerCache);
126 op.setDefaultPass(DEFAULT_PASS);
127 op.setDefaultUser(DEFAULT_USER);
128 return (Server) op.doOperation(params, context);
132 public Server stopServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
134 IProviderOperation op = factory.getOperationObject(Operation.STOP_SERVICE);
135 op.setProviderCache(this.providerCache);
136 op.setDefaultPass(DEFAULT_PASS);
137 op.setDefaultUser(DEFAULT_USER);
138 return (Server) op.doOperation(params, context);
142 public Server startServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
144 IProviderOperation op = factory.getOperationObject(Operation.START_SERVICE);
145 op.setProviderCache(this.providerCache);
146 op.setDefaultPass(DEFAULT_PASS);
147 op.setDefaultUser(DEFAULT_USER);
148 return (Server) op.doOperation(params, context);
152 public Server rebuildServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
154 IProviderOperation op = factory.getOperationObject(Operation.REBUILD_SERVICE);
155 op.setProviderCache(this.providerCache);
156 op.setDefaultPass(DEFAULT_PASS);
157 op.setDefaultUser(DEFAULT_USER);
158 return (Server) op.doOperation(params, context);
162 public Server terminateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
164 IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_SERVICE);
165 op.setProviderCache(this.providerCache);
166 op.setDefaultPass(DEFAULT_PASS);
167 op.setDefaultUser(DEFAULT_USER);
168 return (Server) op.doOperation(params, context);
172 public Server evacuateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
174 IProviderOperation op = factory.getOperationObject(Operation.EVACUATE_SERVICE);
175 op.setProviderCache(this.providerCache);
176 op.setDefaultPass(DEFAULT_PASS);
177 op.setDefaultUser(DEFAULT_USER);
178 // pass this object's reference to EvacuateServer to allow rebuild after evacuate
179 ((EvacuateServer) op).setProvideAdapterRef(this);
180 return (Server) op.doOperation(params, context);
184 public Server migrateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
186 IProviderOperation op = factory.getOperationObject(Operation.MIGRATE_SERVICE);
187 op.setProviderCache(this.providerCache);
188 op.setDefaultPass(DEFAULT_PASS);
189 op.setDefaultUser(DEFAULT_USER);
190 return (Server) op.doOperation(params, context);
194 public Server vmStatuschecker(Map<String, String> params, SvcLogicContext context) throws APPCException {
196 IProviderOperation op = factory.getOperationObject(Operation.VMSTATUSCHECK_SERVICE);
197 op.setProviderCache(this.providerCache);
198 op.setDefaultPass(DEFAULT_PASS);
199 op.setDefaultUser(DEFAULT_USER);
200 return (Server) op.doOperation(params, context);
204 public Stack terminateStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
206 IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_STACK);
207 op.setProviderCache(this.providerCache);
208 op.setDefaultPass(DEFAULT_PASS);
209 op.setDefaultUser(DEFAULT_USER);
210 return (Stack) op.doOperation(params, context);
214 public Stack snapshotStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
216 IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_STACK);
217 op.setProviderCache(this.providerCache);
218 op.setDefaultPass(DEFAULT_PASS);
219 op.setDefaultUser(DEFAULT_USER);
220 return (Stack) op.doOperation(params, context);
224 public Stack restoreStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
226 IProviderOperation op = factory.getOperationObject(Operation.RESTORE_STACK);
227 op.setProviderCache(this.providerCache);
228 op.setDefaultPass(DEFAULT_PASS);
229 op.setDefaultUser(DEFAULT_USER);
230 return (Stack) op.doOperation(params, context);
234 public Server lookupServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
236 IProviderOperation op = factory.getOperationObject(Operation.LOOKUP_SERVICE);
237 op.setProviderCache(this.providerCache);
238 op.setDefaultPass(DEFAULT_PASS);
239 op.setDefaultUser(DEFAULT_USER);
240 return (Server) op.doOperation(params, context);
244 public Image createSnapshot(Map<String, String> params, SvcLogicContext context) throws APPCException {
246 IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_SERVICE);
247 op.setProviderCache(this.providerCache);
248 op.setDefaultPass(DEFAULT_PASS);
249 op.setDefaultUser(DEFAULT_USER);
250 return (Image) op.doOperation(params, context);
254 * Returns the symbolic name of the adapter
256 * @return The adapter name
257 * @see org.openecomp.appc.adapter.iaas.ProviderAdapter#getAdapterName()
260 public String getAdapterName() {
261 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
266 * initialize the provider adapter by building the context cache
268 private void initialize() {
269 configuration = ConfigurationFactory.getConfiguration();
272 * Initialize the provider cache for all defined providers. The definition of the providers uses a structured
273 * property set, where the names form a hierarchical name space (dotted notation, such as one.two.three). Each
274 * name in the name space can also be serialized by appending a sequence number. All nodes at the same level
275 * with the same serial number are grouped together in the namespace hierarchy. This allows a hierarchical
276 * multi-valued property to be defined, which can then be used to setup the provider and tenant caches.
278 * For example, the following definitions show how the namespace hierarchy is defined for two providers, with
279 * two tenants on the first provider and a single tenant for the second provider. <pre>
280 * provider1.type=OpenStackProvider
281 * provider1.name=ILAB
282 * provider1.identity=http://provider1:5000/v2.0
283 * provider1.tenant1.name=CDP-ONAP-APPC
284 * provider1.tenant1.userid=testUser
285 * provider1.tenant1.password=testPassword
286 * provider1.tenant2.name=TEST-TENANT
287 * provider1.tenant2.userid=testUser
288 * provider1.tenant2.password=testPassword
289 * provider2.type=OpenStackProvider
290 * provider2.name=PDK1
291 * provider2.identity=http://provider2:5000/v2.0
292 * provider2.tenant1.name=someName
293 * provider2.tenant1.userid=someUser
294 * provider2.tenant1.password=somePassword
298 providerCache = new HashMap<>();
299 Properties properties = configuration.getProperties();
300 List<Node> providers = StructuredPropertyHelper.getStructuredProperties(properties, Property.PROVIDER);
302 for (Node provider : providers) {
303 ProviderCache cache = new ProviderCache();
304 List<Node> providerNodes = provider.getChildren();
305 for (Node node : providerNodes) {
306 if (node.getName().equals(Property.PROVIDER_TYPE)) {
307 cache.setProviderType(node.getValue());
308 } else if (node.getName().equals(Property.PROVIDER_IDENTITY)) {
309 cache.setIdentityURL(node.getValue());
310 cache.setProviderName(node.getValue());
311 } else if (node.getName().startsWith(Property.PROVIDER_TENANT)) {
312 String tenantName = null;
313 String userId = null;
314 String password = null;
315 for (Node node2 : node.getChildren()) {
316 switch (node2.getName()) {
317 case Property.PROVIDER_TENANT_NAME:
318 tenantName = node2.getValue();
320 case Property.PROVIDER_TENANT_USERID:
321 userId = node2.getValue();
322 DEFAULT_USER = node2.getValue();
324 case Property.PROVIDER_TENANT_PASSWORD:
325 password = node2.getValue();
326 DEFAULT_PASS = node2.getValue();
331 cache.addTenant(null, tenantName, userId, password);
336 * Add the provider to the set of providers cached
338 if (cache.getIdentityURL() != null && cache.getProviderType() != null) {
339 providerCache.put(null, cache);
340 providerCache.put(cache.getIdentityURL(), cache);
344 * Now, initialize the cache for the loaded provider