2 * Copyright (c) 2015 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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
15 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
16 import org.opendaylight.vpnservice.interfacemgr.listeners.InterfaceConfigListener;
17 import org.opendaylight.vpnservice.interfacemgr.listeners.InterfaceInventoryStateListener;
18 import org.opendaylight.vpnservice.interfacemgr.listeners.InterfaceTopologyStateListener;
19 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
20 import org.opendaylight.vpnservice.interfacemgr.rpcservice.InterfaceManagerRpcService;
21 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.listeners.FlowBasedServicesConfigListener;
22 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.listeners.FlowBasedServicesInterfaceStateListener;
23 import org.opendaylight.vpnservice.interfacemgr.listeners.VlanMemberConfigListener;
24 import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
25 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
26 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.CreateIdPoolInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.CreateIdPoolInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.*;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import java.math.BigInteger;
38 import java.util.List;
39 import java.util.concurrent.ExecutionException;
40 import java.util.concurrent.Future;
42 public class InterfacemgrProvider implements BindingAwareProvider, AutoCloseable, IInterfaceManager {
44 private static final Logger LOG = LoggerFactory.getLogger(InterfacemgrProvider.class);
46 private RpcProviderRegistry rpcProviderRegistry;
47 private IdManagerService idManager;
48 private IMdsalApiManager mdsalManager;
49 private InterfaceConfigListener interfaceConfigListener;
50 private InterfaceTopologyStateListener topologyStateListener;
51 private InterfaceInventoryStateListener interfaceInventoryStateListener;
52 private FlowBasedServicesInterfaceStateListener flowBasedServicesInterfaceStateListener;
53 private FlowBasedServicesConfigListener flowBasedServicesConfigListener;
54 private VlanMemberConfigListener vlanMemberConfigListener;
56 private InterfaceManagerRpcService interfaceManagerRpcService;
57 private BindingAwareBroker.RpcRegistration<OdlInterfaceRpcService> rpcRegistration;
59 public void setRpcProviderRegistry(RpcProviderRegistry rpcProviderRegistry) {
60 this.rpcProviderRegistry = rpcProviderRegistry;
63 public void setMdsalManager(IMdsalApiManager mdsalManager) {
64 this.mdsalManager = mdsalManager;
68 public void onSessionInitiated(ProviderContext session) {
69 LOG.info("InterfacemgrProvider Session Initiated");
71 final DataBroker dataBroker = session.getSALService(DataBroker.class);
72 idManager = rpcProviderRegistry.getRpcService(IdManagerService.class);
75 interfaceManagerRpcService = new InterfaceManagerRpcService(dataBroker, mdsalManager);
76 rpcRegistration = getRpcProviderRegistry().addRpcImplementation(
77 OdlInterfaceRpcService.class, interfaceManagerRpcService);
79 interfaceConfigListener = new InterfaceConfigListener(dataBroker, idManager);
80 interfaceConfigListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
82 interfaceInventoryStateListener = new InterfaceInventoryStateListener(dataBroker, idManager);
83 interfaceInventoryStateListener.registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
85 topologyStateListener = new InterfaceTopologyStateListener(dataBroker);
86 topologyStateListener.registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
88 flowBasedServicesConfigListener = new FlowBasedServicesConfigListener(dataBroker);
89 flowBasedServicesConfigListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
91 flowBasedServicesInterfaceStateListener =
92 new FlowBasedServicesInterfaceStateListener(dataBroker);
93 flowBasedServicesInterfaceStateListener.registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
95 vlanMemberConfigListener =
96 new VlanMemberConfigListener(dataBroker, idManager);
97 vlanMemberConfigListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
98 } catch (Exception e) {
99 LOG.error("Error initializing services", e);
103 private void createIdPool() {
104 CreateIdPoolInput createPool = new CreateIdPoolInputBuilder()
105 .setPoolName(IfmConstants.IFM_IDPOOL_NAME)
106 .setLow(IfmConstants.IFM_ID_POOL_START)
107 .setHigh(IfmConstants.IFM_ID_POOL_END)
109 //TODO: Error handling
110 Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
112 if ((result != null) && (result.get().isSuccessful())) {
113 LOG.debug("Created IdPool for InterfaceMgr");
115 } catch (InterruptedException | ExecutionException e) {
116 LOG.error("Failed to create idPool for InterfaceMgr",e);
121 public void close() throws Exception {
122 LOG.info("InterfacemgrProvider Closed");
123 interfaceConfigListener.close();
124 rpcRegistration.close();
127 public RpcProviderRegistry getRpcProviderRegistry() {
128 return rpcProviderRegistry;
132 public Long getPortForInterface(String ifName) {
133 GetPortFromInterfaceInput input = new GetPortFromInterfaceInputBuilder().setIntfName(ifName).build();
134 Future<RpcResult<GetPortFromInterfaceOutput>> output = interfaceManagerRpcService.getPortFromInterface(input);
136 RpcResult<GetPortFromInterfaceOutput> port = output.get();
137 if(port.isSuccessful()){
138 return port.getResult().getPortno();
140 }catch(NullPointerException | InterruptedException | ExecutionException e){
141 LOG.warn("Exception when getting port for interface",e);
147 public Long getPortForInterface(Interface intf) {
148 GetPortFromInterfaceInput input = new GetPortFromInterfaceInputBuilder().setIntfName(intf.getName()).build();
149 Future<RpcResult<GetPortFromInterfaceOutput>> output = interfaceManagerRpcService.getPortFromInterface(input);
151 RpcResult<GetPortFromInterfaceOutput> port = output.get();
152 if(port.isSuccessful()){
153 return port.getResult().getPortno();
155 }catch(NullPointerException | InterruptedException | ExecutionException e){
156 LOG.warn("Exception when getting port for interface",e);
162 public BigInteger getDpnForInterface(String ifName) {
163 GetDpidFromInterfaceInput input = new GetDpidFromInterfaceInputBuilder().setIntfName(ifName).build();
164 Future<RpcResult<GetDpidFromInterfaceOutput>> output = interfaceManagerRpcService.getDpidFromInterface(input);
166 RpcResult<GetDpidFromInterfaceOutput> dpn = output.get();
167 if(dpn.isSuccessful()){
168 return dpn.getResult().getDpid();
170 }catch(NullPointerException | InterruptedException | ExecutionException e){
171 LOG.warn("Exception when getting port for interface",e);
177 public String getEndpointIpForDpn(BigInteger dpnId) {
178 GetEndpointIpForDpnInput input = new GetEndpointIpForDpnInputBuilder().setDpid(dpnId).build();
179 Future<RpcResult<GetEndpointIpForDpnOutput>> output = interfaceManagerRpcService.getEndpointIpForDpn(input);
181 RpcResult<GetEndpointIpForDpnOutput> ipForDpnOutputRpcResult = output.get();
182 if(ipForDpnOutputRpcResult.isSuccessful()){
183 List<IpAddress> localIps = ipForDpnOutputRpcResult.getResult().getLocalIps();
184 if(!localIps.isEmpty()) {
185 return localIps.get(0).getIpv4Address().getValue();
188 }catch(NullPointerException | InterruptedException | ExecutionException e){
189 LOG.warn("Exception when getting port for interface",e);
195 public List<ActionInfo> getInterfaceEgressActions(String ifName) {
196 return interfaceManagerRpcService.getEgressActionInfosForInterface(ifName);
200 public BigInteger getDpnForInterface(Interface intrf) {
201 return getDpnForInterface(intrf.getName());