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.onap.appc.adapter.iaas.impl;
28 import org.onap.appc.Constants;
29 import org.onap.appc.adapter.iaas.ProviderAdapter;
30 import org.onap.appc.adapter.iaas.provider.operation.api.IProviderOperation;
31 import org.onap.appc.adapter.iaas.provider.operation.api.ProviderOperationFactory;
32 import org.onap.appc.adapter.iaas.provider.operation.common.constants.Property;
33 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
34 import org.onap.appc.adapter.iaas.provider.operation.impl.EvacuateServer;
35 import org.onap.appc.configuration.Configuration;
36 import org.onap.appc.configuration.ConfigurationFactory;
37 import org.onap.appc.exceptions.APPCException;
38 import org.onap.appc.util.StructuredPropertyHelper;
39 import org.onap.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.onap.ccsdk.sli.core.sli.SvcLogicContext;
46 import java.util.HashMap;
47 import java.util.List;
49 import java.util.Properties;
52 * This class implements the {@link ProviderAdapter} interface. This interface defines the behaviors that our service
58 @SuppressWarnings("javadoc")
59 public class ProviderAdapterImpl implements ProviderAdapter {
61 * The default domain name for authentication
63 public static final String DEFAULT_DOMAIN_NAME = "Default";
66 * The logger to be used
68 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ProviderAdapterImpl.class);
70 * A reference to the adapter configuration object.
72 private Configuration configuration;
75 * reference to operation factory
77 ProviderOperationFactory factory = ProviderOperationFactory.getInstance();
80 * A cache of providers that are predefined.
82 private Map<String /* provider name */, ProviderCache> providerCache;
85 * The username, password, and domain to use for dynamically created connections
87 private static String DEFAULT_USER;
88 private static String DEFAULT_PASS;
89 private static String DEFAULT_DOMAIN;
93 * This default constructor is used as a work around because the activator wasnt getting called
95 @SuppressWarnings("all")
96 public ProviderAdapterImpl() {
102 * This constructor is used primarily in the test cases to bypass initialization of the adapter for isolated,
103 * disconnected testing
105 * @param initialize True if the adapter is to be initialized, can false if not
107 @SuppressWarnings("all")
108 public ProviderAdapterImpl(boolean initialize) {
109 configuration = ConfigurationFactory.getConfiguration();
116 * @param props not used
118 public ProviderAdapterImpl(@SuppressWarnings("unused") Properties props) {
124 public Server restartServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
126 IProviderOperation op = factory.getOperationObject(Operation.RESTART_SERVICE);
127 op.setProviderCache(this.providerCache);
128 op.setDefaultPass(DEFAULT_PASS);
129 op.setDefaultUser(DEFAULT_USER);
130 op.setDefaultDomain(DEFAULT_DOMAIN);
131 return (Server) op.doOperation(params, context);
135 public Server stopServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
137 IProviderOperation op = factory.getOperationObject(Operation.STOP_SERVICE);
138 op.setProviderCache(this.providerCache);
139 op.setDefaultPass(DEFAULT_PASS);
140 op.setDefaultUser(DEFAULT_USER);
141 op.setDefaultDomain(DEFAULT_DOMAIN);
142 return (Server) op.doOperation(params, context);
146 public Server startServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
148 IProviderOperation op = factory.getOperationObject(Operation.START_SERVICE);
149 op.setProviderCache(this.providerCache);
150 op.setDefaultPass(DEFAULT_PASS);
151 op.setDefaultUser(DEFAULT_USER);
152 op.setDefaultDomain(DEFAULT_DOMAIN);
153 return (Server) op.doOperation(params, context);
157 public Server rebuildServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
159 IProviderOperation op = factory.getOperationObject(Operation.REBUILD_SERVICE);
160 op.setProviderCache(this.providerCache);
161 op.setDefaultPass(DEFAULT_PASS);
162 op.setDefaultUser(DEFAULT_USER);
163 op.setDefaultDomain(DEFAULT_DOMAIN);
164 return (Server) op.doOperation(params, context);
168 public Server terminateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
170 IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_SERVICE);
171 op.setProviderCache(this.providerCache);
172 op.setDefaultPass(DEFAULT_PASS);
173 op.setDefaultUser(DEFAULT_USER);
174 op.setDefaultDomain(DEFAULT_DOMAIN);
175 return (Server) op.doOperation(params, context);
179 public Server evacuateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
181 IProviderOperation op = factory.getOperationObject(Operation.EVACUATE_SERVICE);
182 op.setProviderCache(this.providerCache);
183 op.setDefaultPass(DEFAULT_PASS);
184 op.setDefaultUser(DEFAULT_USER);
185 op.setDefaultDomain(DEFAULT_DOMAIN);
186 // pass this object's reference to EvacuateServer to allow rebuild after evacuate
187 ((EvacuateServer) op).setProvideAdapterRef(this);
188 return (Server) op.doOperation(params, context);
192 public Server migrateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
194 IProviderOperation op = factory.getOperationObject(Operation.MIGRATE_SERVICE);
195 op.setProviderCache(this.providerCache);
196 op.setDefaultPass(DEFAULT_PASS);
197 op.setDefaultUser(DEFAULT_USER);
198 op.setDefaultDomain(DEFAULT_DOMAIN);
199 return (Server) op.doOperation(params, context);
203 public Server vmStatuschecker(Map<String, String> params, SvcLogicContext context) throws APPCException {
205 IProviderOperation op = factory.getOperationObject(Operation.VMSTATUSCHECK_SERVICE);
206 op.setProviderCache(this.providerCache);
207 op.setDefaultPass(DEFAULT_PASS);
208 op.setDefaultUser(DEFAULT_USER);
209 op.setDefaultDomain(DEFAULT_DOMAIN);
210 return (Server) op.doOperation(params, context);
214 public Stack terminateStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
216 IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_STACK);
217 op.setProviderCache(this.providerCache);
218 op.setDefaultPass(DEFAULT_PASS);
219 op.setDefaultUser(DEFAULT_USER);
220 op.setDefaultDomain(DEFAULT_DOMAIN);
221 return (Stack) op.doOperation(params, context);
225 public Stack snapshotStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
227 IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_STACK);
228 op.setProviderCache(this.providerCache);
229 op.setDefaultPass(DEFAULT_PASS);
230 op.setDefaultUser(DEFAULT_USER);
231 op.setDefaultDomain(DEFAULT_DOMAIN);
232 return (Stack) op.doOperation(params, context);
236 public Stack restoreStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
238 IProviderOperation op = factory.getOperationObject(Operation.RESTORE_STACK);
239 op.setProviderCache(this.providerCache);
240 op.setDefaultPass(DEFAULT_PASS);
241 op.setDefaultUser(DEFAULT_USER);
242 op.setDefaultDomain(DEFAULT_DOMAIN);
243 return (Stack) op.doOperation(params, context);
247 public Server lookupServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
249 IProviderOperation op = factory.getOperationObject(Operation.LOOKUP_SERVICE);
250 op.setProviderCache(this.providerCache);
251 op.setDefaultPass(DEFAULT_PASS);
252 op.setDefaultUser(DEFAULT_USER);
253 op.setDefaultDomain(DEFAULT_DOMAIN);
254 return (Server) op.doOperation(params, context);
258 public Image createSnapshot(Map<String, String> params, SvcLogicContext context) throws APPCException {
260 IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_SERVICE);
261 op.setProviderCache(this.providerCache);
262 op.setDefaultPass(DEFAULT_PASS);
263 op.setDefaultUser(DEFAULT_USER);
264 op.setDefaultDomain(DEFAULT_DOMAIN);
265 return (Image) op.doOperation(params, context);
269 * Returns the symbolic name of the adapter
271 * @return The adapter name
272 * @see org.onap.appc.adapter.iaas.ProviderAdapter#getAdapterName()
275 public String getAdapterName() {
276 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
281 * initialize the provider adapter by building the context cache
283 private void initialize() {
284 configuration = ConfigurationFactory.getConfiguration();
287 * Initialize the provider cache for all defined providers. The definition of the providers uses a structured
288 * property set, where the names form a hierarchical name space (dotted notation, such as one.two.three). Each
289 * name in the name space can also be serialized by appending a sequence number. All nodes at the same level
290 * with the same serial number are grouped together in the namespace hierarchy. This allows a hierarchical
291 * multi-valued property to be defined, which can then be used to setup the provider and tenant caches. <p> For
292 * example, the following definitions show how the namespace hierarchy is defined for two providers, with two
293 * tenants on the first provider and a single tenant for the second provider. <pre>
294 * provider1.type=OpenStackProvider provider1.name=ILAB provider1.identity=http://provider1:5000/v2.0
295 * provider1.tenant1.name=CDP-ONAP-APPC provider1.tenant1.userid=testUser
296 * provider1.tenant1.password=testPassword provider1.tenant2.name=TEST-TENANT provider1.tenant2.userid=testUser
297 * provider1.tenant2.password=testPassword provider2.type=OpenStackProvider provider2.name=PDK1
298 * provider2.identity=http://provider2:5000/v2.0 provider2.tenant1.name=someName
299 * provider2.tenant1.userid=someUser provider2.tenant1.password=somePassword </pre> </p>
301 providerCache = new HashMap<>();
302 Properties properties = configuration.getProperties();
303 List<Node> providers = StructuredPropertyHelper.getStructuredProperties(properties, Property.PROVIDER);
305 for (Node provider : providers) {
306 ProviderCache cache = new ProviderCache();
307 List<Node> providerNodes = provider.getChildren();
308 for (Node node : providerNodes) {
309 if (node.getName().equals(Property.PROVIDER_TYPE)) {
310 cache.setProviderType(node.getValue());
311 } else if (node.getName().equals(Property.PROVIDER_IDENTITY)) {
312 cache.setIdentityURL(node.getValue());
313 cache.setProviderName(node.getValue());
314 } else if (node.getName().startsWith(Property.PROVIDER_TENANT)) {
315 String tenantName = null;
316 String userId = null;
317 String password = null;
318 // domain is not required so set a default
319 String domain = DEFAULT_DOMAIN_NAME;
320 for (Node node2 : node.getChildren()) {
321 switch (node2.getName()) {
322 case Property.PROVIDER_TENANT_NAME:
323 tenantName = node2.getValue();
325 case Property.PROVIDER_TENANT_USERID:
326 userId = node2.getValue();
327 DEFAULT_USER = node2.getValue();
329 case Property.PROVIDER_TENANT_PASSWORD:
330 password = node2.getValue();
331 DEFAULT_PASS = node2.getValue();
333 case Property.PROVIDER_TENANT_DOMAIN:
334 domain = node2.getValue();
335 DEFAULT_DOMAIN = node2.getValue();
340 cache.addTenant(null, tenantName, userId, password, domain);
345 * Add the provider to the set of providers cached
347 if (cache.getIdentityURL() != null && cache.getProviderType() != null) {
348 providerCache.put(null, cache);
349 providerCache.put(cache.getIdentityURL(), cache);
353 * Now, initialize the cache for the loaded provider