Committed by
Gerrit Code Review
[ONOS-2753] add restfull service of router
Change-Id: I7764b05d0a43eeaa2fe868afb817ad94d4b8bc64
Showing
3 changed files
with
130 additions
and
0 deletions
This diff is collapsed. Click to expand it.
| 1 | +/* | ||
| 2 | + * Copyright 2015 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.vtnweb.web; | ||
| 17 | + | ||
| 18 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 19 | + | ||
| 20 | +import java.util.Iterator; | ||
| 21 | +import java.util.List; | ||
| 22 | + | ||
| 23 | +import org.onosproject.codec.CodecContext; | ||
| 24 | +import org.onosproject.codec.JsonCodec; | ||
| 25 | +import org.onosproject.vtnrsc.Router; | ||
| 26 | + | ||
| 27 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 28 | + | ||
| 29 | +/** | ||
| 30 | + * Router JSON codec. | ||
| 31 | + */ | ||
| 32 | +public class RouterCodec extends JsonCodec<Router> { | ||
| 33 | + @Override | ||
| 34 | + public ObjectNode encode(Router router, CodecContext context) { | ||
| 35 | + checkNotNull(router, "router cannot be null"); | ||
| 36 | + ObjectNode result = context | ||
| 37 | + .mapper() | ||
| 38 | + .createObjectNode() | ||
| 39 | + .put("id", router.id().routerId()) | ||
| 40 | + .put("status", router.status().toString()) | ||
| 41 | + .put("name", router.name().toString()) | ||
| 42 | + .put("admin_state_up", router.adminStateUp()) | ||
| 43 | + .put("tenant_id", router.tenantId().toString()) | ||
| 44 | + .put("routes", | ||
| 45 | + router.routes() == null ? null : router.routes() | ||
| 46 | + .toString()); | ||
| 47 | + result.set("external_gateway_info", | ||
| 48 | + router.externalGatewayInfo() == null ? null | ||
| 49 | + : new RouterGatewayInfoCodec() | ||
| 50 | + .encode(router.externalGatewayInfo(), context)); | ||
| 51 | + | ||
| 52 | + return result; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + public ObjectNode extracFields(Router router, CodecContext context, | ||
| 56 | + List<String> fields) { | ||
| 57 | + checkNotNull(router, "router cannot be null"); | ||
| 58 | + ObjectNode result = context.mapper().createObjectNode(); | ||
| 59 | + Iterator<String> i = fields.iterator(); | ||
| 60 | + while (i.hasNext()) { | ||
| 61 | + String s = i.next(); | ||
| 62 | + if (s.equals("id")) { | ||
| 63 | + result.put("id", router.id().routerId()); | ||
| 64 | + } | ||
| 65 | + if (s.equals("status")) { | ||
| 66 | + result.put("status", router.status().toString()); | ||
| 67 | + } | ||
| 68 | + if (s.equals("name")) { | ||
| 69 | + result.put("name", router.name().toString()); | ||
| 70 | + } | ||
| 71 | + if (s.equals("admin_state_up")) { | ||
| 72 | + result.put("admin_state_up", router.adminStateUp()); | ||
| 73 | + } | ||
| 74 | + if (s.equals("tenant_id")) { | ||
| 75 | + result.put("tenant_id", router.tenantId().toString()); | ||
| 76 | + } | ||
| 77 | + if (s.equals("routes")) { | ||
| 78 | + result.put("routes", router.routes() == null ? null : router | ||
| 79 | + .routes().toString()); | ||
| 80 | + } | ||
| 81 | + if (s.equals("external_gateway_info")) { | ||
| 82 | + result.set("external_gateway_info", | ||
| 83 | + router.externalGatewayInfo() == null ? null | ||
| 84 | + : new RouterGatewayInfoCodec() | ||
| 85 | + .encode(router.externalGatewayInfo(), | ||
| 86 | + context)); | ||
| 87 | + } | ||
| 88 | + } | ||
| 89 | + return result; | ||
| 90 | + } | ||
| 91 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2015 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.vtnweb.web; | ||
| 17 | + | ||
| 18 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 19 | + | ||
| 20 | +import org.onosproject.codec.CodecContext; | ||
| 21 | +import org.onosproject.codec.JsonCodec; | ||
| 22 | +import org.onosproject.vtnrsc.RouterGateway; | ||
| 23 | + | ||
| 24 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 25 | + | ||
| 26 | +/** | ||
| 27 | + * Subnet Router Gateway Info codec. | ||
| 28 | + */ | ||
| 29 | +public class RouterGatewayInfoCodec extends JsonCodec<RouterGateway> { | ||
| 30 | + @Override | ||
| 31 | + public ObjectNode encode(RouterGateway routerGateway, CodecContext context) { | ||
| 32 | + checkNotNull(routerGateway, "routerGateway cannot be null"); | ||
| 33 | + ObjectNode result = context.mapper().createObjectNode() | ||
| 34 | + .put("network_id", routerGateway.networkId().toString()); | ||
| 35 | + result.set("external_fixed_ips", new FixedIpCodec() | ||
| 36 | + .encode(routerGateway.externalFixedIps(), context)); | ||
| 37 | + return result; | ||
| 38 | + } | ||
| 39 | +} |
-
Please register or login to post a comment