Imported vpnservice as a subtree
[netvirt.git] / vpnservice / itm / itm-impl / src / main / java / org / opendaylight / vpnservice / itm / impl / ItmProvider.java
1 /*
2  * Copyright (c) 2015, 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.vpnservice.itm.impl;
9
10 import java.math.BigInteger;
11 import java.util.List;
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.Future;
14
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.listeners.InterfaceStateListener;
28 import org.opendaylight.vpnservice.itm.listeners.TransportZoneListener;
29 import org.opendaylight.vpnservice.itm.listeners.TunnelMonitorChangeListener;
30 import org.opendaylight.vpnservice.itm.listeners.TunnelMonitorIntervalListener;
31 import org.opendaylight.vpnservice.itm.listeners.VtepConfigSchemaListener;
32 import org.opendaylight.vpnservice.itm.monitoring.ItmTunnelEventListener;
33 import org.opendaylight.vpnservice.itm.rpc.ItmManagerRpcService;
34 import org.opendaylight.vpnservice.itm.snd.ITMStatusMonitor;
35 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
36 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.VtepConfigSchemas;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.vtep.config.schemas.VtepConfigSchema;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.vtep.config.schemas.VtepConfigSchemaBuilder;
40 //import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.itm.config.rev151102.VtepConfigSchemas;
41 //import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.itm.config.rev151102.vtep.config.schemas.VtepConfigSchema;
42 //import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.itm.config.rev151102.vtep.config.schemas.VtepConfigSchemaBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.CreateIdPoolInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.CreateIdPoolInputBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.ItmRpcService;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.TunnelList ;
48 import org.opendaylight.yangtools.yang.common.RpcResult;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 import com.google.common.base.Optional;
53 import com.google.common.base.Preconditions;
54
55 public class ItmProvider implements BindingAwareProvider, AutoCloseable, IITMProvider /*,ItmStateService */{
56
57     private static final Logger LOG = LoggerFactory.getLogger(ItmProvider.class);
58     private IInterfaceManager interfaceManager;
59     private ITMManager itmManager;
60     private IMdsalApiManager mdsalManager;
61     private DataBroker dataBroker;
62     private NotificationPublishService notificationPublishService;
63     private ItmManagerRpcService itmRpcService ;
64     private IdManagerService idManager;
65     private NotificationService notificationService;
66     private TepCommandHelper tepCommandHelper;
67     private TransportZoneListener tzChangeListener;
68     private TunnelMonitorChangeListener tnlToggleListener;
69     private TunnelMonitorIntervalListener tnlIntervalListener;
70     private VtepConfigSchemaListener vtepConfigSchemaListener;
71     private InterfaceStateListener ifStateListener;
72     private RpcProviderRegistry rpcProviderRegistry;
73     private static final ITMStatusMonitor itmStatusMonitor = ITMStatusMonitor.getInstance();
74     private ItmTunnelEventListener itmStateListener;
75     static short flag = 0;
76
77     public ItmProvider() {
78         LOG.info("ItmProvider Before register MBean");
79         itmStatusMonitor.registerMbean();
80     }
81
82     public void setRpcProviderRegistry(RpcProviderRegistry rpcProviderRegistry) {
83         this.rpcProviderRegistry = rpcProviderRegistry;
84     }
85
86     public RpcProviderRegistry getRpcProviderRegistry() {
87         return this.rpcProviderRegistry;
88     }
89
90     @Override
91     public void onSessionInitiated(ProviderContext session) {
92         LOG.info("ItmProvider Session Initiated");
93         itmStatusMonitor.reportStatus("STARTING");
94         try {
95             dataBroker = session.getSALService(DataBroker.class);
96             idManager = getRpcProviderRegistry().getRpcService(IdManagerService.class);
97
98             itmManager = new ITMManager(dataBroker);
99             tzChangeListener = new TransportZoneListener(dataBroker, idManager) ;
100             itmRpcService = new ItmManagerRpcService(dataBroker, idManager);
101             vtepConfigSchemaListener = new VtepConfigSchemaListener(dataBroker);
102             this.ifStateListener = new InterfaceStateListener(dataBroker);
103             tnlToggleListener = new TunnelMonitorChangeListener(dataBroker);
104             tnlIntervalListener = new TunnelMonitorIntervalListener(dataBroker);
105             tepCommandHelper = new TepCommandHelper(dataBroker);
106             final BindingAwareBroker.RpcRegistration<ItmRpcService> rpcRegistration = getRpcProviderRegistry().addRpcImplementation(ItmRpcService.class, itmRpcService);
107             itmRpcService.setMdsalManager(mdsalManager);
108             itmManager.setMdsalManager(mdsalManager);
109             itmManager.setNotificationPublishService(notificationPublishService);
110             itmManager.setMdsalManager(mdsalManager);
111             tzChangeListener.setMdsalManager(mdsalManager);
112             tzChangeListener.setItmManager(itmManager);
113             tzChangeListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
114             tnlIntervalListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
115             tnlToggleListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
116             tepCommandHelper = new TepCommandHelper(dataBroker);
117             tepCommandHelper.setInterfaceManager(interfaceManager);
118             itmStateListener =new ItmTunnelEventListener(dataBroker);
119             createIdPool();
120             itmStatusMonitor.reportStatus("OPERATIONAL");
121         } catch (Exception e) {
122             LOG.error("Error initializing services", e);
123             itmStatusMonitor.reportStatus("ERROR");
124         }
125     }
126
127     public void setInterfaceManager(IInterfaceManager interfaceManager) {
128         this.interfaceManager = interfaceManager;
129     }
130
131     public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
132         this.notificationPublishService = notificationPublishService;
133     }
134     
135     public void setMdsalApiManager(IMdsalApiManager mdsalMgr) {
136         this.mdsalManager = mdsalMgr;
137     }
138     public void setNotificationService(NotificationService notificationService) {
139         this.notificationService = notificationService;
140     }
141
142     @Override
143     public void close() throws Exception {
144         if (itmManager != null) {
145             itmManager.close();
146         }
147         if (tzChangeListener != null) {
148             tzChangeListener.close();
149         }
150         if (tnlIntervalListener != null) {
151             tnlIntervalListener.close();
152         }
153         if(tnlToggleListener!= null){
154             tnlToggleListener.close();
155         }
156         LOG.info("ItmProvider Closed");
157     }
158
159     private void createIdPool() {
160         CreateIdPoolInput createPool = new CreateIdPoolInputBuilder()
161             .setPoolName(ITMConstants.ITM_IDPOOL_NAME)
162             .setLow(ITMConstants.ITM_IDPOOL_START)
163             .setHigh(new BigInteger(ITMConstants.ITM_IDPOOL_SIZE).longValue())
164             .build();
165         try {
166             Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
167             if ((result != null) && (result.get().isSuccessful())) {
168                 LOG.debug("Created IdPool for ITM Service");
169             }
170         } catch (InterruptedException | ExecutionException e) {
171             LOG.error("Failed to create idPool for ITM Service",e);
172         }
173     }
174
175     @Override
176     public DataBroker getDataBroker() {
177         return dataBroker;
178     }
179     @Override
180     public void createLocalCache(BigInteger dpnId, String portName, Integer vlanId, String ipAddress, String subnetMask,
181                     String gatewayIp, String transportZone) {
182         if (tepCommandHelper != null) {
183             tepCommandHelper.createLocalCache(dpnId, portName, vlanId, ipAddress, subnetMask, gatewayIp, transportZone);
184         } else {
185             LOG.trace("tepCommandHelper doesnt exist");
186         }
187     }
188
189     @Override
190     public void commitTeps() {
191         try {
192                 tepCommandHelper.deleteOnCommit();
193                 tepCommandHelper.buildTeps();
194         } catch (Exception e) {
195             LOG.debug("unable to configure teps" + e.toString());
196         }
197     }
198
199     @Override
200     public void showTeps() {
201         tepCommandHelper.showTeps(itmManager.getTunnelMonitorEnabledFromConfigDS(),
202                 itmManager.getTunnelMonitorIntervalFromConfigDS());
203     }
204     public void showState(TunnelList tunnels) {
205         if (tunnels != null)
206            tepCommandHelper.showState(tunnels, itmManager.getTunnelMonitorEnabledFromConfigDS());
207         else
208             LOG.debug("No tunnels available");
209     }
210
211     public void deleteVtep(BigInteger dpnId, String portName, Integer vlanId, String ipAddress, String subnetMask,
212             String gatewayIp, String transportZone) {
213         try {
214            tepCommandHelper.deleteVtep(dpnId,  portName, vlanId, ipAddress, subnetMask, gatewayIp, transportZone);
215         } catch (Exception e) {
216             e.printStackTrace();
217         }
218     }
219
220     @Override
221     public void configureTunnelType(String transportZone, String tunnelType) {
222         LOG .debug("ItmProvider: configureTunnelType {} for transportZone {}", tunnelType, transportZone);
223         tepCommandHelper.configureTunnelType(transportZone,tunnelType);
224     }
225
226     /*
227      * (non-Javadoc)
228      * 
229      * @see
230      * org.opendaylight.vpnservice.itm.api.IITMProvider#addVtepConfigSchema(org.
231      * opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.itm.config.
232      * rev151102.vtep.config.schemas.VtepConfigSchema)
233      */
234     @Override
235     public void addVtepConfigSchema(VtepConfigSchema vtepConfigSchema) {
236         VtepConfigSchema validatedSchema = ItmUtils.validateForAddVtepConfigSchema(vtepConfigSchema,
237                 getAllVtepConfigSchemas());
238
239         String schemaName = validatedSchema.getSchemaName();
240         VtepConfigSchema existingSchema = getVtepConfigSchema(schemaName);
241         if (existingSchema != null) {
242             Preconditions.checkArgument(false, String.format("VtepConfigSchema [%s] already exists!", schemaName));
243         }
244         MDSALUtil.syncWrite(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
245                 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), validatedSchema);
246         LOG.debug("Vtep config schema {} added to config DS", schemaName);
247     }
248
249     /*
250      * (non-Javadoc)
251      * 
252      * @see
253      * org.opendaylight.vpnservice.itm.api.IITMProvider#getVtepConfigSchema(java
254      * .lang.String)
255      */
256     @Override
257     public VtepConfigSchema getVtepConfigSchema(String schemaName) {
258         Optional<VtepConfigSchema> schema = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
259                 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), this.dataBroker);
260         if (schema.isPresent()) {
261             return schema.get();
262         }
263         return null;
264     }
265
266     /*
267      * (non-Javadoc)
268      * 
269      * @see
270      * org.opendaylight.vpnservice.itm.api.IITMProvider#getAllVtepConfigSchemas(
271      * )
272      */
273     @Override
274     public List<VtepConfigSchema> getAllVtepConfigSchemas() {
275         Optional<VtepConfigSchemas> schemas = ItmUtils.read(LogicalDatastoreType.CONFIGURATION,
276                 ItmUtils.getVtepConfigSchemasIdentifier(), this.dataBroker);
277         if (schemas.isPresent()) {
278             return schemas.get().getVtepConfigSchema();
279         }
280         return null;
281     }
282
283     /*
284      * (non-Javadoc)
285      * 
286      * @see
287      * org.opendaylight.vpnservice.itm.api.IITMProvider#updateVtepSchema(java.
288      * lang.String, java.util.List, java.util.List)
289      */
290     @Override
291     public void updateVtepSchema(String schemaName, List<BigInteger> lstDpnsForAdd, List<BigInteger> lstDpnsForDelete) {
292         LOG.trace("Updating VTEP schema {} by adding DPN's {} and deleting DPN's {}.", schemaName, lstDpnsForAdd,
293                 lstDpnsForDelete);
294
295         VtepConfigSchema schema = ItmUtils.validateForUpdateVtepSchema(schemaName, lstDpnsForAdd, lstDpnsForDelete,
296                 this);
297         if (ItmUtils.getDpnIdList(schema.getDpnIds()) == null) {
298             VtepConfigSchemaBuilder builder = new VtepConfigSchemaBuilder(schema);
299         if (ItmUtils.getDpnIdList(schema.getDpnIds()).isEmpty()) {
300             builder.setDpnIds(schema.getDpnIds());
301             schema = builder.build();
302         } else {
303             if (lstDpnsForAdd != null && !lstDpnsForAdd.isEmpty()) {
304                 List<BigInteger> originalDpnList = ItmUtils.getDpnIdList(schema.getDpnIds()) ;
305                 originalDpnList.addAll(lstDpnsForAdd) ;
306                 builder.setDpnIds(ItmUtils.getDpnIdsListFromBigInt(originalDpnList));
307             }
308             if (lstDpnsForDelete != null && !lstDpnsForDelete.isEmpty()) {
309                 List<BigInteger> originalDpnList = ItmUtils.getDpnIdList(schema.getDpnIds()) ;
310                 originalDpnList.removeAll(lstDpnsForAdd) ;
311                 builder.setDpnIds(ItmUtils.getDpnIdsListFromBigInt(originalDpnList)) ;
312             }
313         }
314         schema = builder.build();
315         MDSALUtil.syncWrite(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
316                 ItmUtils.getVtepConfigSchemaIdentifier(schemaName), schema);
317         LOG.debug("Vtep config schema {} updated to config DS with DPN's {}", schemaName, ItmUtils.getDpnIdList(schema.getDpnIds()));
318     }
319     }
320     /*
321      * (non-Javadoc)
322      * 
323      * @see
324      * org.opendaylight.vpnservice.itm.api.IITMProvider#deleteAllVtepSchemas()
325      */
326     @Override
327     public void deleteAllVtepSchemas() {
328         List<VtepConfigSchema> lstSchemas = getAllVtepConfigSchemas();
329         if (lstSchemas != null && !lstSchemas.isEmpty()) {
330             for (VtepConfigSchema schema : lstSchemas) {
331                 MDSALUtil.syncDelete(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
332                         ItmUtils.getVtepConfigSchemaIdentifier(schema.getSchemaName()));
333             }
334         }
335         LOG.debug("Deleted all Vtep schemas from config DS");
336     }
337
338     public void configureTunnelMonitorEnabled(boolean monitorEnabled) {
339         tepCommandHelper.configureTunnelMonitorEnabled(monitorEnabled);
340     }
341
342     public void configureTunnelMonitorInterval(int interval) {
343         tepCommandHelper.configureTunnelMonitorInterval(interval);
344     }
345 }