blob: d71586585bbec8b481676fa95b72c8a014d24c3b [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;
Jian Li5b402c72018-02-27 14:27:34 +090026
27import static org.onosproject.openstacknode.api.Constants.DATA_IP;
28import static org.onosproject.openstacknode.api.Constants.MANAGEMENT_IP;
29import static org.onosproject.openstacknode.api.Constants.VLAN_INTF_NAME;
30
31/**
Jian Li27841662018-04-14 01:59:47 +090032 * Hamcrest matcher for openstack node.
Jian Li5b402c72018-02-27 14:27:34 +090033 */
34public final class OpenstackNodeJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
35
36 private final OpenstackNode node;
37 private static final String INTEGRATION_BRIDGE = "integrationBridge";
38 private static final String STATE = "state";
Jian Lie6312162018-03-21 21:41:00 +090039 private static final String PHYSICAL_INTERFACES = "phyIntfs";
Jian Li789fadb2018-07-10 13:59:47 +090040 private static final String CONTROLLERS = "controllers";
Jian Li27841662018-04-14 01:59:47 +090041 private static final String AUTHENTICATION = "authentication";
Jian Li92d42fc2018-05-25 16:23:49 +090042 private static final String END_POINT = "endPoint";
Jian Li5b402c72018-02-27 14:27:34 +090043
44 private OpenstackNodeJsonMatcher(OpenstackNode node) {
45 this.node = node;
46 }
47
48 @Override
49 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
50 // check hostname
51 String jsonHostname = jsonNode.get(Constants.HOST_NAME).asText();
52 String hostname = node.hostname();
53 if (!jsonHostname.equals(hostname)) {
54 description.appendText("hostname was " + jsonHostname);
55 return false;
56 }
57
58 // check type
59 String jsonType = jsonNode.get(Constants.TYPE).asText();
60 String type = node.type().name();
61 if (!jsonType.equals(type)) {
62 description.appendText("type was " + jsonType);
63 return false;
64 }
65
66 // check management IP
67 String jsonMgmtIp = jsonNode.get(MANAGEMENT_IP).asText();
68 String mgmtIp = node.managementIp().toString();
69 if (!jsonMgmtIp.equals(mgmtIp)) {
70 description.appendText("management IP was " + jsonMgmtIp);
71 return false;
72 }
73
74 // check integration bridge
Jian Li27841662018-04-14 01:59:47 +090075 JsonNode jsonIntgBridge = jsonNode.get(INTEGRATION_BRIDGE);
76 if (jsonIntgBridge != null) {
77 String intgBridge = node.intgBridge().toString();
78 if (!jsonIntgBridge.asText().equals(intgBridge)) {
79 description.appendText("integration bridge was " + jsonIntgBridge);
80 return false;
81 }
Jian Li5b402c72018-02-27 14:27:34 +090082 }
83
84 // check state
85 String jsonState = jsonNode.get(STATE).asText();
86 String state = node.state().name();
87 if (!jsonState.equals(state)) {
88 description.appendText("state was " + jsonState);
89 return false;
90 }
91
92 // check VLAN interface
93 JsonNode jsonVlanIntf = jsonNode.get(VLAN_INTF_NAME);
94 if (jsonVlanIntf != null) {
95 String vlanIntf = node.vlanIntf();
96 if (!jsonVlanIntf.asText().equals(vlanIntf)) {
97 description.appendText("VLAN interface was " + jsonVlanIntf.asText());
98 return false;
99 }
100 }
101
102 // check data IP
103 JsonNode jsonDataIp = jsonNode.get(DATA_IP);
104 if (jsonDataIp != null) {
105 String dataIp = node.dataIp().toString();
106 if (!jsonDataIp.asText().equals(dataIp)) {
107 description.appendText("Data IP was " + jsonDataIp.asText());
108 return false;
109 }
110 }
111
Jian Li27841662018-04-14 01:59:47 +0900112 // check openstack auth
113 JsonNode jsonAuth = jsonNode.get(AUTHENTICATION);
114 if (jsonAuth != null) {
115 OpenstackAuth auth = node.authentication();
116 OpenstackAuthJsonMatcher authMatcher =
117 OpenstackAuthJsonMatcher.matchOpenstackAuth(auth);
118 if (!authMatcher.matches(jsonAuth)) {
119 return false;
120 }
121 }
122
Jian Li92d42fc2018-05-25 16:23:49 +0900123 // check endpoint URL
124 JsonNode jsonEndPoint = jsonNode.get(END_POINT);
125 if (jsonEndPoint != null) {
126 String endPoint = node.endPoint();
127 if (!jsonEndPoint.asText().equals(endPoint)) {
128 description.appendText("endpoint URL was " + jsonEndPoint);
129 return false;
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}