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; |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame^] | 19 | import com.fasterxml.jackson.databind.node.NullNode; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onosproject.codec.CodecContext; |
| 26 | import org.onosproject.codec.JsonCodec; |
Hyunsun Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame] | 27 | import org.opencord.cordvtn.api.net.AddressPair; |
| 28 | import org.opencord.cordvtn.api.net.PortId; |
| 29 | import org.opencord.cordvtn.api.net.ServicePort; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 30 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 31 | import java.util.Set; |
| 32 | |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame^] | 33 | import static com.google.common.base.Preconditions.checkArgument; |
| 34 | |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Service port JSON codec. |
| 37 | */ |
| 38 | public final class ServicePortCodec extends JsonCodec<ServicePort> { |
| 39 | |
| 40 | private static final String ID = "id"; |
| 41 | private static final String VLAN_ID = "vlan_id"; |
| 42 | private static final String FLOATING_ADDRESS_PAIRS = "floating_address_pairs"; |
| 43 | private static final String IP_ADDRESS = "ip_address"; |
| 44 | private static final String MAC_ADDRESS = "mac_address"; |
| 45 | |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame^] | 46 | private static final String ERR_JSON = "Invalid ServicePort received"; |
| 47 | private static final String ERR_ID = ": port ID cannot be null"; |
| 48 | |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 49 | @Override |
| 50 | public ObjectNode encode(ServicePort sport, CodecContext context) { |
| 51 | ObjectNode result = context.mapper().createObjectNode() |
| 52 | .put(ID, sport.id().id()); |
| 53 | if (sport.vlanId().isPresent()) { |
| 54 | result.put(VLAN_ID, sport.vlanId().get().id()); |
| 55 | } |
| 56 | |
| 57 | ArrayNode addressPairs = context.mapper().createArrayNode(); |
| 58 | sport.addressPairs().forEach(pair -> { |
| 59 | ObjectNode pairJson = context.mapper().createObjectNode() |
| 60 | .put(IP_ADDRESS, pair.ip().toString()) |
| 61 | .put(MAC_ADDRESS, pair.mac().toString()); |
| 62 | addressPairs.add(pairJson); |
| 63 | }); |
| 64 | result.set(FLOATING_ADDRESS_PAIRS, addressPairs); |
| 65 | return result; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public ServicePort decode(ObjectNode json, CodecContext context) { |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame^] | 70 | checkArgument(json != null && json.isObject(), ERR_JSON); |
| 71 | checkArgument(json.get(ID) != null && |
| 72 | json.get(ID) != NullNode.getInstance(), |
| 73 | ERR_JSON + ERR_ID); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 74 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 75 | PortId portId = PortId.of(json.get(ID).asText()); |
| 76 | VlanId vlanId = null; |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame^] | 77 | if (json.get(VLAN_ID) != NullNode.getInstance()) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 78 | try { |
| 79 | vlanId = VlanId.vlanId(json.get(VLAN_ID).asText()); |
| 80 | } catch (Exception ignore) { |
| 81 | } |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 84 | Set<AddressPair> addressPairs = Sets.newHashSet(); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 85 | if (json.get(FLOATING_ADDRESS_PAIRS) != null) { |
| 86 | json.get(FLOATING_ADDRESS_PAIRS).forEach(pair -> { |
| 87 | AddressPair addrPair = AddressPair.of( |
| 88 | IpAddress.valueOf(pair.get(IP_ADDRESS).asText()), |
| 89 | MacAddress.valueOf(pair.get(MAC_ADDRESS).asText())); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 90 | addressPairs.add(addrPair); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 91 | }); |
| 92 | } |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 93 | return new ServicePort(portId, vlanId, addressPairs); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 94 | } |
| 95 | } |