diff --git a/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/service/generation/GenerationDelegateProxyService.java b/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/service/generation/GenerationDelegateProxyService.java index 1b1188444a..959f5bfc51 100644 --- a/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/service/generation/GenerationDelegateProxyService.java +++ b/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/service/generation/GenerationDelegateProxyService.java @@ -1,175 +1,174 @@ -/******************************************************************************* - * Copyright (c) 2015, 2016 Bosch Software Innovations GmbH and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Eclipse Distribution License v1.0 which accompany this distribution. - * - * The Eclipse Public License is available at - * http://www.eclipse.org/legal/epl-v10.html - * The Eclipse Distribution License is available at - * http://www.eclipse.org/org/documents/edl-v10.php. - * - * Contributors: - * Bosch Software Innovations GmbH - Please refer to git log - *******************************************************************************/ -package org.eclipse.vorto.repository.internal.service.generation; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Comparator; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import org.eclipse.vorto.repository.model.GeneratedOutput; -import org.eclipse.vorto.repository.model.Generator; -import org.eclipse.vorto.repository.model.GeneratorServiceInfo; -import org.eclipse.vorto.repository.model.ModelId; -import org.eclipse.vorto.repository.model.ModelType; -import org.eclipse.vorto.repository.model.ServiceClassifier; -import org.eclipse.vorto.repository.service.GenerationException; -import org.eclipse.vorto.repository.service.GeneratorAlreadyExistsException; -import org.eclipse.vorto.repository.service.IGeneratorService; -import org.eclipse.vorto.repository.service.IModelRepository; -import org.modeshape.common.collection.Collections; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; -import org.springframework.http.converter.ByteArrayHttpMessageConverter; -import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; - -/** - * @author Alexander Edelmann - Robert Bosch (SEA) Pte. Ltd. - */ -@Service -public class GenerationDelegateProxyService implements IGeneratorService { - - @Autowired - private GeneratorLookupRepository registeredGeneratorsRepository; - - @Autowired - private IModelRepository modelRepositoryService; - - - private RestTemplate restTemplate; - - private static final Logger LOGGER = LoggerFactory.getLogger(GenerationDelegateProxyService.class); - - public GenerationDelegateProxyService() { - this.restTemplate = new RestTemplate(); - } - - @Override - public void registerGenerator(String serviceKey, String baseUrl, ServiceClassifier classifier) { - if (!registeredGeneratorsRepository.findByGeneratorKey(serviceKey).isEmpty()) { - throw new GeneratorAlreadyExistsException(serviceKey); - } else { - LOGGER.info("Registered generator {} under base url {} and classifier {}",serviceKey,baseUrl,classifier); - - this.registeredGeneratorsRepository.save(new Generator(serviceKey, baseUrl, classifier.name())); - } - } - - @Override - public void unregisterGenerator(String serviceKey) { - Generator generator = getGenerator(serviceKey); - if (generator != null) { - this.registeredGeneratorsRepository.delete(generator); - } - } - - @Override - public Set getRegisteredGeneratorServiceKeys(ServiceClassifier classifier) { - Set serviceKeys = new HashSet<>(); - for (Generator generator : this.registeredGeneratorsRepository.findByClassifier(classifier.name())) { - serviceKeys.add(generator.getKey()); - } - return Collections.unmodifiableSet(serviceKeys); - } - - @Override - public GeneratorServiceInfo getGeneratorServiceInfo(String serviceKey) { - Generator generatorEntity = getGenerator(serviceKey); - GeneratorServiceInfo generatorInfo = restTemplate.getForObject(generatorEntity.getGenerationInfoUrl(), GeneratorServiceInfo.class); - generatorInfo.setGeneratorInfoUrl(generatorEntity.getGenerationInfoUrl()); - generatorInfo.performRating(generatorEntity.getInvocationCount()); - return generatorInfo; - } - - @Override - public GeneratedOutput generate(ModelId modelId, String serviceKey) { - if (modelRepositoryService.getById(modelId).getModelType() != ModelType.InformationModel) { - throw new GenerationException("Provided model is not an information model. Generation aborted."); - } - restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter()); - Generator generatorEntity = getGenerator(serviceKey); - if (generatorEntity == null) { - throw new GenerationException("Generator with key "+serviceKey+" is not a registered generator"); - } - generatorEntity.increaseInvocationCount(); - this.registeredGeneratorsRepository.save(generatorEntity); - - ResponseEntity entity = restTemplate.getForEntity(generatorEntity.getGenerationEndpointUrl(), byte[].class, modelId.getNamespace(), modelId.getName(), modelId.getVersion()); - return new GeneratedOutput(entity.getBody(), extractFileNameFromHeader(entity), entity.getHeaders().getContentLength()); - } - - private String extractFileNameFromHeader(ResponseEntity entity) { - List values = entity.getHeaders().get("content-disposition"); - if (values.size() > 0) { - int indexOfFileNameStart = values.get(0).indexOf("="); - return values.get(0).substring(indexOfFileNameStart+1); - } - return "generated.output"; - } - - private Generator getGenerator(String serviceKey) { - List generators = this.registeredGeneratorsRepository.findByGeneratorKey(serviceKey); - if (!generators.isEmpty()) { - return generators.get(0); - } else { - return null; - } - } - - @Override - public Collection getMostlyUsedGenerators(int top) { - List topResult = new ArrayList(); - - for (Generator entity : this.registeredGeneratorsRepository.findByClassifier(ServiceClassifier.platform.name())) { - topResult.add(entity); - } - - topResult.sort(new Comparator() { - - @Override - public int compare(Generator o1, Generator o2) { - if (o1.getInvocationCount() > o2.getInvocationCount()) { - return -1; - } else if (o1.getInvocationCount() < o2.getInvocationCount()) { - return +1; - } else { - return 0; - } - } - }); - - List result = new ArrayList<>(top); - int counter = 0; - for (Generator entity : topResult) { - if (counter < top) { - try { - result.add(getGeneratorServiceInfo(entity.getKey())); - counter++; - } catch(Throwable t) { - LOGGER.warn("Generator " + entity.getKey()+" appears to be offline or not deployed. Skipping..."); - } - - } - } - - return result; - } - -} +/******************************************************************************* + * Copyright (c) 2015, 2016 Bosch Software Innovations GmbH and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * The Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Bosch Software Innovations GmbH - Please refer to git log + *******************************************************************************/ +package org.eclipse.vorto.repository.internal.service.generation; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.eclipse.vorto.repository.model.GeneratedOutput; +import org.eclipse.vorto.repository.model.Generator; +import org.eclipse.vorto.repository.model.GeneratorServiceInfo; +import org.eclipse.vorto.repository.model.ModelId; +import org.eclipse.vorto.repository.model.ModelType; +import org.eclipse.vorto.repository.model.ServiceClassifier; +import org.eclipse.vorto.repository.service.GenerationException; +import org.eclipse.vorto.repository.service.GeneratorAlreadyExistsException; +import org.eclipse.vorto.repository.service.IGeneratorService; +import org.eclipse.vorto.repository.service.IModelRepository; +import org.modeshape.common.collection.Collections; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.ByteArrayHttpMessageConverter; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +/** + * @author Alexander Edelmann - Robert Bosch (SEA) Pte. Ltd. + */ +@Service +public class GenerationDelegateProxyService implements IGeneratorService { + + @Autowired + private GeneratorLookupRepository registeredGeneratorsRepository; + + @Autowired + private IModelRepository modelRepositoryService; + + + private RestTemplate restTemplate; + + private static final Logger LOGGER = LoggerFactory.getLogger(GenerationDelegateProxyService.class); + + public GenerationDelegateProxyService() { + this.restTemplate = new RestTemplate(); + } + + @Override + public void registerGenerator(String serviceKey, String baseUrl, ServiceClassifier classifier) { + if (!registeredGeneratorsRepository.findByGeneratorKey(serviceKey).isEmpty()) { + throw new GeneratorAlreadyExistsException(serviceKey); + } else { + LOGGER.info("Registered generator {} under base url {} and classifier {}",serviceKey,baseUrl,classifier); + + this.registeredGeneratorsRepository.save(new Generator(serviceKey, baseUrl, classifier.name())); + } + } + + @Override + public void unregisterGenerator(String serviceKey) { + Generator generator = getGenerator(serviceKey); + if (generator != null) { + this.registeredGeneratorsRepository.delete(generator); + } + } + + @Override + public Set getRegisteredGeneratorServiceKeys(ServiceClassifier classifier) { + Set serviceKeys = new HashSet<>(); + for (Generator generator : this.registeredGeneratorsRepository.findByClassifier(classifier.name())) { + serviceKeys.add(generator.getKey()); + } + return Collections.unmodifiableSet(serviceKeys); + } + + @Override + public GeneratorServiceInfo getGeneratorServiceInfo(String serviceKey) { + Generator generatorEntity = getGenerator(serviceKey); + GeneratorServiceInfo generatorInfo = restTemplate.getForObject(generatorEntity.getGenerationInfoUrl(), GeneratorServiceInfo.class); + generatorInfo.performRating(generatorEntity.getInvocationCount()); + return generatorInfo; + } + + @Override + public GeneratedOutput generate(ModelId modelId, String serviceKey) { + if (modelRepositoryService.getById(modelId).getModelType() != ModelType.InformationModel) { + throw new GenerationException("Provided model is not an information model. Generation aborted."); + } + restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter()); + Generator generatorEntity = getGenerator(serviceKey); + if (generatorEntity == null) { + throw new GenerationException("Generator with key "+serviceKey+" is not a registered generator"); + } + generatorEntity.increaseInvocationCount(); + this.registeredGeneratorsRepository.save(generatorEntity); + + ResponseEntity entity = restTemplate.getForEntity(generatorEntity.getGenerationEndpointUrl(), byte[].class, modelId.getNamespace(), modelId.getName(), modelId.getVersion()); + return new GeneratedOutput(entity.getBody(), extractFileNameFromHeader(entity), entity.getHeaders().getContentLength()); + } + + private String extractFileNameFromHeader(ResponseEntity entity) { + List values = entity.getHeaders().get("content-disposition"); + if (values.size() > 0) { + int indexOfFileNameStart = values.get(0).indexOf("="); + return values.get(0).substring(indexOfFileNameStart+1); + } + return "generated.output"; + } + + private Generator getGenerator(String serviceKey) { + List generators = this.registeredGeneratorsRepository.findByGeneratorKey(serviceKey); + if (!generators.isEmpty()) { + return generators.get(0); + } else { + return null; + } + } + + @Override + public Collection getMostlyUsedGenerators(int top) { + List topResult = new ArrayList(); + + for (Generator entity : this.registeredGeneratorsRepository.findByClassifier(ServiceClassifier.platform.name())) { + topResult.add(entity); + } + + topResult.sort(new Comparator() { + + @Override + public int compare(Generator o1, Generator o2) { + if (o1.getInvocationCount() > o2.getInvocationCount()) { + return -1; + } else if (o1.getInvocationCount() < o2.getInvocationCount()) { + return +1; + } else { + return 0; + } + } + }); + + List result = new ArrayList<>(top); + int counter = 0; + for (Generator entity : topResult) { + if (counter < top) { + try { + result.add(getGeneratorServiceInfo(entity.getKey())); + counter++; + } catch(Throwable t) { + LOGGER.warn("Generator " + entity.getKey()+" appears to be offline or not deployed. Skipping..."); + } + + } + } + + return result; + } + +} diff --git a/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/model/GeneratorServiceInfo.java b/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/model/GeneratorServiceInfo.java index 8e6addbf84..4077e41abd 100644 --- a/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/model/GeneratorServiceInfo.java +++ b/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/model/GeneratorServiceInfo.java @@ -1,95 +1,89 @@ -/******************************************************************************* - * Copyright (c) 2015, 2016 Bosch Software Innovations GmbH and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Eclipse Distribution License v1.0 which accompany this distribution. - * - * The Eclipse Public License is available at - * http://www.eclipse.org/legal/epl-v10.html - * The Eclipse Distribution License is available at - * http://www.eclipse.org/org/documents/edl-v10.php. - * - * Contributors: - * Bosch Software Innovations GmbH - Please refer to git log - *******************************************************************************/ -package org.eclipse.vorto.repository.model; - -public class GeneratorServiceInfo { - private String key; - private String name; - private String description; - private String creator; - private String documentationUrl; - private String image32x32; - private String image144x144; - private ServiceClassifier classifier; - private GeneratorRating rating; - private String generatorInfoUrl; - - public String getKey() { - return key; - } - public void setKey(String key) { - this.key = key; - } - public String getName() { - return name; - } - public void setName(String name) { - this.name = name; - } - public String getDescription() { - return description; - } - public void setDescription(String description) { - this.description = description; - } - public String getCreator() { - return creator; - } - public void setCreator(String creator) { - this.creator = creator; - } - - public String getDocumentationUrl() { - return documentationUrl; - } - public void setDocumentationUrl(String documentationUrl) { - this.documentationUrl = documentationUrl; - } - public String getImage32x32() { - return image32x32; - } - public void setImage32x32(String image32x32) { - this.image32x32 = image32x32; - } - public String getImage144x144() { - return image144x144; - } - public void setImage144x144(String image144x144) { - this.image144x144 = image144x144; - } - public GeneratorRating getRating() { - return rating; - } - public void setRating(GeneratorRating rating) { - this.rating = rating; - } - - public ServiceClassifier getClassifier() { - return classifier; - } - public void setClassifier(ServiceClassifier classifier) { - this.classifier = classifier; - } - public void performRating(int invocationCount) { - this.rating = GeneratorRating.performRating(invocationCount); - } - public String getGeneratorInfoUrl() { - return generatorInfoUrl; - } - public void setGeneratorInfoUrl(String generatorInfoUrl) { - this.generatorInfoUrl = generatorInfoUrl; - } - -} +/******************************************************************************* + * Copyright (c) 2015, 2016 Bosch Software Innovations GmbH and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * The Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Bosch Software Innovations GmbH - Please refer to git log + *******************************************************************************/ +package org.eclipse.vorto.repository.model; + +public class GeneratorServiceInfo { + private String key; + private String name; + private String description; + private String creator; + private String documentationUrl; + private String image32x32; + private String image144x144; + private ServiceClassifier classifier; + private GeneratorRating rating; + + public String getKey() { + return key; + } + public void setKey(String key) { + this.key = key; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + public String getCreator() { + return creator; + } + public void setCreator(String creator) { + this.creator = creator; + } + + public String getDocumentationUrl() { + return documentationUrl; + } + public void setDocumentationUrl(String documentationUrl) { + this.documentationUrl = documentationUrl; + } + public String getImage32x32() { + return image32x32; + } + public void setImage32x32(String image32x32) { + this.image32x32 = image32x32; + } + public String getImage144x144() { + return image144x144; + } + public void setImage144x144(String image144x144) { + this.image144x144 = image144x144; + } + public GeneratorRating getRating() { + return rating; + } + public void setRating(GeneratorRating rating) { + this.rating = rating; + } + + public ServiceClassifier getClassifier() { + return classifier; + } + public void setClassifier(ServiceClassifier classifier) { + this.classifier = classifier; + } + public void performRating(int invocationCount) { + this.rating = GeneratorRating.performRating(invocationCount); + } + + +} diff --git a/server/scripts/urls.lst b/server/scripts/urls.lst index 025a122c77..f7d0f329eb 100644 --- a/server/scripts/urls.lst +++ b/server/scripts/urls.lst @@ -1,2 +1,2 @@ -http://www.eclipse.org/vorto/ -http://vorto.eclipse.org/repo/me +http://www.eclipse.org/vorto/ +http://vorto.eclipse.org/repo/me \ No newline at end of file diff --git a/server/scripts/webmonitor.sh b/server/scripts/webmonitor.sh index c241a2c227..e5fb78f4e2 100644 --- a/server/scripts/webmonitor.sh +++ b/server/scripts/webmonitor.sh @@ -1,42 +1,43 @@ -#!/bin/bash - -# Script to monitor Vorto website and generators. -# -# urls.lst - all urls to monitor -# GEN_URL - URLS to get all registered generators -# EMAIL - Email to sent for alerts - -URLS=urls.lst -GEN_URL=http://vorto.eclipse.org/repo/rest/generation-router/platform -EMAIL=vorto-dev@eclipse.org - - - -function checkurl { - response=$(curl --write-out %{http_code} --silent --output /dev/null $1) - - if [ $response == "200" ] || [ $response == "401" ]; then - echo `date` $1 " working!!!" - else - echo $1 " not working!!!. http_code: $response. Sending email to -> " $EMAIL; echo -n "$response "; - echo "$1 Website is down" `date` | mail -s "$1 Website is down. Please check." "$EMAIL" - fi -} - -function check-all-generators { - urls=$(curl -s -X GET $GEN_URL | ./JSON.sh | egrep '\[*,"generatorInfoUrl"\]' | awk -F " " '{ print $2 }' | tr -d '"') - if [ -n "$output" ]; then - while read -r url; do - echo "Checking $url ..." - checkurl $url - done <<< "$urls" - fi -} - -# Check for all web urls. -while read url; do - checkurl $url -done < $URLS - -# Check for all generators -check-all-generators +@@ -0,0 1,42 @@ +#!/bin/bash + +# Script to monitor Vorto website and generators. +# +# urls.lst - all urls to monitor +# GEN_URL - URLS to get all registered generators +# EMAIL - Email to sent for alerts + +URLS=urls.lst +GEN_URL=http://vorto.eclipse.org/repo/rest/generation-router/platform +EMAIL=vorto-dev@eclipse.org + + + +function checkurl { + response=$(curl --write-out %{http_code} --silent --output /dev/null $1) + + if [ $response == "200" ] || [ $response == "401" ]; then + echo `date` $1 " working!!!" + else + echo $1 " not working!!!. http_code: $response. Sending email to -> " $EMAIL; echo -n "$response "; + echo "$1 Website is down" `date` | mail -s "$1 Website is down. Please check." "$EMAIL" + fi +} + +function check-all-generators { + urls=$(curl -s -X GET $GEN_URL | ./JSON.sh | egrep '\[*,"generatorInfoUrl"\]' | awk -F " " '{ print $2 }' | tr -d '"') + if [ -n "$output" ]; then + while read -r url; do + echo "Checking $url ..." + checkurl $url + done <<< "$urls" + fi +} + +# Check for all web urls. +while read url; do + checkurl $url +done < $URLS + +# Check for all generators +check-all-generators \ No newline at end of file