blob: 7cbc587cc6eafdf9417350e75d362a6a66d3459f [file] [log] [blame]
Jian Lifc0b8902021-01-12 16:34:49 +09001/*
2 * Copyright 2021-present Open Networking Foundation
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.onosproject.kubevirtnetworking.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
Jian Li4c35a262021-02-01 20:36:45 +090021import org.onlab.packet.IpAddress;
Jian Lifc0b8902021-01-12 16:34:49 +090022import org.onosproject.kubevirtnetworking.api.KubevirtHostRoute;
23import org.onosproject.kubevirtnetworking.api.KubevirtIpPool;
24import org.onosproject.kubevirtnetworking.api.KubevirtNetwork;
25
26/**
27 * Hamcrest matcher for kubevirt network.
28 */
29public final class KubevirtNetworkJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
30
31 private final KubevirtNetwork network;
32 private static final String NETWORK_ID = "networkId";
33 private static final String TYPE = "type";
34 private static final String NAME = "name";
35 private static final String MTU = "mtu";
36 private static final String SEGMENT_ID = "segmentId";
37 private static final String GATEWAY_IP = "gatewayIp";
Jian Lie2abe812021-08-12 18:03:30 +090038 private static final String DEFAULT_ROUTE = "defaultRoute";
Jian Lifc0b8902021-01-12 16:34:49 +090039 private static final String CIDR = "cidr";
40 private static final String HOST_ROUTES = "hostRoutes";
41 private static final String IP_POOL = "ipPool";
Jian Li4c35a262021-02-01 20:36:45 +090042 private static final String DNSES = "dnses";
Jian Lifc0b8902021-01-12 16:34:49 +090043
44 private KubevirtNetworkJsonMatcher(KubevirtNetwork network) {
45 this.network = network;
46 }
47
48 @Override
49 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
50 // check network ID
51 String jsonNetworkId = jsonNode.get(NETWORK_ID).asText();
52 String networkId = network.networkId();
53 if (!jsonNetworkId.equals(networkId)) {
54 description.appendText("network ID was " + jsonNetworkId);
55 return false;
56 }
57
58 // check type
59 String jsonType = jsonNode.get(TYPE).asText();
60 String type = network.type().name();
61 if (!jsonType.equals(type)) {
62 description.appendText("network type was " + jsonType);
63 return false;
64 }
65
66 // check name
67 String jsonName = jsonNode.get(NAME).asText();
68 String name = network.name();
69 if (!jsonName.equals(name)) {
70 description.appendText("network name was " + jsonName);
71 return false;
72 }
73
74 // check MTU
75 int jsonMtu = jsonNode.get(MTU).asInt();
76 int mtu = network.mtu();
77 if (jsonMtu != mtu) {
78 description.appendText("network MTU was " + jsonMtu);
79 return false;
80 }
81
82 // check gateway IP
83 String jsonGatewayIp = jsonNode.get(GATEWAY_IP).asText();
84 String gatewayIp = network.gatewayIp().toString();
85 if (!jsonGatewayIp.equals(gatewayIp)) {
86 description.appendText("gateway IP was " + jsonGatewayIp);
87 return false;
88 }
89
Jian Lie2abe812021-08-12 18:03:30 +090090 // check default route
91 boolean jsonDefaultRoute = jsonNode.get(DEFAULT_ROUTE).asBoolean();
92 boolean defaultRoute = network.defaultRoute();
93 if (jsonDefaultRoute != defaultRoute) {
94 description.appendText("Default route was " + jsonDefaultRoute);
95 return false;
96 }
97
Jian Lifc0b8902021-01-12 16:34:49 +090098 // check CIDR
99 String jsonCidr = jsonNode.get(CIDR).asText();
100 String cidr = network.cidr();
101 if (!jsonCidr.equals(cidr)) {
102 description.appendText("CIDR was " + jsonCidr);
103 return false;
104 }
105
106 // check segment ID
107 JsonNode jsonSegmentId = jsonNode.get(SEGMENT_ID);
108 if (jsonSegmentId != null) {
109 String segmentId = network.segmentId();
110 if (!jsonSegmentId.asText().equals(segmentId)) {
111 description.appendText("segment ID was " + jsonSegmentId.asText());
112 return false;
113 }
114 }
115
116 // check ip pool
117 JsonNode jsonIpPool = jsonNode.get(IP_POOL);
118 if (jsonIpPool != null) {
119 KubevirtIpPool ipPool = network.ipPool();
120 KubevirtIpPoolJsonMatcher ipPoolMatcher =
121 KubevirtIpPoolJsonMatcher.matchesKubevirtIpPool(ipPool);
122 if (ipPoolMatcher.matches(jsonIpPool)) {
123 return true;
124 } else {
125 description.appendText("IP pool was " + jsonIpPool.toString());
126 return false;
127 }
128 }
129
130 // check host routes
131 JsonNode jsonHostRoutes = jsonNode.get(HOST_ROUTES);
132 if (jsonHostRoutes != null) {
133 if (jsonHostRoutes.size() != network.hostRoutes().size()) {
134 description.appendText("host routes size was " + jsonHostRoutes.size());
135 return false;
136 }
137
138 for (KubevirtHostRoute hostRoute : network.hostRoutes()) {
139 boolean routeFound = false;
140 for (int routeIndex = 0; routeIndex < jsonHostRoutes.size(); routeIndex++) {
141 KubevirtHostRouteJsonMatcher routeMatcher =
142 KubevirtHostRouteJsonMatcher.matchesKubevirtHostRoute(hostRoute);
143 if (routeMatcher.matches(jsonHostRoutes.get(routeIndex))) {
144 routeFound = true;
145 break;
146 }
147 }
148
149 if (!routeFound) {
150 description.appendText("Host route not found " + hostRoute.toString());
151 return false;
152 }
153 }
154 }
155
Jian Li4c35a262021-02-01 20:36:45 +0900156 // check dnses
157 JsonNode jsonDnses = jsonNode.get(DNSES);
158 if (jsonDnses != null) {
159 if (jsonDnses.size() != network.dnses().size()) {
160 description.appendText("DNSes size was " + jsonDnses.size());
161 return false;
162 }
163
164
165 for (IpAddress dns : network.dnses()) {
166 boolean dnsFound = false;
167 for (int dnsIndex = 0; dnsIndex < jsonDnses.size(); dnsIndex++) {
168 String jsonDns = jsonDnses.get(dnsIndex).asText();
169 if (jsonDns.equals(dns.toString())) {
170 dnsFound = true;
171 break;
172 }
173 }
174
175 if (!dnsFound) {
176 description.appendText("DNS not found " + dns.toString());
177 return false;
178 }
179 }
180 }
181
Jian Lifc0b8902021-01-12 16:34:49 +0900182 return true;
183 }
184
185 @Override
186 public void describeTo(Description description) {
187 description.appendText(network.toString());
188 }
189
190 /**
191 * Factory to allocate an kubevirt network matcher.
192 *
193 * @param network kubevirt network object we are looking for
194 * @return matcher
195 */
196 public static KubevirtNetworkJsonMatcher
197 matchesKubevirtNetwork(KubevirtNetwork network) {
198 return new KubevirtNetworkJsonMatcher(network);
199 }
200}