- static ListenableFuture<RpcResult<List<MultipartReply>>> getNodeStaticInfo(final MultipartType type, final DeviceContext deviceContext,
- final InstanceIdentifier<Node> nodeII, final short version) {
-
- final OutboundQueue queue = deviceContext.getPrimaryConnectionContext().getOutboundQueueProvider();
-
- final Long reserved = deviceContext.getReservedXid();
- final RequestContext<List<MultipartReply>> requestContext = new AbstractRequestContext<List<MultipartReply>>(reserved) {
- @Override
- public void close() {
- //NOOP
- }
- };
-
- final Xid xid = requestContext.getXid();
-
- LOG.trace("Hooking xid {} to device context - precaution.", reserved);
-
- final MultiMsgCollector multiMsgCollector = deviceContext.getMultiMsgCollector(requestContext);
- queue.commitEntry(xid.getValue(), MultipartRequestInputFactory.makeMultipartRequestInput(xid.getValue(), version, type), new FutureCallback<OfHeader>() {
- @Override
- public void onSuccess(final OfHeader ofHeader) {
- if (ofHeader instanceof MultipartReply) {
- final MultipartReply multipartReply = (MultipartReply) ofHeader;
- multiMsgCollector.addMultipartMsg(multipartReply);
- } else if (null != ofHeader) {
- LOG.info("Unexpected response type received {}.", ofHeader.getClass());
- } else {
- multiMsgCollector.endCollecting();
- LOG.info("Response received is null.");
- }
- }
-
- @Override
- public void onFailure(final Throwable t) {
- LOG.info("Fail response from OutboundQueue for multipart type {}.", type);
- final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.<List<MultipartReply>>failed().build();
- requestContext.setResult(rpcResult);
- if (MultipartType.OFPMPTABLEFEATURES.equals(type)) {
- makeEmptyTables(deviceContext, nodeII, deviceContext.getPrimaryConnectionContext().getFeatures().getTables());
- }
- requestContext.close();
- }
- });
-
- return requestContext.getFuture();
- }
-
- static void createSuccessProcessingCallback(final MultipartType type, final DeviceContext deviceContext, final InstanceIdentifier<Node> nodeII, final ListenableFuture<RpcResult<List<MultipartReply>>> requestContextFuture) {
- Futures.addCallback(requestContextFuture, new FutureCallback<RpcResult<List<MultipartReply>>>() {
- @Override
- public void onSuccess(final RpcResult<List<MultipartReply>> rpcResult) {
- final List<MultipartReply> result = rpcResult.getResult();
- if (result != null) {
- LOG.info("Static node {} info: {} collected", deviceContext.getDeviceState().getNodeId(), type);
- translateAndWriteReply(type, deviceContext, nodeII, result);
- } else {
- final Iterator<RpcError> rpcErrorIterator = rpcResult.getErrors().iterator();
- while (rpcErrorIterator.hasNext()) {
- final RpcError rpcError = rpcErrorIterator.next();
- LOG.info("Failed to retrieve static node {} info: {}", type, rpcError.getMessage());
- if (null != rpcError.getCause()) {
- LOG.trace("Detailed error:", rpcError.getCause());
- }
- }
- if (MultipartType.OFPMPTABLEFEATURES.equals(type)) {
- makeEmptyTables(deviceContext, nodeII, deviceContext.getPrimaryConnectionContext().getFeatures().getTables());
- }
- }
- }
-
- @Override
- public void onFailure(final Throwable throwable) {
- LOG.info("Request of type {} for static info of node {} failed.", type, nodeII);
- }
- });
- }
-
- // FIXME : remove after ovs tableFeatures fix
- static void makeEmptyTables(final DeviceContext dContext, final InstanceIdentifier<Node> nodeII, final Short nrOfTables) {
- LOG.debug("About to create {} empty tables.", nrOfTables);
- for (int i = 0; i < nrOfTables; i++) {
- final short tId = (short) i;
- final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tId));
- final TableBuilder tableBuilder = new TableBuilder().setId(tId).addAugmentation(FlowTableStatisticsData.class, new FlowTableStatisticsDataBuilder().build());
-
- try {
- dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, tableBuilder.build());
- } catch (final Exception e) {
- LOG.debug("Failed to write node {} to DS ", dContext.getDeviceState().getNodeId().toString(), e);
- }
-
- }
- }
-
- private static IpAddress getIpAddressOf(final DeviceContext deviceContext) {
-
- InetSocketAddress remoteAddress = deviceContext.getPrimaryConnectionContext().getConnectionAdapter().getRemoteAddress();
-
- if (remoteAddress == null) {
- LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", deviceContext.getDeviceState().getNodeId());
- return null;
- }
- LOG.info("IP address of switch is :"+remoteAddress);
-
- final InetAddress address = remoteAddress.getAddress();
- String hostAddress = address.getHostAddress();
- if (address instanceof Inet4Address) {
- return new IpAddress(new Ipv4Address(hostAddress));
- }
- if (address instanceof Inet6Address) {
- return new IpAddress(new Ipv6Address(hostAddress));
- }
- LOG.info("Illegal IP address {} of switch:{} ", address, deviceContext.getDeviceState().getNodeId());
- return null;
-
- }
-
- static void translateAndWriteReply(final MultipartType type, final DeviceContext dContext,
- final InstanceIdentifier<Node> nodeII, final Collection<MultipartReply> result) {
- try {
- for (final MultipartReply reply : result) {
- final MultipartReplyBody body = reply.getMultipartReplyBody();
- switch (type) {
- case OFPMPDESC:
- Preconditions.checkArgument(body instanceof MultipartReplyDescCase);
- final MultipartReplyDesc replyDesc = ((MultipartReplyDescCase) body).getMultipartReplyDesc();
- final FlowCapableNode fcNode = NodeStaticReplyTranslatorUtil.nodeDescTranslator(replyDesc, getIpAddressOf(dContext));
- final InstanceIdentifier<FlowCapableNode> fNodeII = nodeII.augmentation(FlowCapableNode.class);
- dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, fcNode);
- break;
-
- case OFPMPTABLEFEATURES:
- Preconditions.checkArgument(body instanceof MultipartReplyTableFeaturesCase);
- final MultipartReplyTableFeatures tableFeatures = ((MultipartReplyTableFeaturesCase) body).getMultipartReplyTableFeatures();
- final List<TableFeatures> tables = NodeStaticReplyTranslatorUtil.nodeTableFeatureTranslator(tableFeatures);
- for (final TableFeatures table : tables) {
- final Short tableId = table.getTableId();
- final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId));
- final TableBuilder tableBuilder = new TableBuilder().setId(tableId).setTableFeatures(Collections.singletonList(table));
- tableBuilder.addAugmentation(FlowTableStatisticsData.class, new FlowTableStatisticsDataBuilder().build());
- dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, tableBuilder.build());
- }
- break;
-
- case OFPMPMETERFEATURES:
- Preconditions.checkArgument(body instanceof MultipartReplyMeterFeaturesCase);
- final MultipartReplyMeterFeatures meterFeatures = ((MultipartReplyMeterFeaturesCase) body).getMultipartReplyMeterFeatures();
- final NodeMeterFeatures mFeature = NodeStaticReplyTranslatorUtil.nodeMeterFeatureTranslator(meterFeatures);
- final InstanceIdentifier<NodeMeterFeatures> mFeatureII = nodeII.augmentation(NodeMeterFeatures.class);
- dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, mFeatureII, mFeature);
- if (0L < mFeature.getMeterFeatures().getMaxMeter().getValue()) {
- dContext.getDeviceState().setMeterAvailable(true);
- }
- break;
-
- case OFPMPGROUPFEATURES:
- Preconditions.checkArgument(body instanceof MultipartReplyGroupFeaturesCase);
- final MultipartReplyGroupFeatures groupFeatures = ((MultipartReplyGroupFeaturesCase) body).getMultipartReplyGroupFeatures();
- final NodeGroupFeatures gFeature = NodeStaticReplyTranslatorUtil.nodeGroupFeatureTranslator(groupFeatures);
- final InstanceIdentifier<NodeGroupFeatures> gFeatureII = nodeII.augmentation(NodeGroupFeatures.class);
- dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gFeatureII, gFeature);
- break;
-
- case OFPMPPORTDESC:
- Preconditions.checkArgument(body instanceof MultipartReplyPortDescCase);
- final MultipartReplyPortDesc portDesc = ((MultipartReplyPortDescCase) body).getMultipartReplyPortDesc();
- for (final PortGrouping port : portDesc.getPorts()) {
- final short ofVersion = dContext.getDeviceState().getVersion();
- final TranslatorKey translatorKey = new TranslatorKey(ofVersion, PortGrouping.class.getName());
- final MessageTranslator<PortGrouping, FlowCapableNodeConnector> translator = dContext.oook().lookupTranslator(translatorKey);
- final FlowCapableNodeConnector fcNodeConnector = translator.translate(port, dContext, null);
-
- final BigInteger dataPathId = dContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
- final NodeConnectorId nodeConnectorId = NodeStaticReplyTranslatorUtil.nodeConnectorId(dataPathId.toString(), port.getPortNo(), ofVersion);
- final NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder().setId(nodeConnectorId);
- ncBuilder.addAugmentation(FlowCapableNodeConnector.class, fcNodeConnector);
-
- ncBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, new FlowCapableNodeConnectorStatisticsDataBuilder().build());
- final NodeConnector connector = ncBuilder.build();
-
- final InstanceIdentifier<NodeConnector> connectorII = nodeII.child(NodeConnector.class, connector.getKey());
- dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, connectorII, connector);
- }
-
- break;
-
- default:
- throw new IllegalArgumentException("Unnexpected MultipartType " + type);
- }
- }
- } catch (final Exception e) {
- LOG.debug("Failed to write node {} to DS ", dContext.getDeviceState().getNodeId().toString(), e);
- }
- }
-