From d3fe3fdafdb8e8d0fb3879801de5ef8f3fd1a54a Mon Sep 17 00:00:00 2001
From: Jerry Flood <jflood@att.com>
Date: Tue, 2 Apr 2019 18:53:17 -0400
Subject: Commit 4 for Integrate minizinc optimizer engine

Multiple commits required due to commit size limitation.

Change-Id: Ic8c15314da7c2ffc4e0bc7a937ff3835c3947bfd
Issue-ID: OPTFRA-436
Signed-off-by: Jerry Flood <jflood@att.com>
---
 .../optf/cmso/optimizer/common/LogMessages.java    |  2 +
 .../optf/cmso/optimizer/core/OptimizerManager.java | 88 +++++++++++++++++++---
 .../optimizer/service/rs/models/ChangeWindow.java  | 24 +++++-
 .../service/rs/models/OptimizerResponse.java       | 11 ++-
 4 files changed, 110 insertions(+), 15 deletions(-)

(limited to 'cmso-optimizer/src/main/java/org/onap')

diff --git a/cmso-optimizer/src/main/java/org/onap/optf/cmso/optimizer/common/LogMessages.java b/cmso-optimizer/src/main/java/org/onap/optf/cmso/optimizer/common/LogMessages.java
index f36caee..f8d23eb 100644
--- a/cmso-optimizer/src/main/java/org/onap/optf/cmso/optimizer/common/LogMessages.java
+++ b/cmso-optimizer/src/main/java/org/onap/optf/cmso/optimizer/common/LogMessages.java
@@ -85,6 +85,8 @@ public enum LogMessages implements ObservationInterface {
                     Level.ERROR),
     FAILED_TO_CREATE_OPTIMIZER_REQUEST("Failed to create optimizer request for id={0}", Status.INTERNAL_SERVER_ERROR,
                     Level.ERROR),
+    OPTIMIZER_REQUEST_TIMEOUT("Optimizer engine request timed out id={0} timelimit={1}", Status.INTERNAL_SERVER_ERROR,
+                    Level.ERROR),
 
     ;
     private final String defaultId;
diff --git a/cmso-optimizer/src/main/java/org/onap/optf/cmso/optimizer/core/OptimizerManager.java b/cmso-optimizer/src/main/java/org/onap/optf/cmso/optimizer/core/OptimizerManager.java
index 43ecf3f..eabebec 100644
--- a/cmso-optimizer/src/main/java/org/onap/optf/cmso/optimizer/core/OptimizerManager.java
+++ b/cmso-optimizer/src/main/java/org/onap/optf/cmso/optimizer/core/OptimizerManager.java
@@ -26,13 +26,17 @@ import java.util.Optional;
 import java.util.UUID;
 import javax.ws.rs.core.Response.Status;
 import org.onap.optf.cmso.common.exceptions.CmsoException;
+import org.onap.optf.cmso.optimizer.clients.optimizer.OptimizerRequestManager;
+import org.onap.optf.cmso.optimizer.clients.optimizer.models.OptimizerEngineResponse;
 import org.onap.optf.cmso.optimizer.clients.ticketmgt.TicketMgtRequestManager;
 import org.onap.optf.cmso.optimizer.clients.ticketmgt.models.ActiveTicketsResponse;
 import org.onap.optf.cmso.optimizer.clients.topology.TopologyRequestManager;
 import org.onap.optf.cmso.optimizer.clients.topology.models.TopologyResponse;
 import org.onap.optf.cmso.optimizer.common.LogMessages;
 import org.onap.optf.cmso.optimizer.model.Request;
+import org.onap.optf.cmso.optimizer.model.Response;
 import org.onap.optf.cmso.optimizer.model.dao.RequestDao;
+import org.onap.optf.cmso.optimizer.model.dao.ResponseDao;
 import org.onap.optf.cmso.optimizer.service.rs.models.ChangeWindow;
 import org.onap.optf.cmso.optimizer.service.rs.models.ElementInfo;
 import org.onap.optf.cmso.optimizer.service.rs.models.OptimizerRequest;
