import org.opendaylight.controller.sal.core.ComponentActivatorAbstractBase;
import org.opendaylight.affinity.affinity.IAffinityManager;
import org.opendaylight.affinity.affinity.IAffinityManagerAware;
-import org.opendaylight.controller.sal.utils.IObjectReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.opendaylight.controller.sal.flowprogrammer.IFlowProgrammerService;
-import org.opendaylight.controller.switchmanager.ISwitchManager;
/**
* AffinityManager Bundle Activator
* instantiated in order to get an fully working implementation
* Object
*/
- @Override
public Object[] getImplementations() {
Object[] res = { AffinityManagerImpl.class };
return res;
* also optional per-container different behavior if needed, usually
* should not be the case though.
*/
- @Override
public void configureInstance(Component c, Object imp, String containerName) {
if (imp.equals(AffinityManagerImpl.class)) {
Dictionary<String, Set<String>> props = new Hashtable<String, Set<String>>();
// export the service
c.setInterface(new String[] {
IAffinityManager.class.getName(),
- IConfigurationContainerAware.class.getName(),
- IObjectReader.class.getName(),
- ICacheUpdateAware.class.getName() }, props);
+ ICacheUpdateAware.class.getName(),
+ IConfigurationContainerAware.class.getName() }, props);
// Now lets add a service dependency to make sure the
// provider of service exists
c.add(createContainerServiceDependency(containerName).setService(
IAffinityManagerAware.class).setCallbacks(
"setAffinityManagerAware", "unsetAffinityManagerAware")
- .setRequired(true));
+ .setRequired(false));
c.add(createContainerServiceDependency(containerName).setService(
IClusterContainerServices.class).setCallbacks(
"setClusterContainerService",
"unsetClusterContainerService").setRequired(true));
- c.add(createContainerServiceDependency(containerName).setService(
- IFlowProgrammerService.class).setCallbacks(
- "setFlowProgrammerService", "unsetFlowProgrammerService")
- .setRequired(true));
- c.add(createContainerServiceDependency(containerName).setService(
- ISwitchManager.class).setCallbacks(
- "setSwitchManager", "unsetSwitchManager")
- .setRequired(true));
c.add(createContainerServiceDependency(containerName).setService(IfIptoHost.class)
.setCallbacks("setHostTracker", "unsetHostTracker").setRequired(true));
}
import org.opendaylight.affinity.affinity.AffinityLink;
import org.opendaylight.affinity.affinity.AffinityGroup;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
/**
* The class provides Northbound REST APIs to access affinity configuration.
*
public class AffinityNorthbound {
private String username;
+ private static final Logger log = LoggerFactory.getLogger(AffinityNorthbound.class);
@Context
public void setSecurityContext(SecurityContext context) {
}
private IAffinityManager getIfAffinityManagerService(String containerName) {
+ log.debug("In getIfAffinityManager");
+
IContainerManager containerManager = (IContainerManager) ServiceHelper
.getGlobalInstance(IContainerManager.class, this);
if (containerManager == null) {
throw new UnauthorizedException("User is not authorized to perform this operation on container "
+ containerName);
}
-
+ log.info("add a new affinitygroup = {}, containerName = {}", affinityGroupName, containerName);
IAffinityManager affinityManager = getIfAffinityManagerService(containerName);
if (affinityManager == null) {
throw new ServiceUnavailableException("Affinity Manager "
AffinityGroup ag1 = new AffinityGroup(affinityGroupName);
Status ret = affinityManager.addAffinityGroup(ag1);
- if (ret.isSuccess()) {
- return Response.status(Response.Status.CREATED).build();
- }
- throw new InternalServerErrorException(ret.getDescription());
+
+ return Response.status(Response.Status.CREATED).build();
}
/**
+ RestMessages.SERVICEUNAVAILABLE.toString());
}
+ log.info("Get affinity group details" + affinityGroupName);
AffinityGroup ag = affinityManager.getAffinityGroup(affinityGroupName);
if (ag == null) {
throw new ResourceNotFoundException(RestMessages.SERVICEUNAVAILABLE.toString());
return ag;
}
}
+
+ /**
+ * Add an affinity link with one "from" and one "to" affinity group.
+ *
+ * @param containerName
+ * Name of the Container
+ * @param affinityLinkName
+ * Name of the new affinity link being added
+ * @return Response as dictated by the HTTP Response Status code
+ */
+
+ @Path("/{containerName}/create/link/{affinityLinkName}/from/{fromAffinityGroup}/to/{toAffinityGroup}")
+ @PUT
+ @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
+ @TypeHint(Response.class)
+ @StatusCodes({
+ @ResponseCode(code = 200, condition = "Operation successful"),
+ @ResponseCode(code = 404, condition = "The Container Name or nodeId or configuration name is not found"),
+ @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
+ public Response createAffinityLink(
+ @PathParam("containerName") String containerName,
+ @PathParam("affinityLinkName") String affinityLinkName,
+ @PathParam("fromAffinityGroup") String fromAffinityGroup,
+ @PathParam("toAffinityGroup") String toAffinityGroup) {
+
+ if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
+ throw new UnauthorizedException("User is not authorized to perform this operation on container "
+ + containerName);
+ }
+
+ IAffinityManager affinityManager = getIfAffinityManagerService(containerName);
+ if (affinityManager == null) {
+ throw new ServiceUnavailableException("Affinity Manager "
+ + RestMessages.SERVICEUNAVAILABLE.toString());
+ }
+
+
+ log.info("Create affinity link" + affinityLinkName + "fromGroup" + fromAffinityGroup + "toGroup" + toAffinityGroup);
+ AffinityGroup from = affinityManager.getAffinityGroup(fromAffinityGroup);
+ AffinityGroup to = affinityManager.getAffinityGroup(toAffinityGroup);
+ AffinityLink al1 = new AffinityLink(affinityLinkName, from, to);
+
+ Status ret = affinityManager.addAffinityLink(al1);
+ if (!ret.isSuccess()) {
+ throw new InternalServerErrorException(ret.getDescription());
+ }
+ return Response.status(Response.Status.CREATED).build();
+ }
+
+
+ /**
+ * Add path redirect details to an affinity link.
+ *
+ * @param containerName
+ * Name of the Container
+ * @param affinityLinkName
+ * Name of the new affinity link being added
+ * @param wayPoint
+ * IP address string of a waypoint server or VM
+ * @return Response as dictated by the HTTP Response Status code
+ */
+
+ @Path("/{containerName}/link/{affinityLinkName}/setwaypoint/{waypointIP}")
+ @PUT
+ @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
+ @TypeHint(Response.class)
+ @StatusCodes({
+ @ResponseCode(code = 200, condition = "Operation successful"),
+ @ResponseCode(code = 404, condition = "The Container Name or nodeId or configuration name is not found"),
+ @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
+ public Response setLinkWaypoint(
+ @PathParam("containerName") String containerName,
+ @PathParam("affinityLinkName") String affinityLinkName,
+ @PathParam("waypointIP") String waypointIP) {
+
+ if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
+ throw new UnauthorizedException("User is not authorized to perform this operation on container "
+ + containerName);
+ }
+
+ IAffinityManager affinityManager = getIfAffinityManagerService(containerName);
+ if (affinityManager == null) {
+ throw new ServiceUnavailableException("Affinity Manager "
+ + RestMessages.SERVICEUNAVAILABLE.toString());
+ }
+ log.info("Set waypoint address (link)" + affinityLinkName + " (waypoint ip) " + waypointIP);
+
+ AffinityLink al1 = affinityManager.getAffinityLink(affinityLinkName);
+ al1.setWaypoint(waypointIP);
+ return Response.status(Response.Status.CREATED).build();
+ }
+
+
+ /**
+ * Add IP addresses to a group.
+ *
+ * @param containerName
+ * Name of the Container
+ * @param affinityGroupName
+ * Name of the affinity group to add to.
+ * @param ipaddress
+ * IP address of the new affinity member.
+ * @return Response as dictated by the HTTP Response Status code
+ */
+ @Path("/{containerName}/group/{affinityGroupName}/add/ip/{ipaddress}")
+ @PUT
+ @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
+ @TypeHint(Response.class)
+ @StatusCodes({
+ @ResponseCode(code = 200, condition = "Operation successful"),
+ @ResponseCode(code = 404, condition = "The Container Name or nodeId or configuration name is not found"),
+ @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
+ public Response addInetAddress(
+ @PathParam("containerName") String containerName,
+ @PathParam("affinityGroupName") String affinityGroupName,
+ @PathParam("ipaddress") String ipaddress) {
+ if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
+ throw new UnauthorizedException("User is not authorized to perform this operation on container "
+ + containerName);
+ }
+
+ IAffinityManager affinityManager = getIfAffinityManagerService(containerName);
+ if (affinityManager == null) {
+ throw new ServiceUnavailableException("Affinity Manager "
+ + RestMessages.SERVICEUNAVAILABLE.toString());
+ }
+
+ log.info("add Inet address " + affinityGroupName + " (ipaddress) " + ipaddress);
+ AffinityGroup ag1 = affinityManager.getAffinityGroup(affinityGroupName);
+ ag1.add(ipaddress);
+
+ return Response.status(Response.Status.CREATED).build();
+ }
+
+ /**
+ * Add prefix/mask subnet as a member of the affinity group.
+ *
+ * @param containerName
+ * Name of the Container
+ * @param affinityGroupName
+ * Name of the affinity group to add to.
+ * @param ipmask
+ * a.b.c.d/mm format of a set of IP addresses to add.
+ * @return Response as dictated by the HTTP Response Status code
+ */
+ @Path("/{containerName}/group/{affinityGroupName}/addsubnet/ipprefix/{ipprefix}/mask/{mask}")
+ @PUT
+ @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
+ @TypeHint(Response.class)
+ @StatusCodes({
+ @ResponseCode(code = 200, condition = "Operation successful"),
+ @ResponseCode(code = 404, condition = "The Container Name or nodeId or configuration name is not found"),
+ @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
+ public Response addSubnet(
+ @PathParam("containerName") String containerName,
+ @PathParam("affinityGroupName") String affinityGroupName,
+ @PathParam("ipprefix") String ipprefix,
+ @PathParam("mask") String mask) {
+ if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
+ throw new UnauthorizedException("User is not authorized to perform this operation on container "
+ + containerName);
+ }
+
+ IAffinityManager affinityManager = getIfAffinityManagerService(containerName);
+ if (affinityManager == null) {
+ throw new ServiceUnavailableException("Affinity Manager "
+ + RestMessages.SERVICEUNAVAILABLE.toString());
+ }
+
+ log.info("addSubnet to affinitygroup" + affinityGroupName);
+ AffinityGroup ag1 = affinityManager.getAffinityGroup(affinityGroupName);
+ String ipmask = ipprefix + "/" + mask;
+ ag1.addInetMask(ipmask);
+
+ return Response.status(Response.Status.CREATED).build();
+ }
+
+
+ @Path("/{containerName}/affinity-groups")
+ @GET
+ @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
+ @TypeHint(AffinityGroupList.class)
+ @StatusCodes({ @ResponseCode(code = 200, condition = "Operation successful"),
+ @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
+ @ResponseCode(code = 404, condition = "The containerName is not found"),
+ @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
+ public AffinityGroupList getAllAffinityGroups(@PathParam("containerName") String containerName) {
+
+ // if (!isValidContainer(containerName)) {
+ // throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
+ //}
+
+ if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this)) {
+ throw new UnauthorizedException("User is not authorized to perform this operation on container "
+ + containerName);
+ }
+
+ IAffinityManager affinityManager = getIfAffinityManagerService(containerName);
+ if (affinityManager == null) {
+ throw new ServiceUnavailableException("Affinity Manager "
+ + RestMessages.SERVICEUNAVAILABLE.toString());
+ }
+ log.info("getallgroups");
+ return new AffinityGroupList(affinityManager.getAllAffinityGroups());
+ }
+
}
print "REST call " + url
resp, content = h.request(url, verb)
- if (resp.status != 200):
- print "Error code %d" % (resp.status)
- return
-
- if (verb == "GET"):
- print content
-
+ print content
+ print "return code %d" % (resp.status)
print "done"
-def waypoint_demo():
+def waypoint_init():
# Create two affinity groups
print "create web servers group"
put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/create/group/webservers'
- rest_method(put_url, "POST")
+ rest_method(put_url, "PUT")
print "create external addresses"
- put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/create/group/external'
+ put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/create/group/clients'
rest_method(put_url, "PUT")
print "create link inflows"
- put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/create/link/inflows'
+ put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/create/link/inflows/from/clients/to/webservers'
rest_method(put_url, "PUT")
print "add ip to webservers"
- put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/add/ip/webservers/192.168.1.1'
+ put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/group/webservers/add/ip/192.168.1.1'
rest_method(put_url, "PUT")
print "add ip to webservers"
- put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/add/ip/webservers/192.168.1.2'
+ put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/group/webservers/add/ip/192.168.1.2'
rest_method(put_url, "PUT")
print "add ip to external"
- put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/add/ip/external/10.10.0.0'
+ put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/group/clients/add/ip/10.10.0.0'
rest_method(put_url, "PUT")
get_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/affinity-groups'
rest_method(get_url, "GET")
-def get_affinity_group():
- print "get affinity group"
- get_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/group/webserver'
+def get_all_affinity_links():
+ print "get all affinity groups"
+ get_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/affinity-links'
+ rest_method(get_url, "GET")
+
+def get_affinity_group(groupname):
+ get_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/group/' + groupname
rest_method(get_url, "GET")
+def set_waypoint_address():
+ wp = "192.168.1.10"
+ put_url = 'http://localhost:8080/affinity/nb/v2/affinity/default/link/inflows/setwaypoint/' + wp
+ rest_method(put_url, "PUT")
+
# Add waypoint IP to an affinity link.
def main():
global h
h = httplib2.Http(".cache")
h.add_credentials('admin', 'admin')
- waypoint_demo()
-# get_all_affinity_groups()
-# get_affinity_group()
+# waypoint_init()
+ set_waypoint_address()
+# unset_waypoint_address()
+
+ get_affinity_group('webservers')
+ get_affinity_group('clients')
+
+ get_all_affinity_groups()
+# get_all_affinity_links()
+
if __name__ == "__main__":
main()