[ONOS-3801] Implements L3 REST call json parser
- Nullability check for RouterCodec, RoutingInterfaceCodec, RoutingWebResource.
- Copyright fixed.
- externalFixedIps() method in OpenstackExternalGateway class returns the immutable Map.
Change-Id: I841cc1774a074e167ffe327c6e81d3f245cc8ee0
diff --git a/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpensatckRouterWebResource.java b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpensatckRouterWebResource.java
new file mode 100644
index 0000000..aa9dfe4
--- /dev/null
+++ b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpensatckRouterWebResource.java
@@ -0,0 +1,172 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.openstackswitching.web;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.openstackrouting.OpenstackRouter;
+import org.onosproject.openstackrouting.OpenstackRouterInterface;
+import org.onosproject.openstackswitching.OpenstackSwitchingService;
+import org.onosproject.rest.AbstractWebResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.InputStream;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Handles REST API call of Neturon L3 plugin.
+ */
+
+@Path("routers")
+public class OpensatckRouterWebResource extends AbstractWebResource {
+ protected static final Logger log = LoggerFactory
+ .getLogger(OpenstackNetworkWebResource.class);
+
+ private static final OpenstackRouterInterfaceCodec ROUTER_INTERFACE_CODEC
+ = new OpenstackRouterInterfaceCodec();
+ private static final OpenstackRouterCodec ROUTER_CODEC
+ = new OpenstackRouterCodec();
+
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response createRouter(InputStream input) {
+ checkNotNull(input);
+ try {
+ ObjectMapper mapper = new ObjectMapper();
+ ObjectNode routerNode = (ObjectNode) mapper.readTree(input);
+
+ OpenstackRouter openstackRouter
+ = ROUTER_CODEC.decode(routerNode, this);
+
+ OpenstackSwitchingService switchingService
+ = getService(OpenstackSwitchingService.class);
+ switchingService.createRouter(openstackRouter);
+
+ log.debug("REST API CREATE router is called {}", input.toString());
+ return Response.status(Response.Status.OK).build();
+ } catch (Exception e) {
+ log.error("Create Router failed because of exception {}",
+ e.toString());
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString())
+ .build();
+ }
+ }
+
+ @PUT
+ @Path("{id}")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response updateRouter(@PathParam("id") String id) {
+ checkNotNull(id);
+ try {
+ OpenstackSwitchingService switchingService
+ = getService(OpenstackSwitchingService.class);
+ switchingService.updateRouter(id);
+
+ log.debug("REST API UPDATE router is called from router {}", id);
+ return Response.status(Response.Status.OK).build();
+ } catch (Exception e) {
+ log.error("Updates Router failed because of exception {}",
+ e.toString());
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString())
+ .build();
+ }
+ }
+
+ @PUT
+ @Path("{id}/add_router_interface")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response addRouterInterface(InputStream input) {
+ checkNotNull(input);
+ try {
+ ObjectMapper mapper = new ObjectMapper();
+ ObjectNode routerIfNode = (ObjectNode) mapper.readTree(input);
+
+ OpenstackRouterInterface openstackRouterInterface
+ = ROUTER_INTERFACE_CODEC.decode(routerIfNode, this);
+
+ OpenstackSwitchingService switchingService
+ = getService(OpenstackSwitchingService.class);
+ switchingService.updateRouterInterface(openstackRouterInterface);
+
+ log.debug("REST API AddRouterInterface is called from router {} portId: {}, subnetId: {}, tenantId: {}",
+ openstackRouterInterface.id(), openstackRouterInterface.portId(),
+ openstackRouterInterface.subnetId(), openstackRouterInterface.tenantId());
+
+ return Response.status(Response.Status.OK).build();
+ } catch (Exception e) {
+ log.error("AddRouterInterface failed because of exception {}",
+ e.toString());
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString())
+ .build();
+ }
+ }
+
+ @DELETE
+ @Path("{id}")
+ public Response deleteRouter(@PathParam("id") String id) {
+ checkNotNull(id);
+ OpenstackSwitchingService switchingService =
+ getService(OpenstackSwitchingService.class);
+ switchingService.deleteRouter(id);
+
+ log.debug("REST API DELETE routers is called {}", id);
+ return Response.status(Response.Status.OK).build();
+ }
+
+ @PUT
+ @Path("{id}/remove_router_interface")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response removeRouterInterface(@PathParam("id") String id, InputStream input) {
+ checkNotNull(id);
+ checkNotNull(input);
+ try {
+ ObjectMapper mapper = new ObjectMapper();
+ ObjectNode routerIfNode = (ObjectNode) mapper.readTree(input);
+
+ OpenstackRouterInterface openstackRouterInterface
+ = ROUTER_INTERFACE_CODEC.decode(routerIfNode, this);
+
+ OpenstackSwitchingService switchingService
+ = getService(OpenstackSwitchingService.class);
+ switchingService.removeRouterInterface(openstackRouterInterface);
+
+ log.debug("REST API RemoveRouterInterface is called from router {} portId: {}, subnetId: {}," +
+ "tenantId: {}", openstackRouterInterface.id(), openstackRouterInterface.portId(),
+ openstackRouterInterface.subnetId(), openstackRouterInterface.tenantId());
+
+ return Response.status(Response.Status.OK).build();
+ } catch (Exception e) {
+ log.error("RemoveRouterInterface failed because of exception {}",
+ e.toString());
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString())
+ .build();
+ }
+ }
+}
diff --git a/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortWebResource.java b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortWebResource.java
index 4e23753..206e4b1 100644
--- a/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortWebResource.java
+++ b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackPortWebResource.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015 Open Networking Laboratory
+ * Copyright 2015-2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
diff --git a/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackRouterCodec.java b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackRouterCodec.java
new file mode 100644
index 0000000..afcf560
--- /dev/null
+++ b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackRouterCodec.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.openstackswitching.web;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.Maps;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.openstackrouting.OpenstackExternalGateway;
+import org.onosproject.openstackrouting.OpenstackRouter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.Map;
+/**
+ * Implementation of the OpenstackRouter Codec.
+ */
+public class OpenstackRouterCodec extends JsonCodec<OpenstackRouter> {
+ protected static final Logger log = LoggerFactory
+ .getLogger(OpenstackNetworkCodec.class);
+
+ private static final String ROUTER = "router";
+ private static final String TENANT_ID = "tenant_id";
+ private static final String NETWORK_ID = "network_id";
+ private static final String ID = "id";
+ private static final String NAME = "name";
+ private static final String STATUS = "status";
+ private static final String ADMIN_STATE_UP = "admin_state_up";
+ private static final String EXTERNAL_GW_INFO = "external_gateway_info";
+ private static final String EXTERNAL_FIXED_IPS = "external_fixed_ips";
+ private static final String SUBNET_ID = "subnet_id";
+ private static final String IP_ADDRESS = "ip_address";
+
+ /**
+ * Decodes the OpenstackRouter.
+ *
+ * @param json JSON to decode
+ * @param context decoding context
+ * @return OpenstackRouter
+ */
+ @Override
+ public OpenstackRouter decode(ObjectNode json, CodecContext context) {
+
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+ JsonNode routerInfo = json.get(ROUTER);
+ if (routerInfo == null) {
+ routerInfo = json;
+ }
+
+ String tenantId = checkNotNull(routerInfo.path(TENANT_ID).asText());
+ String id = checkNotNull(routerInfo.path(ID).asText());
+ String name = checkNotNull(routerInfo.path(NAME).asText());
+ String status = checkNotNull(routerInfo.path(STATUS).asText());
+ String adminStateUp = checkNotNull(routerInfo.path(ADMIN_STATE_UP).asText());
+
+ OpenstackExternalGateway.Builder osExtBuiler = OpenstackExternalGateway.builder();
+
+ if (!routerInfo.path(EXTERNAL_GW_INFO).isMissingNode()) {
+ String externalGatewayNetId = checkNotNull(routerInfo.path(EXTERNAL_GW_INFO).path(NETWORK_ID).asText());
+ Map<String, Ip4Address> fixedIpMap = Maps.newHashMap();
+
+
+ if (!routerInfo.path(EXTERNAL_GW_INFO).path(EXTERNAL_FIXED_IPS).isMissingNode()) {
+ ArrayNode fixedIpList = (ArrayNode) routerInfo.path(EXTERNAL_GW_INFO).path(EXTERNAL_FIXED_IPS);
+
+ for (JsonNode fixedIpInfo : fixedIpList) {
+ String subnetId = checkNotNull(fixedIpInfo.path(SUBNET_ID).asText());
+ String ipAddressStr = checkNotNull(fixedIpInfo.path(IP_ADDRESS).asText());
+ if (!fixedIpInfo.path(IP_ADDRESS).isMissingNode() && ipAddressStr != null) {
+ fixedIpMap.put(subnetId, Ip4Address.valueOf(ipAddressStr));
+ }
+ }
+ }
+
+ osExtBuiler.networkId(externalGatewayNetId)
+ .enablePnat(true)
+ .externalFixedIps(fixedIpMap);
+ }
+ OpenstackRouter.Builder osBuilder = new OpenstackRouter.Builder()
+ .tenantId(tenantId)
+ .id(id)
+ .name(name)
+ .status(OpenstackRouter.RouterStatus.valueOf(status))
+ .adminStateUp(Boolean.valueOf(adminStateUp))
+ .gatewayExternalInfo(osExtBuiler.build());
+
+ return osBuilder.build();
+ }
+}
diff --git a/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackRouterInterfaceCodec.java b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackRouterInterfaceCodec.java
new file mode 100644
index 0000000..88de443
--- /dev/null
+++ b/apps/openstackswitching/app/src/main/java/org/onosproject/openstackswitching/web/OpenstackRouterInterfaceCodec.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.openstackswitching.web;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.openstackrouting.OpenstackRouterInterface;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import static com.google.common.base.Preconditions.checkNotNull;
+/**
+ * Implementation of the OpenstackRouterInterface Codec.
+ */
+public class OpenstackRouterInterfaceCodec extends JsonCodec<OpenstackRouterInterface> {
+ protected static final Logger log = LoggerFactory
+ .getLogger(OpenstackNetworkCodec.class);
+
+ private static final String ID = "id";
+ private static final String TENANT_ID = "tenant_id";
+ private static final String SUBNET_ID = "subnet_id";
+ private static final String PORT_ID = "port_id";
+
+ /**
+ * Decodes the OpenstackRouterInterface.
+ *
+ * @param json JSON to decode
+ * @param context decoding context
+ * @return OpenstackRouterInterface
+ */
+ @Override
+ public OpenstackRouterInterface decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+ JsonNode routerIfInfo = json;
+
+ String id = checkNotNull(routerIfInfo.path(ID).asText());
+ String tenantId = checkNotNull(routerIfInfo.path(TENANT_ID).asText());
+ String subnetId = checkNotNull(routerIfInfo.path(SUBNET_ID).asText());
+ String portId = checkNotNull(routerIfInfo.path(PORT_ID).asText());
+
+ OpenstackRouterInterface.Builder osBuilder = new OpenstackRouterInterface.Builder()
+ .id(id)
+ .tenantId(tenantId)
+ .subnetId(subnetId)
+ .portId(portId);
+
+ return osBuilder.build();
+ }
+}