blob: 1b650ade6a61cd2504ba488e11b58cd5d4226440 [file] [log] [blame]
jiangrui330b0c92015-11-28 14:09:50 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
jiangrui330b0c92015-11-28 14:09:50 +08003 *
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 */
16package org.onosproject.vtnweb.web;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Iterator;
21import java.util.List;
22
23import org.onosproject.codec.CodecContext;
24import org.onosproject.codec.JsonCodec;
25import org.onosproject.vtnrsc.Router;
26
27import com.fasterxml.jackson.databind.node.ObjectNode;
28
29/**
30 * Router JSON codec.
31 */
32public 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();
Jon Halla3fcf672017-03-28 16:53:22 -070062 if ("id".equals(s)) {
jiangrui330b0c92015-11-28 14:09:50 +080063 result.put("id", router.id().routerId());
64 }
Jon Halla3fcf672017-03-28 16:53:22 -070065 if ("status".equals(s)) {
jiangrui330b0c92015-11-28 14:09:50 +080066 result.put("status", router.status().toString());
67 }
Jon Halla3fcf672017-03-28 16:53:22 -070068 if ("name".equals(s)) {
jiangrui330b0c92015-11-28 14:09:50 +080069 result.put("name", router.name().toString());
70 }
Jon Halla3fcf672017-03-28 16:53:22 -070071 if ("admin_state_up".equals(s)) {
jiangrui330b0c92015-11-28 14:09:50 +080072 result.put("admin_state_up", router.adminStateUp());
73 }
Jon Halla3fcf672017-03-28 16:53:22 -070074 if ("tenant_id".equals(s)) {
jiangrui330b0c92015-11-28 14:09:50 +080075 result.put("tenant_id", router.tenantId().toString());
76 }
Jon Halla3fcf672017-03-28 16:53:22 -070077 if ("routes".equals(s)) {
jiangrui330b0c92015-11-28 14:09:50 +080078 result.put("routes", router.routes() == null ? null : router
79 .routes().toString());
80 }
Jon Halla3fcf672017-03-28 16:53:22 -070081 if ("external_gateway_info".equals(s)) {
jiangrui330b0c92015-11-28 14:09:50 +080082 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}