blob: 2b63bfe90f9aaf78d6a5b008e9797d518b93423e [file] [log] [blame]
Phaneendra Manda5e5cb942015-10-29 16:44:20 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Phaneendra Manda5e5cb942015-10-29 16:44:20 +05303 *
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;
19import static org.onlab.util.Tools.nullIsIllegal;
20
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.core.CoreService;
24import org.onosproject.vtnrsc.DefaultPortPair;
25import org.onosproject.vtnrsc.PortPair;
26import org.onosproject.vtnrsc.PortPairId;
27import org.onosproject.vtnrsc.TenantId;
28
29import com.fasterxml.jackson.databind.node.ObjectNode;
30
31/**
32 * Port Pair JSON codec.
33 */
34public 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 Huaweid0b1d132015-11-05 11:00:18 +053057 resultBuilder.setId(PortPairId.of(id));
Phaneendra Manda5e5cb942015-10-29 16:44:20 +053058
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}