|
| 1 | +package restAPI.architecture_analysis; |
| 2 | + |
| 3 | +import cambio.simulator.ExperimentCreator; |
| 4 | +import cambio.simulator.ExperimentStartupConfig; |
| 5 | +import cambio.simulator.entities.NamedEntity; |
| 6 | +import cambio.simulator.entities.microservice.Microservice; |
| 7 | +import cambio.simulator.entities.microservice.Operation; |
| 8 | +import cambio.simulator.models.ArchitectureModel; |
| 9 | +import cambio.simulator.models.MiSimModel; |
| 10 | +import com.google.common.collect.Multimap; |
| 11 | +import desmoj.core.simulator.Experiment; |
| 12 | +import org.springframework.stereotype.Service; |
| 13 | +import restAPI.data_objects.ArchitectureAnalysisResponse; |
| 14 | +import restAPI.data_objects.ArchitectureAnalysisResponseImpl; |
| 15 | + |
| 16 | +import java.io.File; |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +import static java.util.stream.Collectors.toSet; |
| 23 | + |
| 24 | +@Service |
| 25 | +public class ArchitectureAnalysisService { |
| 26 | + private final File experiment; |
| 27 | + |
| 28 | + public ArchitectureAnalysisService() throws IOException { |
| 29 | + this.experiment = (new ArchitectureAnalysisExperimentFileGenerator()).generateExperimentFile(); |
| 30 | + } |
| 31 | + |
| 32 | + public ArchitectureAnalysisResponse runAnalysis(Multimap<String, String> inputFiles) throws Exception { |
| 33 | + String archDescPath = getArchtectureDescPath(inputFiles); |
| 34 | + ArchitectureModelAdapter architectureModel = new ArchitectureModelAdapter(getArchitectureModel(archDescPath)); |
| 35 | + return new ArchitectureAnalysisResponseImpl(architectureModel.getServiceNames(), architectureModel.getEndpointNames()); |
| 36 | + } |
| 37 | + |
| 38 | + private String getArchtectureDescPath(Multimap<String, String> inputFiles) throws Exception { |
| 39 | + Collection<String> archDescPathCollection = inputFiles.get(ArchitectureAnalysisController.architectureFieldName); |
| 40 | + if (archDescPathCollection.isEmpty()) { |
| 41 | + throw new Exception("You have to provide an architecture description file."); |
| 42 | + } |
| 43 | + return archDescPathCollection.iterator().next(); |
| 44 | + } |
| 45 | + |
| 46 | + private ArchitectureModel getArchitectureModel(final String archDescPath) { |
| 47 | + ExperimentStartupConfig config = new ExperimentStartupConfig(archDescPath, experiment.getAbsolutePath(), |
| 48 | + null, null, null, false, |
| 49 | + false, false, null); |
| 50 | + Experiment experiment = (new ExperimentCreator()).createSimulationExperiment(config); |
| 51 | + MiSimModel model = (MiSimModel) experiment.getModel(); |
| 52 | + return model.getArchitectureModel(); |
| 53 | + } |
| 54 | + |
| 55 | + private static class ArchitectureModelAdapter { |
| 56 | + private final ArchitectureModel architectureModel; |
| 57 | + |
| 58 | + public ArchitectureModelAdapter(ArchitectureModel architectureModel) { |
| 59 | + this.architectureModel = architectureModel; |
| 60 | + } |
| 61 | + |
| 62 | + public Set<String> getServiceNames() { |
| 63 | + return architectureModel.getMicroservices().stream().map(NamedEntity::getPlainName).collect(toSet()); |
| 64 | + } |
| 65 | + |
| 66 | + public Set<String> getEndpointNames() { |
| 67 | + Set<String> names = new HashSet<>(); |
| 68 | + for (Microservice service : architectureModel.getMicroservices()) { |
| 69 | + for (Operation operation : service.getOperations()) { |
| 70 | + names.add(operation.getFullyQualifiedPlainName()); |
| 71 | + } |
| 72 | + } |
| 73 | + return names; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments