import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
+import java.net.URI;
import java.nio.charset.StandardCharsets;
+import java.util.Collections;
import java.util.List;
import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedHashMap;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.PathSegment;
+import javax.ws.rs.core.UriBuilder;
+import javax.ws.rs.core.UriInfo;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
* @author Thomas Pantelis
*/
public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseable {
- private final static Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceImpl.class);
+ private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceImpl.class);
private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
+ @SuppressWarnings("checkstyle:IllegalCatch")
@Override
- public void put(String uriPath, String payload) throws OperationFailedException {
+ public void put(final String uriPath, final String payload) throws OperationFailedException {
Preconditions.checkNotNull(payload, "payload can't be null");
LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
- InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
- NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, false);
+ final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
+ final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, false);
LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
LOG.debug("Parsed NormalizedNode: {}", context.getData());
try {
- RestconfImpl.getInstance().updateConfigurationData(uriPath, context);
- } catch (Exception e) {
+ RestconfImpl.getInstance().updateConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
+ } catch (final Exception e) {
propagateExceptionAs(uriPath, e, "PUT");
}
}
+ @SuppressWarnings("checkstyle:IllegalCatch")
@Override
- public void post(String uriPath, String payload) throws OperationFailedException {
+ public void post(final String uriPath, final String payload)
+ throws OperationFailedException {
Preconditions.checkNotNull(payload, "payload can't be null");
LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
- InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
- NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
+ final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
+ final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
LOG.debug("Parsed NormalizedNode: {}", context.getData());
try {
- RestconfImpl.getInstance().createConfigurationData(uriPath, context, null);
- } catch (Exception e) {
+ RestconfImpl.getInstance().createConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
+ } catch (final Exception e) {
propagateExceptionAs(uriPath, e, "POST");
}
}
+ @SuppressWarnings("checkstyle:IllegalCatch")
@Override
- public void delete(String uriPath) throws OperationFailedException {
+ public void delete(final String uriPath) throws OperationFailedException {
LOG.debug("delete: uriPath: {}", uriPath);
try {
RestconfImpl.getInstance().deleteConfigurationData(uriPath);
- } catch (Exception e) {
+ } catch (final Exception e) {
propagateExceptionAs(uriPath, e, "DELETE");
}
}
+ @SuppressWarnings("checkstyle:IllegalCatch")
@Override
- public Optional<String> get(String uriPath, LogicalDatastoreType datastoreType) throws OperationFailedException {
+ public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType)
+ throws OperationFailedException {
LOG.debug("get: uriPath: {}", uriPath);
try {
NormalizedNodeContext readData;
- if(datastoreType == LogicalDatastoreType.CONFIGURATION) {
- readData = RestconfImpl.getInstance().readConfigurationData(uriPath, null);
+ final SimpleUriInfo uriInfo = new SimpleUriInfo(uriPath);
+ if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
+ readData = RestconfImpl.getInstance().readConfigurationData(uriPath, uriInfo);
} else {
- readData = RestconfImpl.getInstance().readOperationalData(uriPath, null);
+ readData = RestconfImpl.getInstance().readOperationalData(uriPath, uriInfo);
}
- Optional<String> result = Optional.of(toJson(readData));
+ final Optional<String> result = Optional.of(toJson(readData));
LOG.debug("get returning: {}", result.get());
return result;
- } catch (Exception e) {
- if(!isDataMissing(e)) {
+ } catch (final Exception e) {
+ if (!isDataMissing(e)) {
propagateExceptionAs(uriPath, e, "GET");
}
}
}
+ @SuppressWarnings("checkstyle:IllegalCatch")
@Override
- public Optional<String> invokeRpc(String uriPath, Optional<String> input) throws OperationFailedException {
+ public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
+ throws OperationFailedException {
Preconditions.checkNotNull(uriPath, "uriPath can't be null");
- String actualInput = input.isPresent() ? input.get() : null;
+ final String actualInput = input.isPresent() ? input.get() : null;
LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
String output = null;
try {
NormalizedNodeContext outputContext;
- if(actualInput != null) {
- InputStream entityStream = new ByteArrayInputStream(actualInput.getBytes(StandardCharsets.UTF_8));
- NormalizedNodeContext inputContext = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
+ if (actualInput != null) {
+ final InputStream entityStream = new ByteArrayInputStream(actualInput.getBytes(StandardCharsets.UTF_8));
+ final NormalizedNodeContext inputContext =
+ JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true);
LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
.getInstanceIdentifier());
outputContext = RestconfImpl.getInstance().invokeRpc(uriPath, "", null);
}
- if(outputContext.getData() != null) {
+ if (outputContext.getData() != null) {
output = toJson(outputContext);
}
- } catch (Exception e) {
+ } catch (final Exception e) {
propagateExceptionAs(uriPath, e, "RPC");
}
public void close() {
}
- private String toJson(NormalizedNodeContext readData) throws IOException {
- NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ private static String toJson(final NormalizedNodeContext readData) throws IOException {
+ final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
+ final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
- MediaType.APPLICATION_JSON_TYPE, null, outputStream );
+ MediaType.APPLICATION_JSON_TYPE, null, outputStream);
return outputStream.toString(StandardCharsets.UTF_8.name());
}
- private boolean isDataMissing(Exception e) {
+ private static boolean isDataMissing(final Exception exception) {
boolean dataMissing = false;
- if(e instanceof RestconfDocumentedException) {
- RestconfDocumentedException rde = (RestconfDocumentedException)e;
- if(!rde.getErrors().isEmpty()) {
- if(rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING) {
+ if (exception instanceof RestconfDocumentedException) {
+ final RestconfDocumentedException rde = (RestconfDocumentedException)exception;
+ if (!rde.getErrors().isEmpty()) {
+ if (rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING) {
dataMissing = true;
}
}
return dataMissing;
}
- private static void propagateExceptionAs(String uriPath, Exception e, String operation) throws OperationFailedException {
- LOG.debug("Error for uriPath: {}", uriPath, e);
+ private static void propagateExceptionAs(final String uriPath, final Exception exception, final String operation)
+ throws OperationFailedException {
+ LOG.debug("Error for uriPath: {}", uriPath, exception);
- if(e instanceof RestconfDocumentedException) {
- throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), e.getCause(),
- toRpcErrors(((RestconfDocumentedException)e).getErrors()));
+ if (exception instanceof RestconfDocumentedException) {
+ throw new OperationFailedException(String.format(
+ "%s failed for URI %s", operation, uriPath), exception.getCause(),
+ toRpcErrors(((RestconfDocumentedException)exception).getErrors()));
}
- throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), e);
+ throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
}
- private static RpcError[] toRpcErrors(List<RestconfError> from) {
- RpcError[] to = new RpcError[from.size()];
- int i = 0;
- for(RestconfError e: from) {
- to[i++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
+ private static RpcError[] toRpcErrors(final List<RestconfError> from) {
+ final RpcError[] to = new RpcError[from.size()];
+ int index = 0;
+ for (final RestconfError e: from) {
+ to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
e.getErrorMessage());
}
return to;
}
- private static ErrorType toRpcErrorType(RestconfError.ErrorType errorType) {
- switch(errorType) {
+ private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
+ switch (errorType) {
case TRANSPORT: {
return ErrorType.TRANSPORT;
}
}
}
}
+
+ private static class SimpleUriInfo implements UriInfo {
+ private final String path;
+ private final MultivaluedMap<String, String> queryParams;
+
+ SimpleUriInfo(String path) {
+ this(path, new MultivaluedHashMap<>());
+ }
+
+ SimpleUriInfo(String path, MultivaluedMap<String, String> queryParams) {
+ this.path = path;
+ this.queryParams = queryParams;
+ }
+
+ @Override
+ public String getPath() {
+ return path;
+ }
+
+ @Override
+ public String getPath(boolean decode) {
+ return path;
+ }
+
+ @Override
+ public List<PathSegment> getPathSegments() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public List<PathSegment> getPathSegments(boolean decode) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public URI getRequestUri() {
+ return URI.create(path);
+ }
+
+ @Override
+ public UriBuilder getRequestUriBuilder() {
+ return UriBuilder.fromUri(getRequestUri());
+ }
+
+ @Override
+ public URI getAbsolutePath() {
+ return getRequestUri();
+ }
+
+ @Override
+ public UriBuilder getAbsolutePathBuilder() {
+ return UriBuilder.fromUri(getAbsolutePath());
+ }
+
+ @Override
+ public URI getBaseUri() {
+ return URI.create("");
+ }
+
+ @Override
+ public UriBuilder getBaseUriBuilder() {
+ return UriBuilder.fromUri(getBaseUri());
+ }
+
+ @Override
+ public MultivaluedMap<String, String> getPathParameters() {
+ return new MultivaluedHashMap<>();
+ }
+
+ @Override
+ public MultivaluedMap<String, String> getPathParameters(boolean decode) {
+ return getPathParameters();
+ }
+
+ @Override
+ public MultivaluedMap<String, String> getQueryParameters() {
+ return queryParams;
+ }
+
+ @Override
+ public MultivaluedMap<String, String> getQueryParameters(boolean decode) {
+ return getQueryParameters();
+ }
+
+ @Override
+ public List<String> getMatchedURIs() {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public List<String> getMatchedURIs(boolean decode) {
+ return getMatchedURIs();
+ }
+
+ @Override
+ public List<Object> getMatchedResources() {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public URI resolve(URI uri) {
+ return uri;
+ }
+
+ @Override
+ public URI relativize(URI uri) {
+ return uri;
+ }
+ }
}