2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.sync;
25 import javax.ws.rs.core.MediaType;
27 import org.onap.aai.cl.api.Logger;
28 import org.onap.aai.cl.eelf.LoggerFactory;
29 import org.onap.aai.restclient.client.OperationResult;
30 import org.onap.aai.sparky.dal.ElasticSearchAdapter;
31 import org.onap.aai.sparky.logging.AaiUiMsgs;
32 import org.onap.aai.sparky.sync.config.ElasticSearchEndpointConfig;
33 import org.onap.aai.sparky.sync.config.ElasticSearchSchemaConfig;
36 * The Class IndexIntegrityValidator.
38 public class IndexIntegrityValidator implements IndexValidator {
40 private static final Logger LOG =
41 LoggerFactory.getInstance().getLogger(IndexIntegrityValidator.class);
43 private ElasticSearchEndpointConfig endpointConfig;
44 private ElasticSearchSchemaConfig schemaConfig;
45 private String tableConfigJson;
47 private final ElasticSearchAdapter esAdapter;
50 * Instantiates a new index integrity validator.
52 * @param restDataProvider the rest data provider
53 * @param indexName the index name
54 * @param indexType the index type
55 * @param host the host
56 * @param port the port
57 * @param tableConfigJson the table config json
59 public IndexIntegrityValidator(ElasticSearchAdapter esAdapter,
60 ElasticSearchSchemaConfig esSchemaConfig, ElasticSearchEndpointConfig esEndpointConfig,
61 String tableConfigJson) {
63 this.esAdapter = esAdapter;
64 this.schemaConfig = esSchemaConfig;
65 this.endpointConfig = esEndpointConfig;
66 this.tableConfigJson = tableConfigJson;
69 public ElasticSearchEndpointConfig getEndpointConfig() {
70 return endpointConfig;
73 public void setEndpointConfig(ElasticSearchEndpointConfig endpointConfig) {
74 this.endpointConfig = endpointConfig;
77 public ElasticSearchSchemaConfig getSchemaConfig() {
81 public void setSchemaConfig(ElasticSearchSchemaConfig schemaConfig) {
82 this.schemaConfig = schemaConfig;
85 public ElasticSearchAdapter getEsAdapter() {
90 public String getIndexName() {
91 return schemaConfig.getIndexName();
98 * @see org.openecomp.sparky.synchronizer.IndexValidator#exists()
101 public boolean exists() {
102 final String fullUrlStr = getFullUrl("/" + schemaConfig.getIndexName() + "/");
103 OperationResult existsResult = esAdapter.doHead(fullUrlStr, MediaType.APPLICATION_JSON_TYPE);
105 int rc = existsResult.getResultCode();
107 if (rc >= 200 && rc < 300) {
108 LOG.info(AaiUiMsgs.INDEX_EXISTS, schemaConfig.getIndexName());
111 LOG.info(AaiUiMsgs.INDEX_NOT_EXIST, schemaConfig.getIndexName());
119 * @see org.openecomp.sparky.synchronizer.IndexValidator#integrityValid()
122 public boolean integrityValid() {
129 * @see org.openecomp.sparky.synchronizer.IndexValidator#createOrRepair()
132 public void createOrRepair() {
135 "IndexIntegrityValidator.createOrRepair() for indexName = " + schemaConfig.getIndexName();
136 LOG.info(AaiUiMsgs.INFO_GENERIC, message);
138 final String fullUrlStr = getFullUrl("/" + schemaConfig.getIndexName() + "/");
139 OperationResult createResult =
140 esAdapter.doPut(fullUrlStr, tableConfigJson, MediaType.APPLICATION_JSON_TYPE);
142 int rc = createResult.getResultCode();
144 if (rc >= 200 && rc < 300) {
145 LOG.info(AaiUiMsgs.INDEX_RECREATED, schemaConfig.getIndexName());
146 } else if (rc == 400) {
147 LOG.info(AaiUiMsgs.INDEX_ALREADY_EXISTS, schemaConfig.getIndexName());
149 LOG.warn(AaiUiMsgs.INDEX_INTEGRITY_CHECK_FAILED, schemaConfig.getIndexName(),
150 createResult.getResult());
158 * @see org.openecomp.sparky.synchronizer.IndexValidator#destroyIndex()
161 public void destroyIndex() {
162 // we don't do this for now
168 * @param resourceUrl the resource url
169 * @return the full url
171 private String getFullUrl(String resourceUrl) {
172 return String.format("http://%s:%s%s", endpointConfig.getEsIpAddress(),
173 endpointConfig.getEsServerPort(), resourceUrl);