Merge "fix parital multipart processing"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / DeviceManagerImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.impl.device;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.JdkFutureAdapters;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.SettableFuture;
16 import io.netty.util.HashedWheelTimer;
17 import java.math.BigInteger;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.concurrent.ScheduledThreadPoolExecutor;
25 import java.util.concurrent.TimeUnit;
26 import javax.annotation.CheckForNull;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
30 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
31 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.openflowplugin.api.ConnectionException;
34 import org.opendaylight.openflowplugin.api.OFConstants;
35 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
36 import org.opendaylight.openflowplugin.api.openflow.connection.ThrottledNotificationsOfferer;
37 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
38 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
39 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
40 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
41 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
42 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
43 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
44 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
45 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
46 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
47 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
48 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
49 import org.opendaylight.openflowplugin.impl.common.MultipartRequestInputFactory;
50 import org.opendaylight.openflowplugin.impl.common.NodeStaticReplyTranslatorUtil;
51 import org.opendaylight.openflowplugin.impl.connection.ThrottledNotificationsOffererImpl;
52 import org.opendaylight.openflowplugin.impl.device.listener.OpenflowProtocolListenerFullImpl;
53 import org.opendaylight.openflowplugin.impl.rpc.RequestContextImpl;
54 import org.opendaylight.openflowplugin.impl.services.OFJResult2RequestCtxFuture;
55 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Capabilities;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.MultipartReplyBody;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCase;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupFeaturesCase;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterFeaturesCase;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortDescCase;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCase;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDesc;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.features._case.MultipartReplyGroupFeatures;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.features._case.MultipartReplyMeterFeatures;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.MultipartReplyPortDesc;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsDataBuilder;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
93 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
94 import org.opendaylight.yangtools.yang.common.RpcError;
95 import org.opendaylight.yangtools.yang.common.RpcResult;
96 import org.slf4j.Logger;
97 import org.slf4j.LoggerFactory;
98
99 /**
100  *
101  */
102 public class DeviceManagerImpl implements DeviceManager, AutoCloseable {
103
104     private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerImpl.class);
105
106     private static final long TICK_DURATION = 10; // 0.5 sec.
107     private ScheduledThreadPoolExecutor spyPool;
108     private final int spyRate = 10;
109
110     private final DataBroker dataBroker;
111     private final HashedWheelTimer hashedWheelTimer;
112     private RequestContextStack emptyRequestContextStack;
113     private TranslatorLibrary translatorLibrary;
114     private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
115     private NotificationService notificationService;
116     private NotificationPublishService notificationPublishService;
117     private ThrottledNotificationsOfferer<PacketInMessage> throttledNotificationsOfferer;
118
119     private final List<DeviceContext> deviceContexts = new ArrayList<DeviceContext>();
120     private final MessageIntelligenceAgency messageIntelligenceAgency;
121
122     public DeviceManagerImpl(@Nonnull final DataBroker dataBroker,
123                              @Nonnull final MessageIntelligenceAgency messageIntelligenceAgency) {
124         this.dataBroker = Preconditions.checkNotNull(dataBroker);
125         hashedWheelTimer = new HashedWheelTimer(TICK_DURATION, TimeUnit.MILLISECONDS, 500);
126         /* merge empty nodes to oper DS to predict any problems with missing parent for Node */
127         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
128         tx.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class), new NodesBuilder().build());
129         tx.submit();
130
131         this.messageIntelligenceAgency = messageIntelligenceAgency;
132
133         emptyRequestContextStack = new RequestContextStack() {
134             @Override
135             public <T> void forgetRequestContext(final RequestContext<T> requestContext) {
136                 //NOOP
137             }
138
139             @Override
140             public <T> SettableFuture<RpcResult<T>> storeOrFail(final RequestContext<T> data) {
141                 return data.getFuture();
142             }
143
144             @Override
145             public <T> RequestContext<T> createRequestContext() {
146                 return new RequestContextImpl<>(this);
147             }
148         };
149     }
150
151     @Override
152     public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
153         deviceInitPhaseHandler = handler;
154     }
155
156     @Override
157     public void onDeviceContextLevelUp(final DeviceContext deviceContext) {
158         // final phase - we have to add new Device to MD-SAL DataStore
159         Preconditions.checkNotNull(deviceContext);
160         ((DeviceContextImpl) deviceContext).submitTransaction();
161         new BarrierTaskBuilder(deviceContext).buildAndFireBarrierTask();
162     }
163
164     @Override
165     public void deviceConnected(@CheckForNull final ConnectionContext connectionContext) {
166         Preconditions.checkArgument(connectionContext != null);
167
168         final DeviceState deviceState = new DeviceStateImpl(connectionContext.getFeatures(), connectionContext.getNodeId());
169
170         final DeviceContext deviceContext = new DeviceContextImpl(connectionContext, deviceState, dataBroker, hashedWheelTimer, messageIntelligenceAgency, throttledNotificationsOfferer);
171
172         deviceContext.setNotificationService(notificationService);
173         deviceContext.setNotificationPublishService(notificationPublishService);
174         deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, deviceState.getNodeInstanceIdentifier(), new NodeBuilder().setId(deviceState.getNodeId()).build());
175
176         connectionContext.setDeviceDisconnectedHandler(deviceContext);
177         deviceContext.setTranslatorLibrary(translatorLibrary);
178         deviceContext.addDeviceContextClosedHandler(this);
179
180         final OpenflowProtocolListenerFullImpl messageListener = new OpenflowProtocolListenerFullImpl(
181                 connectionContext.getConnectionAdapter(), deviceContext);
182
183         deviceContext.attachOpenflowMessageListener(messageListener);
184
185         final ListenableFuture<List<RpcResult<List<MultipartReply>>>> deviceFeaturesFuture;
186
187         final Short version = connectionContext.getFeatures().getVersion();
188         if (OFConstants.OFP_VERSION_1_0 == version) {
189             final CapabilitiesV10 capabilitiesV10 = connectionContext.getFeatures().getCapabilitiesV10();
190
191             DeviceStateUtil.setDeviceStateBasedOnV10Capabilities(deviceState, capabilitiesV10);
192
193             deviceFeaturesFuture = createDeviceFeaturesForOF10(messageListener, deviceContext, deviceState);
194             // create empty tables after device description is processed
195             chainTableTrunkWriteOF10(deviceContext, deviceFeaturesFuture);
196
197             for (final PortGrouping port : connectionContext.getFeatures().getPhyPort()) {
198                 final short ofVersion = deviceContext.getDeviceState().getVersion();
199                 final TranslatorKey translatorKey = new TranslatorKey(ofVersion, PortGrouping.class.getName());
200                 final MessageTranslator<PortGrouping, FlowCapableNodeConnector> translator = deviceContext.oook().lookupTranslator(translatorKey);
201                 final FlowCapableNodeConnector fcNodeConnector = translator.translate(port, deviceContext, null);
202
203                 final BigInteger dataPathId = deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
204                 final NodeConnectorId nodeConnectorId = NodeStaticReplyTranslatorUtil.nodeConnectorId(dataPathId.toString(), port.getPortNo(), ofVersion);
205                 final NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder().setId(nodeConnectorId);
206                 ncBuilder.addAugmentation(FlowCapableNodeConnector.class, fcNodeConnector);
207                 ncBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, new FlowCapableNodeConnectorStatisticsDataBuilder().build());
208                 final NodeConnector connector = ncBuilder.build();
209                 final InstanceIdentifier<NodeConnector> connectorII = deviceState.getNodeInstanceIdentifier().child(NodeConnector.class, connector.getKey());
210                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, connectorII, connector);
211             }
212         } else if (OFConstants.OFP_VERSION_1_3 == version) {
213             final Capabilities capabilities = connectionContext.getFeatures().getCapabilities();
214             LOG.debug("Setting capabilities for device {}", deviceContext.getDeviceState().getNodeId());
215             DeviceStateUtil.setDeviceStateBasedOnV13Capabilities(deviceState, capabilities);
216             deviceFeaturesFuture = createDeviceFeaturesForOF13(messageListener, deviceContext, deviceState);
217         } else {
218             deviceFeaturesFuture = Futures.immediateFailedFuture(new ConnectionException("Unsupported version " + version));
219         }
220
221         Futures.addCallback(deviceFeaturesFuture, new FutureCallback<List<RpcResult<List<MultipartReply>>>>() {
222             @Override
223             public void onSuccess(final List<RpcResult<List<MultipartReply>>> result) {
224                 deviceContext.getDeviceState().setValid(true);
225                 deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
226             }
227
228             @Override
229             public void onFailure(final Throwable t) {
230                 // FIXME : remove session
231                 LOG.trace("Device capabilities gathering future failed.");
232                 LOG.trace("more info in exploration failure..", t);
233             }
234         });
235     }
236
237     private void chainTableTrunkWriteOF10(final DeviceContext deviceContext, final ListenableFuture<List<RpcResult<List<MultipartReply>>>> deviceFeaturesFuture) {
238         Futures.addCallback(deviceFeaturesFuture, new FutureCallback<List<RpcResult<List<MultipartReply>>>>() {
239             @Override
240             public void onSuccess(final List<RpcResult<List<MultipartReply>>> results) {
241                 boolean allSucceeded = true;
242                 for (final RpcResult<List<MultipartReply>> rpcResult : results) {
243                     allSucceeded &= rpcResult.isSuccessful();
244                 }
245                 if (allSucceeded) {
246                     createEmptyFlowCapableNodeInDs(deviceContext);
247                     makeEmptyTables(deviceContext, deviceContext.getDeviceState().getNodeInstanceIdentifier(),
248                             deviceContext.getDeviceState().getFeatures().getTables());
249                 }
250             }
251
252             @Override
253             public void onFailure(final Throwable t) {
254                 //NOOP
255             }
256         });
257     }
258
259
260     private ListenableFuture<RpcResult<List<MultipartReply>>> processReplyDesc(final OpenflowProtocolListenerFullImpl messageListener,
261                                                                                final DeviceContext deviceContext,
262                                                                                final DeviceState deviceState) {
263         final ListenableFuture<RpcResult<List<MultipartReply>>> replyDesc = getNodeStaticInfo(messageListener,
264                 MultipartType.OFPMPDESC, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
265         return replyDesc;
266     }
267
268     private ListenableFuture<List<RpcResult<List<MultipartReply>>>> createDeviceFeaturesForOF10(final OpenflowProtocolListenerFullImpl messageListener,
269                                                                                                 final DeviceContext deviceContext,
270                                                                                                 final DeviceState deviceState) {
271         return Futures.allAsList(Arrays.asList(processReplyDesc(messageListener, deviceContext, deviceState)));
272     }
273
274     private ListenableFuture<List<RpcResult<List<MultipartReply>>>> createDeviceFeaturesForOF13(final OpenflowProtocolListenerFullImpl messageListener,
275                                                                                                 final DeviceContext deviceContext,
276                                                                                                 final DeviceState deviceState) {
277         final ListenableFuture<RpcResult<List<MultipartReply>>> replyDesc = processReplyDesc(messageListener, deviceContext, deviceState);
278
279         final ListenableFuture<RpcResult<List<MultipartReply>>> replyMeterFeature = getNodeStaticInfo(messageListener,
280                 MultipartType.OFPMPMETERFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
281
282         final ListenableFuture<RpcResult<List<MultipartReply>>> replyGroupFeatures = getNodeStaticInfo(messageListener,
283                 MultipartType.OFPMPGROUPFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
284
285         final ListenableFuture<RpcResult<List<MultipartReply>>> replyTableFeatures = getNodeStaticInfo(messageListener,
286                 MultipartType.OFPMPTABLEFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
287
288         final ListenableFuture<RpcResult<List<MultipartReply>>> replyPortDescription = getNodeStaticInfo(messageListener,
289                 MultipartType.OFPMPPORTDESC, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
290
291         return Futures.allAsList(Arrays.asList(replyDesc,
292                 replyMeterFeature,
293                 replyGroupFeatures,
294 //                        replyTableFeatures,
295                 replyPortDescription));
296
297     }
298
299     @Override
300     public TranslatorLibrary oook() {
301         return translatorLibrary;
302     }
303
304     @Override
305     public void setTranslatorLibrary(final TranslatorLibrary translatorLibrary) {
306         this.translatorLibrary = translatorLibrary;
307     }
308
309     private ListenableFuture<RpcResult<List<MultipartReply>>> getNodeStaticInfo(final MultiMsgCollector multiMsgCollector, final MultipartType type, final DeviceContext deviceContext,
310                                                                                 final InstanceIdentifier<Node> nodeII, final short version) {
311
312         final Xid xid = deviceContext.getNextXid();
313         final RequestContext<List<MultipartReply>> requestContext = emptyRequestContextStack.createRequestContext();
314         requestContext.setXid(xid);
315
316         LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
317         deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
318
319
320         multiMsgCollector.registerMultipartXid(xid.getValue());
321         Futures.addCallback(requestContext.getFuture(), new FutureCallback<RpcResult<List<MultipartReply>>>() {
322             @Override
323             public void onSuccess(final RpcResult<List<MultipartReply>> rpcResult) {
324                 final List<MultipartReply> result = rpcResult.getResult();
325                 if (result != null) {
326                     translateAndWriteReply(type, deviceContext, nodeII, result);
327                 } else {
328                     final Iterator<RpcError> rpcErrorIterator = rpcResult.getErrors().iterator();
329                     while (rpcErrorIterator.hasNext()) {
330                         final RpcError rpcError = rpcErrorIterator.next();
331                         LOG.info("Failed to retrieve static node {} info: {}", type, rpcError.getMessage());
332                         if (null != rpcError.getCause()) {
333                             LOG.trace("Detailed error:", rpcError.getCause());
334                         }
335                     }
336                     if (MultipartType.OFPMPTABLEFEATURES.equals(type)) {
337                         makeEmptyTables(deviceContext, nodeII, deviceContext.getPrimaryConnectionContext().getFeatures().getTables());
338                     }
339                 }
340             }
341
342             @Override
343             public void onFailure(final Throwable t) {
344                 LOG.info("Failed to retrieve static node {} info: {}", type, t.getMessage());
345             }
346         });
347
348         final ListenableFuture<RpcResult<Void>> rpcFuture = JdkFutureAdapters.listenInPoolThread(deviceContext.getPrimaryConnectionContext().getConnectionAdapter()
349                 .multipartRequest(MultipartRequestInputFactory.makeMultipartRequestInput(xid.getValue(), version, type)));
350         final OFJResult2RequestCtxFuture OFJResult2RequestCtxFuture = new OFJResult2RequestCtxFuture(requestContext, deviceContext);
351         OFJResult2RequestCtxFuture.processResultFromOfJava(rpcFuture);
352
353         return requestContext.getFuture();
354     }
355
356     // FIXME : remove after ovs tableFeatures fix
357     private static void makeEmptyTables(final DeviceContext dContext, final InstanceIdentifier<Node> nodeII, final Short nrOfTables) {
358         LOG.debug("About to create {} empty tables.", nrOfTables);
359         for (int i = 0; i < nrOfTables; i++) {
360             final short tId = (short) i;
361             final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tId));
362             final TableBuilder tableBuilder = new TableBuilder().setId(tId).addAugmentation(FlowTableStatisticsData.class, new FlowTableStatisticsDataBuilder().build());
363             dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, tableBuilder.build());
364         }
365     }
366
367     private static void translateAndWriteReply(final MultipartType type, final DeviceContext dContext,
368                                                final InstanceIdentifier<Node> nodeII, final Collection<MultipartReply> result) {
369         for (final MultipartReply reply : result) {
370             final MultipartReplyBody body = reply.getMultipartReplyBody();
371             switch (type) {
372                 case OFPMPDESC:
373                     Preconditions.checkArgument(body instanceof MultipartReplyDescCase);
374                     final MultipartReplyDesc replyDesc = ((MultipartReplyDescCase) body).getMultipartReplyDesc();
375                     final FlowCapableNode fcNode = NodeStaticReplyTranslatorUtil.nodeDescTranslator(replyDesc);
376                     final InstanceIdentifier<FlowCapableNode> fNodeII = nodeII.augmentation(FlowCapableNode.class);
377                     dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, fcNode);
378                     break;
379
380                 case OFPMPTABLEFEATURES:
381                     Preconditions.checkArgument(body instanceof MultipartReplyTableFeaturesCase);
382                     final MultipartReplyTableFeatures tableFeatures = ((MultipartReplyTableFeaturesCase) body).getMultipartReplyTableFeatures();
383                     final List<TableFeatures> tables = NodeStaticReplyTranslatorUtil.nodeTableFeatureTranslator(tableFeatures);
384                     for (final TableFeatures table : tables) {
385                         final Short tableId = table.getTableId();
386                         final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId));
387                         final TableBuilder tableBuilder = new TableBuilder().setId(tableId).setTableFeatures(Collections.singletonList(table));
388                         tableBuilder.addAugmentation(FlowTableStatisticsData.class, new FlowTableStatisticsDataBuilder().build());
389                         dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, tableBuilder.build());
390                     }
391                     break;
392
393                 case OFPMPMETERFEATURES:
394                     Preconditions.checkArgument(body instanceof MultipartReplyMeterFeaturesCase);
395                     final MultipartReplyMeterFeatures meterFeatures = ((MultipartReplyMeterFeaturesCase) body).getMultipartReplyMeterFeatures();
396                     final NodeMeterFeatures mFeature = NodeStaticReplyTranslatorUtil.nodeMeterFeatureTranslator(meterFeatures);
397                     final InstanceIdentifier<NodeMeterFeatures> mFeatureII = nodeII.augmentation(NodeMeterFeatures.class);
398                     dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, mFeatureII, mFeature);
399                     if (0L < mFeature.getMeterFeatures().getMaxMeter().getValue()) {
400                         dContext.getDeviceState().setMeterAvailable(true);
401                     }
402                     break;
403
404                 case OFPMPGROUPFEATURES:
405                     Preconditions.checkArgument(body instanceof MultipartReplyGroupFeaturesCase);
406                     final MultipartReplyGroupFeatures groupFeatures = ((MultipartReplyGroupFeaturesCase) body).getMultipartReplyGroupFeatures();
407                     final NodeGroupFeatures gFeature = NodeStaticReplyTranslatorUtil.nodeGroupFeatureTranslator(groupFeatures);
408                     final InstanceIdentifier<NodeGroupFeatures> gFeatureII = nodeII.augmentation(NodeGroupFeatures.class);
409                     dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gFeatureII, gFeature);
410                     break;
411
412                 case OFPMPPORTDESC:
413                     Preconditions.checkArgument(body instanceof MultipartReplyPortDescCase);
414                     final MultipartReplyPortDesc portDesc = ((MultipartReplyPortDescCase) body).getMultipartReplyPortDesc();
415                     for (final PortGrouping port : portDesc.getPorts()) {
416                         final short ofVersion = dContext.getDeviceState().getVersion();
417                         final TranslatorKey translatorKey = new TranslatorKey(ofVersion, PortGrouping.class.getName());
418                         final MessageTranslator<PortGrouping, FlowCapableNodeConnector> translator = dContext.oook().lookupTranslator(translatorKey);
419                         final FlowCapableNodeConnector fcNodeConnector = translator.translate(port, dContext, null);
420
421                         final BigInteger dataPathId = dContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
422                         final NodeConnectorId nodeConnectorId = NodeStaticReplyTranslatorUtil.nodeConnectorId(dataPathId.toString(), port.getPortNo(), ofVersion);
423                         final NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder().setId(nodeConnectorId);
424                         ncBuilder.addAugmentation(FlowCapableNodeConnector.class, fcNodeConnector);
425                         ncBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, new FlowCapableNodeConnectorStatisticsDataBuilder().build());
426                         final NodeConnector connector = ncBuilder.build();
427
428                         final InstanceIdentifier<NodeConnector> connectorII = nodeII.child(NodeConnector.class, connector.getKey());
429                         dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, connectorII, connector);
430                     }
431
432                     break;
433
434                 default:
435                     throw new IllegalArgumentException("Unnexpected MultipartType " + type);
436             }
437         }
438     }
439
440     @Override
441     public void setNotificationService(final NotificationService notificationServiceParam) {
442         notificationService = notificationServiceParam;
443     }
444
445     @Override
446     public void setNotificationPublishService(final NotificationPublishService notificationService) {
447         this.notificationPublishService = notificationService;
448     }
449
450     @Override
451     public void close() throws Exception {
452         for (final DeviceContext deviceContext : deviceContexts) {
453             deviceContext.close();
454         }
455         if (throttledNotificationsOfferer != null) {
456             throttledNotificationsOfferer.close();
457         }
458     }
459
460     private static void createEmptyFlowCapableNodeInDs(final DeviceContext deviceContext) {
461         final FlowCapableNodeBuilder flowCapableNodeBuilder = new FlowCapableNodeBuilder();
462         final InstanceIdentifier<FlowCapableNode> fNodeII = deviceContext.getDeviceState().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
463         deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, flowCapableNodeBuilder.build());
464     }
465
466     @Override
467     public void onDeviceContextClosed(final DeviceContext deviceContext) {
468         deviceContexts.remove(deviceContext);
469     }
470
471     @Override
472     public void initialize() {
473         spyPool = new ScheduledThreadPoolExecutor(1);
474         spyPool.scheduleAtFixedRate(messageIntelligenceAgency, spyRate, spyRate, TimeUnit.SECONDS);
475
476         throttledNotificationsOfferer = new ThrottledNotificationsOffererImpl<>(notificationPublishService, messageIntelligenceAgency);
477     }
478 }