2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sparky.sync;
23 import javax.ws.rs.core.MediaType;
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;
34 * The Class IndexIntegrityValidator.
36 public class IndexIntegrityValidator implements IndexValidator {
38 private static final Logger LOG =
39 LoggerFactory.getInstance().getLogger(IndexIntegrityValidator.class);
41 private RestEndpointConfig endpointConfig;
42 private ElasticSearchSchemaConfig schemaConfig;
43 private String tableConfigJson;
45 private final SearchServiceAdapter searchServiceAdapter;
48 * Instantiates a new index integrity validator.
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
57 public IndexIntegrityValidator(SearchServiceAdapter searchServiceAdapter,
58 ElasticSearchSchemaConfig esSchemaConfig, RestEndpointConfig esEndpointConfig,
59 String tableConfigJson) {
61 this.searchServiceAdapter = searchServiceAdapter;
62 this.schemaConfig = esSchemaConfig;
63 this.endpointConfig = esEndpointConfig;
64 this.tableConfigJson = tableConfigJson;
67 public RestEndpointConfig getEndpointConfig() {
68 return endpointConfig;
71 public void setEndpointConfig(RestEndpointConfig endpointConfig) {
72 this.endpointConfig = endpointConfig;
75 public ElasticSearchSchemaConfig getSchemaConfig() {
79 public void setSchemaConfig(ElasticSearchSchemaConfig schemaConfig) {
80 this.schemaConfig = schemaConfig;
83 public SearchServiceAdapter getSearchServiceAdapter() {
84 return searchServiceAdapter;
88 public String getIndexName() {
89 return schemaConfig.getIndexName();
96 * @see org.openecomp.sparky.synchronizer.IndexValidator#exists()
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.
103 public boolean exists() {
104 final String fullUrlStr = getFullUrl(schemaConfig.getIndexName() + "/");
105 OperationResult existsResult = searchServiceAdapter.doGet(fullUrlStr, "application/json");
107 int rc = existsResult.getResultCode();
109 if (rc >= 200 && rc < 300) {
110 LOG.info(AaiUiMsgs.INDEX_EXISTS, schemaConfig.getIndexName());
113 LOG.info(AaiUiMsgs.INDEX_NOT_EXIST, schemaConfig.getIndexName());
121 * @see org.openecomp.sparky.synchronizer.IndexValidator#integrityValid()
124 public boolean integrityValid() {
131 * @see org.openecomp.sparky.synchronizer.IndexValidator#createOrRepair()
134 public void createOrRepair() {
137 "IndexIntegrityValidator.createOrRepair() for indexName = " + schemaConfig.getIndexName();
138 LOG.info(AaiUiMsgs.INFO_GENERIC, message);
140 final String fullUrlStr = getFullUrl("/" + schemaConfig.getIndexName() + "/");
141 OperationResult createResult =
142 searchServiceAdapter.doPut(fullUrlStr, tableConfigJson,"application/json");
144 int rc = createResult.getResultCode();
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());
151 LOG.warn(AaiUiMsgs.INDEX_INTEGRITY_CHECK_FAILED, schemaConfig.getIndexName(),
152 createResult.getResult());
160 * @see org.openecomp.sparky.synchronizer.IndexValidator#destroyIndex()
163 public void destroyIndex() {
164 // we don't do this for now
170 * @param resourceUrl the resource url
171 * @return the full url
173 private String getFullUrl(String resourceUrl) {
174 String createIndexUrl = searchServiceAdapter.buildSearchServiceCreateIndexUrl(resourceUrl);
175 return createIndexUrl;