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.base.Strings; |
| 21 | import com.google.common.collect.ImmutableSet; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Sets; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onlab.packet.VlanId; |
| 26 | import org.onosproject.codec.CodecContext; |
| 27 | import org.onosproject.codec.JsonCodec; |
Hyunsun Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame] | 28 | import org.opencord.cordvtn.api.net.AddressPair; |
| 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; |
| 36 | |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Service port JSON codec. |
| 39 | */ |
| 40 | public final class ServicePortCodec extends JsonCodec<ServicePort> { |
| 41 | |
| 42 | private static final String ID = "id"; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 43 | private static final String NAME = "name"; |
| 44 | private static final String NETWORK_ID = "network_id"; |
| 45 | private static final String MAC_ADDRESS = "mac_address"; |
| 46 | private static final String IP_ADDRESS = "ip_address"; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 47 | private static final String VLAN_ID = "vlan_id"; |
| 48 | private static final String FLOATING_ADDRESS_PAIRS = "floating_address_pairs"; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 49 | |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 50 | private static final String ERR_JSON = "Invalid ServicePort received"; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 51 | private static final String ERR_ID = "Service port ID cannot be null"; |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 52 | |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 53 | @Override |
| 54 | public ObjectNode encode(ServicePort sport, CodecContext context) { |
| 55 | ObjectNode result = context.mapper().createObjectNode() |
| 56 | .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 | } |
| 61 | if (!Strings.isNullOrEmpty(sport.name())) { |
| 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 | |
| 85 | @Override |
| 86 | public ServicePort decode(ObjectNode json, CodecContext context) { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 87 | validateJson(json); |
| 88 | ServicePort.Builder sportBuilder = DefaultServicePort.builder() |
| 89 | .id(PortId.of(json.get(ID).asText())); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 90 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 91 | // TODO allow removing existing value when explicit null received |
| 92 | if (json.get(NAME) != null && !json.get(NAME).isNull()) { |
| 93 | sportBuilder.name(json.get(NAME).asText()); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 94 | } |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 95 | if (json.get(MAC_ADDRESS) != null && !json.get(MAC_ADDRESS).isNull()) { |
| 96 | sportBuilder.mac(MacAddress.valueOf(json.get(MAC_ADDRESS).asText())); |
| 97 | } |
| 98 | if (json.get(IP_ADDRESS) != null && !json.get(IP_ADDRESS).isNull()) { |
| 99 | sportBuilder.ip(IpAddress.valueOf(json.get(IP_ADDRESS).asText())); |
| 100 | } |
| 101 | if (json.get(VLAN_ID) != null && !json.get(VLAN_ID).isNull()) { |
| 102 | sportBuilder.vlanId(VlanId.vlanId(json.get(VLAN_ID).asText())); |
| 103 | } |
| 104 | if (json.get(FLOATING_ADDRESS_PAIRS).isNull()) { |
| 105 | sportBuilder.addressPairs(ImmutableSet.of()); |
| 106 | } else if (json.get(FLOATING_ADDRESS_PAIRS) != null) { |
| 107 | Set<AddressPair> addressPairs = Sets.newHashSet(); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 108 | json.get(FLOATING_ADDRESS_PAIRS).forEach(pair -> { |
| 109 | AddressPair addrPair = AddressPair.of( |
| 110 | IpAddress.valueOf(pair.get(IP_ADDRESS).asText()), |
| 111 | MacAddress.valueOf(pair.get(MAC_ADDRESS).asText())); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 112 | addressPairs.add(addrPair); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 113 | }); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 114 | sportBuilder.addressPairs(addressPairs); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 115 | } |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 116 | return sportBuilder.build(); |
| 117 | } |
| 118 | |
| 119 | private void validateJson(ObjectNode json) { |
| 120 | checkArgument(json != null && json.isObject(), ERR_JSON); |
| 121 | checkArgument(json.get(ID) != null && !json.get(ID).isNull(), ERR_ID); |
| 122 | |
| 123 | // allow explicit null for removing the existing value |
| 124 | if (json.get(NAME) != null && !json.get(NAME).isNull()) { |
| 125 | if (Strings.isNullOrEmpty(json.get(NAME).asText())) { |
| 126 | final String error = "Null or empty ServiceNetwork name received"; |
| 127 | throw new IllegalArgumentException(error); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (json.get(MAC_ADDRESS) != null && !json.get(MAC_ADDRESS).isNull()) { |
| 132 | try { |
| 133 | MacAddress.valueOf(json.get(MAC_ADDRESS).asText()); |
| 134 | } catch (IllegalArgumentException e) { |
| 135 | final String error = "Invalid ServicePort MAC address received: "; |
| 136 | throw new IllegalArgumentException(error + json.get(MAC_ADDRESS).asText()); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (json.get(IP_ADDRESS) != null && !json.get(IP_ADDRESS).isNull()) { |
| 141 | try { |
| 142 | IpAddress.valueOf(json.get(IP_ADDRESS).asText()); |
| 143 | } catch (IllegalArgumentException e) { |
| 144 | final String error = "Invalid ServicePort IP address received: "; |
| 145 | throw new IllegalArgumentException(error + json.get(IP_ADDRESS).asText()); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (json.get(VLAN_ID) != null && !json.get(VLAN_ID).isNull()) { |
| 150 | try { |
| 151 | VlanId.vlanId(json.get(VLAN_ID).asText()); |
| 152 | } catch (IllegalArgumentException e) { |
| 153 | final String error = "Invalid VLAN ID is received: "; |
| 154 | throw new IllegalArgumentException(error + json.get(VLAN_ID).asText()); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (json.get(FLOATING_ADDRESS_PAIRS) != null && |
| 159 | !json.get(FLOATING_ADDRESS_PAIRS).isNull()) { |
| 160 | json.get(FLOATING_ADDRESS_PAIRS).forEach(pair -> { |
| 161 | if (pair.get(IP_ADDRESS) == null || |
| 162 | pair.get(IP_ADDRESS).isNull() || |
| 163 | pair.get(MAC_ADDRESS) == null || |
| 164 | pair.get(MAC_ADDRESS).isNull()) { |
| 165 | final String error = "Invalid floating address pair received"; |
| 166 | throw new IllegalArgumentException(error); |
| 167 | } |
| 168 | try { |
| 169 | IpAddress.valueOf(pair.get(IP_ADDRESS).asText()); |
| 170 | } catch (IllegalArgumentException e) { |
| 171 | final String error = "Invalid floating address pair IP: "; |
| 172 | throw new IllegalArgumentException(error + pair.get(IP_ADDRESS).asText()); |
| 173 | } |
| 174 | |
| 175 | try { |
| 176 | MacAddress.valueOf(pair.get(MAC_ADDRESS).asText()); |
| 177 | } catch (IllegalArgumentException e) { |
| 178 | final String error = "Invalid floating address pair MAC: "; |
| 179 | throw new IllegalArgumentException(error + pair.get(MAC_ADDRESS).asText()); |
| 180 | } |
| 181 | }); |
| 182 | } |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 183 | } |
| 184 | } |