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; |
| 22 | import org.opencord.cordvtn.api.net.ProviderNetwork; |
| 23 | import org.opencord.cordvtn.api.net.ServiceNetwork; |
| 24 | |
| 25 | import java.util.Objects; |
| 26 | |
| 27 | import static org.opencord.cordvtn.api.dependency.Dependency.Type.BIDIRECTIONAL; |
| 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 | |
| 64 | if (network.providers().isEmpty()) { |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | JsonNode jsonProviders = jsonNet.get("providerNetworks"); |
| 69 | if (jsonProviders == null || jsonProviders == NullNode.getInstance()) { |
| 70 | description.appendText("provider networks were empty"); |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | if (jsonProviders.size() != network.providers().size()) { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | for (JsonNode provider : jsonProviders) { |
| 79 | String id = provider.get("id").asText(); |
| 80 | boolean bidirectional = provider.get("bidirectional").asBoolean(); |
| 81 | ProviderNetwork proNet = network.providers().stream() |
| 82 | .filter(p -> p.id().id().equals(id)) |
| 83 | .findAny().orElse(null); |
| 84 | |
| 85 | if (proNet == null) { |
| 86 | final String msg = String.format("provider id:%s couldn't find", id); |
| 87 | description.appendText(msg); |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | if (proNet.type().equals(BIDIRECTIONAL) != bidirectional) { |
| 92 | final String msg = String.format( |
| 93 | "mismatch provider id:%s, bidirectional: %s", |
| 94 | id, bidirectional); |
| 95 | description.appendText(msg); |
| 96 | return false; |
| 97 | } |
| 98 | } |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void describeTo(Description description) { |
| 104 | description.appendText(network.toString()); |
| 105 | } |
| 106 | } |