blob: da321e306ac44e83f14ac7b3e3ad3e4b0ccd2b65 [file] [log] [blame]
Hyunsun Moonf5b21532016-10-25 15:45:41 -07001/*
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 */
16package org.opencord.cordvtn.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.NullNode;
20import org.hamcrest.Description;
21import org.hamcrest.TypeSafeDiagnosingMatcher;
Hyunsun Moon187bf532017-01-19 10:57:40 +090022import org.opencord.cordvtn.api.net.NetworkId;
Hyunsun Moonf5b21532016-10-25 15:45:41 -070023import org.opencord.cordvtn.api.net.ServiceNetwork;
24
25import java.util.Objects;
26
Hyunsun Moon187bf532017-01-19 10:57:40 +090027import static org.opencord.cordvtn.api.net.ServiceNetwork.DependencyType.BIDIRECTIONAL;
Hyunsun Moonf5b21532016-10-25 15:45:41 -070028
29/**
30 * Json matcher for ServiceNetwork.
31 */
32public 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 Moon187bf532017-01-19 10:57:40 +090064 if (network.providers() == null || network.providers().isEmpty()) {
Hyunsun Moonf5b21532016-10-25 15:45:41 -070065 return true;
66 }
67
Hyunsun Moon187bf532017-01-19 10:57:40 +090068 JsonNode jsonProviders = jsonNet.get("providers");
Hyunsun Moonf5b21532016-10-25 15:45:41 -070069 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) {
Hyunsun Moon187bf532017-01-19 10:57:40 +090079 NetworkId id = NetworkId.of(provider.get("id").asText());
Hyunsun Moonf5b21532016-10-25 15:45:41 -070080 boolean bidirectional = provider.get("bidirectional").asBoolean();
Hyunsun Moonf5b21532016-10-25 15:45:41 -070081
Hyunsun Moon187bf532017-01-19 10:57:40 +090082 if (!network.providers().containsKey(id)) {
Hyunsun Moonf5b21532016-10-25 15:45:41 -070083 final String msg = String.format("provider id:%s couldn't find", id);
84 description.appendText(msg);
85 return false;
86 }
87
Hyunsun Moon187bf532017-01-19 10:57:40 +090088 if (network.providers().get(id).equals(BIDIRECTIONAL) != bidirectional) {
Hyunsun Moonf5b21532016-10-25 15:45:41 -070089 final String msg = String.format(
90 "mismatch provider id:%s, bidirectional: %s",
91 id, bidirectional);
92 description.appendText(msg);
93 return false;
94 }
95 }
96 return true;
97 }
98
99 @Override
100 public void describeTo(Description description) {
101 description.appendText(network.toString());
102 }
103}