@@ -50,12 +54,18 @@ public class OptimizerManager {
     @Autowired
     RequestDao requestDao;
 
+    @Autowired
+    ResponseDao responseDao;
+
     @Autowired
     TopologyRequestManager topologyRequestManager;
 
     @Autowired
     TicketMgtRequestManager ticketMgtRequestManager;
 
+    @Autowired
+    OptimizerRequestManager optimizerRequestManager;
+
     /**
      * Validate optimizer request.
      *
@@ -147,13 +157,22 @@ public class OptimizerManager {
         } catch (JsonProcessingException e) {
             throw new CmsoException(Status.BAD_REQUEST, LogMessages.INVALID_REQUEST, e.getMessage());
         }
-        requestRow.setStatus(OptimizeScheduleStatus.FAILED.toString());
+        requestRow.setStatus(OptimizeScheduleStatus.PENDING_TOPOLOGY.toString());
         requestDao.save(requestRow);
         initiateDataGathering(requestRow);
         requestDao.save(requestRow);
         OptimizeScheduleStatus status = OptimizeScheduleStatus.valueOf(requestRow.getStatus());
-        optimizerResponse.setStatus(status);
-        optimizerResponse.setErrorMessage("");
+        if (status == OptimizeScheduleStatus.COMPLETED)
+        {
+            // COmpletely synchronous optimization
+            optimizerResponse = getCompletedOptimizerResponse(uuid);
+        }
+        else
+        {
+            // One or more steps are asynchronous
+            optimizerResponse.setStatus(status);
+            optimizerResponse.setErrorMessage("");
+        }
         return optimizerResponse;
     }
 
@@ -174,8 +193,8 @@ public class OptimizerManager {
                     return;
                 case IN_PROGRESS:
                     requestRow.setRequestStart(System.currentTimeMillis());
-                    requestRow.setStatus(OptimizeScheduleStatus.PENDING_TOPOLOGY.toString());
-                    break;
+                    requestRow.setStatus(OptimizeScheduleStatus.TOPOLOGY_IN_PROGRESS.toString());
+                   return;
                 default:
                     break;
             }
@@ -192,17 +211,17 @@ public class OptimizerManager {
                     requestRow.setRequestStart(System.currentTimeMillis());
                     requestRow.setStatus(OptimizeScheduleStatus.PENDING_OPTIMIZER.toString());
                     initiateOptimizer(requestRow);
-                    break;
+                    return;
                 case FAILED:
                     requestRow.setRequestStart(System.currentTimeMillis());
                     requestRow.setRequestEnd(System.currentTimeMillis());
                     requestRow.setStatus(OptimizeScheduleStatus.FAILED.toString());
                     requestRow.setMessage(apiResponse.getErrorMessage());
-                    break;
+                    return;
                 case IN_PROGRESS:
                     requestRow.setRequestStart(System.currentTimeMillis());
-                    requestRow.setStatus(OptimizeScheduleStatus.PENDING_TICKETS.toString());
-                    break;
+                    requestRow.setStatus(OptimizeScheduleStatus.TICKETS_IN_PROGRESS.toString());
+                    return;
                 default:
                     break;
             }
@@ -212,9 +231,54 @@ public class OptimizerManager {
     }
 
 
-    private void initiateOptimizer(Request requestRow) {
-        // TODO Auto-generated method stub
+    private void initiateOptimizer(Request requestRow) throws CmsoException {
+        OptimizerEngineResponse apiResponse = optimizerRequestManager.createOptimizerRequest(requestRow);
+        if (apiResponse != null) {
+            switch (apiResponse.getStatus()) {
+                case COMPLETED:
+                    requestRow.setRequestEnd(System.currentTimeMillis());
+                    requestRow.setStatus(OptimizeScheduleStatus.COMPLETED.toString());
+                    return;
+                case FAILED:
+                    requestRow.setRequestStart(System.currentTimeMillis());
+                    requestRow.setRequestEnd(System.currentTimeMillis());
+                    requestRow.setStatus(OptimizeScheduleStatus.FAILED.toString());
+                    requestRow.setMessage(apiResponse.getErrorMessage());
+                    return;
+                case IN_PROGRESS:
+                case IN_QUEUE:
+                    requestRow.setRequestStart(System.currentTimeMillis());
+                    requestRow.setStatus(OptimizeScheduleStatus.OPTIMIZER_IN_PROGRESS.toString());
+                    return;
+                default:
+                    break;
+            }
+        }
+        throw new CmsoException(Status.INTERNAL_SERVER_ERROR, LogMessages.FAILED_TO_CREATE_TICKET_REQUEST,
+                        requestRow.getUuid().toString());
+    }
 
+    public OptimizerResponse getCompletedOptimizerResponse(UUID uuid)
+    {
+        OptimizerResponse response = new OptimizerResponse();
+        response.setRequestId(uuid.toString());
+        response.setStatus(OptimizeScheduleStatus.COMPLETED);
+        Response responseRow  = getResponseRow(uuid);
+        if (responseRow != null)
+        {
+            response.setSchedules(optimizerRequestManager.getScheduleInfo(responseRow));
+        }
+        return response;
+    }
+
+    public Response getResponseRow(UUID uuid)
+    {
+        Optional<Response> opt = responseDao.findById(uuid);
+        if (opt.isPresent())
+        {
+            return opt.get();
+        }
+        return null;
     }
 
     @SuppressWarnings("unused")
@@ -225,7 +289,7 @@ public class OptimizerManager {
             return requestOptional.get();
         }
         throw new CmsoException(Status.INTERNAL_SERVER_ERROR, LogMessages.EXPECTED_DATA_NOT_FOUND,
-                        requestRow.toString(), "Request table");
+                       uuid.toString(), "Request table");
     }
 
 
diff --git a/cmso-optimizer/src/main/java/org/onap/optf/cmso/optimizer/service/rs/models/ChangeWindow.java b/cmso-optimizer/src/main/java/org/onap/optf/cmso/optimizer/service/rs/models/ChangeWindow.java
index fe2c10c..4eb6824 100644
--- a/cmso-optimizer/src/main/java/org/onap/optf/cmso/optimizer/service/rs/models/ChangeWindow.java
+++ b/cmso-optimizer/src/main/java/org/onap/optf/cmso/optimizer/service/rs/models/ChangeWindow.java
@@ -33,6 +33,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import java.io.Serializable;
+import java.time.Instant;
 import java.util.Date;
 import org.springframework.format.annotation.DateTimeFormat;
 
@@ -117,8 +118,26 @@ public class ChangeWindow implements Serializable {
      * @return true, if this change window contains the passed change window
      */
     public boolean contains(ChangeWindow test) {
-        if (!test.getStartTime().before(getStartTime()) &&
-            !test.getEndTime().after(getEndTime())) {
+        if (!test.getStartTime().before(getStartTime()) && !test.getEndTime().after(getEndTime())) {
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * Passed slot time (test) is within this change window adjusted for the time zone of the element.
+     * This is used to interpret global relative availability (maintenance) windows as opposed to
+     * absolute UTC times provided in tickets which should already be adjusted for time zone.
+     *
+     * @param test the test
+     * @param timeZoneOffset the time zone offset
+     * @return true, if successful
+     */
+    public boolean containsInTimeZone(ChangeWindow test, Long timeZoneOffset) {
+        Instant startInstant = startTime.toInstant().plusMillis(timeZoneOffset);
+        Instant endInstant = endTime.toInstant().plusMillis(timeZoneOffset);
+        if (!test.getStartTime().toInstant().isBefore(startInstant)
+                        && !test.getEndTime().toInstant().isAfter(endInstant)) {
             return true;
         }
         return false;
@@ -159,4 +178,5 @@ public class ChangeWindow implements Serializable {
         return "";
     }
 
+
 }
diff --git a/cmso-optimizer/src/main/java/org/onap/optf/cmso/optimizer/service/rs/models/OptimizerResponse.java b/cmso-optimizer/src/main/java/org/onap/optf/cmso/optimizer/service/rs/models/OptimizerResponse.java
index 2f8e705..b8b5a21 100644
--- a/cmso-optimizer/src/main/java/org/onap/optf/cmso/optimizer/service/rs/models/OptimizerResponse.java
+++ b/cmso-optimizer/src/main/java/org/onap/optf/cmso/optimizer/service/rs/models/OptimizerResponse.java
@@ -42,7 +42,16 @@ public class OptimizerResponse implements Serializable {
     private static EELFLogger log = EELFManager.getInstance().getLogger(OptimizerResponse.class);
 
     public enum OptimizeScheduleStatus {
-        CREATED, PENDING_TOPOLOGY, PENDING_TICKETS, PENDING_OPTIMIZER, COMPLETED, FAILED, DELETED,
+        CREATED,
+        PENDING_TOPOLOGY,
+        TOPOLOGY_IN_PROGRESS,
+        PENDING_TICKETS,
+        TICKETS_IN_PROGRESS,
+        PENDING_OPTIMIZER,
+        OPTIMIZER_IN_PROGRESS,
+        COMPLETED,
+        FAILED,
+        DELETED,
     }
 
     @ApiModelProperty(value = "Unique Id of the request")
-- 
cgit 1.2.3-korg