use of org.onosproject.openstacknode.api.OpenstackSshAuth in project onos by opennetworkinglab.
the class OpenstackNetworkingUiMessageHandler method sendTraceRequestToNode.
private String sendTraceRequestToNode(String requestString, OpenstackNode node) {
String traceResult = null;
OpenstackSshAuth sshAuth = node.sshAuthInfo();
try (SshClient client = SshClient.setUpDefaultClient()) {
client.start();
try (ClientSession session = client.connect(sshAuth.id(), node.managementIp().getIp4Address().toString(), SSH_PORT).verify(TIMEOUT_MS, TimeUnit.SECONDS).getSession()) {
session.addPasswordIdentity(sshAuth.password());
session.auth().verify(TIMEOUT_MS, TimeUnit.SECONDS);
try (ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL)) {
log.debug("requestString: {}", requestString);
final InputStream inputStream = new ByteArrayInputStream(requestString.getBytes());
OutputStream outputStream = new ByteArrayOutputStream();
OutputStream errStream = new ByteArrayOutputStream();
channel.setIn(new NoCloseInputStream(inputStream));
channel.setErr(errStream);
channel.setOut(outputStream);
Collection<ClientChannelEvent> eventList = Lists.newArrayList();
eventList.add(ClientChannelEvent.OPENED);
OpenFuture channelFuture = channel.open();
if (channelFuture.await(TIMEOUT_MS, TimeUnit.SECONDS)) {
long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;
while (!channelFuture.isOpened()) {
if ((timeoutExpiredMs - System.currentTimeMillis()) <= 0) {
log.error("Failed to open channel");
return null;
}
}
TimeUnit.SECONDS.sleep(WAIT_OUTPUT_STREAM_SECOND);
traceResult = ((ByteArrayOutputStream) outputStream).toString(Charsets.UTF_8.name());
channel.close();
}
} finally {
session.close();
}
} finally {
client.stop();
}
} catch (Exception e) {
log.error("Exception occurred because of {}", e.toString());
}
return traceResult;
}
use of org.onosproject.openstacknode.api.OpenstackSshAuth in project onos by opennetworkinglab.
the class OpenstackNodeCodecTest method testOpenstackComputeNodeEncode.
/**
* Tests the openstack compute node encoding.
*/
@Test
public void testOpenstackComputeNodeEncode() {
OpenstackPhyInterface phyIntf1 = DefaultOpenstackPhyInterface.builder().network("mgmtnetwork").intf("eth3").build();
OpenstackPhyInterface phyIntf2 = DefaultOpenstackPhyInterface.builder().network("oamnetwork").intf("eth4").build();
OpenstackSshAuth sshAuth = DefaultOpenstackSshAuth.builder().id("sdn").password("sdn").build();
ControllerInfo controller1 = new ControllerInfo(IpAddress.valueOf("10.10.10.2"), 6653, "tcp");
ControllerInfo controller2 = new ControllerInfo(IpAddress.valueOf("10.10.10.3"), 6663, "tcp");
OpenstackNode node = DefaultOpenstackNode.builder().hostname("compute").type(OpenstackNode.NodeType.COMPUTE).state(NodeState.INIT).managementIp(IpAddress.valueOf("10.10.10.1")).intgBridge(DeviceId.deviceId("br-int")).vlanIntf("vlan").dataIp(IpAddress.valueOf("20.20.20.2")).phyIntfs(ImmutableList.of(phyIntf1, phyIntf2)).controllers(ImmutableList.of(controller1, controller2)).sshAuthInfo(sshAuth).build();
ObjectNode nodeJson = openstackNodeCodec.encode(node, context);
assertThat(nodeJson, matchesOpenstackNode(node));
}
use of org.onosproject.openstacknode.api.OpenstackSshAuth in project onos by opennetworkinglab.
the class OpenstackNetworkingUtil method sendTraceRequestToNode.
/**
* Sends flow trace string to node.
*
* @param requestString reqeust string
* @param node src node
* @return flow trace result in string format
*/
public static String sendTraceRequestToNode(String requestString, OpenstackNode node) {
String traceResult = null;
OpenstackSshAuth sshAuth = node.sshAuthInfo();
try (SshClient client = SshClient.setUpDefaultClient()) {
client.start();
try (ClientSession session = client.connect(sshAuth.id(), node.managementIp().getIp4Address().toString(), SSH_PORT).verify(TIMEOUT_MS, TimeUnit.SECONDS).getSession()) {
session.addPasswordIdentity(sshAuth.password());
session.auth().verify(TIMEOUT_MS, TimeUnit.SECONDS);
try (ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL)) {
log.debug("requestString: {}", requestString);
final InputStream inputStream = new ByteArrayInputStream(requestString.getBytes(Charsets.UTF_8));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStream errStream = new ByteArrayOutputStream();
channel.setIn(new NoCloseInputStream(inputStream));
channel.setErr(errStream);
channel.setOut(outputStream);
Collection<ClientChannelEvent> eventList = Lists.newArrayList();
eventList.add(ClientChannelEvent.OPENED);
OpenFuture channelFuture = channel.open();
if (channelFuture.await(TIMEOUT_MS, TimeUnit.SECONDS)) {
long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;
while (!channelFuture.isOpened()) {
if ((timeoutExpiredMs - System.currentTimeMillis()) <= 0) {
log.error("Failed to open channel");
return null;
}
}
TimeUnit.SECONDS.sleep(WAIT_OUTPUT_STREAM_SECOND);
traceResult = outputStream.toString(Charsets.UTF_8.name());
channel.close();
}
} finally {
session.close();
}
} finally {
client.stop();
}
} catch (Exception e) {
log.error("Exception occurred because of {}", e);
}
return traceResult;
}
use of org.onosproject.openstacknode.api.OpenstackSshAuth in project onos by opennetworkinglab.
the class OpenstackNodeCodec method decode.
@Override
public OpenstackNode decode(ObjectNode json, CodecContext context) {
if (json == null || !json.isObject()) {
return null;
}
String hostname = nullIsIllegal(json.get(HOST_NAME).asText(), HOST_NAME + MISSING_MESSAGE);
String type = nullIsIllegal(json.get(TYPE).asText(), TYPE + MISSING_MESSAGE);
String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(), MANAGEMENT_IP + MISSING_MESSAGE);
DefaultOpenstackNode.Builder nodeBuilder = DefaultOpenstackNode.builder().hostname(hostname).type(OpenstackNode.NodeType.valueOf(type)).state(NodeState.INIT).managementIp(IpAddress.valueOf(mIp));
if (type.equals(GATEWAY)) {
nodeBuilder.uplinkPort(nullIsIllegal(json.get(UPLINK_PORT).asText(), UPLINK_PORT + MISSING_MESSAGE));
}
if (type.equals(CONTROLLER)) {
JsonNode keystoneConfigJson = json.get(KEYSTONE_CONFIG);
KeystoneConfig keystoneConfig;
if (keystoneConfigJson != null) {
final JsonCodec<KeystoneConfig> keystoneConfigCodec = context.codec(KeystoneConfig.class);
keystoneConfig = keystoneConfigCodec.decode((ObjectNode) keystoneConfigJson.deepCopy(), context);
} else {
JsonNode authJson = json.get(AUTHENTICATION);
final JsonCodec<OpenstackAuth> authCodec = context.codec(OpenstackAuth.class);
OpenstackAuth auth = authCodec.decode((ObjectNode) authJson.deepCopy(), context);
String endpoint = nullIsIllegal(json.get(ENDPOINT).asText(), ENDPOINT + MISSING_MESSAGE);
keystoneConfig = DefaultKeystoneConfig.builder().authentication(auth).endpoint(endpoint).build();
}
nodeBuilder.keystoneConfig(keystoneConfig);
}
if (json.get(VLAN_INTF_NAME) != null) {
nodeBuilder.vlanIntf(json.get(VLAN_INTF_NAME).asText());
}
if (json.get(DATA_IP) != null) {
nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
}
JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE);
if (intBridgeJson != null) {
nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText()));
}
// parse physical interfaces
List<OpenstackPhyInterface> phyIntfs = new ArrayList<>();
JsonNode phyIntfsJson = json.get(PHYSICAL_INTERFACES);
if (phyIntfsJson != null) {
final JsonCodec<OpenstackPhyInterface> phyIntfCodec = context.codec(OpenstackPhyInterface.class);
IntStream.range(0, phyIntfsJson.size()).forEach(i -> {
ObjectNode intfJson = get(phyIntfsJson, i);
phyIntfs.add(phyIntfCodec.decode(intfJson, context));
});
}
nodeBuilder.phyIntfs(phyIntfs);
// parse customized controllers
List<ControllerInfo> controllers = new ArrayList<>();
JsonNode controllersJson = json.get(CONTROLLERS);
if (controllersJson != null) {
final JsonCodec<ControllerInfo> controllerCodec = context.codec(ControllerInfo.class);
IntStream.range(0, controllersJson.size()).forEach(i -> {
ObjectNode controllerJson = get(controllersJson, i);
controllers.add(controllerCodec.decode(controllerJson, context));
});
}
nodeBuilder.controllers(controllers);
// parse neutron config
JsonNode neutronConfigJson = json.get(NEUTRON_CONFIG);
if (neutronConfigJson != null) {
final JsonCodec<NeutronConfig> neutronConfigJsonCodec = context.codec(NeutronConfig.class);
NeutronConfig neutronConfig = neutronConfigJsonCodec.decode((ObjectNode) neutronConfigJson.deepCopy(), context);
nodeBuilder.neutronConfig(neutronConfig);
}
// parse ssh authentication
JsonNode sshAuthJson = json.get(SSH_AUTH);
if (sshAuthJson != null) {
final JsonCodec<OpenstackSshAuth> sshAuthJsonCodec = context.codec(OpenstackSshAuth.class);
OpenstackSshAuth sshAuth = sshAuthJsonCodec.decode((ObjectNode) sshAuthJson.deepCopy(), context);
nodeBuilder.sshAuthInfo(sshAuth);
}
// parse DPDK configuration
JsonNode dpdkConfigJson = json.get(DPDK_CONFIG);
if (dpdkConfigJson != null) {
final JsonCodec<DpdkConfig> dpdkConfigJsonCodec = context.codec(DpdkConfig.class);
DpdkConfig dpdkConfig = dpdkConfigJsonCodec.decode((ObjectNode) dpdkConfigJson.deepCopy(), context);
nodeBuilder.dpdkConfig(dpdkConfig);
}
log.trace("node is {}", nodeBuilder.build().toString());
return nodeBuilder.build();
}
use of org.onosproject.openstacknode.api.OpenstackSshAuth in project onos by opennetworkinglab.
the class OpenstackNodeJsonMatcher method matchesSafely.
@Override
protected boolean matchesSafely(JsonNode jsonNode, Description description) {
// check hostname
String jsonHostname = jsonNode.get(Constants.HOST_NAME).asText();
String hostname = node.hostname();
if (!jsonHostname.equals(hostname)) {
description.appendText("hostname was " + jsonHostname);
return false;
}
// check type
String jsonType = jsonNode.get(Constants.TYPE).asText();
String type = node.type().name();
if (!jsonType.equals(type)) {
description.appendText("type was " + jsonType);
return false;
}
// check management IP
String jsonMgmtIp = jsonNode.get(MANAGEMENT_IP).asText();
String mgmtIp = node.managementIp().toString();
if (!jsonMgmtIp.equals(mgmtIp)) {
description.appendText("management IP was " + jsonMgmtIp);
return false;
}
// check integration bridge
JsonNode jsonIntgBridge = jsonNode.get(INTEGRATION_BRIDGE);
if (jsonIntgBridge != null) {
String intgBridge = node.intgBridge().toString();
if (!jsonIntgBridge.asText().equals(intgBridge)) {
description.appendText("integration bridge was " + jsonIntgBridge);
return false;
}
}
// check state
String jsonState = jsonNode.get(STATE).asText();
String state = node.state().name();
if (!jsonState.equals(state)) {
description.appendText("state was " + jsonState);
return false;
}
// check VLAN interface
JsonNode jsonVlanIntf = jsonNode.get(VLAN_INTF_NAME);
if (jsonVlanIntf != null) {
String vlanIntf = node.vlanIntf();
if (!jsonVlanIntf.asText().equals(vlanIntf)) {
description.appendText("VLAN interface was " + jsonVlanIntf.asText());
return false;
}
}
// check data IP
JsonNode jsonDataIp = jsonNode.get(DATA_IP);
if (jsonDataIp != null) {
String dataIp = node.dataIp().toString();
if (!jsonDataIp.asText().equals(dataIp)) {
description.appendText("Data IP was " + jsonDataIp.asText());
return false;
}
}
// check openstack ssh auth
JsonNode jsonSshAuth = jsonNode.get(SSH_AUTH);
if (jsonSshAuth != null) {
OpenstackSshAuth sshAuth = node.sshAuthInfo();
OpenstackSshAuthJsonMatcher sshAuthJsonMatcher = OpenstackSshAuthJsonMatcher.matchOpenstackSshAuth(sshAuth);
if (!sshAuthJsonMatcher.matches(jsonSshAuth)) {
return false;
}
}
// check dpdk config
JsonNode jsonDpdkConfig = jsonNode.get(DPDK_CONFIG);
if (jsonDpdkConfig != null) {
DpdkConfig dpdkConfig = node.dpdkConfig();
}
// check physical interfaces
JsonNode jsonPhyIntfs = jsonNode.get(PHYSICAL_INTERFACES);
if (jsonPhyIntfs != null) {
if (jsonPhyIntfs.size() != node.phyIntfs().size()) {
description.appendText("physical interface size was " + jsonPhyIntfs.size());
return false;
}
for (OpenstackPhyInterface phyIntf : node.phyIntfs()) {
boolean intfFound = false;
for (int intfIndex = 0; intfIndex < jsonPhyIntfs.size(); intfIndex++) {
OpenstackPhyInterfaceJsonMatcher intfMatcher = OpenstackPhyInterfaceJsonMatcher.matchesOpenstackPhyInterface(phyIntf);
if (intfMatcher.matches(jsonPhyIntfs.get(intfIndex))) {
intfFound = true;
break;
}
}
if (!intfFound) {
description.appendText("PhyIntf not found " + phyIntf.toString());
return false;
}
}
}
// check controllers
JsonNode jsonControllers = jsonNode.get(CONTROLLERS);
if (jsonControllers != null) {
if (jsonControllers.size() != node.controllers().size()) {
description.appendText("controllers size was " + jsonControllers.size());
return false;
}
for (ControllerInfo controller : node.controllers()) {
boolean ctrlFound = false;
for (int ctrlIndex = 0; ctrlIndex < jsonControllers.size(); ctrlIndex++) {
OpenstackControllerJsonMatcher ctrlMatcher = OpenstackControllerJsonMatcher.matchesOpenstackController(controller);
if (ctrlMatcher.matches(jsonControllers.get(ctrlIndex))) {
ctrlFound = true;
break;
}
}
if (!ctrlFound) {
description.appendText("Controller not found " + controller.toString());
return false;
}
}
}
return true;
}
Aggregations