blob: 1b394e7170dbdd55b25897cb09da0bde907e3cd1 [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
76 // check internal
77 JsonNode jsonInternal = jsonNode.get(INTERNAL);
78 if (jsonInternal != null) {
79 if (jsonInternal.size() != router.internal().size()) {
80 description.appendText("Internal networks size was " + jsonInternal.size());
81 return false;
82 }
83
84 for (int networkIndex = 0; networkIndex < jsonInternal.size(); networkIndex++) {
85 boolean networkFound = false;
86 String jsonNetwork = jsonInternal.get(networkIndex).asText();
87 if (router.internal().contains(jsonNetwork)) {
88 networkFound = true;
89 }
90
91 if (!networkFound) {
92 description.appendText("network not found " + jsonNetwork);
93 return false;
94 }
95 }
96 }
97
98 // check external
99 ArrayNode jsonExternal = (ArrayNode) jsonNode.get(EXTERNAL);
100 if (jsonExternal != null) {
101 if (jsonExternal.size() != router.external().size()) {
102 description.appendText("External networks size was " + jsonExternal.size());
103 return false;
104 }
105
106 for (int itemIndex = 0; itemIndex < jsonExternal.size(); itemIndex++) {
107 boolean itemFound = false;
108 ObjectNode jsonItem = (ObjectNode) jsonExternal.get(itemIndex);
109 String jsonIp = jsonItem.get(IP_ADDRESS).asText();
110 String jsonNetwork = jsonItem.get(NETWORK).asText();
111
112 if (router.external().containsKey(jsonIp)) {
113 if (router.external().get(jsonIp).equals(jsonNetwork)) {
114 itemFound = true;
115 }
116 }
117
118 if (!itemFound) {
119 description.appendText("External not found " + jsonItem.toString());
120 return false;
121 }
122 }
123 }
124
125 // check peer router
126 ObjectNode jsonPeerRouter = (ObjectNode) jsonNode.get(PEER_ROUTER);
127 if (jsonPeerRouter != null) {
128 JsonNode jsonIp = jsonPeerRouter.get(IP_ADDRESS);
129
130 if (jsonIp != null) {
131 if (!jsonIp.asText().equals(router.peerRouter().ipAddress().toString())) {
132 description.appendText("Peer router IP was " + jsonIp);
133 return false;
134 }
135 }
136
137 JsonNode jsonMac = jsonPeerRouter.get(MAC_ADDRESS);
138
139 if (jsonMac != null) {
140 if (!jsonMac.asText().equals(router.peerRouter().macAddress().toString())) {
141 description.appendText("Peer router MAC was " + jsonMac);
142 return false;
143 }
144 }
145 }
146
147 return true;
148 }
149
150 @Override
151 public void describeTo(Description description) {
152 description.appendText(router.toString());
153 }
154
155 /**
156 * Factory to allocate a kubevirt router matcher.
157 *
158 * @param router kubevirt router object we are looking for
159 * @return matcher
160 */
161 public static KubevirtRouterJsonMatcher matchesKubevirtRouter(KubevirtRouter router) {
162 return new KubevirtRouterJsonMatcher(router);
163 }
164}