a6941add48b23b93a5326573cbf3926c7c0f6f82
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / sync / IndexIntegrityValidator.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.sync;
24
25 import javax.ws.rs.core.MediaType;
26
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;
34
35 /**
36  * The Class IndexIntegrityValidator.
37  */
38 public class IndexIntegrityValidator implements IndexValidator {
39
40   private static final Logger LOG =
41       LoggerFactory.getInstance().getLogger(IndexIntegrityValidator.class);
42
43   private ElasticSearchEndpointConfig endpointConfig;
44   private ElasticSearchSchemaConfig schemaConfig;
45   private String tableConfigJson;
46
47   private final ElasticSearchAdapter esAdapter;
48
49   /**
50    * Instantiates a new index integrity validator.
51    *
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
58    */
59   public IndexIntegrityValidator(ElasticSearchAdapter esAdapter,
60       ElasticSearchSchemaConfig esSchemaConfig, ElasticSearchEndpointConfig esEndpointConfig,
61       String tableConfigJson) {
62
63     this.esAdapter = esAdapter;
64     this.schemaConfig = esSchemaConfig;
65     this.endpointConfig = esEndpointConfig;
66     this.tableConfigJson = tableConfigJson;
67   }
68
69   public ElasticSearchEndpointConfig getEndpointConfig() {
70     return endpointConfig;
71   }
72
73   public void setEndpointConfig(ElasticSearchEndpointConfig endpointConfig) {
74     this.endpointConfig = endpointConfig;
75   }
76
77   public ElasticSearchSchemaConfig getSchemaConfig() {
78     return schemaConfig;
79   }
80
81   public void setSchemaConfig(ElasticSearchSchemaConfig schemaConfig) {
82     this.schemaConfig = schemaConfig;
83   }
84
85   public ElasticSearchAdapter getEsAdapter() {
86     return esAdapter;
87   }
88
89   @Override
90   public String getIndexName() {
91     return schemaConfig.getIndexName();
92   }
93
94
95   /*
96    * (non-Javadoc)
97    * 
98    * @see org.openecomp.sparky.synchronizer.IndexValidator#exists()
99    */
100   @Override
101   public boolean exists() {
102     final String fullUrlStr = getFullUrl("/" + schemaConfig.getIndexName() + "/");
103     OperationResult existsResult = esAdapter.doHead(fullUrlStr, MediaType.APPLICATION_JSON_TYPE);
104
105     int rc = existsResult.getResultCode();
106
107     if (rc >= 200 && rc < 300) {
108       LOG.info(AaiUiMsgs.INDEX_EXISTS, schemaConfig.getIndexName());
109       return true;
110     } else {
111       LOG.info(AaiUiMsgs.INDEX_NOT_EXIST, schemaConfig.getIndexName());
112       return false;
113     }
114   }
115
116   /*
117    * (non-Javadoc)
118    * 
119    * @see org.openecomp.sparky.synchronizer.IndexValidator#integrityValid()
120    */
121   @Override
122   public boolean integrityValid() {
123     return true;
124   }
125
126   /*
127    * (non-Javadoc)
128    * 
129    * @see org.openecomp.sparky.synchronizer.IndexValidator#createOrRepair()
130    */
131   @Override
132   public void createOrRepair() {
133
134     String message =
135         "IndexIntegrityValidator.createOrRepair() for indexName = " + schemaConfig.getIndexName();
136     LOG.info(AaiUiMsgs.INFO_GENERIC, message);
137
138     final String fullUrlStr = getFullUrl("/" + schemaConfig.getIndexName() + "/");
139     OperationResult createResult =
140         esAdapter.doPut(fullUrlStr, tableConfigJson, MediaType.APPLICATION_JSON_TYPE);
141
142     int rc = createResult.getResultCode();
143
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());
148     } else {
149       LOG.warn(AaiUiMsgs.INDEX_INTEGRITY_CHECK_FAILED, schemaConfig.getIndexName(),
150           createResult.getResult());
151     }
152
153   }
154
155   /*
156    * (non-Javadoc)
157    * 
158    * @see org.openecomp.sparky.synchronizer.IndexValidator#destroyIndex()
159    */
160   @Override
161   public void destroyIndex() {
162     // we don't do this for now
163   }
164
165   /**
166    * Gets the full url.
167    *
168    * @param resourceUrl the resource url
169    * @return the full url
170    */
171   private String getFullUrl(String resourceUrl) {
172     return String.format("http://%s:%s%s", endpointConfig.getEsIpAddress(),
173         endpointConfig.getEsServerPort(), resourceUrl);
174   }
175
176 }