Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 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 | */ |
Jian Li | 2e818b0 | 2017-04-12 19:28:30 +0900 | [diff] [blame] | 16 | package org.onosproject.mapping.codec; |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.hamcrest.Description; |
| 20 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 21 | import org.onosproject.mapping.addresses.ASMappingAddress; |
| 22 | import org.onosproject.mapping.addresses.DNMappingAddress; |
| 23 | import org.onosproject.mapping.addresses.EthMappingAddress; |
| 24 | import org.onosproject.mapping.addresses.IPMappingAddress; |
| 25 | import org.onosproject.mapping.addresses.MappingAddress; |
| 26 | |
| 27 | /** |
| 28 | * Hamcrest matcher for mapping objects. |
| 29 | */ |
| 30 | public final class MappingAddressJsonMatcher extends |
| 31 | TypeSafeDiagnosingMatcher<JsonNode> { |
| 32 | |
| 33 | private final MappingAddress address; |
| 34 | private Description description; |
| 35 | private JsonNode node; |
| 36 | |
| 37 | /** |
| 38 | * Constructs a matcher object. |
| 39 | * |
| 40 | * @param address mapping address to match |
| 41 | */ |
| 42 | private MappingAddressJsonMatcher(MappingAddress address) { |
| 43 | this.address = address; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Factory to allocate a mapping address matcher. |
| 48 | * |
| 49 | * @param address mapping address object we are looking for |
| 50 | * @return matcher |
| 51 | */ |
| 52 | public static MappingAddressJsonMatcher matchesMappingAddress(MappingAddress address) { |
| 53 | return new MappingAddressJsonMatcher(address); |
| 54 | } |
| 55 | |
| 56 | /** |
Hyunsun Moon | c8d8fc7 | 2017-04-12 14:50:04 +0900 | [diff] [blame] | 57 | * Matches an AS mapping address object. |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 58 | * |
| 59 | * @param address mapping address to match |
| 60 | * @return true if the JSON matches the mapping address, false otherwise |
| 61 | */ |
| 62 | private boolean matchMappingAddress(ASMappingAddress address) { |
| 63 | final String as = address.asNumber(); |
| 64 | final String jsonAs = node.get(MappingAddressCodec.AS).textValue(); |
| 65 | if (!as.equals(jsonAs)) { |
| 66 | description.appendText("AS was " + jsonAs); |
| 67 | return false; |
| 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Matches a Distinguished Name mapping address object. |
| 74 | * |
| 75 | * @param address mapping address to match |
| 76 | * @return true if the JSON matches the mapping address, false otherwise |
| 77 | */ |
| 78 | private boolean matchMappingAddress(DNMappingAddress address) { |
| 79 | final String dn = address.name(); |
| 80 | final String jsonDn = node.get(MappingAddressCodec.DN).textValue(); |
| 81 | if (!dn.equals(jsonDn)) { |
| 82 | description.appendText("Distinguished Name was " + jsonDn); |
| 83 | return false; |
| 84 | } |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Matches an IP mapping address object. |
| 90 | * |
| 91 | * @param address mapping address to match |
| 92 | * @return true if the JSON matches the mapping address, false otherwise |
| 93 | */ |
| 94 | private boolean matchMappingAddress(IPMappingAddress address) { |
| 95 | final String ip = address.ip().toString(); |
| 96 | String jsonIp = null; |
| 97 | if (address.type() == MappingAddress.Type.IPV4) { |
| 98 | jsonIp = node.get(MappingAddressCodec.IPV4).textValue(); |
| 99 | |
| 100 | } else if (address.type() == MappingAddress.Type.IPV6) { |
| 101 | jsonIp = node.get(MappingAddressCodec.IPV6).textValue(); |
| 102 | } |
| 103 | if (!ip.equals(jsonIp)) { |
| 104 | description.appendText("IP was " + jsonIp); |
| 105 | return false; |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Matches a MAC mapping address object. |
| 112 | * |
| 113 | * @param address mapping address to match |
| 114 | * @return true if the JSON matches the mapping address, false otherwise |
| 115 | */ |
| 116 | private boolean matchMappingAddress(EthMappingAddress address) { |
| 117 | final String mac = address.mac().toString(); |
| 118 | final String jsonMac = node.get(MappingAddressCodec.MAC).textValue(); |
| 119 | if (!mac.equals(jsonMac)) { |
| 120 | description.appendText("MAC was " + jsonMac); |
| 121 | return false; |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 128 | |
| 129 | this.description = description; |
| 130 | this.node = jsonNode; |
| 131 | final String type = address.type().name(); |
| 132 | final String jsonType = jsonNode.get(MappingAddressCodec.TYPE).asText(); |
| 133 | if (!type.equals(jsonType)) { |
| 134 | description.appendText("type was " + type); |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | switch (address.type()) { |
| 139 | |
| 140 | case IPV4: |
| 141 | case IPV6: |
| 142 | return matchMappingAddress((IPMappingAddress) address); |
| 143 | |
| 144 | case AS: |
| 145 | return matchMappingAddress((ASMappingAddress) address); |
| 146 | |
| 147 | case DN: |
| 148 | return matchMappingAddress((DNMappingAddress) address); |
| 149 | |
| 150 | case ETH: |
| 151 | return matchMappingAddress((EthMappingAddress) address); |
| 152 | |
| 153 | default: |
| 154 | // Don't know how to format this type |
Jian Li | cb42c31 | 2017-03-30 16:20:33 +0900 | [diff] [blame] | 155 | description.appendText("unknown mapping address type " + address.type()); |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 156 | return false; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void describeTo(Description description) { |
| 162 | description.appendText(address.toString()); |
| 163 | } |
| 164 | } |