[ONOS-4171] Merge PCEP and BGP providers into common BGPCEP provider.

Change-Id: Ia87f0898e51704ce8d02d877e9f0d57219099a53
diff --git a/providers/bgpcep/flow/BUCK b/providers/bgpcep/flow/BUCK
new file mode 100644
index 0000000..28e1e81
--- /dev/null
+++ b/providers/bgpcep/flow/BUCK
@@ -0,0 +1,16 @@
+SRC = 'src/main/java/org/onosproject/**/'
+
+CURRENT_NAME = 'onos-bgpcep-provider-flow'
+CURRENT_TARGET = ':' + CURRENT_NAME
+
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//protocols/bgp/api:onos-bgp-api',
+]
+
+osgi_jar(
+    name = CURRENT_NAME,
+    srcs = glob([SRC + '/*.java']),
+    deps = COMPILE_DEPS,
+    visibility = ['PUBLIC'],
+)
diff --git a/providers/bgpcep/flow/pom.xml b/providers/bgpcep/flow/pom.xml
new file mode 100644
index 0000000..55c65a1
--- /dev/null
+++ b/providers/bgpcep/flow/pom.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2016-present Open Networking Laboratory
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~     http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.onosproject</groupId>
+        <artifactId>onos-bgpcep-providers</artifactId>
+        <version>1.6.0-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <artifactId>onos-bgpcep-provider-flow</artifactId>
+    <packaging>bundle</packaging>
+
+    <description>ONOS BGP-PCEP protocol flow provider</description>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+    </dependencies>
+</project>
diff --git a/providers/bgpcep/flow/src/main/java/org/onosproject/provider/bgpcep/flow/impl/BgpcepFlowRuleProvider.java b/providers/bgpcep/flow/src/main/java/org/onosproject/provider/bgpcep/flow/impl/BgpcepFlowRuleProvider.java
new file mode 100644
index 0000000..3afbec4
--- /dev/null
+++ b/providers/bgpcep/flow/src/main/java/org/onosproject/provider/bgpcep/flow/impl/BgpcepFlowRuleProvider.java
@@ -0,0 +1,166 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.provider.bgpcep.flow.impl;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.bgp.controller.BgpController;
+import org.onosproject.cfg.ComponentConfigService;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleBatchOperation;
+import org.onosproject.net.flow.FlowRuleProvider;
+import org.onosproject.net.flow.FlowRuleProviderRegistry;
+import org.onosproject.net.flow.FlowRuleProviderService;
+import org.onosproject.net.flow.criteria.Criterion;
+import org.onosproject.net.provider.AbstractProvider;
+import org.onosproject.net.provider.ProviderId;
+import org.onosproject.net.resource.ResourceService;
+import org.onosproject.pcep.controller.PcepClient;
+import org.onosproject.pcep.controller.PcepClientController;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of BGP-PCEP flow provider.
+ */
+@Component(immediate = true)
+public class BgpcepFlowRuleProvider extends AbstractProvider
+        implements FlowRuleProvider {
+
+    private final Logger log = getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected FlowRuleProviderRegistry providerRegistry;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected ComponentConfigService cfgService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected BgpController bgpController;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected PcepClientController pcepController;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected ResourceService resourceService;
+
+    private FlowRuleProviderService providerService;
+
+    /**
+     * Creates a BgpFlow host provider.
+     */
+    public BgpcepFlowRuleProvider() {
+        super(new ProviderId("l3", "org.onosproject.provider.bgpcep"));
+    }
+
+    @Activate
+    public void activate(ComponentContext context) {
+        cfgService.registerProperties(getClass());
+        providerService = providerRegistry.register(this);
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate(ComponentContext context) {
+        cfgService.unregisterProperties(getClass(), false);
+        providerRegistry.unregister(this);
+        providerService = null;
+        log.info("Stopped");
+    }
+
+    @Override
+    public void applyFlowRule(FlowRule... flowRules) {
+        for (FlowRule flowRule : flowRules) {
+            applyRule(flowRule);
+        }
+    }
+
+    private void applyRule(FlowRule flowRule) {
+        flowRule.selector().criteria()
+                .forEach(c -> {
+                    // If Criterion type is MPLS_LABEL, push labels through PCEP client
+                        if (c.type() == Criterion.Type.MPLS_LABEL) {
+                            PcepClient pcc;
+                            /** PCC client session is based on LSR ID, get the LSR ID for a specific device to
+                            push the flows */
+
+                            //TODO: commented code has dependency with other patch
+                     /*     Set<TeRouterId> lrsIds = resourceService.getAvailableResourceValues(Resources
+                                    .discrete(flowRule.deviceId()).id(), TeRouterId.class);
+
+                            lrsIds.forEach(lsrId ->
+                            {
+                                if (pcepController.getClient(PccId.pccId(lsrId)) != null) {
+                                    pcc = pcepController.getClient(PccId.pccId(lsrId));
+                                }
+                            });*/
+                            // TODO: Build message and send the PCEP label message via PCEP client
+                        } else {
+                            // TODO: Get the BGP peer based on deviceId and send the message
+                        }
+                    });
+    }
+
+    @Override
+    public void removeFlowRule(FlowRule... flowRules) {
+        for (FlowRule flowRule : flowRules) {
+            removeRule(flowRule);
+        }
+    }
+
+    private void removeRule(FlowRule flowRule) {
+        flowRule.selector().criteria()
+        .forEach(c -> {
+            // If Criterion type is MPLS_LABEL, remove the specified flow rules
+                if (c.type() == Criterion.Type.MPLS_LABEL) {
+                    PcepClient pcc;
+                    /** PCC client session is based on LSR ID, get the LSR ID for a specific device to
+                    push the flows */
+
+                    //TODO: commented code has dependency with other patch
+             /*     Set<TeRouterId> lrsIds = resourceService.getAvailableResourceValues(Resources
+                            .discrete(flowRule.deviceId()).id(), TeRouterId.class);
+
+                    lrsIds.forEach(lsrId ->
+                    {
+                        if (pcepController.getClient(PccId.pccId(lsrId)) != null) {
+                            pcc = pcepController.getClient(PccId.pccId(lsrId));
+                        }
+                    });*/
+                    // TODO: Build message and send the PCEP label message via PCEP client
+                } else {
+                    // TODO: Get the BGP peer based on deviceId and send the message
+                }
+            });
+    }
+
+    @Override
+    public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
+        // TODO
+        removeFlowRule(flowRules);
+    }
+
+    @Override
+    public void executeBatch(FlowRuleBatchOperation batch) {
+    //TODO
+    }
+}
diff --git a/providers/bgpcep/flow/src/main/java/org/onosproject/provider/bgpcep/flow/impl/package-info.java b/providers/bgpcep/flow/src/main/java/org/onosproject/provider/bgpcep/flow/impl/package-info.java
new file mode 100644
index 0000000..2faf4f0
--- /dev/null
+++ b/providers/bgpcep/flow/src/main/java/org/onosproject/provider/bgpcep/flow/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Representation of BGP-PCEP flow provider.
+ */
+package org.onosproject.provider.bgpcep.flow.impl;