Bug 5054: Fix for switch restart
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / test / java / org / opendaylight / vpnservice / interfacemgr / test / FlowBasedServicesConfigurationTest.java
1 package org.opendaylight.vpnservice.interfacemgr.test;
2
3 import com.google.common.base.Optional;
4 import com.google.common.util.concurrent.Futures;
5 import org.junit.Before;
6 import org.junit.Test;
7 import org.junit.runner.RunWith;
8 import org.mockito.Mock;
9 import org.mockito.runners.MockitoJUnitRunner;
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
12 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
13 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
17 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.confighelpers.FlowBasedServicesConfigBindHelper;
18 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.confighelpers.FlowBasedServicesConfigUnbindHelper;
19 import org.opendaylight.vpnservice.mdsalutil.*;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.ServiceBindings;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.StypeOpenflow;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfo;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfoKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServices;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServicesKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeGre;
38 import org.opendaylight.yangtools.concepts.ListenerRegistration;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40
41 import java.math.BigInteger;
42 import java.util.ArrayList;
43 import java.util.List;
44
45 import static org.mockito.Matchers.any;
46 import static org.mockito.Mockito.*;
47
48 @RunWith(MockitoJUnitRunner.class)
49 public class FlowBasedServicesConfigurationTest {
50
51     BigInteger dpId = BigInteger.valueOf(1);
52     int flowPriority = 0;
53     int instructionKeyval = 2;
54     long portNum = 2;
55     Interface interfaceEnabled = null;
56     String serviceName = "VPN";
57     InstanceIdentifier<BoundServices> boundServicesIid = null;
58     org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface stateInterface;
59     InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> interfaceStateIdentifier = null;
60     BoundServices boundServiceNew = null;
61     NodeConnectorId nodeConnectorId = null;
62     ServicesInfo servicesInfo = null;
63     ServicesInfo servicesInfoUnbind = null;
64     StypeOpenflow stypeOpenflow = null;
65     InstanceIdentifier<Interface> interfaceInstanceIdentifier = null;
66     InstanceIdentifier<Flow> flowInstanceId = null;
67     Flow ingressFlow = null;
68     Instruction instruction = null;
69     InstructionKey instructionKey = null;
70     List<Instruction>instructions = new ArrayList<>();
71     short key = 0;
72     int ifIndexval = 100;
73
74     @Mock DataBroker dataBroker;
75     @Mock ListenerRegistration<DataChangeListener> dataChangeListenerRegistration;
76     @Mock ReadOnlyTransaction mockReadTx;
77     @Mock WriteTransaction mockWriteTx;
78
79     FlowBasedServicesConfigBindHelper bindHelper;
80     FlowBasedServicesConfigUnbindHelper unbindHelper;
81
82     @Before
83     public void setUp() throws Exception {
84         when(dataBroker.registerDataChangeListener(
85                 any(LogicalDatastoreType.class),
86                 any(InstanceIdentifier.class),
87                 any(DataChangeListener.class),
88                 any(DataChangeScope.class)))
89                 .thenReturn(dataChangeListenerRegistration);
90         setupMocks();
91     }
92
93     private void setupMocks(){
94
95         interfaceEnabled = InterfaceManagerTestUtil.buildInterface(InterfaceManagerTestUtil.interfaceName, "Test Vlan Interface1",true,L2vlan.class,dpId);
96         nodeConnectorId = InterfaceManagerTestUtil.buildNodeConnectorId(dpId, portNum);
97         interfaceInstanceIdentifier = IfmUtil.buildId(InterfaceManagerTestUtil.interfaceName);
98
99         InterfaceBuilder ifaceBuilder = new InterfaceBuilder();
100         List<String> lowerLayerIfList = new ArrayList<>();
101         lowerLayerIfList.add(nodeConnectorId.getValue());
102         ifaceBuilder.setOperStatus(OperStatus.Up).setAdminStatus(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus.Up)
103                 .setPhysAddress(PhysAddress.getDefaultInstance("AA:AA:AA:AA:AA:AA"))
104                 .setIfIndex(ifIndexval)
105                 .setLowerLayerIf(lowerLayerIfList)
106                 .setKey(IfmUtil.getStateInterfaceKeyFromName(InterfaceManagerTestUtil.interfaceName))
107                 .setName(InterfaceManagerTestUtil.interfaceName);
108
109         stypeOpenflow = InterfaceManagerTestUtil.buildStypeOpenflow(dpId, flowPriority,key,instructions);
110         boundServiceNew = InterfaceManagerTestUtil.buildBoundServices(serviceName, key, new BoundServicesKey(key), stypeOpenflow);
111         instructionKey = new InstructionKey(instructionKeyval);
112         BigInteger[] metadataValues = IfmUtil.mergeOpenflowMetadataWriteInstructions(instructions);
113         short sIndex = boundServiceNew.getServicePriority();
114         BigInteger metadata = MetaDataUtil.getMetaDataForLPortDispatcher(ifaceBuilder.getIfIndex(),
115                 ++sIndex, metadataValues[0]);
116         BigInteger mask = MetaDataUtil.getMetaDataMaskForLPortDispatcher(
117                 MetaDataUtil.METADATA_MASK_SERVICE_INDEX,
118                 MetaDataUtil.METADATA_MASK_LPORT_TAG, metadataValues[1]);
119
120         instruction = InterfaceManagerTestUtil.buildInstruction(InterfaceManagerTestUtil.buildWriteMetaDataCase(InterfaceManagerTestUtil.buildWriteMetaData(metadata, mask)),
121                 new InstructionKey(instructionKey));
122         instructions.add(instruction);
123         ServicesInfoKey servicesInfoKey = new ServicesInfoKey(InterfaceManagerTestUtil.interfaceName);
124         boundServicesIid =  InstanceIdentifier.builder(ServiceBindings.class).child(ServicesInfo.class, servicesInfoKey).
125                 child(BoundServices.class, new BoundServicesKey(key)).build();
126
127         interfaceStateIdentifier = IfmUtil.buildStateInterfaceId(interfaceEnabled.getName());
128         stateInterface = ifaceBuilder.build();
129         List<BoundServices> boundServiceslist = new ArrayList<>();
130         boundServiceslist.add(boundServiceNew);
131         servicesInfo = InterfaceManagerTestUtil.buildServicesInfo(InterfaceManagerTestUtil.interfaceName, servicesInfoKey, boundServiceslist);
132         servicesInfoUnbind = InterfaceManagerTestUtil.buildServicesInfo(InterfaceManagerTestUtil.interfaceName,servicesInfoKey,new ArrayList<>());
133
134         String flowRef = InterfaceManagerTestUtil.buildflowRef(dpId,InterfaceManagerTestUtil.interfaceName,boundServiceNew.getServiceName(),boundServiceNew.getServicePriority());
135         List<Instruction> instructionList = boundServiceNew.getAugmentation(StypeOpenflow.class).getInstruction();
136         String serviceRef = boundServiceNew.getServiceName();
137         List<MatchInfo> matches = new ArrayList<>();
138         matches.add(new MatchInfo(MatchFieldType.in_port, new BigInteger[] {dpId, BigInteger.valueOf(portNum)}));
139         ingressFlow = MDSALUtil.buildFlowNew(stypeOpenflow.getDispatcherTableId(), flowRef, boundServiceNew.getServicePriority(), serviceRef, 0, 0,
140                 stypeOpenflow.getFlowCookie(), matches, instructionList);
141         FlowKey flowKey = new FlowKey(new FlowId(ingressFlow.getId()));
142         flowInstanceId = InterfaceManagerTestUtil.getFlowInstanceIdentifier(dpId,ingressFlow.getTableId(),flowKey);
143
144         when(dataBroker.newReadOnlyTransaction()).thenReturn(mockReadTx);
145         when(dataBroker.newWriteOnlyTransaction()).thenReturn(mockWriteTx);
146     }
147
148     @Test
149     public void testConfigBindSingleService(){
150
151         Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> expectedStateInterface = Optional.of(stateInterface);
152         Optional<Interface> expectedInterface = Optional.of(interfaceEnabled);
153         Optional<ServicesInfo>expectedservicesInfo = Optional.of(servicesInfo);
154
155         ServicesInfoKey servicesInfoKey = new ServicesInfoKey(InterfaceManagerTestUtil.interfaceName);
156         InstanceIdentifier.InstanceIdentifierBuilder<ServicesInfo> servicesInfoIdentifierBuilder =
157                 InstanceIdentifier.builder(ServiceBindings.class).child(ServicesInfo.class, servicesInfoKey);
158
159
160         doReturn(Futures.immediateCheckedFuture(expectedStateInterface)).when(mockReadTx).read(
161                 LogicalDatastoreType.OPERATIONAL,interfaceStateIdentifier);
162         doReturn(Futures.immediateCheckedFuture(expectedInterface)).when(mockReadTx).read(
163                 LogicalDatastoreType.CONFIGURATION, interfaceInstanceIdentifier);
164         doReturn(Futures.immediateCheckedFuture(expectedservicesInfo)).when(mockReadTx).read(
165                 LogicalDatastoreType.CONFIGURATION, servicesInfoIdentifierBuilder.build());
166
167         bindHelper.bindService(boundServicesIid,boundServiceNew,dataBroker);
168
169         verify(mockWriteTx).put(LogicalDatastoreType.CONFIGURATION,flowInstanceId,ingressFlow, true);
170     }
171
172     @Test
173     public void testConfigUnbindSingleService(){
174
175         Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> expectedStateInterface = Optional.of(stateInterface);
176         Optional<Interface> expectedInterface = Optional.of(interfaceEnabled);
177         Optional<ServicesInfo>expectedservicesInfo = Optional.of(servicesInfoUnbind);
178
179         ServicesInfoKey servicesInfoKey = new ServicesInfoKey(InterfaceManagerTestUtil.interfaceName);
180         InstanceIdentifier.InstanceIdentifierBuilder<ServicesInfo> servicesInfoIdentifierBuilder =
181                 InstanceIdentifier.builder(ServiceBindings.class).child(ServicesInfo.class, servicesInfoKey);
182
183         doReturn(Futures.immediateCheckedFuture(expectedStateInterface)).when(mockReadTx).read(
184                 LogicalDatastoreType.OPERATIONAL,interfaceStateIdentifier);
185         doReturn(Futures.immediateCheckedFuture(expectedInterface)).when(mockReadTx).read(
186                 LogicalDatastoreType.CONFIGURATION, interfaceInstanceIdentifier);
187         doReturn(Futures.immediateCheckedFuture(expectedservicesInfo)).when(mockReadTx).read(
188                 LogicalDatastoreType.CONFIGURATION,servicesInfoIdentifierBuilder.build());
189
190         unbindHelper.unbindService(boundServicesIid,boundServiceNew,dataBroker);
191
192         verify(mockWriteTx).delete(LogicalDatastoreType.CONFIGURATION,flowInstanceId);
193     }
194
195 }