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
56 @SuppressWarnings("javadoc")
57 public class ProviderAdapterImpl implements ProviderAdapter {
60 * The logger to be used
62 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ProviderAdapterImpl.class);
64 * A reference to the adapter configuration object.
66 private Configuration configuration;
69 * reference to operation factory
71 ProviderOperationFactory factory = ProviderOperationFactory.getInstance();
74 * A cache of providers that are predefined.
76 private Map<String /* provider name */, ProviderCache> providerCache;
79 * The username and password to use for dynamically created connections
81 private static String DEFAULT_USER;
82 private static String DEFAULT_PASS;
86 * This default constructor is used as a work around because the activator wasnt getting called
88 @SuppressWarnings("all")
89 public ProviderAdapterImpl() {
95 * This constructor is used primarily in the test cases to bypass initialization of the adapter for isolated,
96 * disconnected testing
99 * True if the adapter is to be initialized, can false if not
101 @SuppressWarnings("all")
102 public ProviderAdapterImpl(boolean initialize) {
103 configuration = ConfigurationFactory.getConfiguration();
113 public ProviderAdapterImpl(@SuppressWarnings("unused") Properties props) {
119 public Server restartServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
121 IProviderOperation op = factory.getOperationObject(Operation.RESTART_SERVICE);
122 op.setProviderCache(this.providerCache);
123 op.setDefaultPass(DEFAULT_PASS);
124 op.setDefaultUser(DEFAULT_USER);
125 return (Server) op.doOperation(params, context);
129 public Server stopServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
131 IProviderOperation op = factory.getOperationObject(Operation.STOP_SERVICE);
132 op.setProviderCache(this.providerCache);
133 op.setDefaultPass(DEFAULT_PASS);
134 op.setDefaultUser(DEFAULT_USER);
135 return (Server) op.doOperation(params, context);
139 public Server startServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
141 IProviderOperation op = factory.getOperationObject(Operation.START_SERVICE);
142 op.setProviderCache(this.providerCache);
143 op.setDefaultPass(DEFAULT_PASS);
144 op.setDefaultUser(DEFAULT_USER);
145 return (Server) op.doOperation(params, context);
149 public Server rebuildServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
151 IProviderOperation op = factory.getOperationObject(Operation.REBUILD_SERVICE);
152 op.setProviderCache(this.providerCache);
153 op.setDefaultPass(DEFAULT_PASS);
154 op.setDefaultUser(DEFAULT_USER);
155 return (Server) op.doOperation(params, context);
159 public Server terminateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
161 IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_SERVICE);
162 op.setProviderCache(this.providerCache);
163 op.setDefaultPass(DEFAULT_PASS);
164 op.setDefaultUser(DEFAULT_USER);
165 return (Server) op.doOperation(params, context);
169 public Server evacuateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
171 IProviderOperation op = factory.getOperationObject(Operation.EVACUATE_SERVICE);
172 op.setProviderCache(this.providerCache);
173 op.setDefaultPass(DEFAULT_PASS);
174 op.setDefaultUser(DEFAULT_USER);
175 // pass this object's reference to EvacuateServer to allow rebuild after evacuate
176 ((EvacuateServer) op).setProvideAdapterRef(this);
177 return (Server) op.doOperation(params, context);
181 public Server migrateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
183 IProviderOperation op = factory.getOperationObject(Operation.MIGRATE_SERVICE);
184 op.setProviderCache(this.providerCache);
185 op.setDefaultPass(DEFAULT_PASS);
186 op.setDefaultUser(DEFAULT_USER);
187 return (Server) op.doOperation(params, context);
191 public Server vmStatuschecker(Map<String, String> params, SvcLogicContext context) throws APPCException {
193 IProviderOperation op = factory.getOperationObject(Operation.VMSTATUSCHECK_SERVICE);
194 op.setProviderCache(this.providerCache);
195 op.setDefaultPass(DEFAULT_PASS);
196 op.setDefaultUser(DEFAULT_USER);
197 return (Server) op.doOperation(params, context);
201 public Stack terminateStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
203 IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_STACK);
204 op.setProviderCache(this.providerCache);
205 op.setDefaultPass(DEFAULT_PASS);
206 op.setDefaultUser(DEFAULT_USER);
207 return (Stack) op.doOperation(params, context);
211 public Stack snapshotStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
213 IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_STACK);
214 op.setProviderCache(this.providerCache);
215 op.setDefaultPass(DEFAULT_PASS);
216 op.setDefaultUser(DEFAULT_USER);
217 return (Stack) op.doOperation(params, context);
221 public Stack restoreStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
223 IProviderOperation op = factory.getOperationObject(Operation.RESTORE_STACK);
224 op.setProviderCache(this.providerCache);
225 op.setDefaultPass(DEFAULT_PASS);
226 op.setDefaultUser(DEFAULT_USER);
227 return (Stack) op.doOperation(params, context);
231 public Server lookupServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
233 IProviderOperation op = factory.getOperationObject(Operation.LOOKUP_SERVICE);
234 op.setProviderCache(this.providerCache);
235 op.setDefaultPass(DEFAULT_PASS);
236 op.setDefaultUser(DEFAULT_USER);
237 return (Server) op.doOperation(params, context);
241 public Image createSnapshot(Map<String, String> params, SvcLogicContext context) throws APPCException {
243 IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_SERVICE);
244 op.setProviderCache(this.providerCache);
245 op.setDefaultPass(DEFAULT_PASS);
246 op.setDefaultUser(DEFAULT_USER);
247 return (Image) op.doOperation(params, context);
251 * Returns the symbolic name of the adapter
253 * @return The adapter name
254 * @see org.openecomp.appc.adapter.iaas.ProviderAdapter#getAdapterName()
257 public String getAdapterName() {
258 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
263 * initialize the provider adapter by building the context cache
265 private void initialize() {
266 configuration = ConfigurationFactory.getConfiguration();
269 * Initialize the provider cache for all defined providers. The definition of the providers uses a structured
270 * property set, where the names form a hierarchical name space (dotted notation, such as one.two.three). Each
271 * name in the name space can also be serialized by appending a sequence number. All nodes at the same level
272 * with the same serial number are grouped together in the namespace hierarchy. This allows a hierarchical
273 * multi-valued property to be defined, which can then be used to setup the provider and tenant caches.
275 * For example, the following definitions show how the namespace hierarchy is defined for two providers, with
276 * two tenants on the first provider and a single tenant for the second provider. <pre>
277 * provider1.type=OpenStackProvider
278 * provider1.name=ILAB
279 * provider1.identity=http://provider1:5000/v2.0
280 * provider1.tenant1.name=CDP-ONAP-APPC
281 * provider1.tenant1.userid=cdpdev
282 * provider1.tenant1.password=cdpdev@123
283 * provider1.tenant2.name=TEST-TENANT
284 * provider1.tenant2.userid=testUser
285 * provider1.tenant2.password=testPassword
286 * provider2.type=OpenStackProvider
287 * provider2.name=PDK1
288 * provider2.identity=http://provider2:5000/v2.0
289 * provider2.tenant1.name=someName
290 * provider2.tenant1.userid=someUser
291 * provider2.tenant1.password=somePassword
295 providerCache = new HashMap<>();
296 Properties properties = configuration.getProperties();
297 List<Node> providers = StructuredPropertyHelper.getStructuredProperties(properties, Property.PROVIDER);
299 for (Node provider : providers) {
300 ProviderCache cache = new ProviderCache();
301 List<Node> providerNodes = provider.getChildren();
302 for (Node node : providerNodes) {
303 if (node.getName().equals(Property.PROVIDER_TYPE)) {
304 cache.setProviderType(node.getValue());
305 } else if (node.getName().equals(Property.PROVIDER_IDENTITY)) {
306 cache.setIdentityURL(node.getValue());
307 cache.setProviderName(node.getValue());
308 } else if (node.getName().startsWith(Property.PROVIDER_TENANT)) {
309 String tenantName = null;
310 String userId = null;
311 String password = null;
312 for (Node node2 : node.getChildren()) {
313 switch (node2.getName()) {
314 case Property.PROVIDER_TENANT_NAME:
315 tenantName = node2.getValue();
317 case Property.PROVIDER_TENANT_USERID:
318 userId = node2.getValue();
319 DEFAULT_USER = node2.getValue();
321 case Property.PROVIDER_TENANT_PASSWORD:
322 password = node2.getValue();
323 DEFAULT_PASS = node2.getValue();
328 cache.addTenant(null, tenantName, userId, password);
333 * Add the provider to the set of providers cached
335 if (cache.getIdentityURL() != null && cache.getProviderType() != null) {
336 providerCache.put(null, cache);
337 providerCache.put(cache.getIdentityURL(), cache);
341 * Now, initialize the cache for the loaded provider