2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.vpnservice.interfacemgr.test;
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.Futures;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Mock;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
21 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
22 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.vpnservice.interfacemgr.IfmConstants;
26 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
27 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.statehelpers.FlowBasedServicesStateBindHelper;
28 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.statehelpers.FlowBasedServicesStateUnbindHelper;
29 import org.opendaylight.vpnservice.mdsalutil.*;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.ServiceBindings;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.StypeOpenflow;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfo;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfoKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServices;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServicesKey;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeGre;
48 import org.opendaylight.yangtools.concepts.ListenerRegistration;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import java.math.BigInteger;
52 import java.util.ArrayList;
53 import java.util.List;
55 import static org.mockito.Matchers.any;
56 import static org.mockito.Mockito.*;
58 @RunWith(MockitoJUnitRunner.class)
59 public class FlowBasedServicesStateConfigurationTest {
61 Interface interfaceEnabled = null;
63 int instructionKeyval = 2;
64 InstanceIdentifier<Interface> interfaceInstanceIdentifier = null;
65 InstanceIdentifier<Flow> flowInstanceId = null;
66 Flow ingressFlow = null;
67 BigInteger dpId = BigInteger.valueOf(1);
68 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface stateInterface;
69 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> interfaceStateIdentifier = null;
70 ServicesInfo servicesInfo = null;
71 NodeConnectorId nodeConnectorId = null;
72 BoundServices boundService = null;
73 StypeOpenflow stypeOpenflow = null;
74 Instruction instruction = null;
75 InstructionKey instructionKey = null;
76 List<Instruction>instructions = new ArrayList<>();
80 String serviceName = "VPN";
82 @Mock DataBroker dataBroker;
83 @Mock ListenerRegistration<DataChangeListener> dataChangeListenerRegistration;
84 @Mock ReadOnlyTransaction mockReadTx;
85 @Mock WriteTransaction mockWriteTx;
87 FlowBasedServicesStateBindHelper bindHelper;
88 FlowBasedServicesStateUnbindHelper unbindHelper;
91 public void setUp() throws Exception {
92 when(dataBroker.registerDataChangeListener(
93 any(LogicalDatastoreType.class),
94 any(InstanceIdentifier.class),
95 any(DataChangeListener.class),
96 any(DataChangeScope.class)))
97 .thenReturn(dataChangeListenerRegistration);
101 private void setupMocks(){
103 interfaceEnabled = InterfaceManagerTestUtil.buildInterface(InterfaceManagerTestUtil.interfaceName, "Test Vlan Interface1", true, L2vlan.class, dpId);
104 interfaceInstanceIdentifier = IfmUtil.buildId(InterfaceManagerTestUtil.interfaceName);
105 nodeConnectorId = InterfaceManagerTestUtil.buildNodeConnectorId(BigInteger.valueOf(1), portNum);
106 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder ifaceBuilder = new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder();
107 List<String> lowerLayerIfList = new ArrayList<>();
108 lowerLayerIfList.add(nodeConnectorId.getValue());
109 ifaceBuilder.setOperStatus(OperStatus.Up).setAdminStatus(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus.Up)
110 .setPhysAddress(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress.getDefaultInstance("AA:AA:AA:AA:AA:AA"))
111 .setIfIndex(ifIndexval)
112 .setLowerLayerIf(lowerLayerIfList)
113 .setKey(IfmUtil.getStateInterfaceKeyFromName(InterfaceManagerTestUtil.interfaceName))
114 .setName(InterfaceManagerTestUtil.interfaceName).setType(interfaceEnabled.getType());
115 stypeOpenflow = InterfaceManagerTestUtil.buildStypeOpenflow(dpId,flowpriority, NwConstants.LPORT_DISPATCHER_TABLE, instructions);
116 instructionKey = new InstructionKey(instructionKeyval);
117 BigInteger[] metadataValues = IfmUtil.mergeOpenflowMetadataWriteInstructions(instructions);
118 boundService = InterfaceManagerTestUtil.buildBoundServices(serviceName,key,new BoundServicesKey(key),stypeOpenflow);
119 short sIndex = boundService.getServicePriority();
120 BigInteger metadata = MetaDataUtil.getMetaDataForLPortDispatcher(ifaceBuilder.getIfIndex(),
121 ++sIndex, metadataValues[0]);
122 BigInteger mask = MetaDataUtil.getMetaDataMaskForLPortDispatcher(
123 MetaDataUtil.METADATA_MASK_SERVICE_INDEX,
124 MetaDataUtil.METADATA_MASK_LPORT_TAG, metadataValues[1]);
125 instruction = InterfaceManagerTestUtil.buildInstruction(InterfaceManagerTestUtil.buildWriteMetaDataCase(InterfaceManagerTestUtil.buildWriteMetaData(metadata, mask)),
126 new InstructionKey(instructionKey));
127 instructions.add(instruction);
128 stateInterface = ifaceBuilder.build();
129 ServicesInfoKey servicesInfoKey = new ServicesInfoKey(InterfaceManagerTestUtil.interfaceName);
130 List<BoundServices> lowerlayerIfList = new ArrayList<>();
131 lowerlayerIfList.add(boundService);
132 interfaceStateIdentifier = IfmUtil.buildStateInterfaceId(interfaceEnabled.getName());
133 servicesInfo = InterfaceManagerTestUtil.buildServicesInfo(InterfaceManagerTestUtil.interfaceName,servicesInfoKey,lowerlayerIfList);
135 String flowRef = InterfaceManagerTestUtil.buildflowRef(dpId, InterfaceManagerTestUtil.interfaceName, boundService.getServiceName(), boundService.getServicePriority());
136 List<Instruction> instructionList = boundService.getAugmentation(StypeOpenflow.class).getInstruction();
137 String serviceRef = boundService.getServiceName();
138 List<MatchInfo> matches = new ArrayList<>();
139 matches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
140 MetaDataUtil.getMetaDataForLPortDispatcher(ifaceBuilder.getIfIndex(), boundService.getServicePriority()),
141 MetaDataUtil.getMetaDataMaskForLPortDispatcher() }));
142 ingressFlow = MDSALUtil.buildFlowNew(stypeOpenflow.getDispatcherTableId(), flowRef, boundService.getServicePriority(), serviceRef, 0, 0,
143 stypeOpenflow.getFlowCookie(), matches, instructionList);
144 FlowKey flowKey = new FlowKey(new FlowId(ingressFlow.getId()));
145 flowInstanceId = InterfaceManagerTestUtil.getFlowInstanceIdentifier(dpId,ingressFlow.getTableId(),flowKey);
147 when(dataBroker.newReadOnlyTransaction()).thenReturn(mockReadTx);
148 when(dataBroker.newWriteOnlyTransaction()).thenReturn(mockWriteTx);
152 public void testStateBindSingleService(){
154 Optional<ServicesInfo>expectedservicesInfo = Optional.of(servicesInfo);
155 Optional<Interface> expectedInterface = Optional.of(interfaceEnabled);
156 Optional<NodeConnectorId>expectednodeconnectorId = Optional.of(nodeConnectorId);
157 Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> expectedStateInterface = Optional.of(stateInterface);
159 ServicesInfoKey servicesInfoKey = new ServicesInfoKey(InterfaceManagerTestUtil.interfaceName);
160 InstanceIdentifier.InstanceIdentifierBuilder<ServicesInfo> servicesInfoIdentifierBuilder =
161 InstanceIdentifier.builder(ServiceBindings.class).child(ServicesInfo.class, servicesInfoKey);
163 doReturn(Futures.immediateCheckedFuture(expectedservicesInfo)).when(mockReadTx).read(
164 LogicalDatastoreType.CONFIGURATION,servicesInfoIdentifierBuilder.build());
165 doReturn(Futures.immediateCheckedFuture(expectedInterface)).when(mockReadTx).read(
166 LogicalDatastoreType.CONFIGURATION, interfaceInstanceIdentifier);
167 doReturn(Futures.immediateCheckedFuture(expectednodeconnectorId)).when(mockReadTx).read(
168 LogicalDatastoreType.OPERATIONAL,interfaceStateIdentifier);
169 doReturn(Futures.immediateCheckedFuture(expectedStateInterface)).when(mockReadTx).read(
170 LogicalDatastoreType.OPERATIONAL,interfaceStateIdentifier);
172 bindHelper.bindServicesOnInterface(stateInterface,dataBroker);
174 verify(mockWriteTx).put(LogicalDatastoreType.CONFIGURATION,flowInstanceId,ingressFlow, true);
179 public void testStateUnbindSingleService(){
181 Optional<ServicesInfo>expectedservicesInfo = Optional.of(servicesInfo);
182 Optional<Interface> expectedInterface = Optional.of(interfaceEnabled);
183 Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> expectedStateInterface = Optional.of(stateInterface);
185 ServicesInfoKey servicesInfoKey = new ServicesInfoKey(InterfaceManagerTestUtil.interfaceName);
186 InstanceIdentifier.InstanceIdentifierBuilder<ServicesInfo> servicesInfoIdentifierBuilder =
187 InstanceIdentifier.builder(ServiceBindings.class).child(ServicesInfo.class, servicesInfoKey);
189 doReturn(Futures.immediateCheckedFuture(expectedservicesInfo)).when(mockReadTx).read(
190 LogicalDatastoreType.CONFIGURATION,servicesInfoIdentifierBuilder.build());
191 doReturn(Futures.immediateCheckedFuture(expectedInterface)).when(mockReadTx).read(
192 LogicalDatastoreType.CONFIGURATION, interfaceInstanceIdentifier);
193 doReturn(Futures.immediateCheckedFuture(expectedStateInterface)).when(mockReadTx).read(
194 LogicalDatastoreType.OPERATIONAL,interfaceStateIdentifier);
196 unbindHelper.unbindServicesFromInterface(stateInterface,dataBroker);
198 verify(mockWriteTx).delete(LogicalDatastoreType.CONFIGURATION,flowInstanceId);