Software Defined Networking
1752734 Members
5170 Online
108789 Solutions
New Discussion

Creating a REST API

 
franzj
Occasional Contributor

Creating a REST API

I've been trying to create a REST API to adjust some values during runtime of my app and I can't seem to get it to work with the interface provided on https://<CONTROLLER_ADDRESS>:8443/api/

 

The REST API contains of two requests: a GET request to get the current values and a PUT request to set them.

The code compiles fine, I get a SocketTimeOutException or a NullPointer when I activate it and restart the controller.

I'm pretty sure I followed everything the documentation states.

I added a Class to "*-rs/src/main/java/com.*.*.rs" called "MetricResource.java":

@Path("metrics")
public class MetricResource extends ControllerResource {
    /**
     * Changes the metrics.
     * <p>
     * Normal Response Code(s): ok (200)
     * <p>
     * Error Response Codes: badRequest (400), unauthorized (401), forbidden (403), 
     * badMethod (405), serviceUnavailable (503)
     * 
     * @param request JSON representation of the metrics
     * @return JSON object
     */
    @PUT
    @Produces(MediaType.APPLICATION_JSON)
    public Response changeMetrics(String request) {
        MetricService svc = get(MetricService.class);
        // (...)  

        // Encode response
        return ok(r.toString()).build();
    }

    /**
     * Gets the metrics.
     * <p>
     * Normal Response Code(s): ok (200)
     * <p>
     * Error Response Codes: badRequest (400), unauthorized (401), forbidden (403), 
     * badMethod (405), serviceUnavailable (503), itemNotFound (404)
     *
     * @return JSON object
     */
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getMetrics() {
        MetricService svc = get(MetricService.class);
        // (...)
        return ok(r.toString()).build();
    }
}

I pretty much copied the DocProvider from the example:

@Component
public class DocProvider extends SelfRegisteringRSDocProvider {
    
    public DocProvider() {
        super("metrics", "rsdoc", DocProvider.class.getClassLoader());
    }

}

Same for the ServiceAssistant:

@Component(immediate=true, specVersion="1.1")
@References(value={
    @Reference(name="MetricService",
               referenceInterface=com.hp.mdd.api.MetricService.class,
               policy=ReferencePolicy.DYNAMIC, 
               cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE)
    })
public class ServiceAssistant {
    
    private ServiceLocator sl = ServiceLocator.INSTANCE;

    /**
     * Hook for registering MetricService implementation via declarative services.
     *
     * @param s newly advertised service to register
     * @param properties the properties associated with the service
     */
    protected void bindMetricService(com.hp.mdd.api.MetricService s, Map<String, Object> properties) {
        sl.register(com.hp.mdd.api.MetricService.class, s, properties);
    }
    
    /**
     * Hook for unregistering deactivated SystemInformationService via declarative services.
     *
     * @param s deactivated service to unregister
     */
    protected void unbindMetricService(com.hp.mdd.api.MetricService s) {
        sl.unregister(com.hp.mdd.api.MetricService.class, s);
    }

}

I tried to adjust the model.json, even though the documentation was pretty scarce on that:

{
    "${restPath}":
    {
        "metric": {
            "port": {"type": "long"},
            "packetthresh": {"type": "long"},
            "timewindow": {"type": "long"},
            "bytethresh": {"type": "long"},
            "packetwindow": {"type": "long"},
            "idletimeout": {"type": "int"}
        }
    }
}

And added my Resource to the param-values:

<param-value>
                com.hp.mdd.rs.MetricResource

                com.hp.sdn.rs.misc.DuplicateIdErrorHandler
                com.hp.sdn.rs.misc.NotFoundErrorHandler
                com.hp.sdn.rs.misc.ServiceNotFoundErrorHandler
                com.hp.sdn.rs.misc.IllegalDataHandler
                com.hp.sdn.rs.misc.IllegalStateHandler
                com.hp.sdn.rs.misc.AuthenticationHandler
         </param-value>

When I activate the app normally and go to the REST API it just "fetching resource list: https://<controller_ip>:8443/api/rsdoc/metrics/1.0/resources.json" forever.

When I restart then I get the exceptions and I can't use the REST API anymore from the web interface (/in browser).

 

The API/Model part are pretty much working and simplistic.

 

Any ideas?