package com.oneops.search.msg.processor;

import com.oneops.cms.dj.domain.CmsRelease;
import com.oneops.search.msg.index.Indexer;
import org.apache.http.client.fluent.Request;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/*******************************************************************************
 *
 *   Copyright 2016 Walmart, Inc.
 *
 *   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.
 *
 *******************************************************************************/

@Service
public class ReleaseMessageProcessor implements MessageProcessor {
    private static Logger logger = Logger.getLogger(ReleaseMessageProcessor.class);
    @Autowired
    private Indexer indexer;
    @Value("${snapshotURL}")
    private String snapshotURL;
    private static final String RELEASE = "release";

    @Override
    public void processMessage(String message, String msgType, String msgId) {
        CmsRelease release = GSON.fromJson(message, CmsRelease.class);
        String releaseMsg = GSON_ES.toJson(release);
        indexer.indexEvent(RELEASE, releaseMsg);
        indexer.index(String.valueOf(release.getReleaseId()), RELEASE, releaseMsg);
        try {
            String url = snapshotURL + release.getNsPath();
            logger.info("Retrieving snapshot for:" + url);
            message = Request.Get(url).execute().returnContent().asString();
            indexer.index(String.valueOf(release.getReleaseId()), "snapshot", message);
            logger.info("Snapshot indexed");
        } catch (Exception e) {
            logger.error("Error while retrieving snapshot" + e.getMessage(), e);
        }
    }
}

@@ -50,39 +50,215 @@
 com.
-oneops.cms.dj.domain.CmsRelease
+google.gson.Gson;%0Aimport com.google.gson.JsonElement;%0Aimport com.google.gson.JsonObject;%0Aimport com.google.gson.JsonParser;%0Aimport com.oneops.cms.dj.domain.CmsRelease;%0Aimport com.oneops.cms.util.CmsConstants
 ;%0Aim
@@ -2010,16 +2010,92 @@
   try %7B%0A
+            if (%22closed%22.equalsIgnoreCase(release.getReleaseState())) %7B%0A    
         
@@ -2150,32 +2150,36 @@
 ();%0A            
+ 
+   
 logger.info(%22Ret
@@ -2206,19 +2206,70 @@
 :%22 + url
-);%0A
+ + %22 expected release:%22 + release.getReleaseId());%0A   
         
@@ -2272,16 +2272,17 @@
         
+ 
 message 
@@ -2300,16 +2300,62 @@
 et(url).
+addHeader(%22Content-Type%22, %22application/json%22).
 execute(
@@ -2380,24 +2380,378 @@
 asString();%0A
+                long releaseId = new JsonParser().parse(message).getAsJsonObject().get(%22release%22).getAsLong();%0A                if (releaseId %3E release.getReleaseId()) %7B%0A                    logger.warn(%22Snapshot is dirty, so discarding. Was expecting release:%22 + release.getReleaseId() + %22 snapshot is for:%22 + releaseId);%0A                %7D else %7B%0A        
             
@@ -2830,32 +2830,40 @@
 e);%0A            
+        
 logger.info(%22Sna
@@ -2875,19 +2875,174 @@
  indexed
-%22);
+ for release ID: %22 + releaseId);%0A                %7D%0A            %7D else %7B%0A                logger.info(%22Release is not closed. Won't do snapshot%22);%0A            %7D
 %0A       

fix for