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.itm.impl;
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.binding.api.NotificationPublishService;
17 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
22 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
23 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
24 import org.opendaylight.vpnservice.itm.api.IITMProvider;
25 import org.opendaylight.vpnservice.itm.cli.TepCommandHelper;
26 import org.opendaylight.vpnservice.itm.globals.ITMConstants;
27 import org.opendaylight.vpnservice.itm.listeners.InterfaceStateListener;
28 import org.opendaylight.vpnservice.itm.listeners.TransportZoneListener;
29 import org.opendaylight.vpnservice.itm.listeners.TunnelMonitorChangeListener;
30 import org.opendaylight.vpnservice.itm.listeners.TunnelMonitorIntervalListener;
31 import org.opendaylight.vpnservice.itm.listeners.VtepConfigSchemaListener;
32 import org.opendaylight.vpnservice.itm.monitoring.ItmTunnelEventListener;
33 import org.opendaylight.vpnservice.itm.rpc.ItmManagerRpcService;
34 import org.opendaylight.vpnservice.itm.snd.ITMStatusMonitor;
35 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
36 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.VtepConfigSchemas;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.vtep.config.schemas.VtepConfigSchema;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.vtep.config.schemas.VtepConfigSchemaBuilder;
40 //import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.itm.config.rev151102.VtepConfigSchemas;
41 //import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.itm.config.rev151102.vtep.config.schemas.VtepConfigSchema;
42 //import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.itm.config.rev151102.vtep.config.schemas.VtepConfigSchemaBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.CreateIdPoolInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.CreateIdPoolInputBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.ItmRpcService;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.TunnelList ;
48 import org.opendaylight.yangtools.yang.common.RpcResult;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 import com.google.common.base.Optional;
53 import com.google.common.base.Preconditions;
55 public class ItmProvider implements BindingAwareProvider, AutoCloseable, IITMProvider /*,ItmStateService */{
57 private static final Logger LOG = LoggerFactory.getLogger(ItmProvider.class);
58 private IInterfaceManager interfaceManager;
59 private ITMManager itmManager;
60 private IMdsalApiManager mdsalManager;
61 private DataBroker dataBroker;
62 private NotificationPublishService notificationPublishService;
63 private ItmManagerRpcService itmRpcService ;
64 private IdManagerService idManager;
65 private NotificationService notificationService;
66 private TepCommandHelper tepCommandHelper;
67 private TransportZoneListener tzChangeListener;
68 private TunnelMonitorChangeListener tnlToggleListener;
69 private TunnelMonitorIntervalListener tnlIntervalListener;
70 private VtepConfigSchemaListener vtepConfigSchemaListener;
71 private InterfaceStateListener ifStateListener;
72 private RpcProviderRegistry rpcProviderRegistry;
73 private static final ITMStatusMonitor itmStatusMonitor = ITMStatusMonitor.getInstance();
74 private ItmTunnelEventListener itmStateListener;
75 static short flag = 0;
77 public ItmProvider() {
78 LOG.info("ItmProvider Before register MBean");
79 itmStatusMonitor.registerMbean();
82 public void setRpcProviderRegistry(RpcProviderRegistry rpcProviderRegistry) {
83 this.rpcProviderRegistry = rpcProviderRegistry;
86 public RpcProviderRegistry getRpcProviderRegistry() {
87 return this.rpcProviderRegistry;
91 public void onSessionInitiated(ProviderContext session) {
92 LOG.info("ItmProvider Session Initiated");
93 itmStatusMonitor.reportStatus("STARTING");
95 dataBroker = session.getSALService(DataBroker.class);
96 idManager = getRpcProviderRegistry().getRpcService(IdManagerService.class);
98 itmManager = new ITMManager(dataBroker);
99 tzChangeListener = new TransportZoneListener(dataBroker, idManager) ;
100 itmRpcService = new ItmManagerRpcService(dataBroker, idManager);
101 vtepConfigSchemaListener = new VtepConfigSchemaListener(dataBroker);
102 this.ifStateListener = new InterfaceStateListener(dataBroker);
103 tnlToggleListener = new TunnelMonitorChangeListener(dataBroker);
104 tnlIntervalListener = new TunnelMonitorIntervalListener(dataBroker);
105 tepCommandHelper = new TepCommandHelper(dataBroker);
106 final BindingAwareBroker.RpcRegistration<ItmRpcService> rpcRegistration = getRpcProviderRegistry().addRpcImplementation(ItmRpcService.class, itmRpcService);
107 itmRpcService.setMdsalManager(mdsalManager);
108 itmManager.setMdsalManager(mdsalManager);
109 itmManager.setNotificationPublishService(notificationPublishService);
110 itmManager.setMdsalManager(mdsalManager);
111 tzChangeListener.setMdsalManager(mdsalManager);
112 tzChangeListener.setItmManager(itmManager);
113 tzChangeListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
114 tnlIntervalListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
115 tnlToggleListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
116 tepCommandHelper = new TepCommandHelper(dataBroker);
117 tepCommandHelper.setInterfaceManager(interfaceManager);
118 itmStateListener =new ItmTunnelEventListener(dataBroker);
120 itmStatusMonitor.reportStatus("OPERATIONAL");
121 } catch (Exception e) {
122 LOG.error("Error initializing services", e);
123 itmStatusMonitor.reportStatus("ERROR");
127 public void setInterfaceManager(IInterfaceManager interfaceManager) {
128 this.interfaceManager = interfaceManager;
131 public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
132 this.notificationPublishService = notificationPublishService;
135 public void setMdsalApiManager(IMdsalApiManager mdsalMgr) {
136 this.mdsalManager = mdsalMgr;
138 public void setNotificationService(NotificationService notificationService) {
139 this.notificationService = notificationService;
143 public void close() throws Exception {
144 if (itmManager != null) {
147 if (tzChangeListener != null) {
148 tzChangeListener.close();
150 if (tnlIntervalListener != null) {
151 tnlIntervalListener.close();
153 if(tnlToggleListener!= null){
154 tnlToggleListener.close();
156 LOG.info("ItmProvider Closed");
159 private void createIdPool() {
160 CreateIdPoolInput createPool = new CreateIdPoolInputBuilder()
161 .setPoolName(ITMConstants.ITM_IDPOOL_NAME)
162 .setLow(ITMConstants.ITM_IDPOOL_START)
163 .setHigh(new BigInteger(ITMConstants.ITM_IDPOOL_SIZE).longValue())
166 Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
167 if ((result != null) && (result.get().isSuccessful())) {
168 LOG.debug("Created IdPool for ITM Service");
170 } catch (InterruptedException | ExecutionException e) {
171 LOG.error("Failed to create idPool for ITM Service",e);
176 public DataBroker getDataBroker() {
180 public void createLocalCache(BigInteger dpnId, String portName, Integer vlanId, String ipAddress, String subnetMask,
181 String gatewayIp, String transportZone) {
182 if (tepCommandHelper != null) {
183 tepCommandHelper.createLocalCache(dpnId, portName, vlanId, ipAddress, subnetMask, gatewayIp, transportZone);
185 LOG.trace("tepCommandHelper doesnt exist");
190 public void commitTeps() {
192 tepCommandHelper.deleteOnCommit();
193 tepCommandHelper.buildTeps();
194 } catch (Exception e) {
195 LOG.debug("unable to configure teps" + e.toString());
200 public void showTeps() {
201 tepCommandHelper.showTeps(itmManager.getTunnelMonitorEnabledFromConfigDS(),
202 itmManager.getTunnelMonitorIntervalFromConfigDS());
204 public void showState(TunnelList tunnels) {
206 tepCommandHelper.showState(tunnels, itmManager.getTunnelMonitorEnabledFromConfigDS());
208 LOG.debug("No tunnels available");
211 public void deleteVtep(BigInteger dpnId, String portName, Integer vlanId, String ipAddress, String subnetMask,
212 String gatewayIp, String transportZone) {
214 tepCommandHelper.deleteVtep(dpnId, portName, vlanId, ipAddress, subnetMask, gatewayIp, transportZone);
215 } catch (Exception e) {
221 public void configureTunnelType(String transportZone, String tunnelType) {
222 LOG .debug("ItmProvider: configureTunnelType {} for transportZone {}", tunnelType, transportZone);
223 tepCommandHelper.configureTunnelType(transportZone,tunnelType);
230 * org.opendaylight.vpnservice.itm.api.IITMProvider#addVtepConfigSchema(org.
231 * opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.itm.config.
232 * rev151102.vtep.config.schemas.VtepConfigSchema)
235 public void addVtepConfigSchema(VtepConfigSchema vtepConfigSchema) {
236 VtepConfigSchema validatedSchema = ItmUtils.validateForAddVtepConfigSchema(vtepConfigSchema,
237 getAllVtepConfigSchemas());
239 String schemaName = validatedSchema.getSchemaName();
240 VtepConfigSchema existingSchema = getVtepConfigSchema(schemaName);
241 if (existingSchema != null) {
242 Preconditions.checkArgument(false, String.format("VtepConfigSchema [%s] already exists!", schemaName));
244 MDSALUtil.syncWrite(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
245 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), validatedSchema);
246 LOG.debug("Vtep config schema {} added to config DS", schemaName);
253 * org.opendaylight.vpnservice.itm.api.IITMProvider#getVtepConfigSchema(java
257 public VtepConfigSchema getVtepConfigSchema(String schemaName) {
258 Optional<VtepConfigSchema> schema = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
259 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), this.dataBroker);
260 if (schema.isPresent()) {
270 * org.opendaylight.vpnservice.itm.api.IITMProvider#getAllVtepConfigSchemas(
274 public List<VtepConfigSchema> getAllVtepConfigSchemas() {
275 Optional<VtepConfigSchemas> schemas = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
276 ItmUtils.getVtepConfigSchemasIdentifier(), this.dataBroker);
277 if (schemas.isPresent()) {
278 return schemas.get().getVtepConfigSchema();
287 * org.opendaylight.vpnservice.itm.api.IITMProvider#updateVtepSchema(java.
288 * lang.String, java.util.List, java.util.List)
291 public void updateVtepSchema(String schemaName, List<BigInteger> lstDpnsForAdd, List<BigInteger> lstDpnsForDelete) {
292 LOG.trace("Updating VTEP schema {} by adding DPN's {} and deleting DPN's {}.", schemaName, lstDpnsForAdd,
295 VtepConfigSchema schema = ItmUtils.validateForUpdateVtepSchema(schemaName, lstDpnsForAdd, lstDpnsForDelete,
297 if (ItmUtils.getDpnIdList(schema.getDpnIds()) == null) {
298 VtepConfigSchemaBuilder builder = new VtepConfigSchemaBuilder(schema);
299 if (ItmUtils.getDpnIdList(schema.getDpnIds()).isEmpty()) {
300 builder.setDpnIds(schema.getDpnIds());
301 schema = builder.build();
303 if (lstDpnsForAdd != null && !lstDpnsForAdd.isEmpty()) {
304 List<BigInteger> originalDpnList = ItmUtils.getDpnIdList(schema.getDpnIds()) ;
305 originalDpnList.addAll(lstDpnsForAdd) ;
306 builder.setDpnIds(ItmUtils.getDpnIdsListFromBigInt(originalDpnList));
308 if (lstDpnsForDelete != null && !lstDpnsForDelete.isEmpty()) {
309 List<BigInteger> originalDpnList = ItmUtils.getDpnIdList(schema.getDpnIds()) ;
310 originalDpnList.removeAll(lstDpnsForAdd) ;
311 builder.setDpnIds(ItmUtils.getDpnIdsListFromBigInt(originalDpnList)) ;
314 schema = builder.build();
315 MDSALUtil.syncWrite(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
316 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), schema);
317 LOG.debug("Vtep config schema {} updated to config DS with DPN's {}", schemaName, ItmUtils.getDpnIdList(schema.getDpnIds()));
324 * org.opendaylight.vpnservice.itm.api.IITMProvider#deleteAllVtepSchemas()
327 public void deleteAllVtepSchemas() {
328 List<VtepConfigSchema> lstSchemas = getAllVtepConfigSchemas();
329 if (lstSchemas != null && !lstSchemas.isEmpty()) {
330 for (VtepConfigSchema schema : lstSchemas) {
331 MDSALUtil.syncDelete(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
332 ItmUtils.getVtepConfigSchemaIdentifier(schema.getSchemaName()));
335 LOG.debug("Deleted all Vtep schemas from config DS");
338 public void configureTunnelMonitorEnabled(boolean monitorEnabled) {
339 tepCommandHelper.configureTunnelMonitorEnabled(monitorEnabled);
342 public void configureTunnelMonitorInterval(int interval) {
343 tepCommandHelper.configureTunnelMonitorInterval(interval);