[VID-3] Updating pom w/ dav plugin
[vid.git] / vid / src / main / java / org / openecomp / vid / controller / VidController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
21 package org.openecomp.vid.controller;
22
23 import java.io.InputStream;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Map.Entry;
31 import java.util.UUID;
32
33 import javax.servlet.http.HttpServletRequest;
34 import javax.ws.rs.client.ClientBuilder;
35
36 import org.json.JSONObject;
37 import org.json.JSONTokener;
38 import org.openecomp.vid.exceptions.VidServiceUnavailableException;
39 import org.openecomp.vid.model.Network;
40 import org.openecomp.vid.model.ServiceModel;
41 import org.openecomp.vid.model.VNF;
42 import org.openecomp.vid.model.VfModule;
43 import org.openecomp.vid.model.VolumeGroup;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.context.ApplicationContext;
46 import org.springframework.context.annotation.Bean;
47 import org.springframework.web.bind.annotation.PathVariable;
48 import org.springframework.web.bind.annotation.RequestMapping;
49 import org.springframework.web.bind.annotation.RequestMethod;
50 import org.springframework.web.bind.annotation.RestController;
51 import org.springframework.web.servlet.ModelAndView;
52
53 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
54 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
55 import org.openecomp.vid.asdc.AsdcCatalogException;
56 import org.openecomp.vid.asdc.AsdcClient;
57 import org.openecomp.vid.asdc.beans.Resource;
58 import org.openecomp.vid.asdc.beans.Service;
59 import org.openecomp.vid.asdc.beans.tosca.Group;
60 import org.openecomp.vid.asdc.beans.tosca.NodeTemplate;
61 import org.openecomp.vid.asdc.beans.tosca.ToscaCsar;
62 import org.openecomp.vid.asdc.beans.tosca.ToscaModel;
63 import org.openecomp.vid.asdc.memory.InMemoryAsdcClient;
64 import org.openecomp.vid.asdc.rest.RestfulAsdcClient;
65 import org.openecomp.vid.properties.AsdcClientConfiguration;
66 import org.openecomp.vid.properties.AsdcClientConfiguration.AsdcClientType;
67 import com.fasterxml.jackson.databind.ObjectMapper;
68
69 /**
70  * The Class VidController.
71  */
72 @RestController
73 public class VidController extends RestrictedBaseController {
74         
75         /** The Constant LOG. */
76         private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(VidController.class);
77         
78         /** The app context. */
79         @Autowired
80         private ApplicationContext appContext;
81         
82         /**
83          * Gets the object mapper.
84          *
85          * @return the object mapper
86          */
87         @Bean
88         public ObjectMapper getObjectMapper() {
89                 return new ObjectMapper();
90         }
91         
92         /**
93          * Gets the asdc client.
94          *
95          * @return the asdc client
96          */
97         @Bean
98         public AsdcClient getAsdcClient() {
99                 
100                 final AsdcClientConfiguration asdcClientConfig = appContext.getBean(AsdcClientConfiguration.class);
101
102                 switch (asdcClientConfig.getAsdcClientType()) {
103                 case IN_MEMORY:
104                         final InputStream asdcCatalogFile = VidController.class.getClassLoader().getResourceAsStream("catalog.json");
105                         final JSONTokener tokener = new JSONTokener(asdcCatalogFile);
106                         final JSONObject catalog = new JSONObject(tokener);
107
108                         return new InMemoryAsdcClient.Builder().catalog(catalog).build();
109                 case REST:
110
111                         final String protocol = asdcClientConfig.getAsdcClientProtocol();
112                         final String host = asdcClientConfig.getAsdcClientHost();
113                         final int port = asdcClientConfig.getAsdcClientPort();
114                         final String auth = asdcClientConfig.getAsdcClientAuth();
115
116                         try {
117                                 final URI uri = new URI(protocol + "://" + host + ":" + port + "/");
118                                 return new RestfulAsdcClient.Builder(ClientBuilder.newClient(), uri)
119                                                                 .auth(auth)
120                                                                 .build();
121                         } catch (URISyntaxException e) {
122                                 throw new RuntimeException("SDC Client could not be instantiated due to a syntax error in the URI", e);
123                         }
124                         
125                 default:
126                         throw new RuntimeException(asdcClientConfig.getAsdcClientType() + " is invalid; must be one of " + Arrays.toString(AsdcClientType.values()));
127                 }
128         }
129         
130         /**
131          * Gets the services.
132          *
133          * @param request the request
134          * @return the services
135          * @throws VidServiceUnavailableException the vid service unavailable exception
136          */
137         @RequestMapping(value={"/rest/models/services"}, method = RequestMethod.GET)
138         public Collection<Service> getServices(HttpServletRequest request) throws VidServiceUnavailableException {
139                 try {
140                         return getAsdcClient().getServices(request.getParameterMap());
141                 } catch (AsdcCatalogException e) {
142                         LOG.error("Failed to retrieve service definitions from SDC", e);
143                         throw new VidServiceUnavailableException("Failed to retrieve service definitions from SDC", e);
144                 } catch (Throwable t) {
145                         LOG.debug("Unexpected error while retrieving service definitions from SDC: " + t.getMessage() + ":", t);
146                         t.printStackTrace();
147                         throw new VidServiceUnavailableException("Unexpected error while retrieving service definitions from SDC: " + t.getMessage(), t);
148                 }
149         }
150         
151         /**
152          * Gets the services.
153          *
154          * @param uuid the uuid
155          * @return the services
156          * @throws VidServiceUnavailableException the vid service unavailable exception
157          */
158         @RequestMapping(value={"/rest/models/services/{uuid}"}, method = RequestMethod.GET)
159         public ServiceModel getServices(@PathVariable("uuid") String uuid) throws VidServiceUnavailableException {
160                 try {
161                         final ServiceModel serviceModel = new ServiceModel();
162                         final Map<UUID, VNF> vnfs = new HashMap<UUID, VNF> ();
163                         final Map<UUID, Network> networks = new HashMap<UUID, Network> ();
164                         
165                         final ToscaCsar serviceCsar = getAsdcClient().getServiceToscaModel(UUID.fromString(uuid));
166                         final Service asdcServiceMetadata = getAsdcClient().getService(UUID.fromString(uuid));
167                         final ToscaModel asdcService = serviceCsar.getParent();
168                         
169                         serviceModel.setService(ServiceModel.extractService(asdcService, asdcServiceMetadata));
170                         
171                         for (Entry<String, NodeTemplate> component: asdcService.gettopology_template().getnode_templates().entrySet()) {
172                                 final String modelCustomizationName = component.getKey();
173                                 final NodeTemplate nodeTemplate = component.getValue();
174                                 final String type = nodeTemplate.getType();
175                                 
176                                 if (type.startsWith("org.openecomp.resource.vf")) {
177                                         final UUID vnfUuid = UUID.fromString(nodeTemplate.getMetadata().getUUID());
178                                         final VNF vnf = VNF.extractVnf(modelCustomizationName, nodeTemplate);
179                                         
180                                         if (vnf.getVersion() == null) {
181                                                 final Resource vnfMetadata = getAsdcClient().getResource(UUID.fromString(nodeTemplate.getMetadata().getUUID()));
182                                                 vnf.setVersion(vnfMetadata.getVersion());
183                                         }
184                                         
185                                         vnfs.put(vnfUuid, vnf);
186                                 }
187                         }
188                         
189                         for (ToscaModel vnfModel : serviceCsar.getChildren()) {
190                                 final UUID vnfUuid = UUID.fromString(vnfModel.getMetadata().getUUID());
191                                 final VNF vnf = vnfs.get(vnfUuid);
192                                 final Map<UUID, VfModule> vfModules = new HashMap<UUID, VfModule> ();
193                                 final Map<UUID, VolumeGroup> volumeGroups = new HashMap<UUID, VolumeGroup> ();
194                                 
195                                 if (vnf == null) {
196                                         LOG.warn("Couldn't find VNF object " + vnfUuid + ". Problem with Tosca model?");
197                                         continue;
198                                 }
199
200                                 vnf.setInputs(vnfModel.gettopology_template().getInputs());
201
202                                 for (Entry<String, Group> component : vnfModel.gettopology_template().getGroups().entrySet()) {
203                                         final Group group = component.getValue();
204                                         final String type = group.getType();
205                                         
206                                         if (type.startsWith("org.openecomp.groups.VfModule")) {
207                                                 final UUID vfModuleUuid = UUID.fromString(group.getMetadata().getVfModuleModelUUID());
208                                                 
209                                                 vfModules.put(vfModuleUuid, VfModule.extractVfModule(group));
210                                                 
211                                                 if (Boolean.valueOf(group.getProperties().get("volume_group"))) {
212                                                         volumeGroups.put(vfModuleUuid, VolumeGroup.extractVolumeGroup(group));
213                                                 }
214                                         }
215                                 }
216                                 
217                                 vnf.setVfModules(vfModules);
218                                 vnf.setVolumeGroups(volumeGroups);
219                         }
220                         
221                         serviceModel.setVnfs(vnfs);
222                         serviceModel.setNetworks(networks);
223                         
224                         return serviceModel;
225                 } catch (AsdcCatalogException e) {
226                         LOG.error("Failed to retrieve service definitions from SDC", e);
227                         throw new VidServiceUnavailableException("Failed to retrieve service definitions from SDC", e);
228                 }
229         }
230
231         /**
232          * Gets the services view.
233          *
234          * @param request the request
235          * @return the services view
236          * @throws VidServiceUnavailableException the vid service unavailable exception
237          */
238         @RequestMapping(value={"/serviceModels"}, method=RequestMethod.GET)
239         public ModelAndView getServicesView(HttpServletRequest request) throws VidServiceUnavailableException {
240                 return new ModelAndView("serviceModels");
241         }
242 }