Working northbound rest api calls for affinity objects. 30/1630/2
authorSuchi Raman <suchi.raman@plexxi.com>
Tue, 1 Oct 2013 22:19:40 +0000 (18:19 -0400)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 3 Oct 2013 11:47:11 +0000 (11:47 +0000)
Signed-off-by: Suchi Raman <suchi.raman@plexxi.com>
affinity/implementation/src/main/java/org/opendaylight/affinity/affinity/internal/Activator.java
affinity/northbound/src/main/java/org/opendaylight/affinity/affinity/northbound/AffinityNorthbound.java
analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/AnalyticsNorthboundRSApplication.java
scripts/affinity.py
scripts/osgi.txt

index 7b2402956d33d6f8444be1f677467912f7bd424b..a8ff077a43d2fa3434c8cf5551df1d68598631b9 100644 (file)
@@ -21,11 +21,8 @@ import org.opendaylight.controller.hosttracker.IfIptoHost;
 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
@@ -64,7 +61,6 @@ public class Activator extends ComponentActivatorAbstractBase {
      * instantiated in order to get an fully working implementation
      * Object
      */
-    @Override
     public Object[] getImplementations() {
         Object[] res = { AffinityManagerImpl.class };
         return res;
@@ -83,7 +79,6 @@ public class Activator extends ComponentActivatorAbstractBase {
      * 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>>();
@@ -93,28 +88,19 @@ public class Activator extends ComponentActivatorAbstractBase {
             // 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));
         }
index d065fae6f4ec7d3754c36b43f448b70ca66779d5..eba5207c182cd10e9d21a32a095dc09b9f053989 100644 (file)
@@ -46,6 +46,9 @@ import org.opendaylight.affinity.affinity.IAffinityManager;
 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.
  *
@@ -55,6 +58,7 @@ import org.opendaylight.affinity.affinity.AffinityGroup;
 public class AffinityNorthbound {
 
     private String username;
+    private static final Logger log = LoggerFactory.getLogger(AffinityNorthbound.class);
 
     @Context
     public void setSecurityContext(SecurityContext context) {
@@ -66,6 +70,8 @@ public class AffinityNorthbound {
     }
 
     private IAffinityManager getIfAffinityManagerService(String containerName) {
+        log.debug("In getIfAffinityManager");
+
         IContainerManager containerManager = (IContainerManager) ServiceHelper
                 .getGlobalInstance(IContainerManager.class, this);
         if (containerManager == null) {
@@ -124,7 +130,7 @@ public class AffinityNorthbound {
             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 "
@@ -133,10 +139,8 @@ public class AffinityNorthbound {
 
         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();
     }
 
     /**
@@ -173,6 +177,7 @@ public class AffinityNorthbound {
                                                   + RestMessages.SERVICEUNAVAILABLE.toString());
         }
 
+        log.info("Get affinity group details" + affinityGroupName);
         AffinityGroup ag = affinityManager.getAffinityGroup(affinityGroupName);
         if (ag == null) {
             throw new ResourceNotFoundException(RestMessages.SERVICEUNAVAILABLE.toString());
@@ -180,4 +185,210 @@ public class AffinityNorthbound {
             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());
+    }
+
 }
index 4e26fdab1c8359f7f42cd891e9bea007fabf837b..363859122af00c08e1a4cb13d8a353c33ec4a1b2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013 Plexxio, Inc. and others.  All rights reserved.
+ * Copyright (c) 2013 Plexxi, Inc. and others.  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,
index 0cd8832f31c146ab4969104c0382c7d304bc41f4..a70eb69b282080e8261e5f921dee31d8a14c19ea 100644 (file)
@@ -14,41 +14,36 @@ def rest_method(url, verb):
     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")
 
 
@@ -57,20 +52,36 @@ def get_all_affinity_groups():
     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()
 
index ff483a75476e79b182b92bbf4f4ec30f6f5b8a8a..a766bff3e4f764e8b2bf55c9dd7d4a505594530e 100644 (file)
@@ -51,3 +51,4 @@ start <id_affinityimpl>
 #ls -l /Users/sraman/.m2/repository/org/opendaylight/controller/affinity/northbound/0.4.1-SNAPSHOT/northbound-0.4.1-SNAPSHOT.jar
 
 
+####################