b29509726bcac765c887629e6033c4d43e41ed07
[groupbasedpolicy.git] / groupbasedpolicy-ise-adapter / src / main / java / org / opendaylight / groupbasedpolicy / gbp_ise_adapter / impl / GbpIseAdapterProvider.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, 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.groupbasedpolicy.gbp_ise_adapter.impl;
10
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.DataTreeChangeListener;
14 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
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.BindingAwareProvider;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.gbp.ise.adapter.model.rev160630.GbpIseAdapter;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.gbp.ise.adapter.model.rev160630.gbp.ise.adapter.IseHarvestConfig;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Purpose: main provider of gbp-ise adapter (for reading sgts and generating EndpointPolicyTemplates)
27  */
28 public class GbpIseAdapterProvider implements AutoCloseable, BindingAwareProvider {
29
30     private static final Logger LOG = LoggerFactory.getLogger(GbpIseAdapterProvider.class);
31
32     private final DataBroker dataBroker;
33     private ListenerRegistration<DataTreeChangeListener<IseHarvestConfig>> registration;
34
35     public GbpIseAdapterProvider(final DataBroker dataBroker, final BindingAwareBroker brokerDependency) {
36         this.dataBroker = Preconditions.checkNotNull(dataBroker, "provided dataBroker must not be null");
37         brokerDependency.registerProvider(this);
38     }
39
40     @Override
41     public void close() throws Exception {
42         if (registration != null) {
43             LOG.info("closing GbpIseAdapterProvider");
44             registration.close();
45             registration = null;
46         }
47     }
48
49     @Override
50     public void onSessionInitiated(final BindingAwareBroker.ProviderContext providerContext) {
51         LOG.info("Starting GbpIseAdapterProvider ..");
52
53         // setup harvesting and processing pipeline
54         final SgtInfoProcessor epgGenerator = new SgtToEpgGeneratorImpl(dataBroker);
55         final SgtInfoProcessor templateGenerator = new SgtToEPTemplateGeneratorImpl(dataBroker);
56         final GbpIseSgtHarvester gbpIseSgtHarvester = new GbpIseSgtHarvesterImpl(epgGenerator, templateGenerator);
57         final GbpIseConfigListenerImpl gbpIseConfigListener = new GbpIseConfigListenerImpl(dataBroker, gbpIseSgtHarvester);
58
59         // build data-tree path
60         final DataTreeIdentifier<IseHarvestConfig> dataTreePath = new DataTreeIdentifier<>(
61                 LogicalDatastoreType.CONFIGURATION,
62                 InstanceIdentifier.create(GbpIseAdapter.class).child(IseHarvestConfig.class));
63
64         // register config listener
65         registration = dataBroker.registerDataTreeChangeListener(dataTreePath,
66                 gbpIseConfigListener);
67
68         LOG.info("Started");
69     }
70 }