2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.mso.aria;
22 import com.gigaspaces.aria.rest.client.AriaClient;
23 import com.gigaspaces.aria.rest.client.AriaClientFactory;
24 import com.gigaspaces.aria.rest.client.ExecutionDetails;
25 import com.gigaspaces.aria.rest.client.Input;
26 import com.gigaspaces.aria.rest.client.InputImpl;
27 import com.gigaspaces.aria.rest.client.Output;
28 import com.gigaspaces.aria.rest.client.Service;
29 import com.gigaspaces.aria.rest.client.ServiceTemplate;
30 import com.gigaspaces.aria.rest.client.ServiceTemplateImpl;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
35 import org.openecomp.mso.adapters.vdu.CloudInfo;
36 import org.openecomp.mso.adapters.vdu.VduException;
37 import org.openecomp.mso.adapters.vdu.VduInstance;
38 import org.openecomp.mso.adapters.vdu.VduModelInfo;
39 import org.openecomp.mso.adapters.vdu.VduPlugin;
40 import org.openecomp.mso.adapters.vdu.VduStateType;
41 import org.openecomp.mso.adapters.vdu.VduStatus;
42 import org.openecomp.mso.logger.MessageEnum;
43 import org.openecomp.mso.logger.MsoLogger;
46 * ARIA VDU Plugin. Pluggable interface for the ARIA REST API to support TOSCA orchestration.
50 public class AriaVduPlugin implements VduPlugin {
51 private static final String API_VERSION = "0.1";
52 private static final MsoLogger logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
53 private AriaClient client = null;
54 private Map<String, Integer> templateIds = new HashMap<>();
55 private Map<String, Integer> serviceIds = new HashMap<>();
56 private Map<String, Map<String, Object>> inputsCache = new HashMap<>();
58 public AriaVduPlugin() {
62 public AriaVduPlugin(String host, int port) {
64 client = new AriaClientFactory().createRestClient("http", host, port, API_VERSION);
65 } catch (Exception e) {
67 MessageEnum.RA_CREATE_VNF_ERR,
73 MsoLogger.ErrorCode.AvailabilityError,
74 "Connection to ARIA REST API failed",
81 * Instantiate VDU in ARIA. <code>instanceName</code> is used for both service template name and
84 @SuppressWarnings("unchecked")
86 public VduInstance instantiateVdu(
89 Map<String, Object> inputs,
90 VduModelInfo vduModel,
91 boolean rollbackOnFailure)
94 String cloudSiteId = cloudInfo.getCloudSiteId();
95 String tenantId = cloudInfo.getTenantId();
97 // Currently only support simple CSAR with single main template
98 byte[] csar = new CSAR(vduModel).create();
100 ServiceTemplate template = new ServiceTemplateImpl(instanceName, csar);
102 client.install_service_template(template);
103 } catch (Exception e) {
105 MessageEnum.RA_CREATE_VNF_ERR,
111 MsoLogger.ErrorCode.BusinessProcesssError,
112 "instantiate vdu via csar failed",
114 throw new VduException(e.getMessage());
117 /** Create a service */
120 for (ServiceTemplate stemplate :
121 (List<ServiceTemplate>) client.list_service_templates()) {
122 if (stemplate.getName().equals(instanceName)) {
123 templateId = stemplate.getId();
126 List<Input> sinputs = new ArrayList<>();
127 for (Map.Entry<String, ? extends Object> entry : inputs.entrySet()) {
128 Input inp = new InputImpl(entry.getKey(), entry.getValue().toString(), "");
131 client.create_service(templateId, instanceName, sinputs);
132 } catch (Exception e) {
134 MessageEnum.RA_CREATE_VNF_ERR,
140 MsoLogger.ErrorCode.BusinessProcesssError,
141 "aria service creation failed",
143 throw new VduException(e.getMessage());
146 // Get the service ID and cache it
147 int sid = getServiceId(instanceName);
148 serviceIds.put(instanceName, sid);
152 client.start_execution(sid, "install", new ExecutionDetails());
153 } catch (Exception e) {
155 MessageEnum.RA_CREATE_VNF_ERR,
161 MsoLogger.ErrorCode.BusinessProcesssError,
162 "aria install workflow failed",
164 throw new VduException(e.getMessage());
167 /** Get the outputs and return */
169 Map<String, Object> voutputs = getOutputs(sid);
171 VduInstance vi = new VduInstance();
172 vi.setVduInstanceName(instanceName);
173 vi.setInputs((Map<String, Object>) inputs);
174 inputsCache.put(instanceName, vi.getInputs());
175 vi.setOutputs(voutputs);
176 vi.setStatus(new VduStatus(VduStateType.INSTANTIATED));
178 } catch (Exception e) {
180 MessageEnum.RA_CREATE_VNF_ERR,
186 MsoLogger.ErrorCode.BusinessProcesssError,
187 "aria service output fetch failed",
189 throw new VduException(e.getMessage());
194 * Queries ARIA for VDU status. instanceId used as template and service name in ARIA (by
198 public VduInstance queryVdu(CloudInfo cloudInfo, String instanceId) throws VduException {
199 if (client == null) {
200 throw new VduException("Internal error: no ARIA connection found");
203 VduInstance vif = new VduInstance();
204 vif.setVduInstanceId(instanceId);
205 Integer sid = serviceIds.get(instanceId);
207 // service doesn't exist
208 vif.setStatus(new VduStatus(VduStateType.NOTFOUND));
211 Service service = client.get_service(sid);
212 if (service == null) {
213 throw new VduException(
214 String.format("Internal error: cached service id %s not found in ARIA", sid));
216 Map<String, Object> voutputs = getOutputs(sid);
217 vif.setOutputs(voutputs);
218 vif.setInputs(inputsCache.get(instanceId));
219 vif.setStatus(new VduStatus(VduStateType.INSTANTIATED));
224 public VduInstance deleteVdu(CloudInfo cloudInfo, String instanceId, int timeoutMinutes)
225 throws VduException {
226 VduInstance vif = new VduInstance();
227 vif.setVduInstanceId(instanceId);
229 if (client == null) {
230 throw new VduException("Internal error: no ARIA connection found");
232 Integer sid = serviceIds.get(instanceId);
234 // service doesn't exist
235 vif.setStatus(new VduStatus(VduStateType.NOTFOUND));
241 client.start_execution(sid, "uninstall", new ExecutionDetails());
242 } catch (Exception e) {
244 MessageEnum.RA_CREATE_VNF_ERR,
250 MsoLogger.ErrorCode.BusinessProcesssError,
251 "aria uninstall workflow failed",
253 throw new VduException(e.getMessage());
256 /** Delete the service */
258 client.delete_service(sid);
259 } catch (Exception e) {
261 MessageEnum.RA_CREATE_VNF_ERR,
267 MsoLogger.ErrorCode.BusinessProcesssError,
268 String.format("aria service delete failed. Service id: %d", sid),
270 throw new VduException(e.getMessage());
273 /** Delete the blueprint */
275 client.delete_service_template(templateIds.get(instanceId));
276 } catch (Exception e) {
278 MessageEnum.RA_CREATE_VNF_ERR,
284 MsoLogger.ErrorCode.BusinessProcesssError,
285 "aria template delete failed",
287 throw new VduException(e.getMessage());
290 vif.setStatus(new VduStatus(VduStateType.DELETED));
294 /** Deployment update not possible with ARIA */
296 public VduInstance updateVdu(
299 Map<String, Object> inputs,
300 VduModelInfo vduModel,
301 boolean rollbackOnFailure)
302 throws VduException {
303 throw new VduException("NOT IMPLEMENTED");
309 * p Gets and repacks service outputs for internal use
311 * @param sid the service id (ARIA service id)
314 private Map<String, Object> getOutputs(int sid) {
315 @SuppressWarnings("unchecked")
316 List<Output> outputs = (List<Output>) client.list_service_outputs(sid);
317 Map<String, Object> voutputs = new HashMap<>();
318 for (Output output : outputs) {
319 voutputs.put(output.getName(), output.getValue());
324 @SuppressWarnings("unchecked")
325 private int getServiceId(String service_name) throws VduException {
327 List<Service> services = (List<Service>) client.list_services();
328 for (Service service : services) {
329 if (service.getName().equals(service_name)) {
330 sid = service.getId();
334 throw new VduException(
336 "Internal error: just created service not found: %s", service_name));