blob: 215e289b4ea99077c636a64340051030e4ddea82 [file] [log] [blame]
Jian Lifeb84802021-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;
21import org.onosproject.kubevirtnetworking.api.KubevirtHostRoute;
22import org.onosproject.kubevirtnetworking.api.KubevirtIpPool;
23import org.onosproject.kubevirtnetworking.api.KubevirtNetwork;
24
25/**
26 * Hamcrest matcher for kubevirt network.
27 */
28public final class KubevirtNetworkJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
29
30 private final KubevirtNetwork network;
31 private static final String NETWORK_ID = "networkId";
32 private static final String TYPE = "type";
33 private static final String NAME = "name";
34 private static final String MTU = "mtu";
35 private static final String SEGMENT_ID = "segmentId";
36 private static final String GATEWAY_IP = "gatewayIp";
37 private static final String CIDR = "cidr";
38 private static final String HOST_ROUTES = "hostRoutes";
39 private static final String IP_POOL = "ipPool";
40
41 private KubevirtNetworkJsonMatcher(KubevirtNetwork network) {
42 this.network = network;
43 }
44
45 @Override
46 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
47 // check network ID
48 String jsonNetworkId = jsonNode.get(NETWORK_ID).asText();
49 String networkId = network.networkId();
50 if (!jsonNetworkId.equals(networkId)) {
51 description.appendText("network ID was " + jsonNetworkId);
52 return false;
53 }
54
55 // check type
56 String jsonType = jsonNode.get(TYPE).asText();
57 String type = network.type().name();
58 if (!jsonType.equals(type)) {
59 description.appendText("network type was " + jsonType);
60 return false;
61 }
62
63 // check name
64 String jsonName = jsonNode.get(NAME).asText();
65 String name = network.name();
66 if (!jsonName.equals(name)) {
67 description.appendText("network name was " + jsonName);
68 return false;
69 }
70
71 // check MTU
72 int jsonMtu = jsonNode.get(MTU).asInt();
73 int mtu = network.mtu();
74 if (jsonMtu != mtu) {
75 description.appendText("network MTU was " + jsonMtu);
76 return false;
77 }
78
79 // check gateway IP
80 String jsonGatewayIp = jsonNode.get(GATEWAY_IP).asText();
81 String gatewayIp = network.gatewayIp().toString();
82 if (!jsonGatewayIp.equals(gatewayIp)) {
83 description.appendText("gateway IP was " + jsonGatewayIp);
84 return false;
85 }
86
87 // check CIDR
88 String jsonCidr = jsonNode.get(CIDR).asText();
89 String cidr = network.cidr();
90 if (!jsonCidr.equals(cidr)) {
91 description.appendText("CIDR was " + jsonCidr);
92 return false;
93 }
94
95 // check segment ID
96 JsonNode jsonSegmentId = jsonNode.get(SEGMENT_ID);
97 if (jsonSegmentId != null) {
98 String segmentId = network.segmentId();
99 if (!jsonSegmentId.asText().equals(segmentId)) {
100 description.appendText("segment ID was " + jsonSegmentId.asText());
101 return false;
102 }
103 }
104
105 // check ip pool
106 JsonNode jsonIpPool = jsonNode.get(IP_POOL);
107 if (jsonIpPool != null) {
108 KubevirtIpPool ipPool = network.ipPool();
109 KubevirtIpPoolJsonMatcher ipPoolMatcher =
110 KubevirtIpPoolJsonMatcher.matchesKubevirtIpPool(ipPool);
111 if (ipPoolMatcher.matches(jsonIpPool)) {
112 return true;
113 } else {
114 description.appendText("IP pool was " + jsonIpPool.toString());
115 return false;
116 }
117 }
118
119 // check host routes
120 JsonNode jsonHostRoutes = jsonNode.get(HOST_ROUTES);
121 if (jsonHostRoutes != null) {
122 if (jsonHostRoutes.size() != network.hostRoutes().size()) {
123 description.appendText("host routes size was " + jsonHostRoutes.size());
124 return false;
125 }
126
127 for (KubevirtHostRoute hostRoute : network.hostRoutes()) {
128 boolean routeFound = false;
129 for (int routeIndex = 0; routeIndex < jsonHostRoutes.size(); routeIndex++) {
130 KubevirtHostRouteJsonMatcher routeMatcher =
131 KubevirtHostRouteJsonMatcher.matchesKubevirtHostRoute(hostRoute);
132 if (routeMatcher.matches(jsonHostRoutes.get(routeIndex))) {
133 routeFound = true;
134 break;
135 }
136 }
137
138 if (!routeFound) {
139 description.appendText("Host route not found " + hostRoute.toString());
140 return false;
141 }
142 }
143 }
144
145 return true;
146 }
147
148 @Override
149 public void describeTo(Description description) {
150 description.appendText(network.toString());
151 }
152
153 /**
154 * Factory to allocate an kubevirt network matcher.
155 *
156 * @param network kubevirt network object we are looking for
157 * @return matcher
158 */
159 public static KubevirtNetworkJsonMatcher
160 matchesKubevirtNetwork(KubevirtNetwork network) {
161 return new KubevirtNetworkJsonMatcher(network);
162 }
163}