Update license header in appc-dg-shared files
[appc.git] / appc-dg / appc-dg-shared / appc-dg-mdsal-store / appc-dg-mdsal-bundle / src / main / java / org / onap / appc / mdsal / impl / MDSALStoreImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
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  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.mdsal.impl;
25
26 import com.fasterxml.jackson.annotation.JsonIgnore;
27 import com.fasterxml.jackson.annotation.JsonProperty;
28 import com.fasterxml.jackson.annotation.JsonValue;
29 import com.fasterxml.jackson.core.JsonProcessingException;
30 import com.fasterxml.jackson.databind.JsonNode;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import org.apache.commons.io.IOUtils;
33 import org.apache.http.HttpResponse;
34 import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.StoreYangInput;
35 import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.StoreYangInputBuilder;
36 import org.opendaylight.yangtools.yang.binding.DataContainer;
37 import org.onap.appc.configuration.Configuration;
38 import org.onap.appc.configuration.ConfigurationFactory;
39 import org.onap.appc.exceptions.APPCException;
40 import org.onap.appc.mdsal.MDSALStore;
41 import org.onap.appc.mdsal.exception.MDSALStoreException;
42 import org.onap.appc.mdsal.objects.BundleInfo;
43 import com.att.eelf.configuration.EELFLogger;
44 import com.att.eelf.configuration.EELFManager;
45 import org.onap.appc.mdsal.operation.ConfigOperationRequestFormatter;
46 import org.onap.appc.rest.client.RestClientInvoker;
47 import org.osgi.framework.Bundle;
48 import org.osgi.framework.BundleContext;
49 import org.osgi.framework.FrameworkUtil;
50
51 import java.io.ByteArrayInputStream;
52 import java.io.ByteArrayOutputStream;
53 import java.io.IOException;
54 import java.net.MalformedURLException;
55 import java.net.URL;
56 import java.util.*;
57 import java.util.jar.Attributes;
58 import java.util.jar.JarEntry;
59 import java.util.jar.JarOutputStream;
60 import java.util.jar.Manifest;
61
62 /**
63  * Implementation of MDSALStore
64  */
65 public class MDSALStoreImpl implements MDSALStore {
66
67     private final EELFLogger logger = EELFManager.getInstance().getLogger(MDSALStoreImpl.class);
68     private RestClientInvoker client;
69     private ConfigOperationRequestFormatter requestFormatter = new ConfigOperationRequestFormatter();
70     private ObjectMapper mapper = new ObjectMapper();
71     private Map<String, RestClientInvoker> remoteClientMap = new HashMap<>();
72
73     MDSALStoreImpl() {
74         String configUrl = null;
75         String user = null;
76         String password = null;
77         Configuration configuration = ConfigurationFactory.getConfiguration();
78         Properties properties = configuration.getProperties();
79         if (properties != null) {
80             configUrl = properties.getProperty(Constants.CONFIG_URL_PROPERTY, Constants.CONFIG_URL_DEFAULT);
81             user = properties.getProperty(Constants.CONFIG_USER_PROPERTY);
82             password = properties.getProperty(Constants.CONFIG_PASS_PROPERTY);
83         }
84         if (configUrl != null) {
85             try {
86                 client = new RestClientInvoker(new URL(configUrl));
87                 client.setAuthentication(user, password);
88             } catch (MalformedURLException e) {
89                 logger.error("Error initializing RestConf client: " + e.getMessage(), e);
90             }
91         }
92     }
93
94
95     @Override
96     public boolean isModulePresent(String moduleName, Date revision) {
97
98         if (logger.isDebugEnabled()) {
99             logger.debug("isModulePresent invoked with moduleName = " + moduleName + " , revision = " + revision);
100         }
101
102         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
103         /*
104          * SchemaContext interface of ODL provides APIs for querying details of yang modules
105          * loaded into MD-SAL store, but its limitation is, it only returns information about
106          * static yang modules loaded on server start up, it does not return information about
107          * the yang modules loaded dynamically. Due to this limitation, we are checking the
108          * presence of OSGI bundle instead of yang module. (Note: Assuming OSGI bundle is named
109          * with the yang module name).
110          */
111
112         Bundle bundle = bundleContext.getBundle(moduleName);
113         if (logger.isDebugEnabled()) {
114             logger.debug("isModulePresent returned = " + (bundle != null));
115         }
116         return bundle != null;
117     }
118
119     @Override
120     public void storeYangModule(String yang, BundleInfo bundleInfo) throws MDSALStoreException {
121
122         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
123         byte[] byteArray = createBundleJar(yang, Constants.BLUEPRINT, bundleInfo);
124
125         try (ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray)) {
126             Bundle bundle = bundleContext.installBundle(bundleInfo.getLocation(), inputStream);
127             bundle.start();
128         } catch (Exception e) {
129             logger.error(String.format("Error storing yang module: %s. Error message: %s.", yang, e.getMessage()));
130             throw new MDSALStoreException("Error storing yang module: " + yang + " " + e.getMessage(), e);
131         }
132     }
133
134     @Override
135     public void storeYangModuleOnLeader(String yang, String moduleName) throws MDSALStoreException {
136         try {
137             String leader = getLeaderNode();
138             if (Constants.SELF.equals(leader)){
139                 logger.debug("Current node is a leader.");
140             }else{
141                 logger.debug("Attempting to load yang module on Leader: " + leader );
142                 String inputJson = createInputJson(yang, moduleName);
143                 RestClientInvoker remoteClient = getRemoteClient(leader);
144                 HttpResponse response = remoteClient.doPost(Constants.YANG_LOADER_PATH, inputJson);
145                 int httpCode = response.getStatusLine().getStatusCode();
146                 String respBody = IOUtils.toString(response.getEntity().getContent());
147                 if (httpCode < 200 || httpCode >= 300) {
148                     logger.debug("Error while loading yang module on leader. Response code: " + httpCode);
149                     processRestconfResponse(respBody);
150                 } else {
151                     logger.debug("Yang module successfully loaded on leader. Response code: " + httpCode);
152                 }
153             }
154         } catch (APPCException e) {
155             logger.error("Error loading Yang on Leader. Error message: " + e.getMessage());
156             throw new MDSALStoreException("Error loading Yang on Leader. Error message: " + e.getMessage(), e);
157         } catch (IOException e) {
158             logger.error("Error reading response from remote client. Error message: " + e.getMessage());
159             throw new MDSALStoreException("Error reading response from remote client. Error message: " + e.getMessage(), e);
160         }
161     }
162
163     private String createInputJson(String yang, String moduleName)  throws MDSALStoreException {
164         StoreYangInputBuilder builder = new StoreYangInputBuilder();
165         builder.setYang(yang).setModuleName(moduleName);
166         StoreYangInput input = builder.build();
167         try {
168             ObjectMapper objectMapper = new ObjectMapper();
169             objectMapper.addMixInAnnotations(StoreYangInput.class, MixIn.class);
170             String inputJson = objectMapper.writer().withRootName("input").writeValueAsString(input);
171             logger.debug("Input JSON :" + inputJson);
172             return inputJson;
173         } catch (JsonProcessingException e) {
174             logger.error(String.format("Error creating JSON input using yang: %s. Error message: %s",yang ,e.getMessage()));
175             throw new MDSALStoreException(String.format("Error creating JSON input using yang: %s. Error message: %s",yang ,e.getMessage()), e);
176         }
177     }
178
179     private RestClientInvoker getRemoteClient(String leader) throws MDSALStoreException {
180         if (remoteClientMap.containsKey(leader)) {
181             return remoteClientMap.get(leader);
182         } else {
183             Configuration configuration = ConfigurationFactory.getConfiguration();
184             Properties properties = configuration.getProperties();
185             if (properties != null) {
186                 try {
187                     URL configUrl = new URL(properties.getProperty(Constants.CONFIG_URL_PROPERTY, Constants.CONFIG_URL_DEFAULT));
188                     String user = properties.getProperty(Constants.CONFIG_USER_PROPERTY);
189                     String password = properties.getProperty(Constants.CONFIG_PASS_PROPERTY);
190                     RestClientInvoker remoteClient = new RestClientInvoker(new URL(configUrl.getProtocol(), leader, configUrl.getPort(), ""));
191                     remoteClient.setAuthentication(user, password);
192                     remoteClientMap.put(leader, remoteClient);
193                     return remoteClient;
194                 } catch (MalformedURLException e) {
195                     logger.error("Error initializing remote RestConf client: " + e.getMessage(), e);
196                     throw new MDSALStoreException("Error initializing Remote RestConf client: " + e.getMessage(), e);
197                 }
198             } else {
199                 logger.error("Error initializing Remote RestConf client. Could not read appc properties");
200                 throw new MDSALStoreException("Error initializing Remote RestConf client. Could not read appc properties");
201             }
202         }
203     }
204
205     abstract class MixIn {
206         @JsonIgnore
207         abstract Class<? extends DataContainer> getImplementedInterface(); // to be removed during serialization
208
209         @JsonValue
210         abstract java.lang.String getValue();
211
212         @JsonProperty("module-name")
213         abstract java.lang.String getModuleName();
214     }
215
216     @Override
217     public void storeJson(String module, String requestId, String configJson) throws MDSALStoreException {
218         if (configJson == null) {
219             throw new MDSALStoreException("Configuration JSON is empty or null");
220         }
221         logger.debug("Configuration JSON: " + configJson + "\n" + "module" + module);
222         try {
223             String path = requestFormatter.buildPath(module, org.onap.appc.Constants.YANG_BASE_CONTAINER,
224                     org.onap.appc.Constants.YANG_VNF_CONFIG_LIST, requestId, org.onap.appc.Constants.YANG_VNF_CONFIG);
225             logger.debug("Configuration Path : " + path);
226             HttpResponse response = client.doPut(path, configJson);
227             int httpCode = response.getStatusLine().getStatusCode();
228             String respBody = IOUtils.toString(response.getEntity().getContent());
229             if (httpCode < 200 || httpCode >= 300) {
230                 logger.debug("Error while storing configuration JSON to MD-SAL store. Response code: " + httpCode);
231                 processRestconfResponse(respBody);
232             } else {
233                 logger.debug("Configuration JSON stored to MD-SAL store successfully. Response code: " + httpCode);
234             }
235         } catch (IOException | APPCException e) {
236             logger.error("Error while storing configuration json. Error Message" + e.getMessage(), e);
237             throw new MDSALStoreException(e);
238         }
239     }
240
241     private void processRestconfResponse(String response) throws MDSALStoreException {
242         try {
243             JsonNode responseJson = mapper.readTree(response);
244             ArrayList<String> errorMessage = new ArrayList<>();
245             if (responseJson != null && responseJson.get("errors") != null) {
246                 JsonNode errors = responseJson.get("errors").get("error");
247                 for (Iterator<JsonNode> i = errors.elements(); i.hasNext(); ) {
248                     JsonNode error = i.next();
249                     errorMessage.add(error.get("error-message").textValue());
250                 }
251             }
252             logger.error("Failed to load config JSON to MD SAL store. " + errorMessage.toString());
253             throw new MDSALStoreException("Failed to load config JSON to MD SAL store. Error Message: " + errorMessage.toString());
254         } catch (IOException e) {
255             logger.error("Failed to process error response from RestConf: " + e.getMessage());
256             throw new MDSALStoreException("Failed to process RestConf response. Error Message: " + e.toString(), e);
257         }
258     }
259
260     private byte[] createBundleJar(String yang, String blueprint, BundleInfo bundleInfo) throws MDSALStoreException {
261
262         Manifest manifest = new Manifest();
263         manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, Constants.MANIFEST_VALUE_VERSION);
264         manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_NAME), bundleInfo.getName());
265         manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_SYMBOLIC_NAME), bundleInfo.getName());
266         manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_DESCRIPTION), bundleInfo.getDescription());
267         manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_MANIFEST_VERSION), Constants.MANIFEST_VALUE_BUNDLE_MAN_VERSION);
268         manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_VERSION), String.valueOf(bundleInfo.getVersion()));
269         manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_BLUEPRINT), Constants.MANIFEST_VALUE_BUNDLE_BLUEPRINT);
270
271         byte[] retunValue;
272
273         try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
274              JarOutputStream jarOutputStream = new JarOutputStream(outputStream, manifest)) {
275             jarOutputStream.putNextEntry(new JarEntry("META-INF/yang/"));
276             jarOutputStream.putNextEntry(new JarEntry("META-INF/yang/" + bundleInfo.getName() + ".yang"));
277             jarOutputStream.write(yang.getBytes());
278             jarOutputStream.closeEntry();
279
280             jarOutputStream.putNextEntry(new JarEntry("OSGI-INF/blueprint/"));
281             jarOutputStream.putNextEntry(new JarEntry(Constants.MANIFEST_VALUE_BUNDLE_BLUEPRINT));
282             jarOutputStream.write(blueprint.getBytes());
283             jarOutputStream.closeEntry();
284             jarOutputStream.close();
285             retunValue = outputStream.toByteArray();
286         } catch (Exception e) {
287             logger.error("Error creating bundle jar: " + bundleInfo.getName() + ". Error message: " + e.getMessage());
288             throw new MDSALStoreException("Error creating bundle jar: " + bundleInfo.getName() + " " + e.getMessage(), e);
289         }
290         return retunValue;
291     }
292
293     private String getLeaderNode() throws MDSALStoreException {
294         try {
295             String shardName = String.format(Constants.SHARD_NAME_FORMAT, getNodeName());
296             HttpResponse response = client.doGet(String.format(Constants.GET_NODE_STATUS_PATH_FORMAT, shardName));
297             int httpCode = response.getStatusLine().getStatusCode();
298             String respBody = IOUtils.toString(response.getEntity().getContent());
299             logger.debug(String.format("Get node status returned Code: %s. Response: %s ", httpCode, respBody));
300             if (httpCode == 200 && mapper.readTree(respBody).get(Constants.JSON_RESPONSE_VALUE) !=null ) {
301                 JsonNode responseValue = mapper.readTree(respBody).get(Constants.JSON_RESPONSE_VALUE);
302                     String leaderShard = responseValue.get("Leader").asText();
303                     if (shardName.equals(leaderShard)) {
304                         logger.debug("Current node is leader.");
305                         return Constants.SELF;
306                     } else {
307                         String[] peers = responseValue.get("PeerAddresses").asText().split(",");
308                         for (String peer : peers) {
309                             if (peer.trim().startsWith(leaderShard)) {
310                                 String leader = peer.substring(peer.indexOf('@') + 1, peer.indexOf(':', peer.indexOf('@')));
311                                 logger.debug(String.format("Node %s is a leader", leader));
312                                 return leader;
313                             }
314                         }
315                         logger.error("No Leader found for a cluster");
316                         throw new MDSALStoreException("No Leader found for a cluster");
317                     }
318             } else {
319                 logger.error("Error while retrieving leader node.");
320                 throw new MDSALStoreException("Error while retrieving leader node.");
321             }
322         } catch (IOException | APPCException e) {
323             logger.error(String.format("Error while retrieving leader Node. Error message : %s ", e.getMessage()), e);
324             throw new MDSALStoreException(e);
325         }
326     }
327
328     private String getNodeName() throws MDSALStoreException {
329         try {
330             HttpResponse response = client.doGet(Constants.GET_SHARD_LIST_PATH);
331             int httpCode = response.getStatusLine().getStatusCode();
332             String respBody = IOUtils.toString(response.getEntity().getContent());
333             logger.debug(String.format("Get shard list returned Code: %s. Response: %s ", httpCode, respBody));
334             if (httpCode == 200) {
335                 JsonNode responseValue = mapper.readTree(respBody).get(Constants.JSON_RESPONSE_VALUE);
336                 if (responseValue != null && responseValue.get(Constants.JSON_RESPONSE_MEMBER_NAME) != null) {
337                     String name = responseValue.get(Constants.JSON_RESPONSE_MEMBER_NAME).asText();
338                     logger.debug("Node name : " + name);
339                     return name;
340                 }else{
341                     logger.error(String.format("Error while retrieving node name from response. Response body: %s.", respBody));
342                     throw new MDSALStoreException(String.format("Error while retrieving node name from response. Response body: %s.", respBody));
343                 }
344             } else {
345                 logger.error(String.format("Error while retrieving node name. Error code: %s. Error response: %s.", httpCode, respBody));
346                 throw new MDSALStoreException(String.format("Error while retrieving node name. Error code: %s. Error response: %s.", httpCode, respBody));
347             }
348         } catch (IOException | APPCException e) {
349             logger.error("Error while getting node name " + e.getMessage(), e);
350             throw new MDSALStoreException(e);
351         }
352     }
353 }