Merge "Remove unneeded param type definition"
[so.git] / adapters / mso-vnf-adapter / src / main / java / org / openecomp / mso / adapters / vnf / AriaVduPlugin.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.mso.adapters.vnf;
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import com.gigaspaces.aria.rest.client.AriaClient;
28 import com.gigaspaces.aria.rest.client.AriaClientFactory;
29 import com.gigaspaces.aria.rest.client.ExecutionDetails;
30 import com.gigaspaces.aria.rest.client.Input;
31 import com.gigaspaces.aria.rest.client.InputImpl;
32 import com.gigaspaces.aria.rest.client.Output;
33 import com.gigaspaces.aria.rest.client.Service;
34 import com.gigaspaces.aria.rest.client.ServiceTemplate;
35 import com.gigaspaces.aria.rest.client.ServiceTemplateImpl;
36 import org.openecomp.mso.logger.MessageEnum;
37 import org.openecomp.mso.logger.MsoLogger;
38 import org.openecomp.mso.openstack.exceptions.MsoAdapterException;
39 import org.openecomp.mso.openstack.exceptions.MsoException;
40 import org.openecomp.mso.vdu.utils.VduBlueprint;
41 import org.openecomp.mso.vdu.utils.VduInfo;
42 import org.openecomp.mso.vdu.utils.VduPlugin;
43 import org.openecomp.mso.vdu.utils.VduStatus;
44
45 /**
46  * ARIA VDU Plugin.  Pluggable interface for the ARIA REST API to support TOSCA
47  * orchestration.
48  * 
49  * @author DeWayne
50  *
51  */
52 public class AriaVduPlugin implements VduPlugin {
53         private static final String API_VERSION = "0.1";
54         private static final MsoLogger logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
55         private AriaClient client=null;
56         private Map<String,Integer> templateIds = new HashMap<>();
57         private Map<String,Integer> serviceIds = new HashMap<>();
58         private Map<String,Map<String,Object>> inputsCache = new HashMap<>();
59
60         public AriaVduPlugin() {
61                 super();
62         }
63
64         public AriaVduPlugin( String host, int port) {
65                 try {
66                         client = new AriaClientFactory().createRestClient("http", host, port, API_VERSION);
67                 }catch(Exception e) {
68                         logger.error (MessageEnum.RA_CREATE_VNF_ERR,  "", "", "", "", "aria", MsoLogger.ErrorCode.AvailabilityError, "Connection to ARIA REST API failed", e);
69                         throw e;
70                 }
71         }
72
73         /**
74          * Instantiate VDU in ARIA. <code>vduInstanceName</code> is used for both service template
75          * name and service name.< 
76          */
77         @SuppressWarnings("unchecked")
78         @Override
79         public VduInfo instantiateVdu(String cloudSiteId, String tenantId, String vduInstanceName,
80                         VduBlueprint vduBlueprint, Map<String, ? extends Object> inputs, String environmentFile, int timeoutMinutes,
81                         boolean suppressBackout) throws MsoException {
82
83                 VduInfo vinfo = new VduInfo(vduInstanceName);
84                 byte[] csar = new CSAR(vduBlueprint).create();
85                 ServiceTemplate template = new ServiceTemplateImpl( vduInstanceName, csar);
86                 try {
87                         client.install_service_template(template);
88                 }
89                 catch(Exception e) {
90                         logger.error(MessageEnum.RA_CREATE_VNF_ERR, "","","","", vduInstanceName, MsoLogger.ErrorCode.BusinessProcesssError,
91                                         "instantiate vdu via csar failed", e);
92                         throw new MsoAdapterException(e.getMessage());
93                 }
94
95                 /**
96                  * Create a service
97                  */
98
99                 try {
100                         int templateId=-1;
101                         for(ServiceTemplate stemplate:(List<ServiceTemplate>)client.list_service_templates()) {
102                                 if(stemplate.getName().equals(vduInstanceName)) {
103                                         templateId = stemplate.getId();
104                                 }
105                         }
106                         List<Input> sinputs = new ArrayList<Input>();
107                         for(Map.Entry<String, ? extends Object> entry: inputs.entrySet()) {
108                                 Input inp = new InputImpl(entry.getKey(),entry.getValue().toString(),"");
109                                 sinputs.add(inp);
110                         }
111                         client.create_service(templateId, vduInstanceName, sinputs);
112                 }
113                 catch(Exception e) {
114                         logger.error(MessageEnum.RA_CREATE_VNF_ERR, "","","","", vduInstanceName, MsoLogger.ErrorCode.BusinessProcesssError,
115                                         "aria service creation failed", e);
116                         throw new MsoAdapterException(e.getMessage());
117                 }
118                 
119                 // Get the service ID and cache it
120                 int sid = getServiceId(vduInstanceName);
121                 serviceIds.put(vduInstanceName, sid);
122
123                 /**
124                  * Run install
125                  */
126
127                 try {
128                         client.start_execution( sid, "install", new ExecutionDetails());
129                 }
130                 catch(Exception e) {
131                         logger.error(MessageEnum.RA_CREATE_VNF_ERR, "","","","", vduInstanceName, MsoLogger.ErrorCode.BusinessProcesssError,
132                                         "aria install workflow failed", e);
133                         throw new MsoAdapterException(e.getMessage());
134                 }
135
136                 /**
137                  * Get the outputs and return 
138                  */
139
140                 try {
141                         Map<String,Object> voutputs = getOutputs(sid);
142
143                         VduInfo vi = new VduInfo(vduInstanceName);
144                         vi.setInputs((Map<String,Object>)inputs);
145                         inputsCache.put(vduInstanceName,vi.getInputs());
146                         vi.setOutputs(voutputs);
147                         vi.setStatus(VduStatus.INSTANTIATED);
148                         return vi;
149                 }
150                 catch(Exception e) {
151                         logger.error(MessageEnum.RA_CREATE_VNF_ERR, "","","","", vduInstanceName, MsoLogger.ErrorCode.BusinessProcesssError,
152                                         "aria service output fetch failed", e);
153                         throw new MsoAdapterException(e.getMessage());
154                 }
155
156         }
157
158         /**
159          * Queries ARIA for VDU status.  vduInstanceId used as template and service name in ARIA (by convention).
160          */
161         @Override
162         public VduInfo queryVdu(String cloudSiteId, String tenantId, String vduInstanceId) throws MsoException {
163                 if(client == null) {
164                         throw new MsoAdapterException("Internal error: no ARIA connection found");
165                 }
166
167                 VduInfo vif = new VduInfo(vduInstanceId);
168                 Integer sid = serviceIds.get(vduInstanceId);
169                 if(sid == null) {
170                         // service doesn't exist
171                         vif.setStatus(VduStatus.NOTFOUND);
172                         return vif;
173                 }
174                 Service service = client.get_service(sid);
175                 if(service == null) {
176                         throw new MsoAdapterException(String.format("Internal error: cached service id %s not found in ARIA",sid)); 
177                 }
178                 Map<String,Object> voutputs = getOutputs(sid);
179                 vif.setOutputs(voutputs);
180                 vif.setInputs(inputsCache.get(vduInstanceId));
181                 vif.setStatus(VduStatus.INSTANTIATED);
182                 return vif;
183         }
184
185         @Override
186         public VduInfo deleteVdu(String cloudSiteId, String tenantId, String vduInstanceId, int timeoutMinutes,
187                         boolean keepBlueprintLoaded) throws MsoException {
188                 
189                 if(client == null) {
190                         throw new MsoAdapterException("Internal error: no ARIA connection found");
191                 }
192                 Integer sid = serviceIds.get(vduInstanceId);
193                 VduInfo vif = new VduInfo(vduInstanceId);
194                 if(sid == null) {
195                         // service doesn't exist
196                         vif.setStatus(VduStatus.NOTFOUND);
197                         return vif;
198                 }
199                 
200                 /**
201                  * Run uninstall
202                  */
203                 try {
204                         client.start_execution( sid, "uninstall", new ExecutionDetails());
205                 }
206                 catch(Exception e) {
207                         logger.error(MessageEnum.RA_CREATE_VNF_ERR, "","","","", vduInstanceId, MsoLogger.ErrorCode.BusinessProcesssError,
208                                         "aria uninstall workflow failed", e);
209                         throw new MsoAdapterException(e.getMessage());
210                 }
211
212                 /**
213                  * Delete the service
214                  */
215                 try {
216                         client.delete_service(sid);
217                 }
218                 catch(Exception e) {
219                         logger.error(MessageEnum.RA_CREATE_VNF_ERR, "","","","", vduInstanceId, MsoLogger.ErrorCode.BusinessProcesssError,
220                                         String.format("aria service delete failed. Service id: %d",sid), e);
221                         throw new MsoAdapterException(e.getMessage());
222                 }
223                 
224                 /**
225                  * Delete the blueprint
226                  */
227                 try {
228                         client.delete_service_template(templateIds.get(vduInstanceId));
229                 }
230                 catch(Exception e) {
231                         logger.error(MessageEnum.RA_CREATE_VNF_ERR, "","","","", vduInstanceId, MsoLogger.ErrorCode.BusinessProcesssError,
232                                         "aria template delete failed", e);
233                         throw new MsoAdapterException(e.getMessage());
234                 }
235                 
236                 vif.setStatus(VduStatus.DELETED);
237                 return vif;
238         }
239
240         /**
241          * Deployment update not possible with ARIA
242          */
243         @Override
244         public VduInfo updateVdu(String cloudSiteId, String tenantId, String vduInstanceId, VduBlueprint vduBlueprint,
245                         Map<String, ? extends Object> inputs, String environmentFile, int timeoutMinutes) throws MsoException {
246                 throw new MsoAdapterException("NOT IMPLEMENTED");
247         }
248
249         /**
250          * Nonsensical in the context of ARIA: blueprint lifespan = vdulifespan
251          */
252         @Override
253         public boolean isBlueprintLoaded(String cloudSiteId, String vduModelId) throws MsoException {
254                 throw new MsoAdapterException("NOT IMPLEMENTED");
255         }
256
257         /**
258          * Nonsensical in the context of ARIA: blueprint lifespan = vdulifespan
259          */
260         @Override
261         public void uploadBlueprint(String cloudSiteId, VduBlueprint vduBlueprint, boolean failIfExists)
262                         throws MsoException {
263                 throw new MsoAdapterException("NOT IMPLEMENTED");
264         }
265
266         @Override
267         public boolean blueprintUploadSupported() {
268                 return false;
269         }
270
271         /**
272          * Private
273          */
274
275         /**p
276          * Gets and repacks service outputs for internal use
277          * @param sid the service id (ARIA service id)
278          * @return
279          */
280         private Map<String,Object> getOutputs(int sid) {
281                 @SuppressWarnings("unchecked")
282                 List<Output> outputs=(List<Output>)client.list_service_outputs(sid);
283                 Map<String,Object> voutputs = new HashMap<>();
284                 for(Output output: outputs) {
285                         voutputs.put(output.getName(), output.getValue());
286                 }
287                 return voutputs;
288         }
289
290         @SuppressWarnings("unchecked")
291         private int getServiceId(String service_name) throws MsoAdapterException{
292                 int sid = -1;
293                 List<Service> services = (List<Service>)client.list_services();
294                 for(Service service:services) {
295                         if(service.getName().equals(service_name)) {
296                                 sid = service.getId();
297                         }
298                 }
299                 if(sid == -1) {
300                         throw new MsoAdapterException(String.format("Internal error: just created service not found: %s",service_name));
301                 }
302                 return sid;
303         }
304
305 }