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