update sync queries to use searh data service
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / sync / IndexIntegrityValidator.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sparky.sync;
22
23 import javax.ws.rs.core.MediaType;
24
25 import org.onap.aai.cl.api.Logger;
26 import org.onap.aai.cl.eelf.LoggerFactory;
27 import org.onap.aai.restclient.client.OperationResult;
28 import org.onap.aai.sparky.search.SearchServiceAdapter;
29 import org.onap.aai.sparky.logging.AaiUiMsgs;
30 import org.onap.aai.sparky.dal.rest.config.RestEndpointConfig;
31 import org.onap.aai.sparky.sync.config.ElasticSearchSchemaConfig;
32
33 /**
34  * The Class IndexIntegrityValidator.
35  */
36 public class IndexIntegrityValidator implements IndexValidator {
37
38   private static final Logger LOG =
39       LoggerFactory.getInstance().getLogger(IndexIntegrityValidator.class);
40
41   private RestEndpointConfig endpointConfig;
42   private ElasticSearchSchemaConfig schemaConfig;
43   private String tableConfigJson;
44
45   private final SearchServiceAdapter searchServiceAdapter;
46
47   /**
48    * Instantiates a new index integrity validator.
49    *
50    * @param restDataProvider the rest data provider
51    * @param indexName the index name
52    * @param indexType the index type
53    * @param host the host
54    * @param port the port
55    * @param tableConfigJson the table config json
56    */
57   public IndexIntegrityValidator(SearchServiceAdapter searchServiceAdapter,
58       ElasticSearchSchemaConfig esSchemaConfig, RestEndpointConfig esEndpointConfig,
59       String tableConfigJson) {
60
61     this.searchServiceAdapter = searchServiceAdapter;
62     this.schemaConfig = esSchemaConfig;
63     this.endpointConfig = esEndpointConfig;
64     this.tableConfigJson = tableConfigJson;
65   }
66
67   public RestEndpointConfig getEndpointConfig() {
68     return endpointConfig;
69   }
70
71   public void setEndpointConfig(RestEndpointConfig endpointConfig) {
72     this.endpointConfig = endpointConfig;
73   }
74
75   public ElasticSearchSchemaConfig getSchemaConfig() {
76     return schemaConfig;
77   }
78
79   public void setSchemaConfig(ElasticSearchSchemaConfig schemaConfig) {
80     this.schemaConfig = schemaConfig;
81   }
82
83   public SearchServiceAdapter getSearchServiceAdapter() {
84     return searchServiceAdapter;
85   }
86
87   @Override
88   public String getIndexName() {
89     return schemaConfig.getIndexName();
90   }
91
92
93   /*
94    * (non-Javadoc)
95    * 
96    * @see org.openecomp.sparky.synchronizer.IndexValidator#exists()
97    */
98   /* TODO       
99    * currently Search does not support head operations on an index neither does it support  get operations
100    * on an index. get is  being used so that it does not break any code.
101    * */
102   @Override
103   public boolean exists() {
104     final String fullUrlStr = getFullUrl(schemaConfig.getIndexName() + "/");
105     OperationResult existsResult = searchServiceAdapter.doGet(fullUrlStr, "application/json");
106     
107     int rc = existsResult.getResultCode();
108
109     if (rc >= 200 && rc < 300) {
110       LOG.info(AaiUiMsgs.INDEX_EXISTS, schemaConfig.getIndexName());
111       return true;
112     } else {
113       LOG.info(AaiUiMsgs.INDEX_NOT_EXIST, schemaConfig.getIndexName());
114       return false;
115     }
116   }
117
118   /*
119    * (non-Javadoc)
120    * 
121    * @see org.openecomp.sparky.synchronizer.IndexValidator#integrityValid()
122    */
123   @Override
124   public boolean integrityValid() {
125     return true;
126   }
127
128   /*
129    * (non-Javadoc)
130    * 
131    * @see org.openecomp.sparky.synchronizer.IndexValidator#createOrRepair()
132    */
133   @Override
134   public void createOrRepair() {
135     
136     String message =
137         "IndexIntegrityValidator.createOrRepair() for indexName = " + schemaConfig.getIndexName();
138     LOG.info(AaiUiMsgs.INFO_GENERIC, message);
139
140     final String fullUrlStr = getFullUrl("/" + schemaConfig.getIndexName() + "/");
141     OperationResult createResult =
142                 searchServiceAdapter.doPut(fullUrlStr, tableConfigJson,"application/json");
143
144     int rc = createResult.getResultCode();
145
146     if (rc >= 200 && rc < 300) {
147       LOG.info(AaiUiMsgs.INDEX_RECREATED, schemaConfig.getIndexName());
148     } else if (rc == 400) {
149       LOG.info(AaiUiMsgs.INDEX_ALREADY_EXISTS, schemaConfig.getIndexName());
150     } else {
151       LOG.warn(AaiUiMsgs.INDEX_INTEGRITY_CHECK_FAILED, schemaConfig.getIndexName(),
152           createResult.getResult());
153     }
154
155   }
156
157   /*
158    * (non-Javadoc)
159    * 
160    * @see org.openecomp.sparky.synchronizer.IndexValidator#destroyIndex()
161    */
162   @Override
163   public void destroyIndex() {
164     // we don't do this for now
165   }
166
167   /**
168    * Gets the full url.
169    *
170    * @param resourceUrl the resource url
171    * @return the full url
172    */
173   private String getFullUrl(String resourceUrl) {
174           String createIndexUrl = searchServiceAdapter.buildSearchServiceCreateIndexUrl(resourceUrl);
175             return createIndexUrl;
176   }
177
178 }