blob: 9122bd08283f62517925ff032e1989288424fad4 [file] [log] [blame]
Jian Lidaa7d6a2021-04-13 17:22:56 +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 org.hamcrest.Description;
21import org.hamcrest.TypeSafeDiagnosingMatcher;
22import org.onlab.packet.IpAddress;
23import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancer;
24import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerRule;
25
26/**
27 * Hamcrest matcher for load balancer.
28 */
29public final class KubevirtLoadBalancerJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
30
31 private static final String NAME = "name";
32 private static final String DESCRIPTION = "description";
33 private static final String VIP = "vip";
34 private static final String NETWORK_ID = "networkId";
35 private static final String MEMBERS = "members";
36 private static final String RULES = "rules";
37
38 private final KubevirtLoadBalancer lb;
39
40 private KubevirtLoadBalancerJsonMatcher(KubevirtLoadBalancer lb) {
41 this.lb = lb;
42 }
43
44 @Override
45 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
46 // check name
47 String jsonName = jsonNode.get(NAME).asText();
48 String name = lb.name();
49 if (!jsonName.equals(name)) {
50 description.appendText("Name was " + jsonName);
51 return false;
52 }
53
54 // check description
55 JsonNode jsonDescription = jsonNode.get(DESCRIPTION);
56 if (jsonDescription != null) {
57 String myDescription = lb.description();
58 if (!jsonDescription.asText().equals(myDescription)) {
59 description.appendText("Description was " + jsonDescription);
60 return false;
61 }
62 }
63
64 // check VIP
65 String jsonVip = jsonNode.get(VIP).asText();
66 String vip = lb.vip().toString();
67 if (!jsonVip.equals(vip)) {
68 description.appendText("VIP was " + jsonVip);
69 return false;
70 }
71
72 // check network ID
73 String jsonNetworkId = jsonNode.get(NETWORK_ID).asText();
74 String networkId = lb.networkId();
75 if (!jsonNetworkId.equals(networkId)) {
76 description.appendText("NetworkId was " + jsonNetworkId);
77 return false;
78 }
79
80 // check members
81 ArrayNode jsonMembers = (ArrayNode) jsonNode.get(MEMBERS);
82 if (jsonMembers != null) {
83 // check size of members array
84 if (jsonMembers.size() != lb.members().size()) {
85 description.appendText("Members was " + jsonMembers.size());
86 return false;
87 }
88
89 for (JsonNode jsonMember : jsonMembers) {
90 IpAddress member = IpAddress.valueOf(jsonMember.asText());
91 if (!lb.members().contains(member)) {
92 description.appendText("Member not found " + jsonMember.toString());
93 return false;
94 }
95 }
96 }
97
98 ArrayNode jsonRules = (ArrayNode) jsonNode.get(RULES);
99 if (jsonRules != null) {
100 // check size of rules array
101 if (jsonRules.size() != lb.rules().size()) {
102 description.appendText("Rules was " + jsonRules.size());
103 return false;
104 }
105
106 // check rules
107 for (KubevirtLoadBalancerRule rule : lb.rules()) {
108 boolean ruleFound = false;
109 for (int ruleIndex = 0; ruleIndex < jsonRules.size(); ruleIndex++) {
110 KubevirtLoadBalancerRuleJsonMatcher ruleMatcher =
111 KubevirtLoadBalancerRuleJsonMatcher
112 .matchesKubevirtLoadBalancerRule(rule);
113 if (ruleMatcher.matches(jsonRules.get(ruleIndex))) {
114 ruleFound = true;
115 break;
116 }
117 }
118
119 if (!ruleFound) {
120 description.appendText("Rule not found " + rule.toString());
121 return false;
122 }
123 }
124 }
125
126 return true;
127 }
128
129 @Override
130 public void describeTo(Description description) {
131 description.appendText(lb.toString());
132 }
133
134 /**
135 * Factory to allocate a kubevirt load balancer matcher.
136 *
137 * @param lb kubevirt load balancer object we are looking for
138 * @return matcher
139 */
140 public static KubevirtLoadBalancerJsonMatcher
141 matchesKubevirtLoadBalancer(KubevirtLoadBalancer lb) {
142 return new KubevirtLoadBalancerJsonMatcher(lb);
143 }
144}