Adding interfaces in documentation
[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-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.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;
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 ElasticSearchEndpointConfig endpointConfig;
42   private ElasticSearchSchemaConfig schemaConfig;
43   private String tableConfigJson;
44
45   private final ElasticSearchAdapter esAdapter;
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(ElasticSearchAdapter esAdapter,
58       ElasticSearchSchemaConfig esSchemaConfig, ElasticSearchEndpointConfig esEndpointConfig,
59       String tableConfigJson) {
60
61     this.esAdapter = esAdapter;
62     this.schemaConfig = esSchemaConfig;
63     this.endpointConfig = esEndpointConfig;
64     this.tableConfigJson = tableConfigJson;
65   }
66
67   public ElasticSearchEndpointConfig getEndpointConfig() {
68     return endpointConfig;
69   }
70
71   public void setEndpointConfig(ElasticSearchEndpointConfig 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 ElasticSearchAdapter getEsAdapter() {
84     return esAdapter;
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   @Override
99   public boolean exists() {
100     final String fullUrlStr = getFullUrl("/" + schemaConfig.getIndexName() + "/");
101     OperationResult existsResult = esAdapter.doHead(fullUrlStr, MediaType.APPLICATION_JSON_TYPE);
102     
103     int rc = existsResult.getResultCode();
104
105     if (rc >= 200 && rc < 300) {
106       LOG.info(AaiUiMsgs.INDEX_EXISTS, schemaConfig.getIndexName());
107       return true;
108     } else {
109       LOG.info(AaiUiMsgs.INDEX_NOT_EXIST, schemaConfig.getIndexName());
110       return false;
111     }
112   }
113
114   /*
115    * (non-Javadoc)
116    * 
117    * @see org.openecomp.sparky.synchronizer.IndexValidator#integrityValid()
118    */
119   @Override
120   public boolean integrityValid() {
121     return true;
122   }
123
124   /*
125    * (non-Javadoc)
126    * 
127    * @see org.openecomp.sparky.synchronizer.IndexValidator#createOrRepair()
128    */
129   @Override
130   public void createOrRepair() {
131     
132     String message =
133         "IndexIntegrityValidator.createOrRepair() for indexName = " + schemaConfig.getIndexName();
134     LOG.info(AaiUiMsgs.INFO_GENERIC, message);
135
136     final String fullUrlStr = getFullUrl("/" + schemaConfig.getIndexName() + "/");
137     OperationResult createResult =
138         esAdapter.doPut(fullUrlStr, tableConfigJson, MediaType.APPLICATION_JSON_TYPE);
139
140     int rc = createResult.getResultCode();
141
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());
146     } else {
147       LOG.warn(AaiUiMsgs.INDEX_INTEGRITY_CHECK_FAILED, schemaConfig.getIndexName(),
148           createResult.getResult());
149     }
150
151   }
152
153   /*
154    * (non-Javadoc)
155    * 
156    * @see org.openecomp.sparky.synchronizer.IndexValidator#destroyIndex()
157    */
158   @Override
159   public void destroyIndex() {
160     // we don't do this for now
161   }
162
163   /**
164    * Gets the full url.
165    *
166    * @param resourceUrl the resource url
167    * @return the full url
168    */
169   private String getFullUrl(String resourceUrl) {
170     return String.format("http://%s:%s%s", endpointConfig.getEsIpAddress(),
171         endpointConfig.getEsServerPort(), resourceUrl);
172   }
173
174 }