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.impl.ItmUtils;
28 import org.opendaylight.vpnservice.itm.listeners.TransportZoneListener;
29 import org.opendaylight.vpnservice.itm.listeners.VtepConfigSchemaListener;
30 import org.opendaylight.vpnservice.itm.rpc.ItmManagerRpcService;
31 import org.opendaylight.vpnservice.itm.snd.ITMStatusMonitor;
32 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
33 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.VtepConfigSchemas;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.vtep.config.schemas.VtepConfigSchema;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.vtep.config.schemas.VtepConfigSchemaBuilder;
37 //import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.itm.config.rev151102.VtepConfigSchemas;
38 //import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.itm.config.rev151102.vtep.config.schemas.VtepConfigSchema;
39 //import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.itm.config.rev151102.vtep.config.schemas.VtepConfigSchemaBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.CreateIdPoolInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.CreateIdPoolInputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.OdlInterfaceRpcService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.ItmRpcService;
45 import org.opendaylight.yangtools.yang.common.RpcResult;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 import com.google.common.base.Optional;
50 import com.google.common.base.Preconditions;
52 public class ItmProvider implements BindingAwareProvider, AutoCloseable, IITMProvider /*,ItmStateService */{
54 private static final Logger LOG = LoggerFactory.getLogger(ItmProvider.class);
55 private IInterfaceManager interfaceManager;
56 private ITMManager itmManager;
57 private IMdsalApiManager mdsalManager;
58 private DataBroker dataBroker;
59 private NotificationPublishService notificationPublishService;
60 private ItmManagerRpcService itmRpcService ;
61 private IdManagerService idManager;
62 private NotificationService notificationService;
63 private TepCommandHelper tepCommandHelper;
64 private TransportZoneListener tzChangeListener;
65 private VtepConfigSchemaListener vtepConfigSchemaListener;
66 private RpcProviderRegistry rpcProviderRegistry;
67 private static final ITMStatusMonitor itmStatusMonitor = ITMStatusMonitor.getInstance();
68 static short flag = 0;
70 public ItmProvider() {
71 LOG.info("ItmProvider Before register MBean");
72 itmStatusMonitor.registerMbean();
75 public void setRpcProviderRegistry(RpcProviderRegistry rpcProviderRegistry) {
76 this.rpcProviderRegistry = rpcProviderRegistry;
79 public RpcProviderRegistry getRpcProviderRegistry() {
80 return this.rpcProviderRegistry;
84 public void onSessionInitiated(ProviderContext session) {
85 LOG.info("ItmProvider Session Initiated");
86 itmStatusMonitor.reportStatus("STARTING");
88 dataBroker = session.getSALService(DataBroker.class);
89 idManager = getRpcProviderRegistry().getRpcService(IdManagerService.class);
91 itmManager = new ITMManager(dataBroker);
92 tzChangeListener = new TransportZoneListener(dataBroker, idManager) ;
93 itmRpcService = new ItmManagerRpcService(dataBroker, idManager);
94 vtepConfigSchemaListener = new VtepConfigSchemaListener(dataBroker);
95 tepCommandHelper = new TepCommandHelper(dataBroker);
96 final BindingAwareBroker.RpcRegistration<ItmRpcService> rpcRegistration = getRpcProviderRegistry().addRpcImplementation(ItmRpcService.class, itmRpcService);
97 itmRpcService.setMdsalManager(mdsalManager);
98 itmManager.setMdsalManager(mdsalManager);
99 itmManager.setNotificationPublishService(notificationPublishService);
100 itmManager.setMdsalManager(mdsalManager);
101 tzChangeListener.setMdsalManager(mdsalManager);
102 tzChangeListener.setItmManager(itmManager);
103 tzChangeListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
104 tepCommandHelper = new TepCommandHelper(dataBroker);
105 tepCommandHelper.setInterfaceManager(interfaceManager);
107 itmStatusMonitor.reportStatus("OPERATIONAL");
108 } catch (Exception e) {
109 LOG.error("Error initializing services", e);
110 itmStatusMonitor.reportStatus("ERROR");
114 public void setInterfaceManager(IInterfaceManager interfaceManager) {
115 this.interfaceManager = interfaceManager;
118 public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
119 this.notificationPublishService = notificationPublishService;
122 public void setMdsalApiManager(IMdsalApiManager mdsalMgr) {
123 this.mdsalManager = mdsalMgr;
125 public void setNotificationService(NotificationService notificationService) {
126 this.notificationService = notificationService;
130 public void close() throws Exception {
131 if (itmManager != null) {
134 if (tzChangeListener != null) {
135 tzChangeListener.close();
138 LOG.info("ItmProvider Closed");
141 private void createIdPool() {
142 CreateIdPoolInput createPool = new CreateIdPoolInputBuilder()
143 .setPoolName(ITMConstants.ITM_IDPOOL_NAME)
144 .setLow(ITMConstants.ITM_IDPOOL_START)
145 .setHigh(new BigInteger(ITMConstants.ITM_IDPOOL_SIZE).longValue())
148 Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
149 if ((result != null) && (result.get().isSuccessful())) {
150 LOG.debug("Created IdPool for ITM Service");
152 } catch (InterruptedException | ExecutionException e) {
153 LOG.error("Failed to create idPool for ITM Service",e);
158 public DataBroker getDataBroker() {
162 public void createLocalCache(BigInteger dpnId, String portName, Integer vlanId, String ipAddress, String subnetMask,
163 String gatewayIp, String transportZone) {
164 if (tepCommandHelper != null) {
165 tepCommandHelper.createLocalCache(dpnId, portName, vlanId, ipAddress, subnetMask, gatewayIp, transportZone);
167 LOG.trace("tepCommandHelper doesnt exist");
172 public void commitTeps() {
174 tepCommandHelper.deleteOnCommit();
175 tepCommandHelper.buildTeps();
176 } catch (Exception e) {
177 LOG.debug("unable to configure teps" + e.toString());
182 public void showTeps() {
183 tepCommandHelper.showTeps(itmManager.getTunnelMonitorEnabledFromConfigDS(),
184 itmManager.getTunnelMonitorIntervalFromConfigDS());
187 public void deleteVtep(BigInteger dpnId, String portName, Integer vlanId, String ipAddress, String subnetMask,
188 String gatewayIp, String transportZone) {
190 tepCommandHelper.deleteVtep(dpnId, portName, vlanId, ipAddress, subnetMask, gatewayIp, transportZone);
191 } catch (Exception e) {
197 public void configureTunnelType(String transportZone, String tunnelType) {
198 LOG .debug("ItmProvider: configureTunnelType {} for transportZone {}", tunnelType, transportZone);
199 tepCommandHelper.configureTunnelType(transportZone,tunnelType);
206 * org.opendaylight.vpnservice.itm.api.IITMProvider#addVtepConfigSchema(org.
207 * opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.itm.config.
208 * rev151102.vtep.config.schemas.VtepConfigSchema)
211 public void addVtepConfigSchema(VtepConfigSchema vtepConfigSchema) {
212 VtepConfigSchema validatedSchema = ItmUtils.validateForAddVtepConfigSchema(vtepConfigSchema,
213 getAllVtepConfigSchemas());
215 String schemaName = validatedSchema.getSchemaName();
216 VtepConfigSchema existingSchema = getVtepConfigSchema(schemaName);
217 if (existingSchema != null) {
218 Preconditions.checkArgument(false, String.format("VtepConfigSchema [%s] already exists!", schemaName));
220 MDSALUtil.syncWrite(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
221 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), validatedSchema);
222 LOG.debug("Vtep config schema {} added to config DS", schemaName);
229 * org.opendaylight.vpnservice.itm.api.IITMProvider#getVtepConfigSchema(java
233 public VtepConfigSchema getVtepConfigSchema(String schemaName) {
234 Optional<VtepConfigSchema> schema = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
235 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), this.dataBroker);
236 if (schema.isPresent()) {
246 * org.opendaylight.vpnservice.itm.api.IITMProvider#getAllVtepConfigSchemas(
250 public List<VtepConfigSchema> getAllVtepConfigSchemas() {
251 Optional<VtepConfigSchemas> schemas = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
252 ItmUtils.getVtepConfigSchemasIdentifier(), this.dataBroker);
253 if (schemas.isPresent()) {
254 return schemas.get().getVtepConfigSchema();
263 * org.opendaylight.vpnservice.itm.api.IITMProvider#updateVtepSchema(java.
264 * lang.String, java.util.List, java.util.List)
267 public void updateVtepSchema(String schemaName, List<BigInteger> lstDpnsForAdd, List<BigInteger> lstDpnsForDelete) {
268 LOG.trace("Updating VTEP schema {} by adding DPN's {} and deleting DPN's {}.", schemaName, lstDpnsForAdd,
271 VtepConfigSchema schema = ItmUtils.validateForUpdateVtepSchema(schemaName, lstDpnsForAdd, lstDpnsForDelete,
273 if (ItmUtils.getDpnIdList(schema.getDpnIds()) == null) {
274 VtepConfigSchemaBuilder builder = new VtepConfigSchemaBuilder(schema);
275 builder.setDpnIds(schema.getDpnIds());
276 schema = builder.build();
278 if (lstDpnsForAdd != null && !lstDpnsForAdd.isEmpty()) {
279 ItmUtils.getDpnIdList(schema.getDpnIds()).addAll(lstDpnsForAdd);
281 if (lstDpnsForDelete != null && !lstDpnsForDelete.isEmpty()) {
282 ItmUtils.getDpnIdList(schema.getDpnIds()).removeAll(lstDpnsForDelete);
285 MDSALUtil.syncWrite(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
286 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), schema);
287 LOG.debug("Vtep config schema {} updated to config DS with DPN's {}", schemaName, ItmUtils.getDpnIdList(schema.getDpnIds()));
294 * org.opendaylight.vpnservice.itm.api.IITMProvider#deleteAllVtepSchemas()
297 public void deleteAllVtepSchemas() {
298 List<VtepConfigSchema> lstSchemas = getAllVtepConfigSchemas();
299 if (lstSchemas != null && !lstSchemas.isEmpty()) {
300 for (VtepConfigSchema schema : lstSchemas) {
301 MDSALUtil.syncDelete(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
302 ItmUtils.getVtepConfigSchemaIdentifier(schema.getSchemaName()));
305 LOG.debug("Deleted all Vtep schemas from config DS");
308 public void configureTunnelMonitorEnabled(boolean monitorEnabled) {
309 tepCommandHelper.configureTunnelMonitorEnabled(monitorEnabled);
312 public void configureTunnelMonitorInterval(int interval) {
313 tepCommandHelper.configureTunnelMonitorInterval(interval);