2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * Copyright (C) 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
 
  12  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  23 package org.openecomp.appc.dg.common.impl;
 
  25 import org.openecomp.appc.Constants;
 
  26 import org.openecomp.appc.dg.common.VNFConfigurator;
 
  27 import org.openecomp.appc.exceptions.APPCException;
 
  28 import org.openecomp.appc.mdsal.MDSALStore;
 
  29 import org.openecomp.appc.mdsal.exception.MDSALStoreException;
 
  30 import org.openecomp.appc.mdsal.impl.MDSALStoreFactory;
 
  31 import org.openecomp.appc.mdsal.objects.BundleInfo;
 
  32 import com.att.eelf.configuration.EELFLogger;
 
  33 import com.att.eelf.configuration.EELFManager;
 
  34 import org.openecomp.sdnc.sli.SvcLogicContext;
 
  35 import org.apache.commons.lang.StringUtils;
 
  37 import java.text.ParseException;
 
  38 import java.text.SimpleDateFormat;
 
  39 import java.util.Date;
 
  43 public class VNFConfiguratorImpl implements VNFConfigurator {
 
  45     private static final EELFLogger logger = EELFManager.getInstance().getLogger(VNFConfiguratorImpl.class);
 
  46     private static final String STATUS = "STATUS";
 
  47     private static final String FAILURE = "FAILURE";
 
  48     private static final String SUCCESS = "SUCCESS";
 
  49     private static final String ERROR_MESSAGE = "ERROR_MESSAGE";
 
  52     public void storeConfig(Map<String, String> params, SvcLogicContext context) throws APPCException {
 
  53         String uniqueId = params.get("uniqueId");
 
  54         String yang = params.get("yang");
 
  55         String configJSON = params.get("configJSON");
 
  56         String requestId = params.get("requestId");
 
  57         String prefix = params.get("prefix");
 
  58         prefix = StringUtils.isEmpty(prefix)? "":prefix+".";
 
  60         MDSALStore store = MDSALStoreFactory.createMDSALStore();
 
  62         logger.debug("Inputs Received : uniqueId = " + uniqueId +
 
  64                                         " , configJSON = " + configJSON +
 
  65                                         " , requestId = " + requestId +
 
  66                                         " , prefix = " +prefix);
 
  70             if(StringUtils.isEmpty(uniqueId)
 
  71                     ||StringUtils.isEmpty(yang)
 
  72                     || StringUtils.isEmpty(configJSON)
 
  73                     || StringUtils.isEmpty(requestId)){
 
  74                 throw new APPCException("One or more input parameters are empty : uniqueId = " + uniqueId + " " +
 
  76                         " , configJSON = " + configJSON +
 
  77                         " , requestId = " + requestId);
 
  80             Date revision = new SimpleDateFormat(Constants.YANG_REVISION_FORMAT).parse(Constants.YANG_REVISION);
 
  82             boolean isYangAlreadyLoaded = store.isModulePresent(uniqueId,revision);
 
  84             if(!isYangAlreadyLoaded){
 
  85                 BundleInfo bundleInfo = getBundleInfo(uniqueId);
 
  86                 store.storeYangModule(yang,bundleInfo);
 
  88             store.storeJson(uniqueId, requestId , configJSON);
 
  89             context.setAttribute(prefix + STATUS, SUCCESS);
 
  90         } catch (ParseException e) {
 
  91             String errorMessage ="Error parsing the date : " + Constants.YANG_REVISION + " into format " + Constants.YANG_REVISION_FORMAT;
 
  92             logger.error(errorMessage,e);
 
  93             context.setAttribute(prefix + STATUS, FAILURE);
 
  94             context.setAttribute(prefix + ERROR_MESSAGE, errorMessage);
 
  95             throw new APPCException(e.getMessage());
 
  96         }catch (MDSALStoreException e){
 
  97             String errorMessage = "Error while adding yang to MD-SAL store." + e.getMessage();
 
  98             logger.error(errorMessage,e);
 
  99             context.setAttribute(prefix + STATUS,FAILURE);
 
 100             context.setAttribute(prefix + ERROR_MESSAGE, errorMessage);
 
 101             throw new APPCException(e.getMessage());
 
 106     private BundleInfo getBundleInfo(String uniqueId) {
 
 107         BundleInfo bundleInfo = new BundleInfo();
 
 108         bundleInfo.setDescription(uniqueId);
 
 109         bundleInfo.setName(uniqueId);
 
 110         bundleInfo.setLocation(uniqueId);