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=========================================================
21 package org.openecomp.vid.controller;
23 import java.io.InputStream;
25 import java.net.URISyntaxException;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.HashMap;
30 import java.util.Map.Entry;
31 import java.util.UUID;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.ws.rs.client.ClientBuilder;
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;
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;
70 * The Class VidController.
73 public class VidController extends RestrictedBaseController {
75 /** The Constant LOG. */
76 private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(VidController.class);
78 /** The app context. */
80 private ApplicationContext appContext;
83 * Gets the object mapper.
85 * @return the object mapper
88 public ObjectMapper getObjectMapper() {
89 return new ObjectMapper();
93 * Gets the asdc client.
95 * @return the asdc client
98 public AsdcClient getAsdcClient() {
100 final AsdcClientConfiguration asdcClientConfig = appContext.getBean(AsdcClientConfiguration.class);
102 switch (asdcClientConfig.getAsdcClientType()) {
104 final InputStream asdcCatalogFile = VidController.class.getClassLoader().getResourceAsStream("catalog.json");
105 final JSONTokener tokener = new JSONTokener(asdcCatalogFile);
106 final JSONObject catalog = new JSONObject(tokener);
108 return new InMemoryAsdcClient.Builder().catalog(catalog).build();
111 final String protocol = asdcClientConfig.getAsdcClientProtocol();
112 final String host = asdcClientConfig.getAsdcClientHost();
113 final int port = asdcClientConfig.getAsdcClientPort();
114 final String auth = asdcClientConfig.getAsdcClientAuth();
117 final URI uri = new URI(protocol + "://" + host + ":" + port + "/");
118 return new RestfulAsdcClient.Builder(ClientBuilder.newClient(), uri)
121 } catch (URISyntaxException e) {
122 throw new RuntimeException("SDC Client could not be instantiated due to a syntax error in the URI", e);
126 throw new RuntimeException(asdcClientConfig.getAsdcClientType() + " is invalid; must be one of " + Arrays.toString(AsdcClientType.values()));
133 * @param request the request
134 * @return the services
135 * @throws VidServiceUnavailableException the vid service unavailable exception
137 @RequestMapping(value={"/rest/models/services"}, method = RequestMethod.GET)
138 public Collection<Service> getServices(HttpServletRequest request) throws VidServiceUnavailableException {
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);
147 throw new VidServiceUnavailableException("Unexpected error while retrieving service definitions from SDC: " + t.getMessage(), t);
154 * @param uuid the uuid
155 * @return the services
156 * @throws VidServiceUnavailableException the vid service unavailable exception
158 @RequestMapping(value={"/rest/models/services/{uuid}"}, method = RequestMethod.GET)
159 public ServiceModel getServices(@PathVariable("uuid") String uuid) throws VidServiceUnavailableException {
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> ();
165 final ToscaCsar serviceCsar = getAsdcClient().getServiceToscaModel(UUID.fromString(uuid));
166 final Service asdcServiceMetadata = getAsdcClient().getService(UUID.fromString(uuid));
167 final ToscaModel asdcService = serviceCsar.getParent();
169 serviceModel.setService(ServiceModel.extractService(asdcService, asdcServiceMetadata));
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();
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);
180 if (vnf.getVersion() == null) {
181 final Resource vnfMetadata = getAsdcClient().getResource(UUID.fromString(nodeTemplate.getMetadata().getUUID()));
182 vnf.setVersion(vnfMetadata.getVersion());
185 vnfs.put(vnfUuid, vnf);
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> ();
196 LOG.warn("Couldn't find VNF object " + vnfUuid + ". Problem with Tosca model?");
200 vnf.setInputs(vnfModel.gettopology_template().getInputs());
202 for (Entry<String, Group> component : vnfModel.gettopology_template().getGroups().entrySet()) {
203 final Group group = component.getValue();
204 final String type = group.getType();
206 if (type.startsWith("org.openecomp.groups.VfModule")) {
207 final UUID vfModuleUuid = UUID.fromString(group.getMetadata().getVfModuleModelUUID());
209 vfModules.put(vfModuleUuid, VfModule.extractVfModule(group));
211 if (Boolean.valueOf(group.getProperties().get("volume_group"))) {
212 volumeGroups.put(vfModuleUuid, VolumeGroup.extractVolumeGroup(group));
217 vnf.setVfModules(vfModules);
218 vnf.setVolumeGroups(volumeGroups);
221 serviceModel.setVnfs(vnfs);
222 serviceModel.setNetworks(networks);
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);
232 * Gets the services view.
234 * @param request the request
235 * @return the services view
236 * @throws VidServiceUnavailableException the vid service unavailable exception
238 @RequestMapping(value={"/serviceModels"}, method=RequestMethod.GET)
239 public ModelAndView getServicesView(HttpServletRequest request) throws VidServiceUnavailableException {
240 return new ModelAndView("serviceModels");