[ONOS-4170] LSP-DB sync

Change-Id: Icda3afd9cca8d1fb8c58b44da6bc26064b300388
diff --git a/protocols/pcep/api/BUCK b/protocols/pcep/api/BUCK
index 8930a95..5ced968 100644
--- a/protocols/pcep/api/BUCK
+++ b/protocols/pcep/api/BUCK
@@ -2,6 +2,7 @@
     '//lib:CORE_DEPS',
     '//protocols/pcep/pcepio:onos-protocols-pcep-pcepio',
     '//apps/pcep-api:onos-apps-pcep-api',
+    '//incubator/api:onos-incubator-api',
 ]
 
 osgi_jar_with_tests (
diff --git a/protocols/pcep/api/pom.xml b/protocols/pcep/api/pom.xml
index f334654..f514985 100644
--- a/protocols/pcep/api/pom.xml
+++ b/protocols/pcep/api/pom.xml
@@ -52,5 +52,9 @@
             <groupId>org.onosproject</groupId>
             <artifactId>onlab-misc</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-incubator-api</artifactId>
+        </dependency>
     </dependencies>
 </project>
diff --git a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepClient.java b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepClient.java
index 297d71e..c5cf003 100644
--- a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepClient.java
+++ b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepClient.java
@@ -19,6 +19,7 @@
 
 import org.onosproject.pcepio.protocol.PcepFactory;
 import org.onosproject.pcepio.protocol.PcepMessage;
+import org.onosproject.pcepio.protocol.PcepStateReport;
 
 /**
  * Represents to provider facing side of a path computation client(pcc).
@@ -165,4 +166,34 @@
      * @return delegation flag
      */
     Boolean delegationInfo(LspKey lspKey);
+
+    /**
+     * Creates a temporary cache to hold report messages received during LSPDB sync.
+     *
+     * @param pccId PCC id which is the key to store report messages
+     */
+    void initializeSyncMsgList(PccId pccId);
+
+    /**
+     * Returns the list of report messages received during LSPDB sync.
+     *
+     * @param pccId PCC id which is the key for all the report messages
+     * @return list of report messages received during LSPDB sync
+     */
+    List<PcepStateReport> getSyncMsgList(PccId pccId);
+
+    /**
+     * Removes the list of report messages received during LSPDB sync.
+     *
+     * @param pccId PCC id which is the key for all the report messages
+     */
+    void removeSyncMsgList(PccId pccId);
+
+    /**
+     * Adds report message received during LSPDB sync into temporary cache.
+     *
+     * @param pccId PCC id which is the key to store report messages
+     * @param rptMsg the report message to be stored
+     */
+    void addSyncMsgToList(PccId pccId, PcepStateReport rptMsg);
 }
diff --git a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepClientController.java b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepClientController.java
index fff6759..0f14778 100644
--- a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepClientController.java
+++ b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepClientController.java
@@ -85,6 +85,20 @@
     void removeNodeListener(PcepNodeListener listener);
 
     /**
+     * Register a listener for packet events.
+     *
+     * @param listener the listener to notify
+     */
+    void addPacketListener(PcepPacketListener listener);
+
+    /**
+     * Unregister a packet listener.
+     *
+     * @param listener the listener to unregister
+     */
+    void removePacketListener(PcepPacketListener listener);
+
+    /**
      * Send a message to a particular pcc client.
      *
      * @param pccId the id of the client to send message.
diff --git a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepEventListener.java b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepEventListener.java
index 7ba8ca7..c7c347a 100644
--- a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepEventListener.java
+++ b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepEventListener.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.pcep.controller;
 
+import org.onosproject.incubator.net.tunnel.Tunnel;
 import org.onosproject.pcepio.protocol.PcepMessage;
 /**
  * Notifies providers about PCEP message events.
@@ -28,4 +29,21 @@
      * @param msg the message
      */
     void handleMessage(PccId pccId, PcepMessage msg);
+
+    /**
+     * Handles end of LSPDB sync actions.
+     *
+     * @param tunnel the tunnel on which action needs to be taken
+     * @param endOfSyncAction the action that needs to be taken for the tunnel
+     */
+    void handleEndOfSyncAction(Tunnel tunnel, PcepLspSyncAction endOfSyncAction);
+
+    /**
+     * Handles sending PCEP message to client on end of LSPDB sync.
+     *
+     * @param pccId id of the pcc
+     * @param msg the message to be sent
+     * @param endOfSyncAction the action that needs to be taken in the message
+     */
+    void handleEndOfSyncAction(PccId pccId, PcepMessage msg, PcepLspSyncAction endOfSyncAction);
 }
diff --git a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepLspSyncAction.java b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepLspSyncAction.java
new file mode 100644
index 0000000..92ce8e6
--- /dev/null
+++ b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepLspSyncAction.java
@@ -0,0 +1,53 @@
+/*
+ * 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.pcep.controller;
+
+/**
+ * Representation of actions to be taken for LSPs on end of LSP-DB sync.
+ */
+public enum PcepLspSyncAction {
+
+    /**
+     * Specifies that delete message for PCE intiiated tunnel should be sent.
+     */
+    SEND_DELETE(0),
+
+    /**
+     * Specifies that update message should be sent.
+     */
+    SEND_UPDATE(1),
+
+    /**
+     * Specifies that the tunnel should be removed from PCE.
+     */
+    REMOVE(2),
+
+    /**
+     * Specifies that the status of the tunnel should be set as unstable.
+     */
+    UNSTABLE(3);
+
+    int value;
+
+    /**
+     * Assigns val with the value for actions to be taken for LSPs on end of LSP-DB sync.
+     *
+     * @param val sync status
+     */
+    PcepLspSyncAction(int val) {
+        value = val;
+    }
+}
diff --git a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepPacketListener.java b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepPacketListener.java
new file mode 100644
index 0000000..c2017ae
--- /dev/null
+++ b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepPacketListener.java
@@ -0,0 +1,22 @@
+/*
+ * 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.pcep.controller;
+
+public interface PcepPacketListener {
+
+    void sendPacketIn(PccId pccId);
+
+}
diff --git a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/driver/PcepAgent.java b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/driver/PcepAgent.java
index de95962..55d5614 100755
--- a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/driver/PcepAgent.java
+++ b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/driver/PcepAgent.java
@@ -73,4 +73,12 @@
      * @param pccId PCEP client ID
      */
     void deleteNode(PccId pccId);
+
+    /**
+     * Analyzes report messages received during LSP DB sync again tunnel store and takes necessary actions.
+     *
+     * @param pccId the id of pcc client
+     * @return success or failure
+     */
+    boolean analyzeSyncMsgList(PccId pccId);
 }