Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.NullNode; |
| 20 | import org.hamcrest.Description; |
| 21 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.opencord.cordvtn.api.net.AddressPair; |
| 25 | import org.opencord.cordvtn.api.net.ServicePort; |
| 26 | |
| 27 | import java.util.Objects; |
| 28 | |
| 29 | /** |
| 30 | * Json matcher for ServicePort. |
| 31 | */ |
| 32 | public final class ServicePortJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 33 | |
| 34 | private final ServicePort port; |
| 35 | |
| 36 | private ServicePortJsonMatcher(ServicePort port) { |
| 37 | this.port = port; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Factory to allocate ServicePort matcher. |
| 42 | * |
| 43 | * @param port service port object to match |
| 44 | * @return matcher |
| 45 | */ |
| 46 | public static ServicePortJsonMatcher matchesServicePort(ServicePort port) { |
| 47 | return new ServicePortJsonMatcher(port); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 52 | final String jsonPortId = jsonNode.get("id").asText(); |
| 53 | if (!Objects.equals(jsonPortId, port.id().id())) { |
| 54 | description.appendText("Port id was " + jsonPortId); |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | final String jsonPortName = jsonNode.get("name").asText(); |
| 59 | if (!Objects.equals(jsonPortName, port.name())) { |
| 60 | description.appendText("Port name was " + jsonPortName); |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | final String jsonPortNetId = jsonNode.get("network_id").asText(); |
| 65 | if (!Objects.equals(jsonPortNetId, port.networkId().id())) { |
| 66 | description.appendText("Network id was " + jsonPortNetId); |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | final String jsonMacAddr = jsonNode.get("mac_address").asText(); |
| 71 | if (!Objects.equals(jsonMacAddr, port.mac().toString())) { |
| 72 | description.appendText("MAC address was " + jsonMacAddr); |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | final String jsonIpAddr = jsonNode.get("ip_address").asText(); |
| 77 | if (!Objects.equals(jsonIpAddr, port.ip().toString())) { |
| 78 | description.appendText("IP address was " + jsonIpAddr); |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | final String jsonVlanId = jsonNode.get("vlan_id").asText(); |
| 83 | if (!Objects.equals(jsonVlanId, port.vlanId().toString())) { |
| 84 | description.appendText("VLAN id was " + jsonVlanId); |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | final JsonNode jsonAddrPairs = jsonNode.get("floating_address_pairs"); |
| 89 | if (port.addressPairs().isEmpty()) { |
| 90 | if (jsonAddrPairs != null && |
| 91 | jsonAddrPairs != NullNode.getInstance() && |
| 92 | jsonAddrPairs.size() != 0) { |
| 93 | description.appendText("Floating address pairs did not match"); |
| 94 | return false; |
| 95 | } |
| 96 | } else { |
| 97 | if (jsonAddrPairs == null || |
| 98 | jsonAddrPairs == NullNode.getInstance() || |
| 99 | jsonAddrPairs.size() == 0) { |
| 100 | description.appendText("Floating address pairs was empty"); |
| 101 | return false; |
| 102 | } else if (jsonAddrPairs.size() != port.addressPairs().size()) { |
| 103 | description.appendText("Floating address pairs size was " + |
| 104 | jsonAddrPairs.size()); |
| 105 | return false; |
| 106 | } else { |
| 107 | for (JsonNode addrPair : jsonAddrPairs) { |
| 108 | final AddressPair tmp = AddressPair.of( |
| 109 | IpAddress.valueOf(addrPair.get("ip_address").asText()), |
| 110 | MacAddress.valueOf(addrPair.get("mac_address").asText()) |
| 111 | ); |
| 112 | if (!port.addressPairs().contains(tmp)) { |
| 113 | description.appendText("Floating address pairs did not match " + tmp); |
| 114 | return false; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void describeTo(Description description) { |
| 124 | description.appendText(port.toString()); |
| 125 | } |
| 126 | } |