Merge "Changes in different modules"
[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 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;
41
42 import java.math.BigInteger;
43 import java.util.List;
44 import java.util.concurrent.ExecutionException;
45 import java.util.concurrent.Future;
46
47 public class ItmProvider implements BindingAwareProvider, AutoCloseable, IITMProvider /*,ItmStateService */{
48
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;
68
69     public ItmProvider() {
70         LOG.info("ItmProvider Before register MBean");
71         itmStatusMonitor.registerMbean();
72     }
73
74     public void setRpcProviderRegistry(RpcProviderRegistry rpcProviderRegistry) {
75         this.rpcProviderRegistry = rpcProviderRegistry;
76     }
77
78     public RpcProviderRegistry getRpcProviderRegistry() {
79         return this.rpcProviderRegistry;
80     }
81
82     @Override
83     public void onSessionInitiated(ProviderContext session) {
84         LOG.info("ItmProvider Session Initiated");
85         itmStatusMonitor.reportStatus("STARTING");
86         try {
87             dataBroker = session.getSALService(DataBroker.class);
88             idManager = getRpcProviderRegistry().getRpcService(IdManagerService.class);
89
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);
111             createIdPool();
112             itmStatusMonitor.reportStatus("OPERATIONAL");
113         } catch (Exception e) {
114             LOG.error("Error initializing services", e);
115             itmStatusMonitor.reportStatus("ERROR");
116         }
117     }
118
119     public void setInterfaceManager(IInterfaceManager interfaceManager) {
120         this.interfaceManager = interfaceManager;
121     }
122
123     public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
124         this.notificationPublishService = notificationPublishService;
125     }
126
127     public void setMdsalApiManager(IMdsalApiManager mdsalMgr) {
128         this.mdsalManager = mdsalMgr;
129     }
130     public void setNotificationService(NotificationService notificationService) {
131         this.notificationService = notificationService;
132     }
133
134     @Override
135     public void close() throws Exception {
136         if (itmManager != null) {
137             itmManager.close();
138         }
139         if (tzChangeListener != null) {
140             tzChangeListener.close();
141         }
142         if (tnlIntervalListener != null) {
143             tnlIntervalListener.close();
144         }
145         if(tnlToggleListener!= null){
146             tnlToggleListener.close();
147         }
148         LOG.info("ItmProvider Closed");
149     }
150
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())
156                 .build();
157         try {
158             Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
159             if ((result != null) && (result.get().isSuccessful())) {
160                 LOG.debug("Created IdPool for ITM Service");
161             }
162         } catch (InterruptedException | ExecutionException e) {
163             LOG.error("Failed to create idPool for ITM Service",e);
164         }
165     }
166
167     @Override
168     public DataBroker getDataBroker() {
169         return dataBroker;
170     }
171     @Override
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);
176         } else {
177             LOG.trace("tepCommandHelper doesnt exist");
178         }
179     }
180
181     @Override
182     public void commitTeps() {
183         try {
184             tepCommandHelper.deleteOnCommit();
185             tepCommandHelper.buildTeps();
186         } catch (Exception e) {
187             LOG.debug("unable to configure teps" + e.toString());
188         }
189     }
190
191     @Override
192     public void showTeps() {
193         tepCommandHelper.showTeps(itmManager.getTunnelMonitorEnabledFromConfigDS(), ItmUtils.determineMonitorInterval(this.dataBroker));
194     }
195
196     public void showState(TunnelList tunnels) {
197         if (tunnels != null)
198             tepCommandHelper.showState(tunnels, itmManager.getTunnelMonitorEnabledFromConfigDS());
199         else
200             LOG.debug("No tunnels available");
201     }
202
203     public void deleteVtep(BigInteger dpnId, String portName, Integer vlanId, String ipAddress, String subnetMask,
204                            String gatewayIp, String transportZone) {
205         try {
206             tepCommandHelper.deleteVtep(dpnId,  portName, vlanId, ipAddress, subnetMask, gatewayIp, transportZone);
207         } catch (Exception e) {
208             e.printStackTrace();
209         }
210     }
211
212     @Override
213     public void configureTunnelType(String transportZone, String tunnelType) {
214         LOG .debug("ItmProvider: configureTunnelType {} for transportZone {}", tunnelType, transportZone);
215         tepCommandHelper.configureTunnelType(transportZone,tunnelType);
216     }
217
218     @Override
219     public void addVtepConfigSchema(VtepConfigSchema vtepConfigSchema) {
220         VtepConfigSchema validatedSchema = ItmUtils.validateForAddVtepConfigSchema(vtepConfigSchema,
221                 getAllVtepConfigSchemas());
222
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));
227         }
228         MDSALUtil.syncWrite(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
229                 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), validatedSchema);
230         LOG.debug("Vtep config schema {} added to config DS", schemaName);
231     }
232
233     @Override
234     public VtepConfigSchema getVtepConfigSchema(String schemaName) {
235         Optional<VtepConfigSchema> schema = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
236                 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), this.dataBroker);
237         if (schema.isPresent()) {
238             return schema.get();
239         }
240         return null;
241     }
242
243     @Override
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();
249         }
250         return null;
251     }
252
253     @Override
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,
256                 lstDpnsForDelete);
257
258         VtepConfigSchema schema = ItmUtils.validateForUpdateVtepSchema(schemaName, lstDpnsForAdd, lstDpnsForDelete,
259                 this);
260         VtepConfigSchemaBuilder builder = new VtepConfigSchemaBuilder(schema);
261        /* if (ItmUtils.getDpnIdList(schema.getDpnIds()).isEmpty()) {
262             builder.setDpnIds(schema.getDpnIds());
263         } else {*/
264             if (lstDpnsForAdd != null && !lstDpnsForAdd.isEmpty()) {
265                 List<BigInteger> originalDpnList = ItmUtils.getDpnIdList(schema.getDpnIds()) ;
266                 originalDpnList.addAll(lstDpnsForAdd) ;
267                 builder.setDpnIds(ItmUtils.getDpnIdsListFromBigInt(originalDpnList));
268             }
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)));
274             }
275        // }
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()));
280     }
281
282     @Override
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()));
289             }
290         }
291         LOG.debug("Deleted all Vtep schemas from config DS");
292     }
293
294     public void configureTunnelMonitorParams(boolean monitorEnabled, String monitorProtocol) {
295         tepCommandHelper.configureTunnelMonitorParams(monitorEnabled, monitorProtocol);
296     }
297
298     public void configureTunnelMonitorInterval(int interval) {
299         tepCommandHelper.configureTunnelMonitorInterval(interval);
300     }
301 }