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.encryption.EncryptionTool;
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 org.onap.ccsdk.sli.core.sli.SvcLogicContext;
44 import java.util.HashMap;
45 import java.util.List;
47 import java.util.Properties;
49 * This class implements the {@link ProviderAdapter} interface. This interface defines the behaviors that our service
55 @SuppressWarnings("javadoc")
56 public class ProviderAdapterImpl implements ProviderAdapter {
58 * The default domain name for authentication
60 public static final String DEFAULT_DOMAIN_NAME = "Default";
62 * A reference to the adapter configuration object.
64 private Configuration configuration;
66 * reference to operation factory
68 private ProviderOperationFactory factory;
70 * A cache of providers that are predefined.
72 private Map<String /* provider name */, ProviderCache> providerCache;
74 * The username, password, and domain to use for dynamically created connections
76 private static String DEFAULT_USER;
77 private static String DEFAULT_PASS;
78 private static String DEFAULT_DOMAIN;
81 * This default constructor is used as a work around because the activator wasnt getting called
83 @SuppressWarnings("all")
84 public ProviderAdapterImpl() {
89 * This constructor is used primarily in the test cases to bypass initialization of the adapter for isolated,
90 * disconnected testing
92 * @param initialize True if the adapter is to be initialized, can false if not
94 @SuppressWarnings("all")
95 public ProviderAdapterImpl(boolean initialize) {
96 configuration = ConfigurationFactory.getConfiguration();
102 * @param props not used
104 public ProviderAdapterImpl(@SuppressWarnings("unused") Properties props) {
109 public Server restartServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
110 IProviderOperation op = factory.getOperationObject(Operation.RESTART_SERVICE);
111 op.setProviderCache(this.providerCache);
112 op.setDefaultPassword(DEFAULT_PASS);
113 op.setDefaultUser(DEFAULT_USER);
114 op.setDefaultDomain(DEFAULT_DOMAIN);
115 return (Server) op.doOperation(params, context);
118 public Server stopServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
119 IProviderOperation op = factory.getOperationObject(Operation.STOP_SERVICE);
120 op.setProviderCache(this.providerCache);
121 op.setDefaultPassword(DEFAULT_PASS);
122 op.setDefaultUser(DEFAULT_USER);
123 op.setDefaultDomain(DEFAULT_DOMAIN);
124 return (Server) op.doOperation(params, context);
127 public Server startServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
128 IProviderOperation op = factory.getOperationObject(Operation.START_SERVICE);
129 op.setProviderCache(this.providerCache);
130 op.setDefaultPassword(DEFAULT_PASS);
131 op.setDefaultUser(DEFAULT_USER);
132 op.setDefaultDomain(DEFAULT_DOMAIN);
133 return (Server) op.doOperation(params, context);
136 public Server rebuildServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
137 IProviderOperation op = factory.getOperationObject(Operation.REBUILD_SERVICE);
138 op.setProviderCache(this.providerCache);
139 op.setDefaultPassword(DEFAULT_PASS);
140 op.setDefaultUser(DEFAULT_USER);
141 op.setDefaultDomain(DEFAULT_DOMAIN);
142 return (Server) op.doOperation(params, context);
145 public Server terminateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
146 IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_SERVICE);
147 op.setProviderCache(this.providerCache);
148 op.setDefaultPassword(DEFAULT_PASS);
149 op.setDefaultUser(DEFAULT_USER);
150 op.setDefaultDomain(DEFAULT_DOMAIN);
151 return (Server) op.doOperation(params, context);
154 public Server evacuateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
155 IProviderOperation op = factory.getOperationObject(Operation.EVACUATE_SERVICE);
156 op.setProviderCache(this.providerCache);
157 op.setDefaultPassword(DEFAULT_PASS);
158 op.setDefaultUser(DEFAULT_USER);
159 op.setDefaultDomain(DEFAULT_DOMAIN);
160 // pass this object's reference to EvacuateServer to allow rebuild after evacuate
161 ((EvacuateServer) op).setProvideAdapterRef(this);
162 return (Server) op.doOperation(params, context);
165 public Server migrateServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
166 IProviderOperation op = factory.getOperationObject(Operation.MIGRATE_SERVICE);
167 op.setProviderCache(this.providerCache);
168 op.setDefaultPassword(DEFAULT_PASS);
169 op.setDefaultUser(DEFAULT_USER);
170 op.setDefaultDomain(DEFAULT_DOMAIN);
171 return (Server) op.doOperation(params, context);
174 public Server vmStatuschecker(Map<String, String> params, SvcLogicContext context) throws APPCException {
175 IProviderOperation op = factory.getOperationObject(Operation.VMSTATUSCHECK_SERVICE);
176 op.setProviderCache(this.providerCache);
177 op.setDefaultPassword(DEFAULT_PASS);
178 op.setDefaultUser(DEFAULT_USER);
179 op.setDefaultDomain(DEFAULT_DOMAIN);
180 return (Server) op.doOperation(params, context);
183 public Stack terminateStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
184 IProviderOperation op = factory.getOperationObject(Operation.TERMINATE_STACK);
185 op.setProviderCache(this.providerCache);
186 op.setDefaultPassword(DEFAULT_PASS);
187 op.setDefaultUser(DEFAULT_USER);
188 op.setDefaultDomain(DEFAULT_DOMAIN);
189 return (Stack) op.doOperation(params, context);
192 public Stack snapshotStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
193 IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_STACK);
194 op.setProviderCache(this.providerCache);
195 op.setDefaultPassword(DEFAULT_PASS);
196 op.setDefaultUser(DEFAULT_USER);
197 op.setDefaultDomain(DEFAULT_DOMAIN);
198 return (Stack) op.doOperation(params, context);
201 public Stack restoreStack(Map<String, String> params, SvcLogicContext context) throws APPCException {
202 IProviderOperation op = factory.getOperationObject(Operation.RESTORE_STACK);
203 op.setProviderCache(this.providerCache);
204 op.setDefaultPassword(DEFAULT_PASS);
205 op.setDefaultUser(DEFAULT_USER);
206 op.setDefaultDomain(DEFAULT_DOMAIN);
207 return (Stack) op.doOperation(params, context);
210 public Server lookupServer(Map<String, String> params, SvcLogicContext context) throws APPCException {
211 IProviderOperation op = factory.getOperationObject(Operation.LOOKUP_SERVICE);
212 op.setProviderCache(this.providerCache);
213 op.setDefaultPassword(DEFAULT_PASS);
214 op.setDefaultUser(DEFAULT_USER);
215 op.setDefaultDomain(DEFAULT_DOMAIN);
216 return (Server) op.doOperation(params, context);
219 public Image createSnapshot(Map<String, String> params, SvcLogicContext context) throws APPCException {
220 IProviderOperation op = factory.getOperationObject(Operation.SNAPSHOT_SERVICE);
221 op.setProviderCache(this.providerCache);
222 op.setDefaultPassword(DEFAULT_PASS);
223 op.setDefaultUser(DEFAULT_USER);
224 op.setDefaultDomain(DEFAULT_DOMAIN);
225 return (Image) op.doOperation(params, context);
228 * Returns the symbolic name of the adapter
230 * @return The adapter name
231 * @see org.onap.appc.adapter.iaas.ProviderAdapter#getAdapterName()
234 public String getAdapterName() {
235 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
238 * initialize the provider adapter by building the context cache
240 private void initialize() {
241 configuration = ConfigurationFactory.getConfiguration();
243 * Initialize the provider cache for all defined providers. The definition of the providers uses a structured
244 * property set, where the names form a hierarchical name space (dotted notation, such as one.two.three). Each
245 * name in the name space can also be serialized by appending a sequence number. All nodes at the same level
246 * with the same serial number are grouped together in the namespace hierarchy. This allows a hierarchical
247 * multi-valued property to be defined, which can then be used to setup the provider and tenant caches. <p> For
248 * example, the following definitions show how the namespace hierarchy is defined for two providers, with two
249 * tenants on the first provider and a single tenant for the second provider. <pre>
250 * provider1.type=OpenStackProvider provider1.name=ILAB provider1.identity=http://provider1:5000/v2.0
251 * provider1.tenant1.name=CDP-ONAP-APPC provider1.tenant1.userid=testUser
252 * provider1.tenant1.password=testPassword provider1.tenant2.name=TEST-TENANT provider1.tenant2.userid=testUser
253 * provider1.tenant2.password=testPassword provider2.type=OpenStackProvider provider2.name=PDK1
254 * provider2.identity=http://provider2:5000/v2.0 provider2.tenant1.name=someName
255 * provider2.tenant1.userid=someUser provider2.tenant1.password=somePassword </pre> </p>
257 factory = ProviderOperationFactory.getInstance();
258 providerCache = new HashMap<>();
259 Properties properties = configuration.getProperties();
260 List<Node> providers = StructuredPropertyHelper.getStructuredProperties(properties, Property.PROVIDER);
261 for (Node provider : providers) {
262 ProviderCache cache = new ProviderCache();
263 List<Node> providerNodes = provider.getChildren();
264 for (Node node : providerNodes) {
265 if (node.getName().equals(Property.PROVIDER_TYPE)) {
266 cache.setProviderType(node.getValue());
267 } else if (node.getName().equals(Property.PROVIDER_IDENTITY)) {
268 cache.setIdentityURL(node.getValue());
269 cache.setProviderName(node.getValue());
270 } else if (node.getName().startsWith(Property.PROVIDER_TENANT)) {
271 String tenantName = null;
272 String userId = null;
273 String password = null;
274 // domain is not required so set a default
275 String domain = ProviderAdapterImpl.DEFAULT_DOMAIN_NAME;
276 for (Node node2 : node.getChildren()) {
277 switch (node2.getName()) {
278 case Property.PROVIDER_TENANT_NAME:
279 tenantName = node2.getValue();
281 case Property.PROVIDER_TENANT_USERID:
282 userId = node2.getValue();
283 ProviderAdapterImpl.DEFAULT_USER = node2.getValue();
285 case Property.PROVIDER_TENANT_PASSWORD:
286 // convert password from Base64 encrypted string prefixed with 'enc:'
287 password = EncryptionTool.getInstance().encrypt(node2.getValue());
288 ProviderAdapterImpl.DEFAULT_PASS = node2.getValue();
290 case Property.PROVIDER_TENANT_DOMAIN:
291 domain = node2.getValue();
292 ProviderAdapterImpl.DEFAULT_DOMAIN = 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(DEFAULT_PASS);
319 op.setDefaultUser(DEFAULT_USER);
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(DEFAULT_PASS);
327 op.setDefaultUser(DEFAULT_USER);
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(DEFAULT_PASS);
335 op.setDefaultUser(DEFAULT_USER);
336 op.setDefaultDomain(DEFAULT_DOMAIN);
337 return (Server) op.doOperation(params, context);