jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
jiangrui | 330b0c9 | 2015-11-28 14:09:50 +0800 | [diff] [blame] | 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 | } |