2 * Copyright (c) 2015 - 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
8 package org.opendaylight.vpnservice.interfacemgr;
10 import java.math.BigInteger;
11 import java.util.List;
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.Future;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
20 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
21 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
22 import org.opendaylight.vpnservice.interfacemgr.globals.InterfaceInfo;
23 import org.opendaylight.vpnservice.interfacemgr.globals.InterfaceInfo.InterfaceAdminState;
24 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
25 import org.opendaylight.vpnservice.interfacemgr.listeners.InterfaceConfigListener;
26 import org.opendaylight.vpnservice.interfacemgr.listeners.InterfaceInventoryStateListener;
27 import org.opendaylight.vpnservice.interfacemgr.listeners.InterfaceTopologyStateListener;
28 import org.opendaylight.vpnservice.interfacemgr.listeners.VlanMemberConfigListener;
29 import org.opendaylight.vpnservice.interfacemgr.rpcservice.InterfaceManagerRpcService;
30 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.listeners.FlowBasedServicesConfigListener;
31 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.listeners.FlowBasedServicesInterfaceStateListener;
32 import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
33 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.CreateIdPoolInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.CreateIdPoolInputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetDpidFromInterfaceInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetDpidFromInterfaceInputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetDpidFromInterfaceOutput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetEndpointIpForDpnInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetEndpointIpForDpnInputBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetEndpointIpForDpnOutput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetPortFromInterfaceInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetPortFromInterfaceInputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetPortFromInterfaceOutput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.OdlInterfaceRpcService;
52 import org.opendaylight.yangtools.yang.common.RpcResult;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 public class InterfacemgrProvider implements BindingAwareProvider, AutoCloseable, IInterfaceManager {
58 private static final Logger LOG = LoggerFactory.getLogger(InterfacemgrProvider.class);
60 private RpcProviderRegistry rpcProviderRegistry;
61 private IdManagerService idManager;
62 private IMdsalApiManager mdsalManager;
63 private InterfaceConfigListener interfaceConfigListener;
64 private InterfaceTopologyStateListener topologyStateListener;
65 private InterfaceInventoryStateListener interfaceInventoryStateListener;
66 private FlowBasedServicesInterfaceStateListener flowBasedServicesInterfaceStateListener;
67 private FlowBasedServicesConfigListener flowBasedServicesConfigListener;
68 private VlanMemberConfigListener vlanMemberConfigListener;
69 private DataBroker dataBroker;
70 private InterfaceManagerRpcService interfaceManagerRpcService;
71 private BindingAwareBroker.RpcRegistration<OdlInterfaceRpcService> rpcRegistration;
73 public void setRpcProviderRegistry(RpcProviderRegistry rpcProviderRegistry) {
74 this.rpcProviderRegistry = rpcProviderRegistry;
77 public void setMdsalManager(IMdsalApiManager mdsalManager) {
78 this.mdsalManager = mdsalManager;
82 public void onSessionInitiated(ProviderContext session) {
83 LOG.info("InterfacemgrProvider Session Initiated");
85 dataBroker = session.getSALService(DataBroker.class);
86 idManager = rpcProviderRegistry.getRpcService(IdManagerService.class);
89 interfaceManagerRpcService = new InterfaceManagerRpcService(dataBroker, mdsalManager);
90 rpcRegistration = getRpcProviderRegistry().addRpcImplementation(
91 OdlInterfaceRpcService.class, interfaceManagerRpcService);
93 interfaceConfigListener = new InterfaceConfigListener(dataBroker, idManager);
94 interfaceConfigListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
96 interfaceInventoryStateListener = new InterfaceInventoryStateListener(dataBroker, idManager, mdsalManager);
97 interfaceInventoryStateListener.registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
99 topologyStateListener = new InterfaceTopologyStateListener(dataBroker);
100 topologyStateListener.registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
102 flowBasedServicesConfigListener = new FlowBasedServicesConfigListener(dataBroker);
103 flowBasedServicesConfigListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
105 flowBasedServicesInterfaceStateListener =
106 new FlowBasedServicesInterfaceStateListener(dataBroker);
107 flowBasedServicesInterfaceStateListener.registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
109 vlanMemberConfigListener =
110 new VlanMemberConfigListener(dataBroker, idManager);
111 vlanMemberConfigListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
112 } catch (Exception e) {
113 LOG.error("Error initializing services", e);
117 private void createIdPool() {
118 CreateIdPoolInput createPool = new CreateIdPoolInputBuilder()
119 .setPoolName(IfmConstants.IFM_IDPOOL_NAME)
120 .setLow(IfmConstants.IFM_ID_POOL_START)
121 .setHigh(IfmConstants.IFM_ID_POOL_END)
123 //TODO: Error handling
124 Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
126 if ((result != null) && (result.get().isSuccessful())) {
127 LOG.debug("Created IdPool for InterfaceMgr");
129 } catch (InterruptedException | ExecutionException e) {
130 LOG.error("Failed to create idPool for InterfaceMgr",e);
135 public void close() throws Exception {
136 LOG.info("InterfacemgrProvider Closed");
137 interfaceConfigListener.close();
138 rpcRegistration.close();
141 public RpcProviderRegistry getRpcProviderRegistry() {
142 return rpcProviderRegistry;
146 public Long getPortForInterface(String ifName) {
147 GetPortFromInterfaceInput input = new GetPortFromInterfaceInputBuilder().setIntfName(ifName).build();
148 Future<RpcResult<GetPortFromInterfaceOutput>> output = interfaceManagerRpcService.getPortFromInterface(input);
150 RpcResult<GetPortFromInterfaceOutput> port = output.get();
151 if(port.isSuccessful()){
152 return port.getResult().getPortno();
154 }catch(NullPointerException | InterruptedException | ExecutionException e){
155 LOG.warn("Exception when getting port for interface",e);
161 public Long getPortForInterface(Interface intf) {
162 GetPortFromInterfaceInput input = new GetPortFromInterfaceInputBuilder().setIntfName(intf.getName()).build();
163 Future<RpcResult<GetPortFromInterfaceOutput>> output = interfaceManagerRpcService.getPortFromInterface(input);
165 RpcResult<GetPortFromInterfaceOutput> port = output.get();
166 if(port.isSuccessful()){
167 return port.getResult().getPortno();
169 }catch(NullPointerException | InterruptedException | ExecutionException e){
170 LOG.warn("Exception when getting port for interface",e);
176 public InterfaceInfo getInterfaceInfo(String interfaceName) {
177 //FIXME [ELANBE] This is not working yet, fix this
179 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface
180 ifState = InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName,dataBroker);
183 LOG.error("Interface {} is not present", interfaceName);
187 Integer lportTag = ifState.getIfIndex();
188 Interface intf = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(new InterfaceKey(interfaceName), dataBroker);
190 NodeConnectorId ncId = IfmUtil.getNodeConnectorIdFromInterface(intf, dataBroker);
191 InterfaceInfo.InterfaceType interfaceType = IfmUtil.getInterfaceType(intf);
192 InterfaceInfo interfaceInfo = null;
193 BigInteger dpId = org.opendaylight.vpnservice.interfacemgr.globals.IfmConstants.INVALID_DPID;
194 Integer portNo = org.opendaylight.vpnservice.interfacemgr.globals.IfmConstants.INVALID_PORT_NO;
196 dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(ncId));
197 portNo = Integer.parseInt(IfmUtil.getPortNoFromNodeConnectorId(ncId));
200 if(interfaceType == InterfaceInfo.InterfaceType.VLAN_INTERFACE){
201 interfaceInfo = IfmUtil.getVlanInterfaceInfo(interfaceName, intf, dpId);
202 } else if (interfaceType == InterfaceInfo.InterfaceType.VXLAN_TRUNK_INTERFACE || interfaceType == InterfaceInfo.InterfaceType.GRE_TRUNK_INTERFACE) {/*
203 trunkInterfaceInfo trunkInterfaceInfo = (TrunkInterfaceInfo) ConfigIfmUtil.getTrunkInterfaceInfo(ifName, ConfigIfmUtil.getInterfaceByIfName(dataBroker, ifName));
204 String higherLayerIf = inf.getHigherLayerIf().get(0);
205 Interface vlanInterface = ConfigIfmUtil.getInterfaceByIfName(dataBroker, higherLayerIf);
206 trunkInterfaceInfo.setPortName(vlanInterface.getAugmentation(BaseConfig.class).getParentInterface());
207 trunkInterfaceManager.updateTargetMacAddressInInterfaceInfo(trunkInterfaceInfo, trunkInterface);
208 if (trunkInterface.getPhysAddress() != null) {
209 trunkInterfaceInfo.setLocalMacAddress(trunkInterface.getPhysAddress().getValue());
211 interfaceInfo = trunkInterfaceInfo;
212 interfaceInfo.setL2domainGroupId(IfmUtil.getGroupId(OperationalIfmUtil.getInterfaceStateByIfName(dataBroker, higherLayerIf).getIfIndex(), InterfaceType.VLAN_INTERFACE));
214 LOG.error("Type of Interface {} is unknown", interfaceName);
217 interfaceInfo.setDpId(dpId);
218 interfaceInfo.setPortNo(portNo);
219 interfaceInfo.setAdminState((intf.isEnabled() == true) ? InterfaceAdminState.ENABLED : InterfaceAdminState.DISABLED);
220 interfaceInfo.setInterfaceName(interfaceName);
221 interfaceInfo.setInterfaceTag(lportTag);
222 interfaceInfo.setInterfaceType(interfaceType);
223 interfaceInfo.setGroupId(IfmUtil.getGroupId(lportTag, interfaceType));
224 interfaceInfo.setOpState((ifState.getOperStatus() == OperStatus.Up) ? InterfaceInfo.InterfaceOpState.UP : InterfaceInfo.InterfaceOpState.DOWN);
227 return interfaceInfo;
232 public InterfaceInfo getInterfaceInfoFromOperationalDataStore(String interfaceName, InterfaceInfo.InterfaceType interfaceType) {
233 InterfaceInfo interfaceInfo = new InterfaceInfo(interfaceName);
234 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState = InterfaceManagerCommonUtils
235 .getInterfaceStateFromOperDS(interfaceName, dataBroker);
236 if (ifState == null) {
237 LOG.error("Interface {} is not present", interfaceName);
240 Integer lportTag = ifState.getIfIndex();
241 Interface intf = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(new InterfaceKey(interfaceName), dataBroker);
242 NodeConnectorId ncId = IfmUtil.getNodeConnectorIdFromInterface(intf, dataBroker);
244 interfaceInfo.setDpId(new BigInteger(IfmUtil.getDpnFromNodeConnectorId(ncId)));
245 interfaceInfo.setPortNo(Integer.parseInt(IfmUtil.getPortNoFromNodeConnectorId(ncId)));
247 interfaceInfo.setAdminState((intf.isEnabled() == true) ? InterfaceAdminState.ENABLED : InterfaceAdminState.DISABLED);
248 interfaceInfo.setInterfaceName(interfaceName);
249 interfaceInfo.setInterfaceTag(lportTag);
250 interfaceInfo.setInterfaceType(interfaceType);
251 interfaceInfo.setGroupId(IfmUtil.getGroupId(lportTag, interfaceType));
252 interfaceInfo.setOpState((ifState.getOperStatus() == OperStatus.Up) ? InterfaceInfo.InterfaceOpState.UP : InterfaceInfo.InterfaceOpState.DOWN);
255 return interfaceInfo;
259 public BigInteger getDpnForInterface(String ifName) {
260 GetDpidFromInterfaceInput input = new GetDpidFromInterfaceInputBuilder().setIntfName(ifName).build();
261 Future<RpcResult<GetDpidFromInterfaceOutput>> output = interfaceManagerRpcService.getDpidFromInterface(input);
263 RpcResult<GetDpidFromInterfaceOutput> dpn = output.get();
264 if(dpn.isSuccessful()){
265 return dpn.getResult().getDpid();
267 }catch(NullPointerException | InterruptedException | ExecutionException e){
268 LOG.warn("Exception when getting port for interface",e);
274 public String getEndpointIpForDpn(BigInteger dpnId) {
275 GetEndpointIpForDpnInput input = new GetEndpointIpForDpnInputBuilder().setDpid(dpnId).build();
276 Future<RpcResult<GetEndpointIpForDpnOutput>> output = interfaceManagerRpcService.getEndpointIpForDpn(input);
278 RpcResult<GetEndpointIpForDpnOutput> ipForDpnOutputRpcResult = output.get();
279 if(ipForDpnOutputRpcResult.isSuccessful()){
280 List<IpAddress> localIps = ipForDpnOutputRpcResult.getResult().getLocalIps();
281 if(!localIps.isEmpty()) {
282 return localIps.get(0).getIpv4Address().getValue();
285 }catch(NullPointerException | InterruptedException | ExecutionException e){
286 LOG.warn("Exception when getting port for interface",e);
292 public List<ActionInfo> getInterfaceEgressActions(String ifName) {
293 return interfaceManagerRpcService.getEgressActionInfosForInterface(ifName);
297 public BigInteger getDpnForInterface(Interface intrf) {
298 return getDpnForInterface(intrf.getName());