Working with OVS
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / servicebindings / flowbased / listeners / FlowBasedServicesConfigListener.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.listeners;
10
11 import com.google.common.util.concurrent.ListenableFuture;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.vpnservice.datastoreutils.AsyncDataTreeChangeListenerBase;
14 import org.opendaylight.vpnservice.datastoreutils.DataStoreJobCoordinator;
15 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.confighelpers.FlowBasedServicesConfigBindHelper;
16 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.confighelpers.FlowBasedServicesConfigUnbindHelper;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.ServiceBindings;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfo;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServices;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import java.util.List;
25 import java.util.concurrent.Callable;
26
27 public class FlowBasedServicesConfigListener extends AsyncDataTreeChangeListenerBase<BoundServices, FlowBasedServicesConfigListener> {
28     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedServicesConfigListener.class);
29     private DataBroker dataBroker;
30
31     public FlowBasedServicesConfigListener(final DataBroker dataBroker) {
32         super(BoundServices.class, FlowBasedServicesConfigListener.class);
33         this.dataBroker = dataBroker;
34     }
35
36     @Override
37     protected InstanceIdentifier<BoundServices> getWildCardPath() {
38         return InstanceIdentifier.create(ServiceBindings.class).child(ServicesInfo.class)
39                 .child(BoundServices.class);
40     }
41
42     @Override
43     protected void remove(InstanceIdentifier<BoundServices> key, BoundServices boundServiceOld) {
44         String interfaceName = InstanceIdentifier.keyOf(key.firstIdentifierOf(ServicesInfo.class)).getInterfaceName();
45         LOG.info("Service Binding Entry removed for Interface: {}, Data: {}",
46                 interfaceName, boundServiceOld);
47
48         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
49         RendererConfigRemoveWorker configWorker = new RendererConfigRemoveWorker(key, boundServiceOld);
50         coordinator.enqueueJob(interfaceName, configWorker);
51     }
52
53     @Override
54     protected void update(InstanceIdentifier<BoundServices> key, BoundServices boundServiceOld,
55                           BoundServices boundServiceNew) {
56         LOG.error("Service Binding entry update not allowed for: {}, Data: {}",
57                 InstanceIdentifier.keyOf(key.firstIdentifierOf(ServicesInfo.class)).getInterfaceName(), boundServiceNew);
58     }
59
60     @Override
61     protected void add(InstanceIdentifier<BoundServices> key, BoundServices boundServicesNew) {
62         String interfaceName = InstanceIdentifier.keyOf(key.firstIdentifierOf(ServicesInfo.class)).getInterfaceName();
63         LOG.info("Service Binding Entry created for Interface: {}, Data: {}",
64                 interfaceName, boundServicesNew);
65
66         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
67         RendererConfigAddWorker configWorker = new RendererConfigAddWorker(key, boundServicesNew);
68         coordinator.enqueueJob(interfaceName, configWorker);
69     }
70
71     @Override
72     protected FlowBasedServicesConfigListener getDataTreeChangeListener() {
73         return FlowBasedServicesConfigListener.this;
74     }
75
76     private class RendererConfigAddWorker implements Callable<List<ListenableFuture<Void>>> {
77         InstanceIdentifier<BoundServices> instanceIdentifier;
78         BoundServices boundServicesNew;
79
80         public RendererConfigAddWorker(InstanceIdentifier<BoundServices> instanceIdentifier,
81                                        BoundServices boundServicesNew) {
82             this.instanceIdentifier = instanceIdentifier;
83             this.boundServicesNew = boundServicesNew;
84         }
85
86         @Override
87         public List<ListenableFuture<Void>> call() throws Exception {
88             return FlowBasedServicesConfigBindHelper.bindService(instanceIdentifier,
89                     boundServicesNew, dataBroker);
90         }
91     }
92
93     private class RendererConfigRemoveWorker implements Callable<List<ListenableFuture<Void>>> {
94         InstanceIdentifier<BoundServices> instanceIdentifier;
95         BoundServices boundServicesNew;
96
97         public RendererConfigRemoveWorker(InstanceIdentifier<BoundServices> instanceIdentifier,
98                                        BoundServices boundServicesNew) {
99             this.instanceIdentifier = instanceIdentifier;
100             this.boundServicesNew = boundServicesNew;
101         }
102
103         @Override
104         public List<ListenableFuture<Void>> call() throws Exception {
105             return FlowBasedServicesConfigUnbindHelper.unbindService(instanceIdentifier,
106                     boundServicesNew, dataBroker);
107         }
108     }
109 }