Optimize port number lookups
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / translator / FeaturesV10ToNodeConnectorUpdatedTranslatorTest.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.openflowplugin.openflow.md.core.translator;
10
11 import static org.mockito.Mockito.when;
12
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.List;
16 import junit.framework.TestCase;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.runners.MockitoJUnitRunner;
21 import org.opendaylight.openflowplugin.api.OFConstants;
22 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
23 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ActionTypeV10;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Capabilities;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortState;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPort;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPortBuilder;
34 import org.opendaylight.yangtools.yang.binding.Augmentation;
35 import org.opendaylight.yangtools.yang.binding.DataContainer;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37
38 @RunWith(MockitoJUnitRunner.class)
39 public class FeaturesV10ToNodeConnectorUpdatedTranslatorTest extends TestCase {
40
41     private static final FeaturesV10ToNodeConnectorUpdatedTranslator FEATURES_V_10_TO_NODE_CONNECTOR_UPDATED_TRANSLATOR = new FeaturesV10ToNodeConnectorUpdatedTranslator();
42     private static final PortFeatures PORT_FEATURES = new PortFeatures(true, false, false, false, false, false, false, false, false, false, true, true, false, false, false, false);
43
44     @Mock
45     SwitchConnectionDistinguisher switchConnectionDistinguisher;
46     private static final PortConfig PORT_CONFIG = new PortConfig(true, false, false, false);
47     @Mock
48     SessionContext sessionContext;
49     @Mock
50     GetFeaturesOutput featuresOutput;
51
52     @Test
53     /**
54      * Test method for basic functionality of {@link FeaturesV10ToNodeConnectorUpdatedTranslator#translate(org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher, org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext, org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader)}
55      */
56     public void testTranslate() throws Exception {
57
58         when(sessionContext.getFeatures()).thenReturn(featuresOutput);
59         when(featuresOutput.getDatapathId()).thenReturn(BigInteger.valueOf(42));
60         List<DataObject> data = FEATURES_V_10_TO_NODE_CONNECTOR_UPDATED_TRANSLATOR.translate(switchConnectionDistinguisher, sessionContext, new MockOfHeader());
61         assertNotNull(data);
62         assertEquals(10, data.size());
63     }
64
65     private final class MockOfHeader implements OfHeader, GetFeaturesOutput {
66
67         @Override
68         public Short getVersion() {
69             return OFConstants.OFP_VERSION_1_3;
70         }
71
72         @Override
73         public Long getXid() {
74             return new Long(1);
75         }
76
77         @Override
78         public Class<? extends DataContainer> getImplementedInterface() {
79             return GetFeaturesOutput.class;
80         }
81
82         @Override
83         public <E extends Augmentation<GetFeaturesOutput>> E getAugmentation(final Class<E> eClass) {
84             return null;
85         }
86
87         @Override
88         public BigInteger getDatapathId() {
89             return null;
90         }
91
92         @Override
93         public Long getBuffers() {
94             return null;
95         }
96
97         @Override
98         public Short getTables() {
99             return null;
100         }
101
102         @Override
103         public Short getAuxiliaryId() {
104             return null;
105         }
106
107         @Override
108         public Capabilities getCapabilities() {
109             return null;
110         }
111
112         @Override
113         public Long getReserved() {
114             return null;
115         }
116
117         @Override
118         public CapabilitiesV10 getCapabilitiesV10() {
119             return null;
120         }
121
122         @Override
123         public ActionTypeV10 getActionsV10() {
124             return null;
125         }
126
127         @Override
128         public List<PhyPort> getPhyPort() {
129             List<PhyPort> phyPorts = new ArrayList<>();
130             for (int i = 0; i < 10; i++) {
131                 PhyPortBuilder phyPortBuilder = new PhyPortBuilder();
132                 phyPortBuilder.setAdvertisedFeatures(PORT_FEATURES);
133                 phyPortBuilder.setConfig(PORT_CONFIG);
134                 phyPortBuilder.setCurrentFeatures(PORT_FEATURES);
135                 phyPortBuilder.setPeerFeatures(PORT_FEATURES);
136                 phyPortBuilder.setState(PortState.getDefaultInstance("live"));
137                 phyPortBuilder.setSupportedFeatures(PORT_FEATURES);
138                 phyPortBuilder.setPortNo(new Long(42));
139                 phyPorts.add(phyPortBuilder.build());
140             }
141             return phyPorts;
142         }
143     }
144
145 }