import org.codehaus.enunciate.jaxrs.ResponseCode;
import org.codehaus.enunciate.jaxrs.StatusCodes;
import org.codehaus.enunciate.jaxrs.TypeHint;
+import org.opendaylight.neutron.spi.INeutronVPNIKEPolicyAware;
import org.opendaylight.neutron.spi.INeutronVPNIPSECSiteConnectionAware;
+import org.opendaylight.neutron.spi.INeutronVPNIPSECSiteConnectionsCRUD;
+import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
import org.opendaylight.neutron.spi.NeutronVPNIPSECSiteConnection;
/**
@Path("/vpn/ipsecsiteconnections")
public class NeutronVPNIPSECSiteConnectionsNorthbound {
+ private NeutronVPNIPSECSiteConnection extractFields(NeutronVPNIPSECSiteConnection o, List<String> fields) {
+ return o.extractFields(fields);
+ }
+
@Context
UriInfo uriInfo;
/**
- * Returns a list of all VPN IPSEC SiteConnections */
+ * Returns a list of all VPN IPSEC SiteConnections
+ */
@GET
@Produces({ MediaType.APPLICATION_JSON })
- @StatusCodes({
- @ResponseCode(code = 501, condition = "Not Implemented") })
+ @StatusCodes({ @ResponseCode(code = 200, condition = "Operation successful"),
+ @ResponseCode(code = 401, condition = "Unauthorized"),
+ @ResponseCode(code = 501, condition = "Not Implemented"),
+ @ResponseCode(code = 503, condition = "No providers available") })
public Response listVPNIPSECSiteConnections(
- ) {
- throw new UnimplementedException("Not Implemented");
+ // return fields
+ @QueryParam("fields") List<String> fields,
+ // filter fields
+ @QueryParam("id") String queryID, @QueryParam("tenant_id") String queryTenantID,
+ @QueryParam("name") String queryName, @QueryParam("description") String queryDescription,
+ @QueryParam("peer_address") String queryPeerAddress, @QueryParam("peer_id") String queryPeerID,
+ @QueryParam("route_mode") String queryRouteMode, @QueryParam("mtu") Integer queryMtu,
+ @QueryParam("auth_mode") String queryAuthMode, @QueryParam("psk") String queryPsk,
+ @QueryParam("initiator") String queryInitiator, @QueryParam("admin_state_up") String queryAdminStateUp,
+ @QueryParam("status") String queryStatus, @QueryParam("ikepolicy_id") String queryIkePolicyID,
+ @QueryParam("ipsecpolicy_id") String queryIpSecPolicyID,
+ @QueryParam("vpnservice_id") String queryVpnServiceID
+ // pagination and sorting are TODO
+ ) {
+ INeutronVPNIPSECSiteConnectionsCRUD labelInterface = NeutronCRUDInterfaces
+ .getINeutronVPNIPSECSiteConnectionsCRUD(this);
+ if (labelInterface == null) {
+ throw new ServiceUnavailableException("NeutronVPNIPSECSiteConnections CRUD Interface "
+ + RestMessages.SERVICEUNAVAILABLE.toString());
+ }
+ List<NeutronVPNIPSECSiteConnection> allNeutronVPNIPSECSiteConnection = labelInterface
+ .getAllNeutronVPNIPSECSiteConnections();
+ List<NeutronVPNIPSECSiteConnection> ans = new ArrayList<NeutronVPNIPSECSiteConnection>();
+ Iterator<NeutronVPNIPSECSiteConnection> i = allNeutronVPNIPSECSiteConnection.iterator();
+ while (i.hasNext()) {
+ NeutronVPNIPSECSiteConnection oSS = i.next();
+ if ((queryID == null || queryID.equals(oSS.getID()))
+ && (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))
+ && (queryName == null || queryName.equals(oSS.getName()))
+ && (queryDescription == null || queryDescription.equals(oSS.getDescription()))
+ && (queryPeerAddress == null || queryPeerAddress.equals(oSS.getPeerAddress()))
+ && (queryPeerID == null || queryPeerID.equals(oSS.getPeerID()))
+ && (queryRouteMode == null || queryRouteMode.equals(oSS.getRouteMode()))
+ && (queryMtu == null || queryMtu.equals(oSS.getMtu()))
+ && (queryAuthMode == null || queryAuthMode.equals(oSS.getAuthMode()))
+ && (queryPsk == null || queryPsk.equals(oSS.getPreSharedKey()))
+ && (queryInitiator == null || queryInitiator.equals(oSS.getInitiator()))
+ && (queryAdminStateUp == null || queryAdminStateUp.equals(oSS.getAdminStateUp()))
+ && (queryStatus == null || queryStatus.equals(oSS.getStatus()))
+ && (queryIkePolicyID == null || queryIkePolicyID.equals(oSS.getIkePolicyID()))
+ && (queryIpSecPolicyID == null || queryIpSecPolicyID.equals(oSS.getIpsecPolicyID()))
+ && (queryVpnServiceID == null || queryVpnServiceID.equals(oSS.getVpnServiceID())))
+ if (fields.size() > 0)
+ ans.add(extractFields(oSS, fields));
+ else
+ ans.add(oSS);
+ }
+
+ // TODO: apply pagination to results
+ return Response.status(200).entity(new NeutronVPNIPSECSiteConnectionRequest(ans)).build();
}
/**
- * Returns a specific VPN IPSEC SiteConnection */
+ * Returns a specific VPN IPSEC SiteConnection
+ */
@Path("{connectionID}")
@GET
@Produces({ MediaType.APPLICATION_JSON })
- @StatusCodes({
- @ResponseCode(code = 501, condition = "Not Implemented") })
- public Response showVPNIPSECSiteConnection(
- @PathParam("connectionID") String connectionID,
- // return fields
- @QueryParam("fields") List<String> fields
- ) {
- throw new UnimplementedException("Not Implemented");
+ @StatusCodes({ @ResponseCode(code = 200, condition = "Operation successful"),
+ @ResponseCode(code = 401, condition = "Unauthorized"), @ResponseCode(code = 403, condition = "Forbidden"),
+ @ResponseCode(code = 404, condition = "Not Found"),
+ @ResponseCode(code = 501, condition = "Not Implemented"),
+ @ResponseCode(code = 503, condition = "No providers available") })
+ public Response showVPNIPSECSiteConnection(@PathParam("policyID") String policyID,
+ // return fields
+ @QueryParam("fields") List<String> fields) {
+ INeutronVPNIPSECSiteConnectionsCRUD connectionInterface = NeutronCRUDInterfaces
+ .getINeutronVPNIPSECSiteConnectionsCRUD(this);
+ if (connectionInterface == null) {
+ throw new ServiceUnavailableException("NeutronVPNIPSECSiteConnections CRUD Interface "
+ + RestMessages.SERVICEUNAVAILABLE.toString());
+ }
+ if (!connectionInterface.neutronVPNIPSECSiteConnectionsExists(policyID)) {
+ throw new ResourceNotFoundException("NeutronVPNIPSECSiteConnections ID not found");
+ }
+ if (fields.size() > 0) {
+ NeutronVPNIPSECSiteConnection ans = connectionInterface.getNeutronVPNIPSECSiteConnections(policyID);
+ return Response.status(200).entity(new NeutronVPNIPSECSiteConnectionRequest(extractFields(ans, fields)))
+ .build();
+ } else {
+ return Response
+ .status(200)
+ .entity(new NeutronVPNIPSECSiteConnectionRequest(connectionInterface
+ .getNeutronVPNIPSECSiteConnections(policyID))).build();
+ }
}
/**
- * Creates new VPN IPSEC SiteConnection */
+ * Creates new VPN IPSEC SiteConnection
+ */
@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
@TypeHint(NeutronVPNIPSECSiteConnection.class)
- @StatusCodes({
- @ResponseCode(code = 501, condition = "Not Implemented") })
+ @StatusCodes({ @ResponseCode(code = 201, condition = "Created"),
+ @ResponseCode(code = 400, condition = "Bad Request"),
+ @ResponseCode(code = 401, condition = "Unauthorized"),
+ @ResponseCode(code = 501, condition = "Not Implemented"),
+ @ResponseCode(code = 503, condition = "No providers available") })
public Response createVPNIPSECSiteConnection(final NeutronVPNIPSECSiteConnectionRequest input) {
- throw new UnimplementedException("Not Implemented");
+ INeutronVPNIPSECSiteConnectionsCRUD ipsecSiteConnectionsInterface = NeutronCRUDInterfaces
+ .getINeutronVPNIPSECSiteConnectionsCRUD(this);
+ if (ipsecSiteConnectionsInterface == null) {
+ throw new ServiceUnavailableException("VPNIPSECSiteConnections CRUD Interface "
+ + RestMessages.SERVICEUNAVAILABLE.toString());
+ }
+ if (input.isSingleton()) {
+ NeutronVPNIPSECSiteConnection singleton = input.getSingleton();
+
+ /*
+ * verify that the ipsec site connection doesn't already exist (issue: is deeper
+ * inspection necessary?)
+ */
+ if (ipsecSiteConnectionsInterface.neutronVPNIPSECSiteConnectionsExists(singleton.getID()))
+ throw new BadRequestException("VPNIPSECSiteConnections ID already exists");
+ Object[] instances = NeutronUtil.getInstances(INeutronVPNIPSECSiteConnectionAware.class, this);
+ if (instances != null) {
+ if (instances.length > 0) {
+ for (Object instance : instances) {
+ INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
+ int status = service.canCreateNeutronVPNIPSECSiteConnection(singleton);
+ if (status < 200 || status > 299)
+ return Response.status(status).build();
+ }
+ } else {
+ throw new ServiceUnavailableException("No providers registered. Please try again later");
+ }
+ } else {
+ throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
+ }
+ /*
+ * add ipsecSiteConnections to the cache
+ */
+ ipsecSiteConnectionsInterface.addNeutronVPNIPSECSiteConnections(singleton);
+ if (instances != null) {
+ for (Object instance : instances) {
+ INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
+ service.neutronVPNIPSECSiteConnectionCreated(singleton);
+ }
+ }
+ } else {
+
+ /*
+ * only singleton ipsecSiteConnections creates supported
+ */
+ throw new BadRequestException("Only singleton ipsecSiteConnections creates supported");
+ }
+ return Response.status(201).entity(input).build();
}
/**
- * Updates a VPN IPSEC SiteConnection */
+ * Updates a VPN IPSEC SiteConnection
+ */
@Path("{policyID}")
@PUT
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
- @StatusCodes({
- @ResponseCode(code = 501, condition = "Not Implemented") })
- public Response updateVPNIPSECSiteConnection(
- @PathParam("policyID") String policyID, final NeutronVPNIPSECSiteConnectionRequest input
- ) {
- throw new UnimplementedException("Not Implemented");
+ @StatusCodes({ @ResponseCode(code = 200, condition = "Operation successful"),
+ @ResponseCode(code = 400, condition = "Bad Request"),
+ @ResponseCode(code = 401, condition = "Unauthorized"), @ResponseCode(code = 404, condition = "Not Found"),
+ @ResponseCode(code = 501, condition = "Not Implemented"),
+ @ResponseCode(code = 503, condition = "No providers available") })
+ public Response updateVPNIPSECSiteConnection(@PathParam("policyID") String policyID,
+ final NeutronVPNIPSECSiteConnectionRequest input) {
+ INeutronVPNIPSECSiteConnectionsCRUD ipsecSiteConnectionsInterface = NeutronCRUDInterfaces
+ .getINeutronVPNIPSECSiteConnectionsCRUD(this);
+
+ if (ipsecSiteConnectionsInterface == null) {
+ throw new ServiceUnavailableException("VPNIPSECSiteConnections CRUD Interface "
+ + RestMessages.SERVICEUNAVAILABLE.toString());
+ }
+
+ /*
+ * ipsecSiteConnection has to exist and only a single delta can be
+ * supplied
+ */
+ if (!ipsecSiteConnectionsInterface.neutronVPNIPSECSiteConnectionsExists(policyID))
+ throw new ResourceNotFoundException("VPNIPSECSiteConnections ID not found");
+ if (!input.isSingleton())
+ throw new BadRequestException("Only singleton deltas supported");
+ NeutronVPNIPSECSiteConnection singleton = input.getSingleton();
+ NeutronVPNIPSECSiteConnection original = ipsecSiteConnectionsInterface
+ .getNeutronVPNIPSECSiteConnections(policyID);
+
+ /*
+ * attribute changes blocked by Neutron
+ */
+ if (singleton.getID() != null || singleton.getTenantID() != null)
+ throw new BadRequestException("Request attribute change not allowed");
+
+ Object[] instances = NeutronUtil.getInstances(INeutronVPNIKEPolicyAware.class, this);
+ if (instances != null) {
+ if (instances.length > 0) {
+ for (Object instance : instances) {
+ INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
+ int status = service.canUpdateNeutronVPNIPSECSiteConnection(singleton, original);
+ if (status < 200 || status > 299)
+ return Response.status(status).build();
+ }
+ } else {
+ throw new ServiceUnavailableException("No providers registered. Please try again later");
+ }
+ } else {
+ throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
+ }
+ /*
+ * update the ipsecSiteConnections entry and return the modified object
+ */
+ ipsecSiteConnectionsInterface.updateNeutronVPNIPSECSiteConnections(policyID, singleton);
+ NeutronVPNIPSECSiteConnection updatedVPNIKEPolicy = ipsecSiteConnectionsInterface
+ .getNeutronVPNIPSECSiteConnections(policyID);
+ if (instances != null) {
+ for (Object instance : instances) {
+ INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
+ service.neutronVPNIPSECSiteConnectionUpdated(updatedVPNIKEPolicy);
+ }
+ }
+ return Response
+ .status(200)
+ .entity(new NeutronVPNIPSECSiteConnectionRequest(ipsecSiteConnectionsInterface
+ .getNeutronVPNIPSECSiteConnections(policyID))).build();
}
/**
- * Deletes a VPN IPSEC SiteConnection */
+ * Deletes a VPN IPSEC SiteConnection
+ */
@Path("{policyID}")
@DELETE
- @StatusCodes({
- @ResponseCode(code = 501, condition = "Not Implemented") })
- public Response deleteVPNIPSECSiteConnection(
- @PathParam("policyID") String policyID) {
- throw new UnimplementedException("Not Implemented");
+ @StatusCodes({ @ResponseCode(code = 204, condition = "No Content"),
+ @ResponseCode(code = 401, condition = "Unauthorized"), @ResponseCode(code = 404, condition = "Not Found"),
+ @ResponseCode(code = 409, condition = "Conflict"),
+ @ResponseCode(code = 501, condition = "Not Implemented"),
+ @ResponseCode(code = 503, condition = "No providers available") })
+ public Response deleteVPNIPSECSiteConnection(@PathParam("policyID") String policyID) {
+ INeutronVPNIPSECSiteConnectionsCRUD ipsecSiteConnectionsInterface = NeutronCRUDInterfaces
+ .getINeutronVPNIPSECSiteConnectionsCRUD(this);
+ if (ipsecSiteConnectionsInterface == null) {
+ throw new ServiceUnavailableException("NeutronVPNIPSECSiteConnections CRUD Interface "
+ + RestMessages.SERVICEUNAVAILABLE.toString());
+ }
+
+ /*
+ * verify that the iSiteConnections exists and is not in use before
+ * removing it
+ */
+ if (!ipsecSiteConnectionsInterface.neutronVPNIPSECSiteConnectionsExists(policyID))
+ throw new ResourceNotFoundException("VPNIPSECSiteConnections ID not found");
+ NeutronVPNIPSECSiteConnection singleton = ipsecSiteConnectionsInterface
+ .getNeutronVPNIPSECSiteConnections(policyID);
+ Object[] instances = NeutronUtil.getInstances(INeutronVPNIPSECSiteConnectionAware.class, this);
+ if (instances != null) {
+ if (instances.length > 0) {
+ for (Object instance : instances) {
+ INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
+ int status = service.canDeleteNeutronVPNIPSECSiteConnection(singleton);
+ if (status < 200 || status > 299)
+ return Response.status(status).build();
+ }
+ } else {
+ throw new ServiceUnavailableException("No providers registered. Please try again later");
+ }
+ } else {
+ throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
+ }
+ ipsecSiteConnectionsInterface.removeNeutronVPNIPSECSiteConnections(policyID);
+ if (instances != null) {
+ for (Object instance : instances) {
+ INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
+ service.neutronVPNIPSECSiteConnectionDeleted(singleton);
+ }
+ }
+ return Response.status(204).build();
}
+
}
--- /dev/null
+/*
+ * Copyright Tata Consultancy Services, 2015. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.neutron.transcriber;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import org.opendaylight.neutron.spi.NeutronVPNIPSECSiteConnection;
+import org.opendaylight.neutron.spi.INeutronVPNIPSECSiteConnectionsCRUD;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class NeutronVPNIPSECSiteConnectionsInterface implements INeutronVPNIPSECSiteConnectionsCRUD {
+ private static final Logger logger = LoggerFactory.getLogger(NeutronVPNIKEPolicyInterface.class);
+ private ConcurrentMap<String, NeutronVPNIPSECSiteConnection> neutronVPNIPSECSiteConnectionDB = new ConcurrentHashMap<String, NeutronVPNIPSECSiteConnection>();
+
+ // this method uses reflection to update an object from it's delta.
+
+ private boolean overwrite(Object target, Object delta) {
+ Method[] methods = target.getClass().getMethods();
+
+ for (Method toMethod : methods) {
+ if (toMethod.getDeclaringClass().equals(target.getClass()) && toMethod.getName().startsWith("set")) {
+
+ String toName = toMethod.getName();
+ String fromName = toName.replace("set", "get");
+
+ try {
+ Method fromMethod = delta.getClass().getMethod(fromName);
+ Object value = fromMethod.invoke(delta, (Object[]) null);
+ if (value != null) {
+ toMethod.invoke(target, value);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ // INeutronVPNIPSECSiteConnectionsCRUD methods
+
+ @Override
+ public boolean neutronVPNIPSECSiteConnectionsExists(String policyID) {
+ return neutronVPNIPSECSiteConnectionDB.containsKey(policyID);
+ }
+
+ @Override
+ public NeutronVPNIPSECSiteConnection getNeutronVPNIPSECSiteConnections(String policyID) {
+ if (!neutronVPNIPSECSiteConnectionsExists(policyID)) {
+ return null;
+ }
+ return neutronVPNIPSECSiteConnectionDB.get(policyID);
+ }
+
+ @Override
+ public List<NeutronVPNIPSECSiteConnection> getAllNeutronVPNIPSECSiteConnections() {
+ Set<NeutronVPNIPSECSiteConnection> allNeutronVPNIPSECSiteConnections = new HashSet<NeutronVPNIPSECSiteConnection>();
+ for (Entry<String, NeutronVPNIPSECSiteConnection> entry : neutronVPNIPSECSiteConnectionDB.entrySet()) {
+ NeutronVPNIPSECSiteConnection meteringLabelRule = entry.getValue();
+ allNeutronVPNIPSECSiteConnections.add(meteringLabelRule);
+ }
+ logger.debug("Exiting getAllNeutronVPNIPSECSiteConnections, Found {} OpenStackVPNIPSECSiteConnections", allNeutronVPNIPSECSiteConnections.size());
+ List<NeutronVPNIPSECSiteConnection> ans = new ArrayList<NeutronVPNIPSECSiteConnection>();
+ ans.addAll(allNeutronVPNIPSECSiteConnections);
+ return ans;
+ }
+
+ @Override
+ public boolean addNeutronVPNIPSECSiteConnections(NeutronVPNIPSECSiteConnection input) {
+ if (neutronVPNIPSECSiteConnectionsExists(input.getID())) {
+ return false;
+ }
+ neutronVPNIPSECSiteConnectionDB.putIfAbsent(input.getID(), input);
+ return true;
+ }
+
+ @Override
+ public boolean removeNeutronVPNIPSECSiteConnections(String policyID) {
+ if (!neutronVPNIPSECSiteConnectionsExists(policyID)) {
+ return false;
+ }
+ neutronVPNIPSECSiteConnectionDB.remove(policyID);
+ return true;
+ }
+
+ @Override
+ public boolean updateNeutronVPNIPSECSiteConnections(String policyID, NeutronVPNIPSECSiteConnection delta) {
+ if (!neutronVPNIPSECSiteConnectionsExists(policyID)) {
+ return false;
+ }
+ NeutronVPNIPSECSiteConnection target = neutronVPNIPSECSiteConnectionDB.get(policyID);
+ return overwrite(target, delta);
+ }
+
+ @Override
+ public boolean neutronVPNIPSECSiteConnectionsInUse(String policyID) {
+ if (!neutronVPNIPSECSiteConnectionsExists(policyID)) {
+ return true;
+ }
+ return false;
+ }
+}
\ No newline at end of file