blob: ea04c60745d2393fa508972a142a6c3fa87e5440 [file] [log] [blame]
Jian Li47e7af72021-03-05 01:32:04 +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.KubevirtSecurityGroup;
22import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupRule;
23
24/**
25 * Hamcrest matcher for security group.
26 */
27public final class KubevirtSecurityGroupJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
28
29 private static final String ID = "id";
30 private static final String NAME = "name";
31 private static final String DESCRIPTION = "description";
32 private static final String RULES = "rules";
33
34 private final KubevirtSecurityGroup sg;
35
36 private KubevirtSecurityGroupJsonMatcher(KubevirtSecurityGroup sg) {
37 this.sg = sg;
38 }
39
40 @Override
41 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
42 // check sg ID
43 String jsonId = jsonNode.get(ID).asText();
44 String id = sg.id();
45 if (!jsonId.equals(id)) {
46 description.appendText("ID was " + jsonId);
47 return false;
48 }
49
50 // check sg name
51 String jsonName = jsonNode.get(NAME).asText();
52 String name = sg.name();
53 if (!jsonName.equals(name)) {
54 description.appendText("Name was " + jsonName);
55 return false;
56 }
57
58 // check description
59 JsonNode jsonDescription = jsonNode.get(DESCRIPTION);
60 if (jsonDescription != null) {
61 String myDescription = sg.description();
62 if (!jsonDescription.asText().equals(myDescription)) {
63 description.appendText("Description was " + jsonDescription);
64 return false;
65 }
66 }
67
68 JsonNode jsonSgr = jsonNode.get(RULES);
69 if (jsonSgr != null) {
70 // check size of rule array
71 if (jsonSgr.size() != sg.rules().size()) {
72 description.appendText("Rules was " + jsonSgr.size());
73 return false;
74 }
75
76 // check rules
77 for (KubevirtSecurityGroupRule sgr : sg.rules()) {
78 boolean ruleFound = false;
79 for (int ruleIndex = 0; ruleIndex < jsonSgr.size(); ruleIndex++) {
80 KubevirtSecurityGroupRuleJsonMatcher ruleMatcher =
81 KubevirtSecurityGroupRuleJsonMatcher
82 .matchesKubevirtSecurityGroupRule(sgr);
83 if (ruleMatcher.matches(jsonSgr.get(ruleIndex))) {
84 ruleFound = true;
85 break;
86 }
87 }
88
89 if (!ruleFound) {
90 description.appendText("Rule not found " + sgr.toString());
91 return false;
92 }
93 }
94 }
95
96 return true;
97 }
98
99 @Override
100 public void describeTo(Description description) {
101 description.appendText(sg.toString());
102 }
103
104 /**
105 * Factory to allocate a kubevirt security group matcher.
106 *
107 * @param sg kubevirt security group object we are looking for
108 * @return matcher
109 */
110 public static KubevirtSecurityGroupJsonMatcher
111 matchesKubevirtSecurityGroup(KubevirtSecurityGroup sg) {
112 return new KubevirtSecurityGroupJsonMatcher(sg);
113 }
114}