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