Priyanka B
Committed by Gerrit Code Review

[ONOS-4169] Global optimization timer part

Change-Id: I75ef6aedc5350985e38512cc36b7df52b4680423
...@@ -26,6 +26,9 @@ import java.util.Map; ...@@ -26,6 +26,9 @@ import java.util.Map;
26 import java.util.Optional; 26 import java.util.Optional;
27 import java.util.Map.Entry; 27 import java.util.Map.Entry;
28 import java.util.Set; 28 import java.util.Set;
29 +import java.util.concurrent.Executors;
30 +import java.util.concurrent.ScheduledExecutorService;
31 +import java.util.concurrent.TimeUnit;
29 32
30 import org.apache.felix.scr.annotations.Activate; 33 import org.apache.felix.scr.annotations.Activate;
31 import org.apache.felix.scr.annotations.Component; 34 import org.apache.felix.scr.annotations.Component;
...@@ -189,6 +192,10 @@ public class PceManager implements PceService { ...@@ -189,6 +192,10 @@ public class PceManager implements PceService {
189 192
190 private final PcepPacketProcessor processor = new PcepPacketProcessor(); 193 private final PcepPacketProcessor processor = new PcepPacketProcessor();
191 private final TopologyListener topologyListener = new InternalTopologyListener(); 194 private final TopologyListener topologyListener = new InternalTopologyListener();
195 + private ScheduledExecutorService executor;
196 +
197 + public static final int INITIAL_DELAY = 30;
198 + public static final int PERIODIC_DELAY = 30;
192 199
193 /** 200 /**
194 * Creates new instance of PceManager. 201 * Creates new instance of PceManager.
...@@ -217,6 +224,9 @@ public class PceManager implements PceService { ...@@ -217,6 +224,9 @@ public class PceManager implements PceService {
217 224
218 packetService.addProcessor(processor, PacketProcessor.director(4)); 225 packetService.addProcessor(processor, PacketProcessor.director(4));
219 topologyService.addListener(topologyListener); 226 topologyService.addListener(topologyListener);
227 + executor = Executors.newSingleThreadScheduledExecutor();
228 + //Start a timer when the component is up, with initial delay of 30min and periodic delays at 30min
229 + executor.scheduleAtFixedRate(new GlobalOptimizationTimer(), INITIAL_DELAY, PERIODIC_DELAY, TimeUnit.MINUTES);
220 log.info("Started"); 230 log.info("Started");
221 } 231 }
222 232
...@@ -225,6 +235,8 @@ public class PceManager implements PceService { ...@@ -225,6 +235,8 @@ public class PceManager implements PceService {
225 tunnelService.removeListener(listener); 235 tunnelService.removeListener(listener);
226 packetService.removeProcessor(processor); 236 packetService.removeProcessor(processor);
227 topologyService.removeListener(topologyListener); 237 topologyService.removeListener(topologyListener);
238 + //Shutdown the thread when component is deactivated
239 + executor.shutdown();
228 log.info("Stopped"); 240 log.info("Stopped");
229 } 241 }
230 242
...@@ -653,7 +665,12 @@ public class PceManager implements PceService { ...@@ -653,7 +665,12 @@ public class PceManager implements PceService {
653 constraintList.add(CostConstraint.of(CostConstraint.Type.valueOf(tunnel.annotations().value( 665 constraintList.add(CostConstraint.of(CostConstraint.Type.valueOf(tunnel.annotations().value(
654 COST_TYPE)))); 666 COST_TYPE))));
655 } 667 }
656 - if (!updatePath(tunnel.tunnelId(), constraintList)) { 668 +
669 + /*
670 + * If tunnel was UP after recomputation failed then store failed path in PCE store send PCIntiate(remove)
671 + * and If tunnel is failed and computation fails nothing to do because tunnel status will be same[Failed]
672 + */
673 + if (!updatePath(tunnel.tunnelId(), constraintList) && !tunnel.state().equals(Tunnel.State.FAILED)) {
657 // If updation fails store in PCE store as failed path 674 // If updation fails store in PCE store as failed path
658 // then PCInitiate (Remove) 675 // then PCInitiate (Remove)
659 pceStore.addFailedPathInfo(new PcePathInfo(tunnel.path().src().deviceId(), tunnel 676 pceStore.addFailedPathInfo(new PcePathInfo(tunnel.path().src().deviceId(), tunnel
...@@ -942,4 +959,44 @@ public class PceManager implements PceService { ...@@ -942,4 +959,44 @@ public class PceManager implements PceService {
942 } 959 }
943 } 960 }
944 961
962 + //Computes path from tunnel store and also path failed to setup.
963 + private void callForOptimization() {
964 + //Recompute the LSPs which it was delegated [LSPs stored in PCE store (failed paths)]
965 + for (PcePathInfo failedPathInfo : pceStore.getFailedPathInfos()) {
966 + checkForMasterAndSetupPath(failedPathInfo);
967 + }
968 +
969 + //Recompute the LSPs for which it was delegated [LSPs stored in tunnel store]
970 + tunnelService.queryTunnel(MPLS).forEach(t -> {
971 + checkForMasterAndUpdateTunnel(t.path().src().deviceId(), t);
972 + });
973 + }
974 +
975 + private boolean checkForMasterAndSetupPath(PcePathInfo failedPathInfo) {
976 + /**
977 + * Master of ingress node will setup the path failed stored in PCE store.
978 + */
979 + if (mastershipService.isLocalMaster(failedPathInfo.src())) {
980 + if (setupPath(failedPathInfo.src(), failedPathInfo.dst(), failedPathInfo.name(),
981 + failedPathInfo.constraints(), failedPathInfo.lspType())) {
982 + // If computation is success remove that path
983 + pceStore.removeFailedPathInfo(failedPathInfo);
984 + return true;
985 + }
986 + }
987 +
988 + return false;
989 + }
990 +
991 + //Timer to call global optimization
992 + private class GlobalOptimizationTimer implements Runnable {
993 +
994 + public GlobalOptimizationTimer() {
995 + }
996 +
997 + @Override
998 + public void run() {
999 + callForOptimization();
1000 + }
1001 + }
945 } 1002 }
...\ No newline at end of file ...\ No newline at end of file
......