Merge "Modification in ResourceBatchingManager.java"
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / impl / ItmProvider.java
1 /*
2  * Copyright (c) 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.genius.itm.impl;
9
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;
49
50 public class ItmProvider implements BindingAwareProvider, AutoCloseable, IITMProvider /*,ItmStateService */{
51
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;
71
72     public ItmProvider() {
73         LOG.info("ItmProvider Before register MBean");
74         itmStatusMonitor.registerMbean();
75     }
76
77     public void setRpcProviderRegistry(RpcProviderRegistry rpcProviderRegistry) {
78         this.rpcProviderRegistry = rpcProviderRegistry;
79     }
80
81     public RpcProviderRegistry getRpcProviderRegistry() {
82         return this.rpcProviderRegistry;
83     }
84
85     @Override
86     public void onSessionInitiated(ProviderContext session) {
87         LOG.info("ItmProvider Session Initiated");
88         itmStatusMonitor.reportStatus("STARTING");
89         try {
90             dataBroker = session.getSALService(DataBroker.class);
91             idManager = getRpcProviderRegistry().getRpcService(IdManagerService.class);
92
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);
114             createIdPool();
115             itmStatusMonitor.reportStatus("OPERATIONAL");
116         } catch (Exception e) {
117             LOG.error("Error initializing services", e);
118             itmStatusMonitor.reportStatus("ERROR");
119         }
120     }
121
122     public void setInterfaceManager(IInterfaceManager interfaceManager) {
123         this.interfaceManager = interfaceManager;
124     }
125
126     public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
127         this.notificationPublishService = notificationPublishService;
128     }
129
130     public void setMdsalApiManager(IMdsalApiManager mdsalMgr) {
131         this.mdsalManager = mdsalMgr;
132     }
133     public void setNotificationService(NotificationService notificationService) {
134         this.notificationService = notificationService;
135     }
136
137     @Override
138     public void close() throws Exception {
139         if (itmManager != null) {
140             itmManager.close();
141         }
142         if (tzChangeListener != null) {
143             tzChangeListener.close();
144         }
145         if (tnlIntervalListener != null) {
146             tnlIntervalListener.close();
147         }
148         if(tnlToggleListener!= null){
149             tnlToggleListener.close();
150         }
151         LOG.info("ItmProvider Closed");
152     }
153
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())
159                 .build();
160         try {
161             Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
162             if ((result != null) && (result.get().isSuccessful())) {
163                 LOG.debug("Created IdPool for ITM Service");
164             }
165         } catch (InterruptedException | ExecutionException e) {
166             LOG.error("Failed to create idPool for ITM Service",e);
167         }
168     }
169
170     @Override
171     public DataBroker getDataBroker() {
172         return dataBroker;
173     }
174     @Override
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);
179         } else {
180             LOG.trace("tepCommandHelper doesnt exist");
181         }
182     }
183
184     @Override
185     public void commitTeps() {
186         try {
187             tepCommandHelper.deleteOnCommit();
188             tepCommandHelper.buildTeps();
189         } catch (Exception e) {
190             LOG.debug("unable to configure teps" + e.toString());
191         }
192     }
193
194     @Override
195     public void showTeps() {
196         tepCommandHelper.showTeps(itmManager.getTunnelMonitorEnabledFromConfigDS(), ItmUtils.determineMonitorInterval(this.dataBroker));
197     }
198
199     public void showState(TunnelList tunnels) {
200         if (tunnels != null)
201             tepCommandHelper.showState(tunnels, itmManager.getTunnelMonitorEnabledFromConfigDS());
202         else
203             LOG.debug("No tunnels available");
204     }
205
206     public void deleteVtep(BigInteger dpnId, String portName, Integer vlanId, String ipAddress, String subnetMask,
207                            String gatewayIp, String transportZone) {
208         try {
209             tepCommandHelper.deleteVtep(dpnId,  portName, vlanId, ipAddress, subnetMask, gatewayIp, transportZone);
210         } catch (Exception e) {
211             e.printStackTrace();
212         }
213     }
214
215     @Override
216     public void configureTunnelType(String transportZone, String tunnelType) {
217         LOG .debug("ItmProvider: configureTunnelType {} for transportZone {}", tunnelType, transportZone);
218         tepCommandHelper.configureTunnelType(transportZone,tunnelType);
219     }
220
221     @Override
222     public void addVtepConfigSchema(VtepConfigSchema vtepConfigSchema) {
223         VtepConfigSchema validatedSchema = ItmUtils.validateForAddVtepConfigSchema(vtepConfigSchema,
224                 getAllVtepConfigSchemas());
225
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));
230         }
231         MDSALUtil.syncWrite(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
232                 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), validatedSchema);
233         LOG.debug("Vtep config schema {} added to config DS", schemaName);
234     }
235
236     @Override
237     public VtepConfigSchema getVtepConfigSchema(String schemaName) {
238         Optional<VtepConfigSchema> schema = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
239                 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), this.dataBroker);
240         if (schema.isPresent()) {
241             return schema.get();
242         }
243         return null;
244     }
245
246     @Override
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();
252         }
253         return null;
254     }
255
256     @Override
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,
259                 lstDpnsForDelete);
260
261         VtepConfigSchema schema = ItmUtils.validateForUpdateVtepSchema(schemaName, lstDpnsForAdd, lstDpnsForDelete,
262                 this);
263         VtepConfigSchemaBuilder builder = new VtepConfigSchemaBuilder(schema);
264        /* if (ItmUtils.getDpnIdList(schema.getDpnIds()).isEmpty()) {
265             builder.setDpnIds(schema.getDpnIds());
266         } else {*/
267             if (lstDpnsForAdd != null && !lstDpnsForAdd.isEmpty()) {
268                 List<BigInteger> originalDpnList = ItmUtils.getDpnIdList(schema.getDpnIds()) ;
269                 originalDpnList.addAll(lstDpnsForAdd) ;
270                 builder.setDpnIds(ItmUtils.getDpnIdsListFromBigInt(originalDpnList));
271             }
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)));
277             }
278        // }
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()));
283     }
284
285     @Override
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()));
292             }
293         }
294         LOG.debug("Deleted all Vtep schemas from config DS");
295     }
296
297     public void configureTunnelMonitorParams(boolean monitorEnabled, String monitorProtocol) {
298         tepCommandHelper.configureTunnelMonitorParams(monitorEnabled, monitorProtocol);
299     }
300
301     public void configureTunnelMonitorInterval(int interval) {
302         tepCommandHelper.configureTunnelMonitorInterval(interval);
303     }
304 }