2 * Copyright (c) 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.genius.itm.impl;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.math.BigInteger;
13 import java.util.List;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.Future;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
18 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
23 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
24 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
25 import org.opendaylight.genius.itm.api.IITMProvider;
26 import org.opendaylight.genius.itm.cli.TepCommandHelper;
27 import org.opendaylight.genius.itm.globals.ITMConstants;
28 import org.opendaylight.genius.itm.listeners.InterfaceStateListener;
29 import org.opendaylight.genius.itm.listeners.TransportZoneListener;
30 import org.opendaylight.genius.itm.listeners.TunnelMonitorChangeListener;
31 import org.opendaylight.genius.itm.listeners.TunnelMonitorIntervalListener;
32 import org.opendaylight.genius.itm.listeners.VtepConfigSchemaListener;
33 import org.opendaylight.genius.itm.monitoring.ItmTunnelEventListener;
34 import org.opendaylight.genius.itm.rpc.ItmManagerRpcService;
35 import org.opendaylight.genius.itm.snd.ITMStatusMonitor;
36 import org.opendaylight.genius.mdsalutil.MDSALUtil;
37 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.VtepConfigSchemas;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.vtep.config.schemas.VtepConfigSchema;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.vtep.config.schemas.VtepConfigSchemaBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.TunnelList;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
46 import org.opendaylight.yangtools.yang.common.RpcResult;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class ItmProvider implements BindingAwareProvider, AutoCloseable, IITMProvider /*,ItmStateService */{
52 private static final Logger LOG = LoggerFactory.getLogger(ItmProvider.class);
53 private IInterfaceManager interfaceManager;
54 private ITMManager itmManager;
55 private IMdsalApiManager mdsalManager;
56 private DataBroker dataBroker;
57 private NotificationPublishService notificationPublishService;
58 private ItmManagerRpcService itmRpcService ;
59 private IdManagerService idManager;
60 private NotificationService notificationService;
61 private TepCommandHelper tepCommandHelper;
62 private TransportZoneListener tzChangeListener;
63 private TunnelMonitorChangeListener tnlToggleListener;
64 private TunnelMonitorIntervalListener tnlIntervalListener;
65 private VtepConfigSchemaListener vtepConfigSchemaListener;
66 private InterfaceStateListener ifStateListener;
67 private RpcProviderRegistry rpcProviderRegistry;
68 private static final ITMStatusMonitor itmStatusMonitor = ITMStatusMonitor.getInstance();
69 private ItmTunnelEventListener itmStateListener;
70 static short flag = 0;
72 public ItmProvider() {
73 LOG.info("ItmProvider Before register MBean");
74 itmStatusMonitor.registerMbean();
77 public void setRpcProviderRegistry(RpcProviderRegistry rpcProviderRegistry) {
78 this.rpcProviderRegistry = rpcProviderRegistry;
81 public RpcProviderRegistry getRpcProviderRegistry() {
82 return this.rpcProviderRegistry;
86 public void onSessionInitiated(ProviderContext session) {
87 LOG.info("ItmProvider Session Initiated");
88 itmStatusMonitor.reportStatus("STARTING");
90 dataBroker = session.getSALService(DataBroker.class);
91 idManager = getRpcProviderRegistry().getRpcService(IdManagerService.class);
93 itmManager = new ITMManager(dataBroker);
94 tzChangeListener = new TransportZoneListener(dataBroker, idManager) ;
95 itmRpcService = new ItmManagerRpcService(dataBroker, idManager);
96 vtepConfigSchemaListener = new VtepConfigSchemaListener(dataBroker);
97 this.ifStateListener = new InterfaceStateListener(dataBroker);
98 tnlToggleListener = new TunnelMonitorChangeListener(dataBroker);
99 tnlIntervalListener = new TunnelMonitorIntervalListener(dataBroker);
100 tepCommandHelper = new TepCommandHelper(dataBroker);
101 getRpcProviderRegistry().addRpcImplementation(ItmRpcService.class, itmRpcService);
102 itmRpcService.setMdsalManager(mdsalManager);
103 itmManager.setMdsalManager(mdsalManager);
104 itmManager.setNotificationPublishService(notificationPublishService);
105 itmManager.setMdsalManager(mdsalManager);
106 tzChangeListener.setMdsalManager(mdsalManager);
107 tzChangeListener.setItmManager(itmManager);
108 tzChangeListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
109 tnlIntervalListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
110 tnlToggleListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
111 tepCommandHelper = new TepCommandHelper(dataBroker);
112 tepCommandHelper.setInterfaceManager(interfaceManager);
113 itmStateListener =new ItmTunnelEventListener(dataBroker);
115 itmStatusMonitor.reportStatus("OPERATIONAL");
116 } catch (Exception e) {
117 LOG.error("Error initializing services", e);
118 itmStatusMonitor.reportStatus("ERROR");
122 public void setInterfaceManager(IInterfaceManager interfaceManager) {
123 this.interfaceManager = interfaceManager;
126 public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
127 this.notificationPublishService = notificationPublishService;
130 public void setMdsalApiManager(IMdsalApiManager mdsalMgr) {
131 this.mdsalManager = mdsalMgr;
133 public void setNotificationService(NotificationService notificationService) {
134 this.notificationService = notificationService;
138 public void close() throws Exception {
139 if (itmManager != null) {
142 if (tzChangeListener != null) {
143 tzChangeListener.close();
145 if (tnlIntervalListener != null) {
146 tnlIntervalListener.close();
148 if(tnlToggleListener!= null){
149 tnlToggleListener.close();
151 LOG.info("ItmProvider Closed");
154 private void createIdPool() {
155 CreateIdPoolInput createPool = new CreateIdPoolInputBuilder()
156 .setPoolName(ITMConstants.ITM_IDPOOL_NAME)
157 .setLow(ITMConstants.ITM_IDPOOL_START)
158 .setHigh(new BigInteger(ITMConstants.ITM_IDPOOL_SIZE).longValue())
161 Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
162 if ((result != null) && (result.get().isSuccessful())) {
163 LOG.debug("Created IdPool for ITM Service");
165 } catch (InterruptedException | ExecutionException e) {
166 LOG.error("Failed to create idPool for ITM Service",e);
171 public DataBroker getDataBroker() {
175 public void createLocalCache(BigInteger dpnId, String portName, Integer vlanId, String ipAddress, String subnetMask,
176 String gatewayIp, String transportZone) {
177 if (tepCommandHelper != null) {
178 tepCommandHelper.createLocalCache(dpnId, portName, vlanId, ipAddress, subnetMask, gatewayIp, transportZone);
180 LOG.trace("tepCommandHelper doesnt exist");
185 public void commitTeps() {
187 tepCommandHelper.deleteOnCommit();
188 tepCommandHelper.buildTeps();
189 } catch (Exception e) {
190 LOG.debug("unable to configure teps" + e.toString());
195 public void showTeps() {
196 tepCommandHelper.showTeps(itmManager.getTunnelMonitorEnabledFromConfigDS(), ItmUtils.determineMonitorInterval(this.dataBroker));
199 public void showState(TunnelList tunnels) {
201 tepCommandHelper.showState(tunnels, itmManager.getTunnelMonitorEnabledFromConfigDS());
203 LOG.debug("No tunnels available");
206 public void deleteVtep(BigInteger dpnId, String portName, Integer vlanId, String ipAddress, String subnetMask,
207 String gatewayIp, String transportZone) {
209 tepCommandHelper.deleteVtep(dpnId, portName, vlanId, ipAddress, subnetMask, gatewayIp, transportZone);
210 } catch (Exception e) {
216 public void configureTunnelType(String transportZone, String tunnelType) {
217 LOG .debug("ItmProvider: configureTunnelType {} for transportZone {}", tunnelType, transportZone);
218 tepCommandHelper.configureTunnelType(transportZone,tunnelType);
222 public void addVtepConfigSchema(VtepConfigSchema vtepConfigSchema) {
223 VtepConfigSchema validatedSchema = ItmUtils.validateForAddVtepConfigSchema(vtepConfigSchema,
224 getAllVtepConfigSchemas());
226 String schemaName = validatedSchema.getSchemaName();
227 VtepConfigSchema existingSchema = getVtepConfigSchema(schemaName);
228 if (existingSchema != null) {
229 Preconditions.checkArgument(false, String.format("VtepConfigSchema [%s] already exists!", schemaName));
231 MDSALUtil.syncWrite(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
232 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), validatedSchema);
233 LOG.debug("Vtep config schema {} added to config DS", schemaName);
237 public VtepConfigSchema getVtepConfigSchema(String schemaName) {
238 Optional<VtepConfigSchema> schema = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
239 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), this.dataBroker);
240 if (schema.isPresent()) {
247 public List<VtepConfigSchema> getAllVtepConfigSchemas() {
248 Optional<VtepConfigSchemas> schemas = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
249 ItmUtils.getVtepConfigSchemasIdentifier(), this.dataBroker);
250 if (schemas.isPresent()) {
251 return schemas.get().getVtepConfigSchema();
257 public void updateVtepSchema(String schemaName, List<BigInteger> lstDpnsForAdd, List<BigInteger> lstDpnsForDelete) {
258 LOG.trace("Updating VTEP schema {} by adding DPN's {} and deleting DPN's {}.", schemaName, lstDpnsForAdd,
261 VtepConfigSchema schema = ItmUtils.validateForUpdateVtepSchema(schemaName, lstDpnsForAdd, lstDpnsForDelete,
263 VtepConfigSchemaBuilder builder = new VtepConfigSchemaBuilder(schema);
264 /* if (ItmUtils.getDpnIdList(schema.getDpnIds()).isEmpty()) {
265 builder.setDpnIds(schema.getDpnIds());
267 if (lstDpnsForAdd != null && !lstDpnsForAdd.isEmpty()) {
268 List<BigInteger> originalDpnList = ItmUtils.getDpnIdList(schema.getDpnIds()) ;
269 originalDpnList.addAll(lstDpnsForAdd) ;
270 builder.setDpnIds(ItmUtils.getDpnIdsListFromBigInt(originalDpnList));
272 if (lstDpnsForDelete != null && !lstDpnsForDelete.isEmpty()) {
273 List<BigInteger> originalDpnList = ItmUtils.getDpnIdList(schema.getDpnIds()) ;
274 originalDpnList.removeAll(lstDpnsForDelete) ;
275 builder.setDpnIds(ItmUtils.getDpnIdsListFromBigInt(originalDpnList)) ;
276 // schema.setDpnIds(ItmUtils.getDpnIdsListFromBigInt(ItmUtils.getDpnIdList(schema.getDpnIds()).removeAll(lstDpnsForAdd)));
279 schema = builder.build();
280 MDSALUtil.syncWrite(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
281 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), schema);
282 LOG.debug("Vtep config schema {} updated to config DS with DPN's {}", schemaName, ItmUtils.getDpnIdList(schema.getDpnIds()));
286 public void deleteAllVtepSchemas() {
287 List<VtepConfigSchema> lstSchemas = getAllVtepConfigSchemas();
288 if (lstSchemas != null && !lstSchemas.isEmpty()) {
289 for (VtepConfigSchema schema : lstSchemas) {
290 MDSALUtil.syncDelete(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
291 ItmUtils.getVtepConfigSchemaIdentifier(schema.getSchemaName()));
294 LOG.debug("Deleted all Vtep schemas from config DS");
297 public void configureTunnelMonitorParams(boolean monitorEnabled, String monitorProtocol) {
298 tepCommandHelper.configureTunnelMonitorParams(monitorEnabled, monitorProtocol);
301 public void configureTunnelMonitorInterval(int interval) {
302 tepCommandHelper.configureTunnelMonitorInterval(interval);