Added missed code to appc-dg-common bundle
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / main / java / org / onap / appc / dg / common / impl / VNFConfiguratorImpl.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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.dg.common.impl;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import java.text.ParseException;
30 import java.text.SimpleDateFormat;
31 import java.util.Date;
32 import java.util.Map;
33 import org.apache.commons.lang3.StringUtils;
34 import org.onap.appc.Constants;
35 import org.onap.appc.dg.common.VNFConfigurator;
36 import org.onap.appc.exceptions.APPCException;
37 import org.onap.appc.mdsal.MDSALStore;
38 import org.onap.appc.mdsal.exception.MDSALStoreException;
39 import org.onap.appc.mdsal.impl.MDSALStoreFactory;
40 import org.onap.appc.mdsal.objects.BundleInfo;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
42
43
44 public class VNFConfiguratorImpl implements VNFConfigurator {
45
46     private static final EELFLogger logger = EELFManager.getInstance().getLogger(VNFConfiguratorImpl.class);
47     private static final String STATUS = "STATUS";
48     private static final String FAILURE = "FAILURE";
49     private static final String SUCCESS = "SUCCESS";
50     private static final String ERROR_MESSAGE = "ERROR_MESSAGE";
51
52     @Override
53     public void storeConfig(Map<String, String> params, SvcLogicContext context) throws APPCException {
54         String uniqueId = params.get("uniqueId");
55         String yang = params.get("yang");
56         String configJSON = params.get("configJSON");
57         String requestId = params.get("requestId");
58         String prefix = params.get("prefix");
59         prefix = StringUtils.isEmpty(prefix) ? "" : prefix + ".";
60
61         MDSALStore store = MDSALStoreFactory.createMDSALStore();
62
63         logger.debug("Inputs Received : uniqueId = " + uniqueId +
64             " , yang = " + yang +
65             " , configJSON = " + configJSON +
66             " , requestId = " + requestId +
67             " , prefix = " + prefix);
68
69         try {
70
71             if (StringUtils.isEmpty(uniqueId)
72                 || StringUtils.isEmpty(yang)
73                 || StringUtils.isEmpty(configJSON)
74                 || StringUtils.isEmpty(requestId)) {
75                 throw new APPCException("One or more input parameters are empty : uniqueId = " + uniqueId + " " +
76                     ", yang = " + yang +
77                     " , configJSON = " + configJSON +
78                     " , requestId = " + requestId);
79             }
80
81             Date revision = new SimpleDateFormat(Constants.YANG_REVISION_FORMAT).parse(Constants.YANG_REVISION);
82
83             boolean isYangAlreadyLoaded = store.isModulePresent(uniqueId, revision);
84
85             if (!isYangAlreadyLoaded) {
86                 BundleInfo bundleInfo = getBundleInfo(uniqueId);
87                 store.storeYangModule(yang, bundleInfo);
88             }
89             store.storeYangModuleOnLeader(yang,uniqueId);
90             store.storeJson(uniqueId, requestId, configJSON);
91             context.setAttribute(prefix + STATUS, SUCCESS);
92         } catch (ParseException e) {
93             String errorMessage = "Error parsing the date : " + Constants.YANG_REVISION + " into format "
94                 + Constants.YANG_REVISION_FORMAT;
95             logger.error(errorMessage, e);
96             context.setAttribute(prefix + STATUS, FAILURE);
97             context.setAttribute(prefix + ERROR_MESSAGE, errorMessage);
98             throw new APPCException(e.getMessage());
99         } catch (MDSALStoreException e) {
100             String errorMessage = "Error while adding yang to MD-SAL store." + e.getMessage();
101             logger.error(errorMessage, e);
102             context.setAttribute(prefix + STATUS, FAILURE);
103             context.setAttribute(prefix + ERROR_MESSAGE, errorMessage);
104             throw new APPCException(e.getMessage());
105         }
106
107     }
108
109     private BundleInfo getBundleInfo(String uniqueId) {
110         BundleInfo bundleInfo = new BundleInfo();
111         bundleInfo.setDescription(uniqueId);
112         bundleInfo.setName(uniqueId);
113         bundleInfo.setLocation(uniqueId);
114         return bundleInfo;
115     }
116 }