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.IfmUtil;
26 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.confighelpers.FlowBasedServicesConfigBindHelper;
27 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.confighelpers.FlowBasedServicesConfigUnbindHelper;
28 import org.opendaylight.vpnservice.mdsalutil.*;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
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.TunnelTypeGre;
47 import org.opendaylight.yangtools.concepts.ListenerRegistration;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import java.math.BigInteger;
51 import java.util.ArrayList;
52 import java.util.List;
54 import static org.mockito.Matchers.any;
55 import static org.mockito.Mockito.*;
57 @RunWith(MockitoJUnitRunner.class)
58 public class FlowBasedServicesConfigurationTest {
60 BigInteger dpId = BigInteger.valueOf(1);
62 int instructionKeyval = 2;
64 Interface interfaceEnabled = null;
65 String serviceName = "VPN";
66 InstanceIdentifier<BoundServices> boundServicesIid = null;
67 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface stateInterface;
68 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> interfaceStateIdentifier = null;
69 BoundServices boundServiceNew = null;
70 NodeConnectorId nodeConnectorId = null;
71 ServicesInfo servicesInfo = null;
72 ServicesInfo servicesInfoUnbind = null;
73 StypeOpenflow stypeOpenflow = null;
74 InstanceIdentifier<Interface> interfaceInstanceIdentifier = null;
75 InstanceIdentifier<Flow> flowInstanceId = null;
76 Flow ingressFlow = null;
77 Instruction instruction = null;
78 InstructionKey instructionKey = null;
79 List<Instruction>instructions = new ArrayList<>();
83 @Mock DataBroker dataBroker;
84 @Mock ListenerRegistration<DataChangeListener> dataChangeListenerRegistration;
85 @Mock ReadOnlyTransaction mockReadTx;
86 @Mock WriteTransaction mockWriteTx;
88 FlowBasedServicesConfigBindHelper bindHelper;
89 FlowBasedServicesConfigUnbindHelper unbindHelper;
92 public void setUp() throws Exception {
93 when(dataBroker.registerDataChangeListener(
94 any(LogicalDatastoreType.class),
95 any(InstanceIdentifier.class),
96 any(DataChangeListener.class),
97 any(DataChangeScope.class)))
98 .thenReturn(dataChangeListenerRegistration);
102 private void setupMocks(){
104 interfaceEnabled = InterfaceManagerTestUtil.buildInterface(InterfaceManagerTestUtil.interfaceName, "Test Vlan Interface1",true,L2vlan.class,dpId);
105 nodeConnectorId = InterfaceManagerTestUtil.buildNodeConnectorId(dpId, portNum);
106 interfaceInstanceIdentifier = IfmUtil.buildId(InterfaceManagerTestUtil.interfaceName);
108 InterfaceBuilder ifaceBuilder = new InterfaceBuilder();
109 List<String> lowerLayerIfList = new ArrayList<>();
110 lowerLayerIfList.add(nodeConnectorId.getValue());
111 ifaceBuilder.setOperStatus(OperStatus.Up).setAdminStatus(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus.Up)
112 .setPhysAddress(PhysAddress.getDefaultInstance("AA:AA:AA:AA:AA:AA"))
113 .setIfIndex(ifIndexval)
114 .setLowerLayerIf(lowerLayerIfList)
115 .setKey(IfmUtil.getStateInterfaceKeyFromName(InterfaceManagerTestUtil.interfaceName))
116 .setName(InterfaceManagerTestUtil.interfaceName);
118 stypeOpenflow = InterfaceManagerTestUtil.buildStypeOpenflow(dpId, flowPriority,NwConstants.LPORT_DISPATCHER_TABLE,instructions);
119 boundServiceNew = InterfaceManagerTestUtil.buildBoundServices(serviceName, key, new BoundServicesKey(key), stypeOpenflow);
120 instructionKey = new InstructionKey(instructionKeyval);
121 BigInteger[] metadataValues = IfmUtil.mergeOpenflowMetadataWriteInstructions(instructions);
122 short sIndex = boundServiceNew.getServicePriority();
123 BigInteger metadata = MetaDataUtil.getMetaDataForLPortDispatcher(ifaceBuilder.getIfIndex(),
124 ++sIndex, metadataValues[0]);
125 BigInteger mask = MetaDataUtil.getMetaDataMaskForLPortDispatcher(
126 MetaDataUtil.METADATA_MASK_SERVICE_INDEX,
127 MetaDataUtil.METADATA_MASK_LPORT_TAG, metadataValues[1]);
129 instruction = InterfaceManagerTestUtil.buildInstruction(InterfaceManagerTestUtil.buildWriteMetaDataCase(InterfaceManagerTestUtil.buildWriteMetaData(metadata, mask)),
130 new InstructionKey(instructionKey));
131 instructions.add(instruction);
132 ServicesInfoKey servicesInfoKey = new ServicesInfoKey(InterfaceManagerTestUtil.interfaceName);
133 boundServicesIid = InstanceIdentifier.builder(ServiceBindings.class).child(ServicesInfo.class, servicesInfoKey).
134 child(BoundServices.class, new BoundServicesKey(key)).build();
136 interfaceStateIdentifier = IfmUtil.buildStateInterfaceId(interfaceEnabled.getName());
137 stateInterface = ifaceBuilder.build();
138 List<BoundServices> boundServiceslist = new ArrayList<>();
139 boundServiceslist.add(boundServiceNew);
140 servicesInfo = InterfaceManagerTestUtil.buildServicesInfo(InterfaceManagerTestUtil.interfaceName, servicesInfoKey, boundServiceslist);
141 servicesInfoUnbind = InterfaceManagerTestUtil.buildServicesInfo(InterfaceManagerTestUtil.interfaceName,servicesInfoKey,new ArrayList<>());
143 String flowRef = InterfaceManagerTestUtil.buildflowRef(dpId,InterfaceManagerTestUtil.interfaceName,boundServiceNew.getServiceName(),boundServiceNew.getServicePriority());
144 List<Instruction> instructionList = boundServiceNew.getAugmentation(StypeOpenflow.class).getInstruction();
145 String serviceRef = boundServiceNew.getServiceName();
146 List<MatchInfo> matches = new ArrayList<>();
147 matches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
148 MetaDataUtil.getMetaDataForLPortDispatcher(ifaceBuilder.getIfIndex(), boundServiceNew.getServicePriority()),
149 MetaDataUtil.getMetaDataMaskForLPortDispatcher() }));
150 ingressFlow = MDSALUtil.buildFlowNew(NwConstants.LPORT_DISPATCHER_TABLE, flowRef, boundServiceNew.getServicePriority(), serviceRef, 0, 0,
151 stypeOpenflow.getFlowCookie(), matches, instructionList);
152 FlowKey flowKey = new FlowKey(new FlowId(ingressFlow.getId()));
153 flowInstanceId = InterfaceManagerTestUtil.getFlowInstanceIdentifier(dpId,ingressFlow.getTableId(),flowKey);
155 when(dataBroker.newReadOnlyTransaction()).thenReturn(mockReadTx);
156 when(dataBroker.newWriteOnlyTransaction()).thenReturn(mockWriteTx);
160 public void testConfigBindSingleService(){
162 Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> expectedStateInterface = Optional.of(stateInterface);
163 Optional<Interface> expectedInterface = Optional.of(interfaceEnabled);
164 Optional<ServicesInfo>expectedservicesInfo = Optional.of(servicesInfo);
166 ServicesInfoKey servicesInfoKey = new ServicesInfoKey(InterfaceManagerTestUtil.interfaceName);
167 InstanceIdentifier.InstanceIdentifierBuilder<ServicesInfo> servicesInfoIdentifierBuilder =
168 InstanceIdentifier.builder(ServiceBindings.class).child(ServicesInfo.class, servicesInfoKey);
171 doReturn(Futures.immediateCheckedFuture(expectedStateInterface)).when(mockReadTx).read(
172 LogicalDatastoreType.OPERATIONAL,interfaceStateIdentifier);
173 doReturn(Futures.immediateCheckedFuture(expectedInterface)).when(mockReadTx).read(
174 LogicalDatastoreType.CONFIGURATION, interfaceInstanceIdentifier);
175 doReturn(Futures.immediateCheckedFuture(expectedservicesInfo)).when(mockReadTx).read(
176 LogicalDatastoreType.CONFIGURATION, servicesInfoIdentifierBuilder.build());
178 bindHelper.bindService(boundServicesIid,boundServiceNew,dataBroker);
180 verify(mockWriteTx).put(LogicalDatastoreType.CONFIGURATION,flowInstanceId,ingressFlow, true);
184 public void testConfigUnbindSingleService(){
186 Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> expectedStateInterface = Optional.of(stateInterface);
187 Optional<Interface> expectedInterface = Optional.of(interfaceEnabled);
188 Optional<ServicesInfo>expectedservicesInfo = Optional.of(servicesInfoUnbind);
190 ServicesInfoKey servicesInfoKey = new ServicesInfoKey(InterfaceManagerTestUtil.interfaceName);
191 InstanceIdentifier.InstanceIdentifierBuilder<ServicesInfo> servicesInfoIdentifierBuilder =
192 InstanceIdentifier.builder(ServiceBindings.class).child(ServicesInfo.class, servicesInfoKey);
194 doReturn(Futures.immediateCheckedFuture(expectedStateInterface)).when(mockReadTx).read(
195 LogicalDatastoreType.OPERATIONAL,interfaceStateIdentifier);
196 doReturn(Futures.immediateCheckedFuture(expectedInterface)).when(mockReadTx).read(
197 LogicalDatastoreType.CONFIGURATION, interfaceInstanceIdentifier);
198 doReturn(Futures.immediateCheckedFuture(expectedservicesInfo)).when(mockReadTx).read(
199 LogicalDatastoreType.CONFIGURATION,servicesInfoIdentifierBuilder.build());
201 unbindHelper.unbindService(boundServicesIid,boundServiceNew,dataBroker);
203 verify(mockWriteTx).delete(LogicalDatastoreType.CONFIGURATION,flowInstanceId);