Merge "add rest of sfc flows"
[netvirt.git] / openstack / net-virt-sfc / impl / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / sfc / NetvirtSfcProvider.java
1 /*
2  * Copyright © 2015 Dell, Inc. 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.ovsdb.openstack.netvirt.sfc;
10
11 import java.util.Dictionary;
12 import java.util.Hashtable;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
16 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
17 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
18 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.OF13Provider;
19 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
20 import org.opendaylight.ovsdb.openstack.netvirt.sfc.standalone.openflow13.NetvirtSfcStandaloneOF13Provider;
21 import org.opendaylight.ovsdb.openstack.netvirt.sfc.standalone.openflow13.services.SfcClassifierService;
22 import org.opendaylight.ovsdb.openstack.netvirt.sfc.workaround.NetvirtSfcWorkaroundOF13Provider;
23 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
24 import org.osgi.framework.BundleContext;
25 import org.osgi.framework.ServiceRegistration;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class NetvirtSfcProvider implements BindingAwareProvider, AutoCloseable {
30     private static final Logger LOG = LoggerFactory.getLogger(NetvirtSfcProvider.class);
31     private NetvirtSfcAclListener aclListener;
32     private NetvirtSfcClassifierListener classifierListener;
33
34     public void setOf13Provider(String of13Provider) {
35         LOG.info("of13Provider is: {}", of13Provider);
36         this.of13Provider = of13Provider;
37     }
38
39     private String of13Provider;
40
41     public void setBundleContext(BundleContext bundleContext) {
42         LOG.info("bundleContext is: {}", bundleContext);
43         this.bundleContext = bundleContext;
44     }
45
46     private BundleContext bundleContext;
47
48     public NetvirtSfcProvider(BundleContext bundleContext) {
49         LOG.info("NetvirtSfcProvider: bundleContext: {}", bundleContext);
50         this.bundleContext = bundleContext;
51     }
52
53     @Override
54     public void onSessionInitiated(ProviderContext session) {
55         LOG.info("NetvirtSfcProvider Session Initiated");
56         DataBroker dataBroker = session.getSALService(DataBroker.class);
57
58         MdsalUtils mdsalUtils = new MdsalUtils(dataBroker);
59         SfcUtils sfcUtils = new SfcUtils(mdsalUtils);
60
61         // Allocate provider based on config
62         INetvirtSfcOF13Provider provider;
63         if (of13Provider.equals("standalone")) {
64             provider = new NetvirtSfcStandaloneOF13Provider(dataBroker);
65         } else {
66             provider = new NetvirtSfcWorkaroundOF13Provider(dataBroker, mdsalUtils, sfcUtils);
67         }
68         aclListener = new NetvirtSfcAclListener(provider, dataBroker);
69         classifierListener = new NetvirtSfcClassifierListener(provider, dataBroker);
70
71         addToPipeline(provider);
72         provider.setDependencies(null);
73     }
74
75     @Override
76     public void close() throws Exception {
77         LOG.info("NetvirtSfcProvider Closed");
78         aclListener.close();
79         classifierListener.close();
80     }
81
82     private void addToPipeline(INetvirtSfcOF13Provider provider) {
83         if (provider instanceof NetvirtSfcStandaloneOF13Provider) {
84             SfcClassifierService sfcClassifierService =
85                     new org.opendaylight.ovsdb.openstack.netvirt.sfc.standalone.openflow13.services.SfcClassifierService();
86             registerService(bundleContext, ISfcClassifierService.class.getName(),
87                     sfcClassifierService, Service.SFC_CLASSIFIER);
88             sfcClassifierService.setDependencies(bundleContext, null);
89         } else {
90             org.opendaylight.ovsdb.openstack.netvirt.sfc.workaround.services.SfcClassifierService sfcClassifierService =
91                     new org.opendaylight.ovsdb.openstack.netvirt.sfc.workaround.services.SfcClassifierService();
92             registerService(bundleContext, ISfcClassifierService.class.getName(),
93                     sfcClassifierService, Service.SFC_CLASSIFIER);
94             sfcClassifierService.setDependencies(bundleContext, null);
95         }
96
97         //provider.setSfcClassifierService(sfcClassifierService);
98     }
99
100     private ServiceRegistration<?> registerService(BundleContext bundleContext, String[] interfaces,
101                                                    Dictionary<String, Object> properties, Object impl) {
102         ServiceRegistration<?> serviceRegistration = bundleContext.registerService(interfaces, impl, properties);
103         return serviceRegistration;
104     }
105
106     private ServiceRegistration<?> registerService(BundleContext bundleContext, String interfaceClassName,
107                                                        Object impl, Object serviceProperty) {
108         Dictionary<String, Object> properties = new Hashtable<>();
109         properties.put(AbstractServiceInstance.SERVICE_PROPERTY, serviceProperty);
110         properties.put(Constants.PROVIDER_NAME_PROPERTY, OF13Provider.NAME);
111         return registerService(bundleContext,
112                 new String[] {AbstractServiceInstance.class.getName(),interfaceClassName},
113                 properties, impl);
114     }
115 }