Phaneendra Manda | 5e5cb94 | 2015-10-29 16:44:20 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Phaneendra Manda | 5e5cb94 | 2015-10-29 16:44:20 +0530 | [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 | import static org.onlab.util.Tools.nullIsIllegal; |
| 20 | |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.core.CoreService; |
| 24 | import org.onosproject.vtnrsc.DefaultPortPair; |
| 25 | import org.onosproject.vtnrsc.PortPair; |
| 26 | import org.onosproject.vtnrsc.PortPairId; |
| 27 | import org.onosproject.vtnrsc.TenantId; |
| 28 | |
| 29 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 30 | |
| 31 | /** |
| 32 | * Port Pair JSON codec. |
| 33 | */ |
| 34 | public final class PortPairCodec extends JsonCodec<PortPair> { |
| 35 | |
| 36 | private static final String ID = "id"; |
| 37 | private static final String TENANT_ID = "tenant_id"; |
| 38 | private static final String NAME = "name"; |
| 39 | private static final String DESCRIPTION = "description"; |
| 40 | private static final String INGRESS = "ingress"; |
| 41 | private static final String EGRESS = "egress"; |
| 42 | private static final String MISSING_MEMBER_MESSAGE = |
| 43 | " member is required in PortPair"; |
| 44 | |
| 45 | @Override |
| 46 | public PortPair decode(ObjectNode json, CodecContext context) { |
| 47 | if (json == null || !json.isObject()) { |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | PortPair.Builder resultBuilder = new DefaultPortPair.Builder(); |
| 52 | |
| 53 | CoreService coreService = context.getService(CoreService.class); |
| 54 | |
| 55 | String id = nullIsIllegal(json.get(ID), |
| 56 | ID + MISSING_MEMBER_MESSAGE).asText(); |
Mahesh Poojary Huawei | d0b1d13 | 2015-11-05 11:00:18 +0530 | [diff] [blame] | 57 | resultBuilder.setId(PortPairId.of(id)); |
Phaneendra Manda | 5e5cb94 | 2015-10-29 16:44:20 +0530 | [diff] [blame] | 58 | |
| 59 | String tenantId = nullIsIllegal(json.get(TENANT_ID), |
| 60 | TENANT_ID + MISSING_MEMBER_MESSAGE).asText(); |
| 61 | resultBuilder.setTenantId(TenantId.tenantId(tenantId)); |
| 62 | |
| 63 | String name = nullIsIllegal(json.get(NAME), |
| 64 | NAME + MISSING_MEMBER_MESSAGE).asText(); |
| 65 | resultBuilder.setName(name); |
| 66 | |
| 67 | String description = nullIsIllegal(json.get(DESCRIPTION), |
| 68 | DESCRIPTION + MISSING_MEMBER_MESSAGE).asText(); |
| 69 | resultBuilder.setDescription(description); |
| 70 | |
| 71 | String ingressPort = nullIsIllegal(json.get(INGRESS), |
| 72 | INGRESS + MISSING_MEMBER_MESSAGE).asText(); |
| 73 | resultBuilder.setIngress(ingressPort); |
| 74 | |
| 75 | String egressPort = nullIsIllegal(json.get(EGRESS), |
| 76 | EGRESS + MISSING_MEMBER_MESSAGE).asText(); |
| 77 | resultBuilder.setEgress(egressPort); |
| 78 | |
| 79 | return resultBuilder.build(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public ObjectNode encode(PortPair portPair, CodecContext context) { |
| 84 | checkNotNull(portPair, "port pair cannot be null"); |
| 85 | ObjectNode result = context.mapper().createObjectNode() |
| 86 | .put(ID, portPair.portPairId().toString()) |
| 87 | .put(TENANT_ID, portPair.tenantId().toString()) |
| 88 | .put(NAME, portPair.name()) |
| 89 | .put(DESCRIPTION, portPair.description()) |
| 90 | .put(INGRESS, portPair.ingress()) |
| 91 | .put(EGRESS, portPair.egress()); |
| 92 | return result; |
| 93 | } |
| 94 | } |