Update the dependencies to use project version
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / synchronizer / 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.synchronizer;
24
25 import org.onap.aai.sparky.dal.rest.OperationResult;
26 import org.onap.aai.sparky.dal.rest.RestDataProvider;
27 import org.onap.aai.sparky.logging.AaiUiMsgs;
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.cl.eelf.LoggerFactory;
30
31 /**
32  * The Class IndexIntegrityValidator.
33  *
34  * @author davea.
35  */
36 public class IndexIntegrityValidator implements IndexValidator {
37
38   private static final Logger LOG =
39       LoggerFactory.getInstance().getLogger(IndexIntegrityValidator.class);
40
41   private String host;
42
43   /**
44    * @return the host
45    */
46   public String getHost() {
47     return host;
48   }
49
50   /**
51    * @param host the host to set
52    */
53   public void setHost(String host) {
54     this.host = host;
55   }
56
57   /**
58    * @return the port
59    */
60   public String getPort() {
61     return port;
62   }
63
64   /**
65    * @param port the port to set
66    */
67   public void setPort(String port) {
68     this.port = port;
69   }
70
71   /**
72    * @return the tableConfigJson
73    */
74   public String getTableConfigJson() {
75     return tableConfigJson;
76   }
77
78   /**
79    * @param tableConfigJson the tableConfigJson to set
80    */
81   public void setTableConfigJson(String tableConfigJson) {
82     this.tableConfigJson = tableConfigJson;
83   }
84
85   /**
86    * @return the log
87    */
88   public static Logger getLog() {
89     return LOG;
90   }
91
92   /**
93    * @return the restDataProvider
94    */
95   public RestDataProvider getRestDataProvider() {
96     return restDataProvider;
97   }
98
99   private String port;
100   private String indexName;
101   private String indexType;
102   private String tableConfigJson;
103
104   private final RestDataProvider restDataProvider;
105
106   /**
107    * Instantiates a new index integrity validator.
108    *
109    * @param restDataProvider the rest data provider
110    * @param indexName the index name
111    * @param indexType the index type
112    * @param host the host
113    * @param port the port
114    * @param tableConfigJson the table config json
115    */
116   public IndexIntegrityValidator(RestDataProvider restDataProvider, String indexName,
117       String indexType, String host, String port, String tableConfigJson) {
118     this.restDataProvider = restDataProvider;
119     this.host = host;
120     this.port = port;
121     this.indexName = indexName;
122     this.indexType = indexType;
123     this.tableConfigJson = tableConfigJson;
124   }
125
126   @Override
127   public String getIndexName() {
128     return indexName;
129   }
130
131   public void setIndexName(String indexName) {
132     this.indexName = indexName;
133   }
134
135   public String getIndexType() {
136     return indexType;
137   }
138
139   public void setIndexType(String indexType) {
140     this.indexType = indexType;
141   }
142
143   /*
144    * (non-Javadoc)
145    * 
146    * @see org.onap.aai.sparky.synchronizer.IndexValidator#exists()
147    */
148   @Override
149   public boolean exists() {
150     final String fullUrlStr = getFullUrl("/" + indexName + "/");
151     OperationResult existsResult = restDataProvider.doHead(fullUrlStr, "application/json");
152
153     int rc = existsResult.getResultCode();
154
155     if (rc >= 200 && rc < 300) {
156       LOG.info(AaiUiMsgs.INDEX_EXISTS, indexName);
157       return true;
158     } else {
159       LOG.info(AaiUiMsgs.INDEX_NOT_EXIST, indexName);
160       return false;
161     }
162   }
163
164   /*
165    * (non-Javadoc)
166    * 
167    * @see org.onap.aai.sparky.synchronizer.IndexValidator#integrityValid()
168    */
169   @Override
170   public boolean integrityValid() {
171     // TODO Auto-generated method stub
172     // logger.info(";
173     // System.out.println("IndexIntegrityValidator.integrityValid() for
174     // indexName = " + indexName);
175     return true;
176   }
177
178   /*
179    * (non-Javadoc)
180    * 
181    * @see org.onap.aai.sparky.synchronizer.IndexValidator#createOrRepair()
182    */
183   @Override
184   public void createOrRepair() {
185     // TODO Auto-generated method stub
186     String message = "IndexIntegrityValidator.createOrRepair() for indexName = " + indexName;
187     LOG.info(AaiUiMsgs.INFO_GENERIC, message);
188
189     final String fullUrlStr = getFullUrl("/" + indexName + "/");
190     OperationResult createResult =
191         restDataProvider.doPut(fullUrlStr, tableConfigJson, "application/json");
192
193     int rc = createResult.getResultCode();
194
195     if (rc >= 200 && rc < 300) {
196       LOG.info(AaiUiMsgs.INDEX_RECREATED, indexName);
197     } else if (rc == 400) {
198       LOG.info(AaiUiMsgs.INDEX_ALREADY_EXISTS, indexName);
199     } else {
200       LOG.warn(AaiUiMsgs.INDEX_INTEGRITY_CHECK_FAILED, indexName, createResult.getResult());
201     }
202
203   }
204
205   /*
206    * (non-Javadoc)
207    * 
208    * @see org.onap.aai.sparky.synchronizer.IndexValidator#destroyIndex()
209    */
210   @Override
211   public void destroyIndex() {
212     // TODO Auto-generated method stub
213     // we don't do this for now
214
215   }
216
217   /**
218    * Gets the full url.
219    *
220    * @param resourceUrl the resource url
221    * @return the full url
222    */
223   private String getFullUrl(String resourceUrl) {
224     return String.format("http://%s:%s%s", host, port, resourceUrl);
225   }
226
227 }