blob: f5a484b464ef2b27c1cdd12dc3f412bee8b0e62d [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 Moonaf3822c2017-04-04 15:56:27 +090069 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 Moonf5b21532016-10-25 15:45:41 -070074 return false;
75 }
Hyunsun Moonaf3822c2017-04-04 15:56:27 +090076 } else {
77 if (jsonProviders == null ||
78 jsonProviders == NullNode.getInstance() ||
79 jsonProviders.size() == 0) {
80 description.appendText("provider networks did not match");
Hyunsun Moonf5b21532016-10-25 15:45:41 -070081 return false;
Hyunsun Moonaf3822c2017-04-04 15:56:27 +090082 } 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 Moonf5b21532016-10-25 15:45:41 -0700103 }
104 }
105 return true;
106 }
107
108 @Override
109 public void describeTo(Description description) {
110 description.appendText(network.toString());
111 }
112}