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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
14 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
19 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
20 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
21 import org.opendaylight.genius.itm.api.IITMProvider;
22 import org.opendaylight.genius.itm.cli.TepCommandHelper;
23 import org.opendaylight.genius.itm.globals.ITMConstants;
24 import org.opendaylight.genius.itm.listeners.*;
25 import org.opendaylight.genius.itm.monitoring.ItmTunnelEventListener;
26 import org.opendaylight.genius.itm.rpc.ItmManagerRpcService;
27 import org.opendaylight.genius.itm.snd.ITMStatusMonitor;
28 import org.opendaylight.genius.mdsalutil.MDSALUtil;
29 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.VtepConfigSchemas;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.vtep.config.schemas.VtepConfigSchema;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.vtep.config.schemas.VtepConfigSchemaBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.TunnelList;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import java.math.BigInteger;
43 import java.util.List;
44 import java.util.concurrent.ExecutionException;
45 import java.util.concurrent.Future;
47 public class ItmProvider implements BindingAwareProvider, AutoCloseable, IITMProvider /*,ItmStateService */{
49 private static final Logger LOG = LoggerFactory.getLogger(ItmProvider.class);
50 private IInterfaceManager interfaceManager;
51 private ITMManager itmManager;
52 private IMdsalApiManager mdsalManager;
53 private DataBroker dataBroker;
54 private NotificationPublishService notificationPublishService;
55 private ItmManagerRpcService itmRpcService ;
56 private IdManagerService idManager;
57 private NotificationService notificationService;
58 private TepCommandHelper tepCommandHelper;
59 private TransportZoneListener tzChangeListener;
60 private TunnelMonitorChangeListener tnlToggleListener;
61 private TunnelMonitorIntervalListener tnlIntervalListener;
62 private VtepConfigSchemaListener vtepConfigSchemaListener;
63 private InterfaceStateListener ifStateListener;
64 private RpcProviderRegistry rpcProviderRegistry;
65 private static final ITMStatusMonitor itmStatusMonitor = ITMStatusMonitor.getInstance();
66 private ItmTunnelEventListener itmStateListener;
67 static short flag = 0;
69 public ItmProvider() {
70 LOG.info("ItmProvider Before register MBean");
71 itmStatusMonitor.registerMbean();
74 public void setRpcProviderRegistry(RpcProviderRegistry rpcProviderRegistry) {
75 this.rpcProviderRegistry = rpcProviderRegistry;
78 public RpcProviderRegistry getRpcProviderRegistry() {
79 return this.rpcProviderRegistry;
83 public void onSessionInitiated(ProviderContext session) {
84 LOG.info("ItmProvider Session Initiated");
85 itmStatusMonitor.reportStatus("STARTING");
87 dataBroker = session.getSALService(DataBroker.class);
88 idManager = getRpcProviderRegistry().getRpcService(IdManagerService.class);
90 itmManager = new ITMManager(dataBroker);
91 tzChangeListener = new TransportZoneListener(dataBroker, idManager) ;
92 itmRpcService = new ItmManagerRpcService(dataBroker, idManager);
93 vtepConfigSchemaListener = new VtepConfigSchemaListener(dataBroker);
94 this.ifStateListener = new InterfaceStateListener(dataBroker);
95 tnlToggleListener = new TunnelMonitorChangeListener(dataBroker);
96 tnlIntervalListener = new TunnelMonitorIntervalListener(dataBroker);
97 tepCommandHelper = new TepCommandHelper(dataBroker);
98 final BindingAwareBroker.RpcRegistration<ItmRpcService> rpcRegistration = getRpcProviderRegistry().addRpcImplementation(ItmRpcService.class, itmRpcService);
99 itmRpcService.setMdsalManager(mdsalManager);
100 itmManager.setMdsalManager(mdsalManager);
101 itmManager.setNotificationPublishService(notificationPublishService);
102 itmManager.setMdsalManager(mdsalManager);
103 tzChangeListener.setMdsalManager(mdsalManager);
104 tzChangeListener.setItmManager(itmManager);
105 tzChangeListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
106 tnlIntervalListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
107 tnlToggleListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
108 tepCommandHelper = new TepCommandHelper(dataBroker);
109 tepCommandHelper.setInterfaceManager(interfaceManager);
110 itmStateListener =new ItmTunnelEventListener(dataBroker);
112 itmStatusMonitor.reportStatus("OPERATIONAL");
113 } catch (Exception e) {
114 LOG.error("Error initializing services", e);
115 itmStatusMonitor.reportStatus("ERROR");
119 public void setInterfaceManager(IInterfaceManager interfaceManager) {
120 this.interfaceManager = interfaceManager;
123 public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
124 this.notificationPublishService = notificationPublishService;
127 public void setMdsalApiManager(IMdsalApiManager mdsalMgr) {
128 this.mdsalManager = mdsalMgr;
130 public void setNotificationService(NotificationService notificationService) {
131 this.notificationService = notificationService;
135 public void close() throws Exception {
136 if (itmManager != null) {
139 if (tzChangeListener != null) {
140 tzChangeListener.close();
142 if (tnlIntervalListener != null) {
143 tnlIntervalListener.close();
145 if(tnlToggleListener!= null){
146 tnlToggleListener.close();
148 LOG.info("ItmProvider Closed");
151 private void createIdPool() {
152 CreateIdPoolInput createPool = new CreateIdPoolInputBuilder()
153 .setPoolName(ITMConstants.ITM_IDPOOL_NAME)
154 .setLow(ITMConstants.ITM_IDPOOL_START)
155 .setHigh(new BigInteger(ITMConstants.ITM_IDPOOL_SIZE).longValue())
158 Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
159 if ((result != null) && (result.get().isSuccessful())) {
160 LOG.debug("Created IdPool for ITM Service");
162 } catch (InterruptedException | ExecutionException e) {
163 LOG.error("Failed to create idPool for ITM Service",e);
168 public DataBroker getDataBroker() {
172 public void createLocalCache(BigInteger dpnId, String portName, Integer vlanId, String ipAddress, String subnetMask,
173 String gatewayIp, String transportZone) {
174 if (tepCommandHelper != null) {
175 tepCommandHelper.createLocalCache(dpnId, portName, vlanId, ipAddress, subnetMask, gatewayIp, transportZone);
177 LOG.trace("tepCommandHelper doesnt exist");
182 public void commitTeps() {
184 tepCommandHelper.deleteOnCommit();
185 tepCommandHelper.buildTeps();
186 } catch (Exception e) {
187 LOG.debug("unable to configure teps" + e.toString());
192 public void showTeps() {
193 tepCommandHelper.showTeps(itmManager.getTunnelMonitorEnabledFromConfigDS(), ItmUtils.determineMonitorInterval(this.dataBroker));
196 public void showState(TunnelList tunnels) {
198 tepCommandHelper.showState(tunnels, itmManager.getTunnelMonitorEnabledFromConfigDS());
200 LOG.debug("No tunnels available");
203 public void deleteVtep(BigInteger dpnId, String portName, Integer vlanId, String ipAddress, String subnetMask,
204 String gatewayIp, String transportZone) {
206 tepCommandHelper.deleteVtep(dpnId, portName, vlanId, ipAddress, subnetMask, gatewayIp, transportZone);
207 } catch (Exception e) {
213 public void configureTunnelType(String transportZone, String tunnelType) {
214 LOG .debug("ItmProvider: configureTunnelType {} for transportZone {}", tunnelType, transportZone);
215 tepCommandHelper.configureTunnelType(transportZone,tunnelType);
219 public void addVtepConfigSchema(VtepConfigSchema vtepConfigSchema) {
220 VtepConfigSchema validatedSchema = ItmUtils.validateForAddVtepConfigSchema(vtepConfigSchema,
221 getAllVtepConfigSchemas());
223 String schemaName = validatedSchema.getSchemaName();
224 VtepConfigSchema existingSchema = getVtepConfigSchema(schemaName);
225 if (existingSchema != null) {
226 Preconditions.checkArgument(false, String.format("VtepConfigSchema [%s] already exists!", schemaName));
228 MDSALUtil.syncWrite(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
229 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), validatedSchema);
230 LOG.debug("Vtep config schema {} added to config DS", schemaName);
234 public VtepConfigSchema getVtepConfigSchema(String schemaName) {
235 Optional<VtepConfigSchema> schema = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
236 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), this.dataBroker);
237 if (schema.isPresent()) {
244 public List<VtepConfigSchema> getAllVtepConfigSchemas() {
245 Optional<VtepConfigSchemas> schemas = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
246 ItmUtils.getVtepConfigSchemasIdentifier(), this.dataBroker);
247 if (schemas.isPresent()) {
248 return schemas.get().getVtepConfigSchema();
254 public void updateVtepSchema(String schemaName, List<BigInteger> lstDpnsForAdd, List<BigInteger> lstDpnsForDelete) {
255 LOG.trace("Updating VTEP schema {} by adding DPN's {} and deleting DPN's {}.", schemaName, lstDpnsForAdd,
258 VtepConfigSchema schema = ItmUtils.validateForUpdateVtepSchema(schemaName, lstDpnsForAdd, lstDpnsForDelete,
260 VtepConfigSchemaBuilder builder = new VtepConfigSchemaBuilder(schema);
261 /* if (ItmUtils.getDpnIdList(schema.getDpnIds()).isEmpty()) {
262 builder.setDpnIds(schema.getDpnIds());
264 if (lstDpnsForAdd != null && !lstDpnsForAdd.isEmpty()) {
265 List<BigInteger> originalDpnList = ItmUtils.getDpnIdList(schema.getDpnIds()) ;
266 originalDpnList.addAll(lstDpnsForAdd) ;
267 builder.setDpnIds(ItmUtils.getDpnIdsListFromBigInt(originalDpnList));
269 if (lstDpnsForDelete != null && !lstDpnsForDelete.isEmpty()) {
270 List<BigInteger> originalDpnList = ItmUtils.getDpnIdList(schema.getDpnIds()) ;
271 originalDpnList.removeAll(lstDpnsForDelete) ;
272 builder.setDpnIds(ItmUtils.getDpnIdsListFromBigInt(originalDpnList)) ;
273 // schema.setDpnIds(ItmUtils.getDpnIdsListFromBigInt(ItmUtils.getDpnIdList(schema.getDpnIds()).removeAll(lstDpnsForAdd)));
276 schema = builder.build();
277 MDSALUtil.syncWrite(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
278 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), schema);
279 LOG.debug("Vtep config schema {} updated to config DS with DPN's {}", schemaName, ItmUtils.getDpnIdList(schema.getDpnIds()));
283 public void deleteAllVtepSchemas() {
284 List<VtepConfigSchema> lstSchemas = getAllVtepConfigSchemas();
285 if (lstSchemas != null && !lstSchemas.isEmpty()) {
286 for (VtepConfigSchema schema : lstSchemas) {
287 MDSALUtil.syncDelete(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
288 ItmUtils.getVtepConfigSchemaIdentifier(schema.getSchemaName()));
291 LOG.debug("Deleted all Vtep schemas from config DS");
294 public void configureTunnelMonitorParams(boolean monitorEnabled, String monitorProtocol) {
295 tepCommandHelper.configureTunnelMonitorParams(monitorEnabled, monitorProtocol);
298 public void configureTunnelMonitorInterval(int interval) {
299 tepCommandHelper.configureTunnelMonitorInterval(interval);