Renaming openecomp to onap
[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   private String port;
43   private String indexName;
44   private String indexType;
45   private String tableConfigJson;
46
47   private final RestDataProvider restDataProvider;
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(RestDataProvider restDataProvider, String indexName,
60       String indexType, String host, String port, String tableConfigJson) {
61     this.restDataProvider = restDataProvider;
62     this.host = host;
63     this.port = port;
64     this.indexName = indexName;
65     this.indexType = indexType;
66     this.tableConfigJson = tableConfigJson;
67   }
68
69   @Override
70   public String getIndexName() {
71     return indexName;
72   }
73
74   public void setIndexName(String indexName) {
75     this.indexName = indexName;
76   }
77
78   public String getIndexType() {
79     return indexType;
80   }
81
82   public void setIndexType(String indexType) {
83     this.indexType = indexType;
84   }
85
86   /* (non-Javadoc)
87    * @see org.onap.aai.sparky.synchronizer.IndexValidator#exists()
88    */
89   @Override
90   public boolean exists() {
91     final String fullUrlStr = getFullUrl("/" + indexName + "/");
92     OperationResult existsResult = restDataProvider.doHead(fullUrlStr, "application/json");
93
94     int rc = existsResult.getResultCode();
95
96     if (rc >= 200 && rc < 300) {
97       LOG.info(AaiUiMsgs.INDEX_EXISTS, indexName);
98       return true;
99     } else {
100       LOG.info(AaiUiMsgs.INDEX_NOT_EXIST, indexName);
101       return false;
102     }
103   }
104
105   /* (non-Javadoc)
106    * @see org.onap.aai.sparky.synchronizer.IndexValidator#integrityValid()
107    */
108   @Override
109   public boolean integrityValid() {
110     // TODO Auto-generated method stub
111     // logger.info(";
112     // System.out.println("IndexIntegrityValidator.integrityValid() for
113     // indexName = " + indexName);
114     return true;
115   }
116
117   /* (non-Javadoc)
118    * @see org.onap.aai.sparky.synchronizer.IndexValidator#createOrRepair()
119    */
120   @Override
121   public void createOrRepair() {
122     // TODO Auto-generated method stub
123     String message = "IndexIntegrityValidator.createOrRepair() for indexName = " + indexName;
124     LOG.info(AaiUiMsgs.INFO_GENERIC, message);
125
126     final String fullUrlStr = getFullUrl("/" + indexName + "/");
127     OperationResult createResult =
128         restDataProvider.doPut(fullUrlStr, tableConfigJson, "application/json");
129
130     int rc = createResult.getResultCode();
131
132     if (rc >= 200 && rc < 300) {
133       LOG.info(AaiUiMsgs.INDEX_RECREATED, indexName);
134     } else if (rc == 400) {
135       LOG.info(AaiUiMsgs.INDEX_ALREADY_EXISTS, indexName);
136     } else {
137       LOG.warn(AaiUiMsgs.INDEX_INTEGRITY_CHECK_FAILED, indexName, createResult.getResult());
138     }
139
140   }
141
142   /* (non-Javadoc)
143    * @see org.onap.aai.sparky.synchronizer.IndexValidator#destroyIndex()
144    */
145   @Override
146   public void destroyIndex() {
147     // TODO Auto-generated method stub
148     // we don't do this for now
149
150   }
151
152   /**
153    * Gets the full url.
154    *
155    * @param resourceUrl the resource url
156    * @return the full url
157    */
158   private String getFullUrl(String resourceUrl) {
159     return String.format("http://%s:%s%s", host, port, resourceUrl);
160   }
161
162 }