Skip to content

Commit

Permalink
Merge branch 'master' into issue-32
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzotomasini authored Apr 12, 2018
2 parents c0eafe2 + 8614868 commit 8dd765b
Show file tree
Hide file tree
Showing 30 changed files with 159 additions and 603 deletions.
30 changes: 0 additions & 30 deletions api/src/main/java/org/openbaton/nfvo/api/admin/RestComponents.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,38 +83,8 @@ public class RestComponents {
@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
public String createService(
@RequestHeader(value = "project-id") String projectId,
// @RequestBody @Valid JsonObject serviceCreateBody)
@RequestBody @Valid ServiceCreateBody serviceCreateBody)
throws NotFoundException, MissingParameterException {

// if (!serviceCreateBody.has("name"))
// throw new BadRequestException("The request's json body has to contain a name property.");
// if (!serviceCreateBody.has("roles")) {
// throw new BadRequestException(
// "The request's json body has to contain a roles property as a list of project ids or names.");
// }
//
// String serviceName = null;
// try {
// serviceName = serviceCreateBody.getAsJsonPrimitive("name").getAsString();
// } catch (ClassCastException e1) {
// throw new BadRequestException(
// "The request's json body has to have this form: {'name':'examplename', 'roles':['project1', 'project2'] }");
// } catch (IllegalStateException e2) {
// throw new BadRequestException(
// "The request's json body has to have this form: {'name':'examplename', 'roles':['project1', 'project2'] }");
// }
// Type baseType = new TypeToken<List<String>>() {}.getType();
// List<String> projects;
// try {
// projects = gson.fromJson(serviceCreateBody.get("roles").getAsJsonArray(), baseType);
// } catch (ClassCastException e1) {
// throw new BadRequestException(
// "The request's json body has to have this form: {'name':'examplename', 'roles':['project1', 'project2'] }");
// } catch (IllegalStateException e2) {
// throw new BadRequestException(
// "The request's json body has to have this form: {'name':'examplename', 'roles':['project1', 'project2'] }");
// }
String serviceName = serviceCreateBody.getName();
List<String> projects = serviceCreateBody.getRoles();
return componentManager.createService(serviceName, projectId, projects);
Expand Down
5 changes: 5 additions & 0 deletions api/src/main/java/org/openbaton/nfvo/api/admin/RestUsers.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.oauth2.common.exceptions.UnauthorizedUserException;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -66,6 +67,7 @@ public class RestUsers {
produces = MediaType.APPLICATION_JSON_VALUE
)
@ResponseStatus(HttpStatus.CREATED)
@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
public User create(@RequestBody @Valid User user)
throws PasswordWeakException, NotAllowedException, BadRequestException, NotFoundException {
log.info("Adding user: " + user.getUsername());
Expand All @@ -89,6 +91,7 @@ public User create(@RequestBody @Valid User user)
)
@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.NO_CONTENT)
@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
public void delete(@PathVariable("id") String id) throws NotAllowedException, NotFoundException {
log.info("Removing user with id " + id);
if (isAdmin()) {
Expand Down Expand Up @@ -178,6 +181,7 @@ public User findCurrentUser() throws NotFoundException {
produces = MediaType.APPLICATION_JSON_VALUE
)
@ResponseStatus(HttpStatus.ACCEPTED)
@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
public User update(@RequestBody @Valid User new_user)
throws NotAllowedException, BadRequestException, NotFoundException {
return userManagement.update(new_user);
Expand Down Expand Up @@ -211,6 +215,7 @@ public void changePassword(@RequestBody /*@Valid*/ JsonObject newPwd)
consumes = MediaType.APPLICATION_JSON_VALUE
)
@ResponseStatus(HttpStatus.ACCEPTED)
@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
public void changePasswordOf(
@PathVariable("username") String username, @RequestBody /*@Valid*/ JsonObject newPwd)
throws UnauthorizedUserException, PasswordWeakException, NotFoundException,
Expand Down
17 changes: 14 additions & 3 deletions api/src/main/java/org/openbaton/nfvo/api/runtime/RestEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public class RestEvent {
/**
* Adds a new EventEndpoint to the EventEndpoint repository
*
* @param endpoint : Image to add
* @return image: The image filled with values from the core
* @param endpoint : Event to add
* @return EventEndpoint: The Event filled with values from the core
*/
@RequestMapping(
method = RequestMethod.POST,
Expand All @@ -87,7 +87,18 @@ public EventEndpoint register(
.getCurrentUser()
.getRoles()
.stream()
.noneMatch(r -> r.getProject().equals(endpoint.getProjectId())))
.noneMatch(
r -> {
try {
return r.getProject().equals(endpoint.getProjectId())
|| r.getProject()
.equals(
eventManagement.query(endpoint.getProjectId(), projectId).getId());
} catch (NotFoundException e) {
e.printStackTrace();
return false;
}
}))
throw new BadRequestException("Only services and admin can register to all events");
return eventDispatcher.register(gson.toJson(endpoint));
}
Expand Down
9 changes: 4 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ buildscript {
}

plugins {
id 'net.researchgate.release' version '2.5.0'
id 'net.researchgate.release' version '2.6.0'
id "nebula.ospackage" version "4.5.1"
id 'com.github.sherter.google-java-format' version '0.5'
// For the managing of the Licences
id "com.github.hierynomus.license" version "0.14.0"
// For managing the dependencies' versions
id 'com.github.ben-manes.versions' version '0.13.0'
id 'org.springframework.boot' version '1.5.8.RELEASE'
id 'org.springframework.boot' version '1.5.11.RELEASE'
}

apply plugin: 'java'
Expand All @@ -36,7 +36,7 @@ apply plugin: 'idea'
apply plugin: 'eclipse'

apply from: 'gradle/gradle/git.helpers.gradle'
apply from: 'gradle/gradle/command.line.helper.gradle'
apply from: 'gradle/gradle/helpers.gradle'

/**
* Project settings
Expand All @@ -46,7 +46,7 @@ ext {
/**
* Spring Boot version (replace it also in the plugin section!)
*/
springBootVersion = '1.5.8.RELEASE'
springBootVersion = '1.5.11.RELEASE'
//------------------------------------------------//
//------------------------------------------------//
/**
Expand Down Expand Up @@ -75,7 +75,6 @@ apply from: 'gradle/gradle/git.release.gradle'

apply from: 'gradle/gradle/packaging.nfvo.debian.gradle'
apply from: 'gradle/gradle/packaging.nfvo.docker.gradle'
apply from: 'gradle/gradle/packaging.innosetup.gradle'

apply from: 'gradle/gradle/distribution.gradle'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import javax.persistence.Entity;

/** Created by lto on 27/05/15. */
@Entity
public class VnfmManagerEndpoint extends Endpoint {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeoutException;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
Expand All @@ -47,7 +43,6 @@
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

/** Created by lto on 25/11/15. */
@Service
@Scope
public class RabbitManager {
Expand Down Expand Up @@ -199,75 +194,50 @@ public static void setRabbitMqUserPermissions(
httpclient.close();
}

public static void removeRabbitMqUser(
/**
* Removes a user from RabbitMQ. This function does <em>not</em> throw exceptions even if the
* removal of the user fails.
*
* @param rabbitUsername the user who shall execute the remove operation
* @param rabbitPassword the password of the RabbitMQ instance
* @param brokerIp the IP where RabbitMQ is running
* @param managementPort the port used by RabbitMQ
* @param userToRemove the user to remove
*/
public static void removeRabbitMqUserQuietly(
String rabbitUsername,
String rabbitPassword,
String brokerIp,
String managementPort,
String userToRemove)
throws IOException, WrongStatusException {
String uri = "http://" + brokerIp + ":" + managementPort + "/api/users/" + userToRemove;

// TODO switch to SSL if possible
CloseableHttpClient httpclient = HttpClients.createDefault();

HttpDelete delete = new HttpDelete(uri);
String authStr = rabbitUsername + ":" + rabbitPassword;
String encoding = Base64.encodeBase64String(authStr.getBytes());
delete.setHeader("Authorization", "Basic " + encoding);
delete.setHeader(new BasicHeader("Accept", "application/json"));
// delete.setHeader(new BasicHeader("Content-type", "application/json"));

log.trace("Executing request: " + delete.getMethod() + " on " + uri);

CloseableHttpResponse response = httpclient.execute(delete);
log.trace(String.valueOf("Status: " + response.getStatusLine().getStatusCode()));
if (response.getStatusLine().getStatusCode() != 204) {
throw new WrongStatusException(
"Error removing RabbitMQ user " + userToRemove + ": " + response.getStatusLine());
}

if (response.getStatusLine().getStatusCode() == 404) {
log.warn("User not found... the database is not consistent...");
return;
String userToRemove) {
try {
String uri = "http://" + brokerIp + ":" + managementPort + "/api/users/" + userToRemove;

// TODO switch to SSL if possible
CloseableHttpClient httpclient = HttpClients.createDefault();

HttpDelete delete = new HttpDelete(uri);
String authStr = rabbitUsername + ":" + rabbitPassword;
String encoding = Base64.encodeBase64String(authStr.getBytes());
delete.setHeader("Authorization", "Basic " + encoding);
delete.setHeader(new BasicHeader("Accept", "application/json"));
// delete.setHeader(new BasicHeader("Content-type", "application/json"));

log.trace("Executing request: " + delete.getMethod() + " on " + uri);

CloseableHttpResponse response = httpclient.execute(delete);
log.trace(String.valueOf("Status: " + response.getStatusLine().getStatusCode()));
if (response.getStatusLine().getStatusCode() == 404) {
log.warn("User not found in RabbitMQ. Assuming that it is removed.");
} else if (response.getStatusLine().getStatusCode() != 204) {
log.warn("Error removing RabbitMQ user " + userToRemove + ": " + response.getStatusLine());
}
} catch (Exception e) {
log.warn(
"Ignoring exception while removing RabbitMQ user "
+ userToRemove
+ ": "
+ e.getMessage());
}
}

public static void createQueue(
String brokerIp,
int port,
String rabbitUsername,
String rabbitPassword,
String virtualHost,
String queue,
String exchange)
throws IOException, TimeoutException {
ConnectionFactory factory =
getConnectionFactory(brokerIp, port, rabbitUsername, rabbitPassword, virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(exchange, "topic", true);
channel.queueDeclare(queue, false, false, true, null);
channel.queueBind(queue, exchange, queue);
channel.basicQos(1);
channel.close();
connection.close();
}

private static ConnectionFactory getConnectionFactory(
String brokerIp, int port, String rabbitUsername, String rabbitPassword, String virtualHost) {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(brokerIp);
factory.setPort(port);
factory.setUsername(rabbitUsername);
factory.setPassword(rabbitPassword);
factory.setVirtualHost(virtualHost);
return factory;
}

/*
public static void main(String[] args) throws IOException {
System.out.println(getQueues("localhost", "admin", "openbaton", "/", 5672));
}
*/
}
2 changes: 1 addition & 1 deletion copyrights
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2015 Fraunhofer FOKUS
Copyright (c) 2015-2018 Open Baton (http://openbaton.org)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.openbaton.nfvo.core.api;

import static org.openbaton.nfvo.common.utils.rabbit.RabbitManager.createRabbitMqUser;
import static org.openbaton.nfvo.common.utils.rabbit.RabbitManager.removeRabbitMqUser;
import static org.openbaton.nfvo.common.utils.rabbit.RabbitManager.removeRabbitMqUserQuietly;
import static org.openbaton.nfvo.common.utils.rabbit.RabbitManager.setRabbitMqUserPermissions;

import com.google.gson.Gson;
Expand Down Expand Up @@ -347,21 +347,19 @@ public String enableManager(String message) {
writePermissions,
readPermissions);
} catch (Exception e) {
try {
removeRabbitMqUser(
rabbitUsername, rabbitPassword, brokerIp, managementPort, username);
} catch (Exception e2) {
log.error("Clean up failed. Could not remove RabbitMQ user " + username);
e2.printStackTrace();
}
// remove the created RabbitMQ user again since the whole registration process failed
removeRabbitMqUserQuietly(
rabbitUsername, rabbitPassword, brokerIp, managementPort, username);
throw e;
}

managerCredentials.setRabbitUsername(username);
managerCredentials.setRabbitPassword(password);
managerCredentials = managerCredentialsRepository.save(managerCredentials);
if (endpoint != null) vnfmManagerEndpointRepository.save(endpoint);
log.info("Registered a new manager.");
log.info(
String.format(
"Registered a new manager: %s.", managerCredentials.getRabbitUsername()));
if (!isManager) {
this.refreshVims(username);
}
Expand Down Expand Up @@ -392,7 +390,7 @@ public String enableManager(String message) {
vnfmRegister.unregister(
gson.fromJson(vnfmManagerEndpoint, VnfmManagerEndpoint.class));

removeRabbitMqUser(
removeRabbitMqUserQuietly(
rabbitUsername, rabbitPassword, brokerIp, managementPort, username);
} else {
log.warn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,19 @@ private void scaleOUT(
}
}

networkServiceRecord
.getVlr()
.forEach(
vlr ->
component
.getConnection_point()
.forEach(
cp -> {
if (cp.getVirtual_link_reference().equals(vlr.getName())) {
cp.setVirtual_link_reference_id(vlr.getExtId());
}
}));

log.info(
"Adding VNFComponent to VirtualNetworkFunctionRecord "
+ virtualNetworkFunctionRecord.getName());
Expand Down Expand Up @@ -1543,6 +1556,8 @@ public Iterable<NetworkServiceRecord> query() {
* Triggers the execution of an {@link org.openbaton.catalogue.nfvo.Action} on a specific
* VNFCInstance.
*
* <p>
*
* <p>Note: Currently only the HEAL action is supported.
*
* @param nfvMessage the NFVMessage to send
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class VimManagement implements org.openbaton.nfvo.core.interfaces.VimMana
@Autowired private VNFRRepository vnfrRepository;

private static Map<String, Long> lastUpdateVim = new ConcurrentHashMap<>();
private static Map<String, Object> lockMap = new HashMap<>();
private static final Map<String, Object> lockMap = new HashMap<>();

private final Logger log = LoggerFactory.getLogger(this.getClass());

Expand Down
Loading

0 comments on commit 8dd765b

Please sign in to comment.