BUG:5021 ELan datapath code-changes and cleanUp some code.
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / InterfacemgrProvider.java
1 /*
2  * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.vpnservice.interfacemgr;
9
10 import java.math.BigInteger;
11 import java.util.List;
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.Future;
14
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;
55
56 public class InterfacemgrProvider implements BindingAwareProvider, AutoCloseable, IInterfaceManager {
57
58     private static final Logger LOG = LoggerFactory.getLogger(InterfacemgrProvider.class);
59
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;
72
73     public void setRpcProviderRegistry(RpcProviderRegistry rpcProviderRegistry) {
74         this.rpcProviderRegistry = rpcProviderRegistry;
75     }
76
77     public void setMdsalManager(IMdsalApiManager mdsalManager) {
78         this.mdsalManager = mdsalManager;
79     }
80
81     @Override
82     public void onSessionInitiated(ProviderContext session) {
83         LOG.info("InterfacemgrProvider Session Initiated");
84         try {
85             dataBroker = session.getSALService(DataBroker.class);
86             idManager = rpcProviderRegistry.getRpcService(IdManagerService.class);
87             createIdPool();
88
89             interfaceManagerRpcService = new InterfaceManagerRpcService(dataBroker, mdsalManager);
90             rpcRegistration = getRpcProviderRegistry().addRpcImplementation(
91                     OdlInterfaceRpcService.class, interfaceManagerRpcService);
92
93             interfaceConfigListener = new InterfaceConfigListener(dataBroker, idManager);
94             interfaceConfigListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
95
96             interfaceInventoryStateListener = new InterfaceInventoryStateListener(dataBroker, idManager, mdsalManager);
97             interfaceInventoryStateListener.registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
98
99             topologyStateListener = new InterfaceTopologyStateListener(dataBroker);
100             topologyStateListener.registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
101
102             flowBasedServicesConfigListener = new FlowBasedServicesConfigListener(dataBroker);
103             flowBasedServicesConfigListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
104
105             flowBasedServicesInterfaceStateListener =
106                     new FlowBasedServicesInterfaceStateListener(dataBroker);
107             flowBasedServicesInterfaceStateListener.registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
108
109             vlanMemberConfigListener =
110                                new VlanMemberConfigListener(dataBroker, idManager);
111             vlanMemberConfigListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
112         } catch (Exception e) {
113             LOG.error("Error initializing services", e);
114         }
115     }
116
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)
122             .build();
123         //TODO: Error handling
124         Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
125         try {
126             if ((result != null) && (result.get().isSuccessful())) {
127                 LOG.debug("Created IdPool for InterfaceMgr");
128             }
129         } catch (InterruptedException | ExecutionException e) {
130             LOG.error("Failed to create idPool for InterfaceMgr",e);
131         }
132     }
133
134     @Override
135     public void close() throws Exception {
136         LOG.info("InterfacemgrProvider Closed");
137         interfaceConfigListener.close();
138         rpcRegistration.close();
139     }
140
141     public RpcProviderRegistry getRpcProviderRegistry() {
142         return rpcProviderRegistry;
143     }
144
145     @Override
146     public Long getPortForInterface(String ifName) {
147         GetPortFromInterfaceInput input = new GetPortFromInterfaceInputBuilder().setIntfName(ifName).build();
148         Future<RpcResult<GetPortFromInterfaceOutput>> output = interfaceManagerRpcService.getPortFromInterface(input);
149         try {
150             RpcResult<GetPortFromInterfaceOutput> port = output.get();
151             if(port.isSuccessful()){
152                 return port.getResult().getPortno();
153             }
154         }catch(NullPointerException | InterruptedException | ExecutionException e){
155             LOG.warn("Exception when getting port for interface",e);
156         }
157         return null;
158     }
159
160     @Override
161     public Long getPortForInterface(Interface intf) {
162         GetPortFromInterfaceInput input = new GetPortFromInterfaceInputBuilder().setIntfName(intf.getName()).build();
163         Future<RpcResult<GetPortFromInterfaceOutput>> output = interfaceManagerRpcService.getPortFromInterface(input);
164         try {
165             RpcResult<GetPortFromInterfaceOutput> port = output.get();
166             if(port.isSuccessful()){
167                 return port.getResult().getPortno();
168             }
169         }catch(NullPointerException | InterruptedException | ExecutionException e){
170             LOG.warn("Exception when getting port for interface",e);
171         }
172         return null;
173     }
174
175     @Override
176     public InterfaceInfo getInterfaceInfo(String interfaceName) {
177         //FIXME [ELANBE] This is not working yet, fix this
178
179         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface
180                 ifState = InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName,dataBroker);
181
182         if(ifState == null){
183             LOG.error("Interface {} is not present", interfaceName);
184             return null;
185         }
186
187         Integer lportTag = ifState.getIfIndex();
188         Interface intf = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(new InterfaceKey(interfaceName), dataBroker);
189
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;
195         if (ncId !=null ) {
196             dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(ncId));
197             portNo = Integer.parseInt(IfmUtil.getPortNoFromNodeConnectorId(ncId));
198         }
199
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());
210             }
211             interfaceInfo = trunkInterfaceInfo;
212             interfaceInfo.setL2domainGroupId(IfmUtil.getGroupId(OperationalIfmUtil.getInterfaceStateByIfName(dataBroker, higherLayerIf).getIfIndex(), InterfaceType.VLAN_INTERFACE));
213         */} else {
214             LOG.error("Type of Interface {} is unknown", interfaceName);
215             return null;
216         }
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);
225
226
227         return interfaceInfo;
228
229     }
230
231     @Override
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);
238             return null;
239         }
240         Integer lportTag = ifState.getIfIndex();
241         Interface intf = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(new InterfaceKey(interfaceName), dataBroker);
242         NodeConnectorId ncId = IfmUtil.getNodeConnectorIdFromInterface(intf, dataBroker);
243         if (ncId != null) {
244             interfaceInfo.setDpId(new BigInteger(IfmUtil.getDpnFromNodeConnectorId(ncId)));
245             interfaceInfo.setPortNo(Integer.parseInt(IfmUtil.getPortNoFromNodeConnectorId(ncId)));
246         }
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);
253
254
255         return interfaceInfo;
256     }
257
258     @Override
259     public BigInteger getDpnForInterface(String ifName) {
260         GetDpidFromInterfaceInput input = new GetDpidFromInterfaceInputBuilder().setIntfName(ifName).build();
261         Future<RpcResult<GetDpidFromInterfaceOutput>> output = interfaceManagerRpcService.getDpidFromInterface(input);
262         try {
263             RpcResult<GetDpidFromInterfaceOutput> dpn = output.get();
264             if(dpn.isSuccessful()){
265                 return dpn.getResult().getDpid();
266             }
267         }catch(NullPointerException | InterruptedException | ExecutionException e){
268             LOG.warn("Exception when getting port for interface",e);
269         }
270         return null;
271     }
272
273     @Override
274     public String getEndpointIpForDpn(BigInteger dpnId) {
275         GetEndpointIpForDpnInput input = new GetEndpointIpForDpnInputBuilder().setDpid(dpnId).build();
276         Future<RpcResult<GetEndpointIpForDpnOutput>> output = interfaceManagerRpcService.getEndpointIpForDpn(input);
277         try {
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();
283                 }
284             }
285         }catch(NullPointerException | InterruptedException | ExecutionException e){
286             LOG.warn("Exception when getting port for interface",e);
287         }
288         return null;
289     }
290
291     @Override
292     public List<ActionInfo> getInterfaceEgressActions(String ifName) {
293         return interfaceManagerRpcService.getEgressActionInfosForInterface(ifName);
294     }
295
296     @Override
297     public BigInteger getDpnForInterface(Interface intrf) {
298         return getDpnForInterface(intrf.getName());
299     }
300 }