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 | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
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; |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 28 | import org.opencord.cordvtn.api.net.NetworkId; |
Hyunsun Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame] | 29 | import org.opencord.cordvtn.api.net.PortId; |
| 30 | import org.opencord.cordvtn.api.net.ServicePort; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 31 | import org.opencord.cordvtn.impl.DefaultServicePort; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 32 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 33 | import java.util.Set; |
| 34 | |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 35 | import static com.google.common.base.Preconditions.checkArgument; |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 36 | import static com.google.common.base.Strings.isNullOrEmpty; |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 37 | |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Service port JSON codec. |
| 40 | */ |
| 41 | public final class ServicePortCodec extends JsonCodec<ServicePort> { |
| 42 | |
| 43 | private static final String ID = "id"; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 44 | private static final String NAME = "name"; |
| 45 | private static final String NETWORK_ID = "network_id"; |
| 46 | private static final String MAC_ADDRESS = "mac_address"; |
| 47 | private static final String IP_ADDRESS = "ip_address"; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 48 | private static final String VLAN_ID = "vlan_id"; |
| 49 | private static final String FLOATING_ADDRESS_PAIRS = "floating_address_pairs"; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 50 | |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 51 | private static final String ERR_JSON = "Invalid ServicePort received"; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 52 | private static final String ERR_ID = "Service port ID cannot be null"; |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 53 | |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 54 | @Override |
| 55 | public ObjectNode encode(ServicePort sport, CodecContext context) { |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 56 | ObjectNode result = context.mapper().createObjectNode().put(ID, sport.id().id()); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 57 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 58 | if (sport.networkId() != null) { |
| 59 | result.put(NETWORK_ID, sport.networkId().id()); |
| 60 | } |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 61 | if (!isNullOrEmpty(sport.name())) { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 62 | result.put(NAME, sport.name()); |
| 63 | } |
| 64 | if (sport.vlanId() != null) { |
| 65 | result.put(VLAN_ID, sport.vlanId().id()); |
| 66 | } |
| 67 | if (sport.mac() != null) { |
| 68 | result.put(MAC_ADDRESS, sport.mac().toString()); |
| 69 | } |
| 70 | if (sport.ip() != null) { |
| 71 | result.put(IP_ADDRESS, sport.ip().toString()); |
| 72 | } |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 73 | ArrayNode addressPairs = context.mapper().createArrayNode(); |
| 74 | sport.addressPairs().forEach(pair -> { |
| 75 | ObjectNode pairJson = context.mapper().createObjectNode() |
| 76 | .put(IP_ADDRESS, pair.ip().toString()) |
| 77 | .put(MAC_ADDRESS, pair.mac().toString()); |
| 78 | addressPairs.add(pairJson); |
| 79 | }); |
| 80 | result.set(FLOATING_ADDRESS_PAIRS, addressPairs); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 81 | |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 82 | return result; |
| 83 | } |
| 84 | |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 85 | // TODO allow removing existing value when explicit null received |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 86 | @Override |
| 87 | public ServicePort decode(ObjectNode json, CodecContext context) { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 88 | checkArgument(json != null && json.isObject(), ERR_JSON); |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 89 | checkArgument(!json.path(ID).isMissingNode() && !json.path(ID).isNull(), ERR_ID); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 90 | |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 91 | ServicePort.Builder sportBuilder = |
| 92 | DefaultServicePort.builder().id(PortId.of(json.get(ID).asText())); |
| 93 | |
| 94 | if (!json.path(NETWORK_ID).isMissingNode()) { |
| 95 | if (json.path(NETWORK_ID).isNull() || |
| 96 | isNullOrEmpty(json.path(NETWORK_ID).asText())) { |
| 97 | final String error = "Null or empty ServicePort network ID received"; |
| 98 | throw new IllegalArgumentException(error); |
| 99 | } else { |
| 100 | sportBuilder.networkId(NetworkId.of(json.get(NETWORK_ID).asText())); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (!json.path(NAME).isMissingNode()) { |
| 105 | if (json.path(NAME).isNull() || isNullOrEmpty(json.path(NAME).asText())) { |
| 106 | final String error = "Null or empty ServicePort name received"; |
| 107 | throw new IllegalArgumentException(error); |
| 108 | } else { |
| 109 | sportBuilder.name(json.get(NAME).asText()); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if (!json.path(MAC_ADDRESS).isMissingNode()) { |
| 114 | try { |
| 115 | sportBuilder.mac(MacAddress.valueOf(json.path(MAC_ADDRESS).asText())); |
| 116 | } catch (IllegalArgumentException | NullPointerException e) { |
| 117 | final String error = "Invalid ServicePort MAC address received"; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 118 | throw new IllegalArgumentException(error); |
| 119 | } |
| 120 | } |
| 121 | |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 122 | if (!json.path(IP_ADDRESS).isMissingNode()) { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 123 | try { |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 124 | sportBuilder.ip(IpAddress.valueOf(json.get(IP_ADDRESS).asText())); |
| 125 | } catch (IllegalArgumentException | NullPointerException e) { |
| 126 | final String error = "Invalid ServicePort IP address received"; |
| 127 | throw new IllegalArgumentException(error); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 131 | if (!json.path(VLAN_ID).isMissingNode()) { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 132 | try { |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 133 | sportBuilder.vlanId(VlanId.vlanId(json.get(VLAN_ID).asText())); |
| 134 | } catch (IllegalArgumentException | NullPointerException e) { |
| 135 | final String error = "Invalid VLAN ID is received"; |
| 136 | throw new IllegalArgumentException(error); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 140 | if (!json.path(FLOATING_ADDRESS_PAIRS).isMissingNode() && |
| 141 | json.path(FLOATING_ADDRESS_PAIRS).isNull()) { |
| 142 | sportBuilder.addressPairs(ImmutableSet.of()); |
| 143 | } else if (!json.path(FLOATING_ADDRESS_PAIRS).isMissingNode()) { |
| 144 | Set<AddressPair> addressPairs = Sets.newHashSet(); |
| 145 | json.path(FLOATING_ADDRESS_PAIRS).forEach(pair -> { |
| 146 | if (pair.path(IP_ADDRESS).isMissingNode() || |
| 147 | pair.path(IP_ADDRESS).isNull() || |
| 148 | pair.path(MAC_ADDRESS).isMissingNode() || |
| 149 | pair.path(MAC_ADDRESS).isNull()) { |
| 150 | final String error = "Invalid floating address pair received: "; |
| 151 | throw new IllegalArgumentException(error + pair.asText()); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 152 | } |
| 153 | try { |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 154 | AddressPair addrPair = AddressPair.of( |
| 155 | IpAddress.valueOf(pair.get(IP_ADDRESS).asText()), |
| 156 | MacAddress.valueOf(pair.get(MAC_ADDRESS).asText())); |
| 157 | addressPairs.add(addrPair); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 158 | } catch (IllegalArgumentException e) { |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 159 | final String error = "Invalid floating address pair received: "; |
| 160 | throw new IllegalArgumentException(error + pair.asText()); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 161 | } |
| 162 | }); |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 163 | sportBuilder.addressPairs(addressPairs); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 164 | } |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 165 | return sportBuilder.build(); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 166 | } |
| 167 | } |