Hyunsun Moon | f5b2153 | 2016-10-25 15:45:41 -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.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.NullNode; |
| 20 | import org.hamcrest.Description; |
| 21 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 22 | import org.opencord.cordvtn.api.net.NetworkId; |
Hyunsun Moon | f5b2153 | 2016-10-25 15:45:41 -0700 | [diff] [blame] | 23 | import org.opencord.cordvtn.api.net.ServiceNetwork; |
| 24 | |
| 25 | import java.util.Objects; |
| 26 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 27 | import static org.opencord.cordvtn.api.net.ServiceNetwork.DependencyType.BIDIRECTIONAL; |
Hyunsun Moon | f5b2153 | 2016-10-25 15:45:41 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Json matcher for ServiceNetwork. |
| 31 | */ |
| 32 | public final class ServiceNetworkJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 33 | |
| 34 | private final ServiceNetwork network; |
| 35 | |
| 36 | private ServiceNetworkJsonMatcher(ServiceNetwork network) { |
| 37 | this.network = network; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Factory to allocate ServiceNetwork matcher. |
| 42 | * |
| 43 | * @param network service network object to match |
| 44 | * @return matcher |
| 45 | */ |
| 46 | public static ServiceNetworkJsonMatcher matchesServiceNetwork(ServiceNetwork network) { |
| 47 | return new ServiceNetworkJsonMatcher(network); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | protected boolean matchesSafely(JsonNode jsonNet, Description description) { |
| 52 | String jsonNetId = jsonNet.get("id").asText(); |
| 53 | if (!Objects.equals(jsonNetId, network.id().id())) { |
| 54 | description.appendText("network id was " + jsonNetId); |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | String jsonType = jsonNet.get("type").asText().toUpperCase(); |
| 59 | if (!Objects.equals(jsonType, network.type().name())) { |
| 60 | description.appendText("type was " + jsonType); |
| 61 | return false; |
| 62 | } |
| 63 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 64 | if (network.providers() == null || network.providers().isEmpty()) { |
Hyunsun Moon | f5b2153 | 2016-10-25 15:45:41 -0700 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 68 | JsonNode jsonProviders = jsonNet.get("providers"); |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 69 | if (network.providers().isEmpty()) { |
| 70 | if (jsonProviders != null && |
| 71 | jsonProviders != NullNode.getInstance() && |
| 72 | jsonProviders.size() != 0) { |
| 73 | description.appendText("provider networks did not match"); |
Hyunsun Moon | f5b2153 | 2016-10-25 15:45:41 -0700 | [diff] [blame] | 74 | return false; |
| 75 | } |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 76 | } else { |
| 77 | if (jsonProviders == null || |
| 78 | jsonProviders == NullNode.getInstance() || |
| 79 | jsonProviders.size() == 0) { |
| 80 | description.appendText("provider networks did not match"); |
Hyunsun Moon | f5b2153 | 2016-10-25 15:45:41 -0700 | [diff] [blame] | 81 | return false; |
Hyunsun Moon | af3822c | 2017-04-04 15:56:27 +0900 | [diff] [blame] | 82 | } else if (jsonProviders.size() != network.providers().size()) { |
| 83 | description.appendText("provider networks did not match"); |
| 84 | return false; |
| 85 | } else { |
| 86 | for (JsonNode provider : jsonProviders) { |
| 87 | NetworkId id = NetworkId.of(provider.get("id").asText()); |
| 88 | boolean bidirectional = provider.get("bidirectional").asBoolean(); |
| 89 | |
| 90 | if (!network.providers().containsKey(id)) { |
| 91 | final String msg = String.format("provider id:%s couldn't find", id); |
| 92 | description.appendText(msg); |
| 93 | return false; |
| 94 | } |
| 95 | if (network.providers().get(id).equals(BIDIRECTIONAL) != bidirectional) { |
| 96 | final String msg = String.format( |
| 97 | "mismatch provider id:%s, bidirectional: %s", |
| 98 | id, bidirectional); |
| 99 | description.appendText(msg); |
| 100 | return false; |
| 101 | } |
| 102 | } |
Hyunsun Moon | f5b2153 | 2016-10-25 15:45:41 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void describeTo(Description description) { |
| 110 | description.appendText(network.toString()); |
| 111 | } |
| 112 | } |