2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Modifications Copyright (C) 2019 IBM.
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.iaas.impl;
27 import org.onap.appc.Constants;
28 import org.onap.appc.adapter.iaas.ProviderAdapter;
29 import org.onap.appc.adapter.iaas.provider.operation.api.IProviderOperation;
30 import org.onap.appc.adapter.iaas.provider.operation.api.ProviderOperationFactory;
31 import org.onap.appc.adapter.iaas.provider.operation.common.constants.Property;
32 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
33 import org.onap.appc.adapter.iaas.provider.operation.impl.EvacuateServer;
34 import org.onap.appc.configuration.Configuration;
35 import org.onap.appc.configuration.ConfigurationFactory;
36 import org.onap.appc.exceptions.APPCException;
37 import org.onap.appc.util.StructuredPropertyHelper;
38 import org.onap.appc.util.StructuredPropertyHelper.Node;
39 import com.att.cdp.zones.model.Image;
40 import com.att.cdp.zones.model.Server;
41 import com.att.cdp.zones.model.Stack;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
43 import java.util.HashMap;
44 import java.util.List;
46 import java.util.Properties;
48 * This class implements the {@link ProviderAdapter} interface. This interface defines the behaviors that our service
54 @SuppressWarnings("javadoc")
55 public class ProviderAdapterImpl implements ProviderAdapter {
57 * The default domain name for authentication
59 public static final String DEFAULT_DOMAIN_NAME = "Default";
61 * A reference to the adapter configuration object.
63 private Configuration configuration;
65 * reference to operation factory
67 private ProviderOperationFactory factory;
69 * A cache of providers that are predefined.
71 private Map<String /* provider name */, ProviderCache> providerCache;
73 * The username, password, and domain to use for dynamically created connections
75 private static String defaultUser;
76 private static String defaultPass;
77 private static String defaultDomain;
80 * This default constructor is used as a work around because the activator wasnt getting called
82 @SuppressWarnings("all")
83 public ProviderAdapterImpl() {
88 * This constructor is used primarily in the test cases to bypass initialization of the adapter for isolated,
89 * disconnected testing
91 * @param initialize True if the adapter is to be initialized, can false if not
93 @SuppressWarnings("all")
94 public ProviderAdapterImpl(boolean initialize) {
95 configuration = ConfigurationFactory.getConfiguration();
101 * @param props not used
103 public ProviderAdapterImpl(@SuppressWarnings("unused") Properties props) {
108 public Server restartServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
109 IProviderOperation op = factory.getOperationObject(Operation.RESTART_SERVICE);
110 op.setProviderCache(this.providerCache);
111 op.setDefaultPassword(defaultPass);
112 op.setDefaultUser(defaultUser);
113 op.setDefaultDomain(defaultDomain);
114 return (Server) op.doOperation(params, context);
117 public Server stopServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
118 IProviderOperation op = factory.getOperationObject(Operation.STOP_SERVICE);
119 op.setProviderCache(this.providerCache);
120 op.setDefaultPassword(defaultPass);
121 op.setDefaultUser(defaultUser);
122 op.setDefaultDomain(defaultDomain);
123 return (Server) op.doOperation(params, context);
126 public Server startServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
127 IProviderOperation op = factory.getOperationObject(Operation.START_SERVICE);
128 op.setProviderCache(this.providerCache);
129 op.setDefaultPassword(defaultPass);
130 op.setDefaultUser(defaultUser);
131 op.setDefaultDomain(defaultDomain);
132 return (Server) op.doOperation(params, context);
135 public Server rebuildServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
136 IProviderOperation op = factory.getOperationObject(Operation.REBUILD_SERVICE);
137 op.setProviderCache(this.providerCache);
138 op.setDefaultPassword(defaultPass);
139 op.setDefaultUser(defaultUser);
140 op.setDefaultDomain(defaultDomain);
141 return (Server) op.doOperation(params, context);
144 public Server terminateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
145 IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_SERVICE);
146 op.setProviderCache(this.providerCache);
147 op.setDefaultPassword(defaultPass);
148 op.setDefaultUser(defaultUser);
149 op.setDefaultDomain(defaultDomain);
150 return (Server) op.doOperation(params, context);
153 public Server evacuateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
154 IProviderOperation op = factory.getOperationObject(Operation.EVACUATE_SERVICE);
155 op.setProviderCache(this.providerCache);
156 op.setDefaultPassword(defaultPass);
157 op.setDefaultUser(defaultUser);
158 op.setDefaultDomain(defaultDomain);
159 // pass this object's reference to EvacuateServer to allow rebuild after evacuate
160 ((EvacuateServer) op).setProvideAdapterRef(this);
161 return (Server) op.doOperation(params, context);
164 public Server migrateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
165 IProviderOperation op = factory.getOperationObject(Operation.MIGRATE_SERVICE);
166 op.setProviderCache(this.providerCache);
167 op.setDefaultPassword(defaultPass);
168 op.setDefaultUser(defaultUser);
169 op.setDefaultDomain(defaultDomain);
170 return (Server) op.doOperation(params, context);
173 public Server vmStatuschecker(Map<String, String> params, SvcLogicContext context) throws APPCException {
174 IProviderOperation op = factory.getOperationObject(Operation.VMSTATUSCHECK_SERVICE);
175 op.setProviderCache(this.providerCache);
176 op.setDefaultPassword(defaultPass);
177 op.setDefaultUser(defaultUser);
178 op.setDefaultDomain(defaultDomain);
179 return (Server) op.doOperation(params, context);
182 public Stack terminateStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
183 IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_STACK);
184 op.setProviderCache(this.providerCache);
185 op.setDefaultPassword(defaultPass);
186 op.setDefaultUser(defaultUser);
187 op.setDefaultDomain(defaultDomain);
188 return (Stack) op.doOperation(params, context);
191 public Stack snapshotStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
192 IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_STACK);
193 op.setProviderCache(this.providerCache);
194 op.setDefaultPassword(defaultPass);
195 op.setDefaultUser(defaultUser);
196 op.setDefaultDomain(defaultDomain);
197 return (Stack) op.doOperation(params, context);
200 public Stack restoreStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
201 IProviderOperation op = factory.getOperationObject(Operation.RESTORE_STACK);
202 op.setProviderCache(this.providerCache);
203 op.setDefaultPassword(defaultPass);
204 op.setDefaultUser(defaultUser);
205 op.setDefaultDomain(defaultDomain);
206 return (Stack) op.doOperation(params, context);
209 public Server lookupServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
210 IProviderOperation op = factory.getOperationObject(Operation.LOOKUP_SERVICE);
211 op.setProviderCache(this.providerCache);
212 op.setDefaultPassword(defaultPass);
213 op.setDefaultUser(defaultUser);
214 op.setDefaultDomain(defaultDomain);
215 return (Server) op.doOperation(params, context);
218 public Image createSnapshot(Map<String, String> params, SvcLogicContext context) throws APPCException {
219 IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_SERVICE);
220 op.setProviderCache(this.providerCache);
221 op.setDefaultPassword(defaultPass);
222 op.setDefaultUser(defaultUser);
223 op.setDefaultDomain(defaultDomain);
224 return (Image) op.doOperation(params, context);
227 * Returns the symbolic name of the adapter
229 * @return The adapter name
230 * @see org.onap.appc.adapter.iaas.ProviderAdapter#getAdapterName()
233 public String getAdapterName() {
234 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
237 * initialize the provider adapter by building the context cache
239 private void initialize() {
240 configuration = ConfigurationFactory.getConfiguration();
242 * Initialize the provider cache for all defined providers. The definition of the providers uses a structured
243 * property set, where the names form a hierarchical name space (dotted notation, such as one.two.three). Each
244 * name in the name space can also be serialized by appending a sequence number. All nodes at the same level
245 * with the same serial number are grouped together in the namespace hierarchy. This allows a hierarchical
246 * multi-valued property to be defined, which can then be used to setup the provider and tenant caches. <p> For
247 * example, the following definitions show how the namespace hierarchy is defined for two providers, with two
248 * tenants on the first provider and a single tenant for the second provider. <pre>
249 * provider1.type=OpenStackProvider provider1.name=ILAB provider1.identity=http://provider1:5000/v2.0
250 * provider1.tenant1.name=CDP-ONAP-APPC provider1.tenant1.userid=testUser
251 * provider1.tenant1.password=testPassword provider1.tenant2.name=TEST-TENANT provider1.tenant2.userid=testUser
252 * provider1.tenant2.password=testPassword provider2.type=OpenStackProvider provider2.name=PDK1
253 * provider2.identity=http://provider2:5000/v2.0 provider2.tenant1.name=someName
254 * provider2.tenant1.userid=someUser provider2.tenant1.password=somePassword </pre> </p>
256 factory = ProviderOperationFactory.getInstance();
257 providerCache = new HashMap<>();
258 Properties properties = configuration.getProperties();
259 List<Node> providers = StructuredPropertyHelper.getStructuredProperties(properties, Property.PROVIDER);
260 for (Node provider : providers) {
261 ProviderCache cache = new ProviderCache();
262 List<Node> providerNodes = provider.getChildren();
263 for (Node node : providerNodes) {
264 if (node.getName().equals(Property.PROVIDER_TYPE)) {
265 cache.setProviderType(node.getValue());
266 } else if (node.getName().equals(Property.PROVIDER_IDENTITY)) {
267 cache.setIdentityURL(node.getValue());
268 cache.setProviderName(node.getValue());
269 } else if (node.getName().startsWith(Property.PROVIDER_TENANT)) {
270 String tenantName = null;
271 String userId = null;
272 String password = null;
273 // domain is not required so set a default
274 String domain = ProviderAdapterImpl.DEFAULT_DOMAIN_NAME;
275 for (Node node2 : node.getChildren()) {
276 switch (node2.getName()) {
277 case Property.PROVIDER_TENANT_NAME:
278 tenantName = node2.getValue();
280 case Property.PROVIDER_TENANT_USERID:
281 userId = node2.getValue();
282 ProviderAdapterImpl.defaultUser = node2.getValue();
284 case Property.PROVIDER_TENANT_PASSWORD:
285 //The passwords are automatically decrpyted by the appc Configuration
286 //class and require no additional method calls to decrypt.
287 password = node2.getValue();
288 ProviderAdapterImpl.defaultPass = node2.getValue();
290 case Property.PROVIDER_TENANT_DOMAIN:
291 domain = node2.getValue();
292 ProviderAdapterImpl.defaultDomain = node2.getValue();
298 cache.addTenant(null, tenantName, userId, password, domain);
302 * Add the provider to the set of providers cached
304 if (cache.getIdentityURL() != null && cache.getProviderType() != null) {
305 providerCache.put(null, cache);
306 providerCache.put(cache.getIdentityURL(), cache);
309 * Now, initialize the cache for the loaded provider
315 public Server attachVolume(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
316 IProviderOperation op = factory.getOperationObject(Operation.ATTACHVOLUME_SERVICE);
317 op.setProviderCache(this.providerCache);
318 op.setDefaultPassword(defaultPass);
319 op.setDefaultUser(defaultUser);
320 return (Server) op.doOperation(params, ctx);
323 public Server dettachVolume(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
324 IProviderOperation op = factory.getOperationObject(Operation.DETACHVOLUME_SERVICE);
325 op.setProviderCache(this.providerCache);
326 op.setDefaultPassword(defaultPass);
327 op.setDefaultUser(defaultUser);
328 return (Server) op.doOperation(params, ctx);
331 public Server rebootServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
332 IProviderOperation op = factory.getOperationObject(Operation.REBOOT_SERVICE);
333 op.setProviderCache(this.providerCache);
334 op.setDefaultPassword(defaultPass);
335 op.setDefaultUser(defaultUser);
336 op.setDefaultDomain(defaultDomain);
337 return (Server) op.doOperation(params, context);