blob: a855f42605f9396840a710f9784f32e17c4a3219 [file] [log] [blame]
Jian Li7eb20782021-02-27 01:10:50 +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 com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.hamcrest.Description;
22import org.hamcrest.TypeSafeDiagnosingMatcher;
23import org.onosproject.kubevirtnetworking.api.KubevirtRouter;
24
25/**
26 * Hamcrest matcher for kubevirt router interface.
27 */
28public final class KubevirtRouterJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
29
30 private final KubevirtRouter router;
31 private static final String NAME = "name";
32 private static final String DESCRIPTION = "description";
33 private static final String ENABLE_SNAT = "enableSnat";
34 private static final String INTERNAL = "internal";
35 private static final String EXTERNAL = "external";
36 private static final String PEER_ROUTER = "peerRouter";
37 private static final String IP_ADDRESS = "ip";
38 private static final String MAC_ADDRESS = "mac";
39 private static final String NETWORK = "network";
40
41 private KubevirtRouterJsonMatcher(KubevirtRouter router) {
42 this.router = router;
43 }
44
45 @Override
46 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
47
48 // check name
49 String jsonName = jsonNode.get(NAME).asText();
50 String name = router.name();
51 if (!jsonName.equals(name)) {
52 description.appendText("Name was " + jsonName);
53 return false;
54 }
55
56 // check description
57 JsonNode jsonDescription = jsonNode.get(DESCRIPTION);
58 if (jsonDescription != null) {
59 String myDescription = router.description();
60 if (!jsonDescription.asText().equals(myDescription)) {
61 description.appendText("Description was " + jsonDescription);
62 return false;
63 }
64 }
65
66 // check enable snat
67 JsonNode jsonEnableSnat = jsonNode.get(ENABLE_SNAT);
68 if (jsonEnableSnat != null) {
69 boolean enableSnat = router.enableSnat();
70 if (jsonEnableSnat.asBoolean() != enableSnat) {
71 description.appendText("EnableSNAT was " + jsonEnableSnat);
72 return false;
73 }
74 }
75
Jian Lic49017d2021-03-16 16:06:33 +090076 // check vrouter MAC
77 String jsonMac = jsonNode.get(MAC_ADDRESS).asText();
78 String mac = router.mac().toString();
79 if (!jsonMac.equals(mac)) {
80 description.appendText("MAC was " + jsonMac);
81 return false;
82 }
83
Jian Li7eb20782021-02-27 01:10:50 +090084 // check internal
85 JsonNode jsonInternal = jsonNode.get(INTERNAL);
86 if (jsonInternal != null) {
87 if (jsonInternal.size() != router.internal().size()) {
88 description.appendText("Internal networks size was " + jsonInternal.size());
89 return false;
90 }
91
92 for (int networkIndex = 0; networkIndex < jsonInternal.size(); networkIndex++) {
93 boolean networkFound = false;
94 String jsonNetwork = jsonInternal.get(networkIndex).asText();
95 if (router.internal().contains(jsonNetwork)) {
96 networkFound = true;
97 }
98
99 if (!networkFound) {
100 description.appendText("network not found " + jsonNetwork);
101 return false;
102 }
103 }
104 }
105
106 // check external
107 ArrayNode jsonExternal = (ArrayNode) jsonNode.get(EXTERNAL);
108 if (jsonExternal != null) {
109 if (jsonExternal.size() != router.external().size()) {
110 description.appendText("External networks size was " + jsonExternal.size());
111 return false;
112 }
113
114 for (int itemIndex = 0; itemIndex < jsonExternal.size(); itemIndex++) {
115 boolean itemFound = false;
116 ObjectNode jsonItem = (ObjectNode) jsonExternal.get(itemIndex);
117 String jsonIp = jsonItem.get(IP_ADDRESS).asText();
118 String jsonNetwork = jsonItem.get(NETWORK).asText();
119
120 if (router.external().containsKey(jsonIp)) {
121 if (router.external().get(jsonIp).equals(jsonNetwork)) {
122 itemFound = true;
123 }
124 }
125
126 if (!itemFound) {
127 description.appendText("External not found " + jsonItem.toString());
128 return false;
129 }
130 }
131 }
132
133 // check peer router
134 ObjectNode jsonPeerRouter = (ObjectNode) jsonNode.get(PEER_ROUTER);
135 if (jsonPeerRouter != null) {
136 JsonNode jsonIp = jsonPeerRouter.get(IP_ADDRESS);
137
138 if (jsonIp != null) {
139 if (!jsonIp.asText().equals(router.peerRouter().ipAddress().toString())) {
140 description.appendText("Peer router IP was " + jsonIp);
141 return false;
142 }
143 }
144
Jian Lic49017d2021-03-16 16:06:33 +0900145 JsonNode jsonProuterMac = jsonPeerRouter.get(MAC_ADDRESS);
Jian Li7eb20782021-02-27 01:10:50 +0900146
Jian Lic49017d2021-03-16 16:06:33 +0900147 if (jsonProuterMac != null) {
148 if (!jsonProuterMac.asText().equals(router.peerRouter().macAddress().toString())) {
Jian Li7eb20782021-02-27 01:10:50 +0900149 description.appendText("Peer router MAC was " + jsonMac);
150 return false;
151 }
152 }
153 }
154
155 return true;
156 }
157
158 @Override
159 public void describeTo(Description description) {
160 description.appendText(router.toString());
161 }
162
163 /**
164 * Factory to allocate a kubevirt router matcher.
165 *
166 * @param router kubevirt router object we are looking for
167 * @return matcher
168 */
169 public static KubevirtRouterJsonMatcher matchesKubevirtRouter(KubevirtRouter router) {
170 return new KubevirtRouterJsonMatcher(router);
171 }
172}