blob: 91681d0626cc38fea86dbde99b7d725700713abb [file] [log] [blame]
Hyunsun Moone5a1fc32016-09-02 16:01:01 -07001/*
2 * Copyright 2016-present 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 */
16package org.opencord.cordvtn.codec;
17
18import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070020import com.google.common.collect.Sets;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070021import org.onlab.packet.IpAddress;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
24import org.onosproject.codec.CodecContext;
25import org.onosproject.codec.JsonCodec;
26import org.opencord.cordvtn.api.AddressPair;
27import org.opencord.cordvtn.api.PortId;
28import org.opencord.cordvtn.api.ServicePort;
29
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070030import java.util.Set;
31
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070032/**
33 * Service port JSON codec.
34 */
35public final class ServicePortCodec extends JsonCodec<ServicePort> {
36
37 private static final String ID = "id";
38 private static final String VLAN_ID = "vlan_id";
39 private static final String FLOATING_ADDRESS_PAIRS = "floating_address_pairs";
40 private static final String IP_ADDRESS = "ip_address";
41 private static final String MAC_ADDRESS = "mac_address";
42
43 @Override
44 public ObjectNode encode(ServicePort sport, CodecContext context) {
45 ObjectNode result = context.mapper().createObjectNode()
46 .put(ID, sport.id().id());
47 if (sport.vlanId().isPresent()) {
48 result.put(VLAN_ID, sport.vlanId().get().id());
49 }
50
51 ArrayNode addressPairs = context.mapper().createArrayNode();
52 sport.addressPairs().forEach(pair -> {
53 ObjectNode pairJson = context.mapper().createObjectNode()
54 .put(IP_ADDRESS, pair.ip().toString())
55 .put(MAC_ADDRESS, pair.mac().toString());
56 addressPairs.add(pairJson);
57 });
58 result.set(FLOATING_ADDRESS_PAIRS, addressPairs);
59 return result;
60 }
61
62 @Override
63 public ServicePort decode(ObjectNode json, CodecContext context) {
64 if (json == null || !json.isObject()) {
65 return null;
66 }
67
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070068 PortId portId = PortId.of(json.get(ID).asText());
69 VlanId vlanId = null;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070070 if (json.get(VLAN_ID) != null) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070071 try {
72 vlanId = VlanId.vlanId(json.get(VLAN_ID).asText());
73 } catch (Exception ignore) {
74 }
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070075 }
76
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070077 Set<AddressPair> addressPairs = Sets.newHashSet();
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070078 if (json.get(FLOATING_ADDRESS_PAIRS) != null) {
79 json.get(FLOATING_ADDRESS_PAIRS).forEach(pair -> {
80 AddressPair addrPair = AddressPair.of(
81 IpAddress.valueOf(pair.get(IP_ADDRESS).asText()),
82 MacAddress.valueOf(pair.get(MAC_ADDRESS).asText()));
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070083 addressPairs.add(addrPair);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070084 });
85 }
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070086 return new ServicePort(portId, vlanId, addressPairs);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070087 }
88}