Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.opencord.cordvtn.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.VlanId; |
| 24 | import org.onosproject.codec.CodecContext; |
| 25 | import org.onosproject.codec.JsonCodec; |
Hyunsun Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame^] | 26 | import org.opencord.cordvtn.api.net.AddressPair; |
| 27 | import org.opencord.cordvtn.api.net.PortId; |
| 28 | import org.opencord.cordvtn.api.net.ServicePort; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 29 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 30 | import java.util.Set; |
| 31 | |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Service port JSON codec. |
| 34 | */ |
| 35 | public 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 Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 68 | PortId portId = PortId.of(json.get(ID).asText()); |
| 69 | VlanId vlanId = null; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 70 | if (json.get(VLAN_ID) != null) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 71 | try { |
| 72 | vlanId = VlanId.vlanId(json.get(VLAN_ID).asText()); |
| 73 | } catch (Exception ignore) { |
| 74 | } |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 77 | Set<AddressPair> addressPairs = Sets.newHashSet(); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 78 | 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 Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 83 | addressPairs.add(addrPair); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 84 | }); |
| 85 | } |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 86 | return new ServicePort(portId, vlanId, addressPairs); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 87 | } |
| 88 | } |