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.dal.ElasticSearchAdapter;
29 import org.onap.aai.sparky.logging.AaiUiMsgs;
30 import org.onap.aai.sparky.sync.config.ElasticSearchEndpointConfig;
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 ElasticSearchEndpointConfig endpointConfig;
42 private ElasticSearchSchemaConfig schemaConfig;
43 private String tableConfigJson;
45 private final ElasticSearchAdapter esAdapter;
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(ElasticSearchAdapter esAdapter,
58 ElasticSearchSchemaConfig esSchemaConfig, ElasticSearchEndpointConfig esEndpointConfig,
59 String tableConfigJson) {
61 this.esAdapter = esAdapter;
62 this.schemaConfig = esSchemaConfig;
63 this.endpointConfig = esEndpointConfig;
64 this.tableConfigJson = tableConfigJson;
67 public ElasticSearchEndpointConfig getEndpointConfig() {
68 return endpointConfig;
71 public void setEndpointConfig(ElasticSearchEndpointConfig endpointConfig) {
72 this.endpointConfig = endpointConfig;
75 public ElasticSearchSchemaConfig getSchemaConfig() {
79 public void setSchemaConfig(ElasticSearchSchemaConfig schemaConfig) {
80 this.schemaConfig = schemaConfig;
83 public ElasticSearchAdapter getEsAdapter() {
88 public String getIndexName() {
89 return schemaConfig.getIndexName();
96 * @see org.openecomp.sparky.synchronizer.IndexValidator#exists()
99 public boolean exists() {
100 final String fullUrlStr = getFullUrl("/" + schemaConfig.getIndexName() + "/");
101 OperationResult existsResult = esAdapter.doHead(fullUrlStr, MediaType.APPLICATION_JSON_TYPE);
103 int rc = existsResult.getResultCode();
105 if (rc >= 200 && rc < 300) {
106 LOG.info(AaiUiMsgs.INDEX_EXISTS, schemaConfig.getIndexName());
109 LOG.info(AaiUiMsgs.INDEX_NOT_EXIST, schemaConfig.getIndexName());
117 * @see org.openecomp.sparky.synchronizer.IndexValidator#integrityValid()
120 public boolean integrityValid() {
127 * @see org.openecomp.sparky.synchronizer.IndexValidator#createOrRepair()
130 public void createOrRepair() {
133 "IndexIntegrityValidator.createOrRepair() for indexName = " + schemaConfig.getIndexName();
134 LOG.info(AaiUiMsgs.INFO_GENERIC, message);
136 final String fullUrlStr = getFullUrl("/" + schemaConfig.getIndexName() + "/");
137 OperationResult createResult =
138 esAdapter.doPut(fullUrlStr, tableConfigJson, MediaType.APPLICATION_JSON_TYPE);
140 int rc = createResult.getResultCode();
142 if (rc >= 200 && rc < 300) {
143 LOG.info(AaiUiMsgs.INDEX_RECREATED, schemaConfig.getIndexName());
144 } else if (rc == 400) {
145 LOG.info(AaiUiMsgs.INDEX_ALREADY_EXISTS, schemaConfig.getIndexName());
147 LOG.warn(AaiUiMsgs.INDEX_INTEGRITY_CHECK_FAILED, schemaConfig.getIndexName(),
148 createResult.getResult());
156 * @see org.openecomp.sparky.synchronizer.IndexValidator#destroyIndex()
159 public void destroyIndex() {
160 // we don't do this for now
166 * @param resourceUrl the resource url
167 * @return the full url
169 private String getFullUrl(String resourceUrl) {
170 return String.format("http://%s:%s%s", endpointConfig.getEsIpAddress(),
171 endpointConfig.getEsServerPort(), resourceUrl);