blob: fd98a1342968f317c4efce6a673b34c1f356878e [file] [log] [blame]
Daniel Parkd02d7bd2018-08-23 23:04:31 +09001/*
2 * Copyright 2018-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.openstacknode.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
21import org.onosproject.openstacknode.api.DpdkConfig;
22import org.onosproject.openstacknode.api.DpdkConfig.DatapathType;
23import org.onosproject.openstacknode.api.DpdkInterface;
24
25/**
26 * Hamcrest matcher for dpdk config.
27 */
28public final class DpdkConfigJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
29 private final DpdkConfig dpdkConfig;
30
31 private static final String DATA_PATH_TYPE = "datapathType";
32 private static final String SOCKET_DIR = "socketDir";
33 private static final String DPDK_INTFS = "dpdkIntfs";
34
35 private DpdkConfigJsonMatcher(DpdkConfig dpdkConfig) {
36 this.dpdkConfig = dpdkConfig;
37 }
38
39 @Override
40 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
41
42 // check datapath type
43 DatapathType jsonDatapathType = DatapathType.valueOf(
44 jsonNode.get(DATA_PATH_TYPE).asText().toUpperCase());
45 DatapathType datapathType = dpdkConfig.datapathType();
46
47 if (!jsonDatapathType.equals(datapathType)) {
48 description.appendText("datapath type was " + jsonDatapathType.name());
49 return false;
50 }
51
52 // check socket directory
53 JsonNode jsonSocketDir = jsonNode.get(SOCKET_DIR);
54 if (jsonSocketDir != null) {
55 String socketDir = dpdkConfig.socketDir();
56
57 if (!jsonSocketDir.asText().equals(socketDir)) {
58 description.appendText("socketDir was " + jsonSocketDir);
59 return false;
60 }
61 }
62
63 // check dpdk interfaces
64 JsonNode jsonDpdkintfs = jsonNode.get(DPDK_INTFS);
65 if (jsonDpdkintfs != null) {
66 if (jsonDpdkintfs.size() != dpdkConfig.dpdkIntfs().size()) {
67 description.appendText("dpdk interface size was " + jsonDpdkintfs.size());
68 return false;
69 }
70
71 for (DpdkInterface dpdkIntf : dpdkConfig.dpdkIntfs()) {
72 boolean intfFound = false;
73 for (int intfIndex = 0; intfIndex < jsonDpdkintfs.size(); intfIndex++) {
74 DpdkInterfaceJsonMatcher intfMatcher =
75 DpdkInterfaceJsonMatcher.matchesDpdkInterface(dpdkIntf);
76 if (intfMatcher.matches(jsonDpdkintfs.get(intfIndex))) {
77 intfFound = true;
78 break;
79 }
80 }
81
82 if (!intfFound) {
83 description.appendText("DpdkIntf not found " + dpdkIntf.toString());
84 return false;
85 }
86 }
87 }
88
89 return true;
90 }
91
92 @Override
93 public void describeTo(Description description) {
94 description.appendText(dpdkConfig.toString());
95 }
96
97 /**
98 * Factory to allocate and dpdk config matcher.
99 *
100 * @param dpdkConfig dpdk config object we are looking for
101 * @return matcher
102 */
103 public static DpdkConfigJsonMatcher matchDpdkConfig(DpdkConfig dpdkConfig) {
104 return new DpdkConfigJsonMatcher(dpdkConfig);
105 }
106}