2c1bb16f7b58674b55b6c392a2e78a258abf8873
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / sync / IndexIntegrityValidator.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.onap.aai.sparky.sync;
26
27 import javax.ws.rs.core.MediaType;
28
29 import org.onap.aai.cl.api.Logger;
30 import org.onap.aai.cl.eelf.LoggerFactory;
31 import org.onap.aai.restclient.client.OperationResult;
32 import org.onap.aai.sparky.dal.ElasticSearchAdapter;
33 import org.onap.aai.sparky.logging.AaiUiMsgs;
34 import org.onap.aai.sparky.sync.config.ElasticSearchEndpointConfig;
35 import org.onap.aai.sparky.sync.config.ElasticSearchSchemaConfig;
36
37 /**
38  * The Class IndexIntegrityValidator.
39  */
40 public class IndexIntegrityValidator implements IndexValidator {
41
42   private static final Logger LOG =
43       LoggerFactory.getInstance().getLogger(IndexIntegrityValidator.class);
44
45   private ElasticSearchEndpointConfig endpointConfig;
46   private ElasticSearchSchemaConfig schemaConfig;
47   private String tableConfigJson;
48
49   private final ElasticSearchAdapter esAdapter;
50
51   /**
52    * Instantiates a new index integrity validator.
53    *
54    * @param restDataProvider the rest data provider
55    * @param indexName the index name
56    * @param indexType the index type
57    * @param host the host
58    * @param port the port
59    * @param tableConfigJson the table config json
60    */
61   public IndexIntegrityValidator(ElasticSearchAdapter esAdapter,
62       ElasticSearchSchemaConfig esSchemaConfig, ElasticSearchEndpointConfig esEndpointConfig,
63       String tableConfigJson) {
64
65     this.esAdapter = esAdapter;
66     this.schemaConfig = esSchemaConfig;
67     this.endpointConfig = esEndpointConfig;
68     this.tableConfigJson = tableConfigJson;
69   }
70
71   public ElasticSearchEndpointConfig getEndpointConfig() {
72     return endpointConfig;
73   }
74
75   public void setEndpointConfig(ElasticSearchEndpointConfig endpointConfig) {
76     this.endpointConfig = endpointConfig;
77   }
78
79   public ElasticSearchSchemaConfig getSchemaConfig() {
80     return schemaConfig;
81   }
82
83   public void setSchemaConfig(ElasticSearchSchemaConfig schemaConfig) {
84     this.schemaConfig = schemaConfig;
85   }
86
87   public ElasticSearchAdapter getEsAdapter() {
88     return esAdapter;
89   }
90
91   @Override
92   public String getIndexName() {
93     return schemaConfig.getIndexName();
94   }
95
96
97   /*
98    * (non-Javadoc)
99    * 
100    * @see org.openecomp.sparky.synchronizer.IndexValidator#exists()
101    */
102   @Override
103   public boolean exists() {
104     final String fullUrlStr = getFullUrl("/" + schemaConfig.getIndexName() + "/");
105     OperationResult existsResult = esAdapter.doHead(fullUrlStr, MediaType.APPLICATION_JSON_TYPE);
106     
107     int rc = existsResult.getResultCode();
108
109     if (rc >= 200 && rc < 300) {
110       LOG.info(AaiUiMsgs.INDEX_EXISTS, schemaConfig.getIndexName());
111       return true;
112     } else {
113       LOG.info(AaiUiMsgs.INDEX_NOT_EXIST, schemaConfig.getIndexName());
114       return false;
115     }
116   }
117
118   /*
119    * (non-Javadoc)
120    * 
121    * @see org.openecomp.sparky.synchronizer.IndexValidator#integrityValid()
122    */
123   @Override
124   public boolean integrityValid() {
125     return true;
126   }
127
128   /*
129    * (non-Javadoc)
130    * 
131    * @see org.openecomp.sparky.synchronizer.IndexValidator#createOrRepair()
132    */
133   @Override
134   public void createOrRepair() {
135     
136     String message =
137         "IndexIntegrityValidator.createOrRepair() for indexName = " + schemaConfig.getIndexName();
138     LOG.info(AaiUiMsgs.INFO_GENERIC, message);
139
140     final String fullUrlStr = getFullUrl("/" + schemaConfig.getIndexName() + "/");
141     OperationResult createResult =
142         esAdapter.doPut(fullUrlStr, tableConfigJson, MediaType.APPLICATION_JSON_TYPE);
143
144     int rc = createResult.getResultCode();
145
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());
150     } else {
151       LOG.warn(AaiUiMsgs.INDEX_INTEGRITY_CHECK_FAILED, schemaConfig.getIndexName(),
152           createResult.getResult());
153     }
154
155   }
156
157   /*
158    * (non-Javadoc)
159    * 
160    * @see org.openecomp.sparky.synchronizer.IndexValidator#destroyIndex()
161    */
162   @Override
163   public void destroyIndex() {
164     // we don't do this for now
165   }
166
167   /**
168    * Gets the full url.
169    *
170    * @param resourceUrl the resource url
171    * @return the full url
172    */
173   private String getFullUrl(String resourceUrl) {
174     return String.format("http://%s:%s%s", endpointConfig.getEsIpAddress(),
175         endpointConfig.getEsServerPort(), resourceUrl);
176   }
177
178 }