Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / synchronizer / 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
26 package org.openecomp.sparky.synchronizer;
27
28 import org.openecomp.cl.api.Logger;
29 import org.openecomp.cl.eelf.LoggerFactory;
30 import org.openecomp.sparky.dal.rest.OperationResult;
31 import org.openecomp.sparky.dal.rest.RestDataProvider;
32 import org.openecomp.sparky.logging.AaiUiMsgs;
33
34 /**
35  * The Class IndexIntegrityValidator.
36  *
37  * @author davea.
38  */
39 public class IndexIntegrityValidator implements IndexValidator {
40
41   private static final Logger LOG =
42       LoggerFactory.getInstance().getLogger(IndexIntegrityValidator.class);
43
44   private String host;
45   private String port;
46   private String indexName;
47   private String indexType;
48   private String tableConfigJson;
49
50   private final RestDataProvider restDataProvider;
51
52   /**
53    * Instantiates a new index integrity validator.
54    *
55    * @param restDataProvider the rest data provider
56    * @param indexName the index name
57    * @param indexType the index type
58    * @param host the host
59    * @param port the port
60    * @param tableConfigJson the table config json
61    */
62   public IndexIntegrityValidator(RestDataProvider restDataProvider, String indexName,
63       String indexType, String host, String port, String tableConfigJson) {
64     this.restDataProvider = restDataProvider;
65     this.host = host;
66     this.port = port;
67     this.indexName = indexName;
68     this.indexType = indexType;
69     this.tableConfigJson = tableConfigJson;
70   }
71
72   @Override
73   public String getIndexName() {
74     return indexName;
75   }
76
77   public void setIndexName(String indexName) {
78     this.indexName = indexName;
79   }
80
81   public String getIndexType() {
82     return indexType;
83   }
84
85   public void setIndexType(String indexType) {
86     this.indexType = indexType;
87   }
88
89   /* (non-Javadoc)
90    * @see org.openecomp.sparky.synchronizer.IndexValidator#exists()
91    */
92   @Override
93   public boolean exists() {
94     final String fullUrlStr = getFullUrl("/" + indexName + "/");
95     OperationResult existsResult = restDataProvider.doHead(fullUrlStr, "application/json");
96
97     int rc = existsResult.getResultCode();
98
99     if (rc >= 200 && rc < 300) {
100       LOG.info(AaiUiMsgs.INDEX_EXISTS, indexName);
101       return true;
102     } else {
103       LOG.info(AaiUiMsgs.INDEX_NOT_EXIST, indexName);
104       return false;
105     }
106   }
107
108   /* (non-Javadoc)
109    * @see org.openecomp.sparky.synchronizer.IndexValidator#integrityValid()
110    */
111   @Override
112   public boolean integrityValid() {
113     // TODO Auto-generated method stub
114     // logger.info(";
115     // System.out.println("IndexIntegrityValidator.integrityValid() for
116     // indexName = " + indexName);
117     return true;
118   }
119
120   /* (non-Javadoc)
121    * @see org.openecomp.sparky.synchronizer.IndexValidator#createOrRepair()
122    */
123   @Override
124   public void createOrRepair() {
125     // TODO Auto-generated method stub
126     String message = "IndexIntegrityValidator.createOrRepair() for indexName = " + indexName;
127     LOG.info(AaiUiMsgs.INFO_GENERIC, message);
128
129     final String fullUrlStr = getFullUrl("/" + indexName + "/");
130     OperationResult createResult =
131         restDataProvider.doPut(fullUrlStr, tableConfigJson, "application/json");
132
133     int rc = createResult.getResultCode();
134
135     if (rc >= 200 && rc < 300) {
136       LOG.info(AaiUiMsgs.INDEX_RECREATED, indexName);
137     } else if (rc == 400) {
138       LOG.info(AaiUiMsgs.INDEX_ALREADY_EXISTS, indexName);
139     } else {
140       LOG.warn(AaiUiMsgs.INDEX_INTEGRITY_CHECK_FAILED, indexName, createResult.getResult());
141     }
142
143   }
144
145   /* (non-Javadoc)
146    * @see org.openecomp.sparky.synchronizer.IndexValidator#destroyIndex()
147    */
148   @Override
149   public void destroyIndex() {
150     // TODO Auto-generated method stub
151     // we don't do this for now
152
153   }
154
155   /**
156    * Gets the full url.
157    *
158    * @param resourceUrl the resource url
159    * @return the full url
160    */
161   private String getFullUrl(String resourceUrl) {
162     return String.format("http://%s:%s%s", host, port, resourceUrl);
163   }
164
165 }