2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.device.initialization;
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.eq;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
16 import java.util.Collections;
17 import java.util.concurrent.Future;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.junit.MockitoJUnitRunner;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
25 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
29 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
30 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
31 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
32 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
33 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
34 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProvider;
35 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
36 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPortBuilder;
44 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
47 @RunWith(MockitoJUnitRunner.class)
48 public class OF10DeviceInitializerTest {
50 private DeviceContext deviceContext;
52 private MultipartWriterProvider multipartWriterProvider;
54 private ConvertorExecutor convertorExecutor;
56 private ConnectionContext connectionContext;
58 private FeaturesReply featuresReply;
60 private CapabilitiesV10 capabilitiesV10;
62 private DeviceInfo deviceInfo;
64 private DeviceState deviceState;
66 private MessageSpy messageSpy;
68 private TranslatorLibrary translatorLibrary;
70 private MessageTranslator messageTranslator;
72 private RequestContext requestContext;
74 private OutboundQueueProvider outboundQueueProvider;
75 private AbstractDeviceInitializer deviceInitializer;
78 public void setUp() throws Exception {
79 final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier = DeviceStateUtil
80 .createNodeInstanceIdentifier(new NodeId("openflow:1"));
82 deviceInitializer = new OF10DeviceInitializer();
83 when(featuresReply.getCapabilitiesV10()).thenReturn(capabilitiesV10);
84 when(featuresReply.getPhyPort()).thenReturn(Collections
85 .singletonList(new PhyPortBuilder()
88 when(connectionContext.getFeatures()).thenReturn(featuresReply);
89 when(connectionContext.getOutboundQueueProvider()).thenReturn(outboundQueueProvider);
90 when(deviceContext.getDeviceState()).thenReturn(deviceState);
91 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
92 when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
93 when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
94 when(translatorLibrary.lookupTranslator(any())).thenReturn(messageTranslator);
95 when(deviceContext.oook()).thenReturn(translatorLibrary);
96 when(requestContext.getXid()).thenReturn(new Xid(42L));
97 when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
98 when(deviceContext.createRequestContext()).thenReturn(requestContext);
99 when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
103 public void initializeSingleLayer() throws Exception {
104 when(deviceContext.canUseSingleLayerSerialization()).thenReturn(true);
106 final Future<Void> initialize = deviceInitializer.initialize(deviceContext,
109 multipartWriterProvider,
113 verify(messageSpy).spyMessage(eq(MultipartType.class), any());
114 verify(requestContext).getFuture();
115 verify(featuresReply).getCapabilitiesV10();
116 verify(featuresReply).getPhyPort();
117 verify(featuresReply).getTables();
118 verify(deviceContext, times(3)).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL), any(), any());
122 public void initializeMultiLayer() throws Exception {
123 when(deviceContext.canUseSingleLayerSerialization()).thenReturn(false);
125 final Future<Void> initialize = deviceInitializer.initialize(deviceContext,
128 multipartWriterProvider,
132 verify(messageSpy).spyMessage(eq(MultipartType.class), any());
133 verify(requestContext).getFuture();
134 verify(featuresReply).getCapabilitiesV10();
135 verify(featuresReply).getPhyPort();
136 verify(featuresReply).getTables();
137 verify(deviceContext, times(3)).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL), any(), any());