blob: 5a94649e6035ef57eb5f786390a5f2efa1baf484 [file] [log] [blame]
Jian Li5b402c72018-02-27 14:27:34 +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;
Jian Li789fadb2018-07-10 13:59:47 +090021import org.onosproject.net.behaviour.ControllerInfo;
Jian Li5b402c72018-02-27 14:27:34 +090022import org.onosproject.openstacknode.api.Constants;
Daniel Parkd02d7bd2018-08-23 23:04:31 +090023import org.onosproject.openstacknode.api.DpdkConfig;
Jian Li5b402c72018-02-27 14:27:34 +090024import org.onosproject.openstacknode.api.OpenstackNode;
Jian Lie6312162018-03-21 21:41:00 +090025import org.onosproject.openstacknode.api.OpenstackPhyInterface;
Daniel Parkdeefa702018-07-17 17:55:51 +090026import org.onosproject.openstacknode.api.OpenstackSshAuth;
Jian Li5b402c72018-02-27 14:27:34 +090027
28import static org.onosproject.openstacknode.api.Constants.DATA_IP;
29import static org.onosproject.openstacknode.api.Constants.MANAGEMENT_IP;
30import static org.onosproject.openstacknode.api.Constants.VLAN_INTF_NAME;
31
32/**
Jian Li27841662018-04-14 01:59:47 +090033 * Hamcrest matcher for openstack node.
Jian Li5b402c72018-02-27 14:27:34 +090034 */
35public final class OpenstackNodeJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
36
37 private final OpenstackNode node;
38 private static final String INTEGRATION_BRIDGE = "integrationBridge";
39 private static final String STATE = "state";
Jian Lie6312162018-03-21 21:41:00 +090040 private static final String PHYSICAL_INTERFACES = "phyIntfs";
Daniel Parkd02d7bd2018-08-23 23:04:31 +090041 private static final String DPDK_CONFIG = "dpdkConfig";
Jian Li789fadb2018-07-10 13:59:47 +090042 private static final String CONTROLLERS = "controllers";
Jian Li27841662018-04-14 01:59:47 +090043 private static final String AUTHENTICATION = "authentication";
Jian Li6d410362018-08-17 09:41:08 +090044 private static final String END_POINT = "endpoint";
Daniel Parkdeefa702018-07-17 17:55:51 +090045 private static final String SSH_AUTH = "sshAuth";
Jian Li5b402c72018-02-27 14:27:34 +090046
47 private OpenstackNodeJsonMatcher(OpenstackNode node) {
48 this.node = node;
49 }
50
51 @Override
52 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
53 // check hostname
54 String jsonHostname = jsonNode.get(Constants.HOST_NAME).asText();
55 String hostname = node.hostname();
56 if (!jsonHostname.equals(hostname)) {
57 description.appendText("hostname was " + jsonHostname);
58 return false;
59 }
60
61 // check type
62 String jsonType = jsonNode.get(Constants.TYPE).asText();
63 String type = node.type().name();
64 if (!jsonType.equals(type)) {
65 description.appendText("type was " + jsonType);
66 return false;
67 }
68
69 // check management IP
70 String jsonMgmtIp = jsonNode.get(MANAGEMENT_IP).asText();
71 String mgmtIp = node.managementIp().toString();
72 if (!jsonMgmtIp.equals(mgmtIp)) {
73 description.appendText("management IP was " + jsonMgmtIp);
74 return false;
75 }
76
77 // check integration bridge
Jian Li27841662018-04-14 01:59:47 +090078 JsonNode jsonIntgBridge = jsonNode.get(INTEGRATION_BRIDGE);
79 if (jsonIntgBridge != null) {
80 String intgBridge = node.intgBridge().toString();
81 if (!jsonIntgBridge.asText().equals(intgBridge)) {
82 description.appendText("integration bridge was " + jsonIntgBridge);
83 return false;
84 }
Jian Li5b402c72018-02-27 14:27:34 +090085 }
86
87 // check state
88 String jsonState = jsonNode.get(STATE).asText();
89 String state = node.state().name();
90 if (!jsonState.equals(state)) {
91 description.appendText("state was " + jsonState);
92 return false;
93 }
94
95 // check VLAN interface
96 JsonNode jsonVlanIntf = jsonNode.get(VLAN_INTF_NAME);
97 if (jsonVlanIntf != null) {
98 String vlanIntf = node.vlanIntf();
99 if (!jsonVlanIntf.asText().equals(vlanIntf)) {
100 description.appendText("VLAN interface was " + jsonVlanIntf.asText());
101 return false;
102 }
103 }
104
105 // check data IP
106 JsonNode jsonDataIp = jsonNode.get(DATA_IP);
107 if (jsonDataIp != null) {
108 String dataIp = node.dataIp().toString();
109 if (!jsonDataIp.asText().equals(dataIp)) {
110 description.appendText("Data IP was " + jsonDataIp.asText());
111 return false;
112 }
113 }
114
Daniel Parkdeefa702018-07-17 17:55:51 +0900115 // check openstack ssh auth
116 JsonNode jsonSshAuth = jsonNode.get(SSH_AUTH);
117 if (jsonSshAuth != null) {
118 OpenstackSshAuth sshAuth = node.sshAuthInfo();
119 OpenstackSshAuthJsonMatcher sshAuthJsonMatcher =
120 OpenstackSshAuthJsonMatcher.matchOpenstackSshAuth(sshAuth);
121 if (!sshAuthJsonMatcher.matches(jsonSshAuth)) {
122 return false;
123 }
124 }
125
Daniel Parkd02d7bd2018-08-23 23:04:31 +0900126 // check dpdk config
127 JsonNode jsonDpdkConfig = jsonNode.get(DPDK_CONFIG);
128 if (jsonDpdkConfig != null) {
129 DpdkConfig dpdkConfig = node.dpdkConfig();
130
131 }
132
Jian Lie6312162018-03-21 21:41:00 +0900133 // check physical interfaces
134 JsonNode jsonPhyIntfs = jsonNode.get(PHYSICAL_INTERFACES);
135 if (jsonPhyIntfs != null) {
136 if (jsonPhyIntfs.size() != node.phyIntfs().size()) {
137 description.appendText("physical interface size was " + jsonPhyIntfs.size());
138 return false;
139 }
140
141 for (OpenstackPhyInterface phyIntf : node.phyIntfs()) {
142 boolean intfFound = false;
143 for (int intfIndex = 0; intfIndex < jsonPhyIntfs.size(); intfIndex++) {
144 OpenstackPhyInterfaceJsonMatcher intfMatcher =
145 OpenstackPhyInterfaceJsonMatcher.matchesOpenstackPhyInterface(phyIntf);
146 if (intfMatcher.matches(jsonPhyIntfs.get(intfIndex))) {
147 intfFound = true;
148 break;
149 }
150 }
151
152 if (!intfFound) {
153 description.appendText("PhyIntf not found " + phyIntf.toString());
154 return false;
155 }
156 }
157 }
158
Jian Li789fadb2018-07-10 13:59:47 +0900159 // check controllers
160 JsonNode jsonControllers = jsonNode.get(CONTROLLERS);
161 if (jsonControllers != null) {
162 if (jsonControllers.size() != node.controllers().size()) {
163 description.appendText("controllers size was " + jsonControllers.size());
164 return false;
165 }
166
167 for (ControllerInfo controller : node.controllers()) {
168 boolean ctrlFound = false;
169 for (int ctrlIndex = 0; ctrlIndex < jsonControllers.size(); ctrlIndex++) {
170 OpenstackControllerJsonMatcher ctrlMatcher =
171 OpenstackControllerJsonMatcher.matchesOpenstackController(controller);
172 if (ctrlMatcher.matches(jsonControllers.get(ctrlIndex))) {
173 ctrlFound = true;
174 break;
175 }
176 }
177
178 if (!ctrlFound) {
179 description.appendText("Controller not found " + controller.toString());
180 return false;
181 }
182 }
183 }
184
Jian Li5b402c72018-02-27 14:27:34 +0900185 return true;
186 }
187
188 @Override
189 public void describeTo(Description description) {
190 description.appendText(node.toString());
191 }
192
193 /**
194 * Factory to allocate an openstack node matcher.
195 *
196 * @param node openstack node object we are looking for
197 * @return matcher
198 */
199 public static OpenstackNodeJsonMatcher matchesOpenstackNode(OpenstackNode node) {
200 return new OpenstackNodeJsonMatcher(node);
201 }
202}