blob: e8b6d071ce3de00ca5c1d5fb4c5cbbff45d2893b [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;
Jian Li27841662018-04-14 01:59:47 +090023import org.onosproject.openstacknode.api.OpenstackAuth;
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";
Jian Li789fadb2018-07-10 13:59:47 +090041 private static final String CONTROLLERS = "controllers";
Jian Li27841662018-04-14 01:59:47 +090042 private static final String AUTHENTICATION = "authentication";
Jian Li92d42fc2018-05-25 16:23:49 +090043 private static final String END_POINT = "endPoint";
Daniel Parkdeefa702018-07-17 17:55:51 +090044 private static final String SSH_AUTH = "sshAuth";
Jian Li5b402c72018-02-27 14:27:34 +090045
46 private OpenstackNodeJsonMatcher(OpenstackNode node) {
47 this.node = node;
48 }
49
50 @Override
51 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
52 // check hostname
53 String jsonHostname = jsonNode.get(Constants.HOST_NAME).asText();
54 String hostname = node.hostname();
55 if (!jsonHostname.equals(hostname)) {
56 description.appendText("hostname was " + jsonHostname);
57 return false;
58 }
59
60 // check type
61 String jsonType = jsonNode.get(Constants.TYPE).asText();
62 String type = node.type().name();
63 if (!jsonType.equals(type)) {
64 description.appendText("type was " + jsonType);
65 return false;
66 }
67
68 // check management IP
69 String jsonMgmtIp = jsonNode.get(MANAGEMENT_IP).asText();
70 String mgmtIp = node.managementIp().toString();
71 if (!jsonMgmtIp.equals(mgmtIp)) {
72 description.appendText("management IP was " + jsonMgmtIp);
73 return false;
74 }
75
76 // check integration bridge
Jian Li27841662018-04-14 01:59:47 +090077 JsonNode jsonIntgBridge = jsonNode.get(INTEGRATION_BRIDGE);
78 if (jsonIntgBridge != null) {
79 String intgBridge = node.intgBridge().toString();
80 if (!jsonIntgBridge.asText().equals(intgBridge)) {
81 description.appendText("integration bridge was " + jsonIntgBridge);
82 return false;
83 }
Jian Li5b402c72018-02-27 14:27:34 +090084 }
85
86 // check state
87 String jsonState = jsonNode.get(STATE).asText();
88 String state = node.state().name();
89 if (!jsonState.equals(state)) {
90 description.appendText("state was " + jsonState);
91 return false;
92 }
93
94 // check VLAN interface
95 JsonNode jsonVlanIntf = jsonNode.get(VLAN_INTF_NAME);
96 if (jsonVlanIntf != null) {
97 String vlanIntf = node.vlanIntf();
98 if (!jsonVlanIntf.asText().equals(vlanIntf)) {
99 description.appendText("VLAN interface was " + jsonVlanIntf.asText());
100 return false;
101 }
102 }
103
104 // check data IP
105 JsonNode jsonDataIp = jsonNode.get(DATA_IP);
106 if (jsonDataIp != null) {
107 String dataIp = node.dataIp().toString();
108 if (!jsonDataIp.asText().equals(dataIp)) {
109 description.appendText("Data IP was " + jsonDataIp.asText());
110 return false;
111 }
112 }
113
Jian Li27841662018-04-14 01:59:47 +0900114 // check openstack auth
115 JsonNode jsonAuth = jsonNode.get(AUTHENTICATION);
116 if (jsonAuth != null) {
117 OpenstackAuth auth = node.authentication();
118 OpenstackAuthJsonMatcher authMatcher =
119 OpenstackAuthJsonMatcher.matchOpenstackAuth(auth);
120 if (!authMatcher.matches(jsonAuth)) {
121 return false;
122 }
123 }
124
Daniel Parkdeefa702018-07-17 17:55:51 +0900125 // check openstack ssh auth
126 JsonNode jsonSshAuth = jsonNode.get(SSH_AUTH);
127 if (jsonSshAuth != null) {
128 OpenstackSshAuth sshAuth = node.sshAuthInfo();
129 OpenstackSshAuthJsonMatcher sshAuthJsonMatcher =
130 OpenstackSshAuthJsonMatcher.matchOpenstackSshAuth(sshAuth);
131 if (!sshAuthJsonMatcher.matches(jsonSshAuth)) {
132 return false;
133 }
134 }
135
Jian Li92d42fc2018-05-25 16:23:49 +0900136 // check endpoint URL
137 JsonNode jsonEndPoint = jsonNode.get(END_POINT);
138 if (jsonEndPoint != null) {
139 String endPoint = node.endPoint();
140 if (!jsonEndPoint.asText().equals(endPoint)) {
141 description.appendText("endpoint URL was " + jsonEndPoint);
142 return false;
143 }
144 }
145
Jian Lie6312162018-03-21 21:41:00 +0900146 // check physical interfaces
147 JsonNode jsonPhyIntfs = jsonNode.get(PHYSICAL_INTERFACES);
148 if (jsonPhyIntfs != null) {
149 if (jsonPhyIntfs.size() != node.phyIntfs().size()) {
150 description.appendText("physical interface size was " + jsonPhyIntfs.size());
151 return false;
152 }
153
154 for (OpenstackPhyInterface phyIntf : node.phyIntfs()) {
155 boolean intfFound = false;
156 for (int intfIndex = 0; intfIndex < jsonPhyIntfs.size(); intfIndex++) {
157 OpenstackPhyInterfaceJsonMatcher intfMatcher =
158 OpenstackPhyInterfaceJsonMatcher.matchesOpenstackPhyInterface(phyIntf);
159 if (intfMatcher.matches(jsonPhyIntfs.get(intfIndex))) {
160 intfFound = true;
161 break;
162 }
163 }
164
165 if (!intfFound) {
166 description.appendText("PhyIntf not found " + phyIntf.toString());
167 return false;
168 }
169 }
170 }
171
Jian Li789fadb2018-07-10 13:59:47 +0900172 // check controllers
173 JsonNode jsonControllers = jsonNode.get(CONTROLLERS);
174 if (jsonControllers != null) {
175 if (jsonControllers.size() != node.controllers().size()) {
176 description.appendText("controllers size was " + jsonControllers.size());
177 return false;
178 }
179
180 for (ControllerInfo controller : node.controllers()) {
181 boolean ctrlFound = false;
182 for (int ctrlIndex = 0; ctrlIndex < jsonControllers.size(); ctrlIndex++) {
183 OpenstackControllerJsonMatcher ctrlMatcher =
184 OpenstackControllerJsonMatcher.matchesOpenstackController(controller);
185 if (ctrlMatcher.matches(jsonControllers.get(ctrlIndex))) {
186 ctrlFound = true;
187 break;
188 }
189 }
190
191 if (!ctrlFound) {
192 description.appendText("Controller not found " + controller.toString());
193 return false;
194 }
195 }
196 }
197
Jian Li5b402c72018-02-27 14:27:34 +0900198 return true;
199 }
200
201 @Override
202 public void describeTo(Description description) {
203 description.appendText(node.toString());
204 }
205
206 /**
207 * Factory to allocate an openstack node matcher.
208 *
209 * @param node openstack node object we are looking for
210 * @return matcher
211 */
212 public static OpenstackNodeJsonMatcher matchesOpenstackNode(OpenstackNode node) {
213 return new OpenstackNodeJsonMatcher(node);
214 }
215}