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