bdbafb8771fd4b2c975618241c8eaf7e37671f2a
[vfc/nfvo/catalog.git] /
1 /**
2  * Copyright 2016 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openo.commontosca.catalog.model.service;
17
18 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
19 import org.openo.commontosca.catalog.db.resource.TemplateManager;
20 import org.openo.commontosca.catalog.model.entity.ServiceTemplate;
21 import org.openo.commontosca.catalog.model.entity.ServiceTemplateOperation;
22 import org.openo.commontosca.catalog.model.plan.wso2.Wso2ServiceConsumer;
23 import org.openo.commontosca.catalog.model.wrapper.ServiceTemplateWrapper;
24 import org.openo.commontosca.catalog.resources.CatalogBadRequestException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class ModelService {
29   private static final Logger logger = LoggerFactory.getLogger(ModelService.class);
30
31   private static ModelService instance;
32
33   public static ModelService getInstance() {
34     if (instance == null) {
35       instance = new ModelService();
36     }
37     return instance;
38   }
39
40
41   /**
42    * delete service template according package id.
43    * 
44    * @param packageId package id
45    * @throws CatalogBadRequestException e1
46    * @throws CatalogResourceException e2
47    */
48   public void delete(String packageId) throws CatalogResourceException {
49     logger.info("delete package model data begin.");
50
51     ServiceTemplate st = getServiceTemplateByCsarIdIgnoreError(packageId);
52     if (st == null) {
53       return;
54     }
55
56     undeployOperationPackage(st.getOperations());
57
58     TemplateManager.getInstance().deleteServiceTemplateById(st.getServiceTemplateId());
59     TemplateManager.getInstance().deleteServiceTemplateMapping(null, st.getServiceTemplateId());
60
61     logger.info("delete package model data end.");
62   }
63
64   private void undeployOperationPackage(ServiceTemplateOperation[] operations)
65       throws CatalogResourceException {
66     if (operations != null && operations.length > 0) {
67       for (ServiceTemplateOperation op : operations) {
68         Wso2ServiceConsumer.deletePackage(op.getPackageName());
69       }
70     }
71   }
72
73   private ServiceTemplate getServiceTemplateByCsarIdIgnoreError(String packageId) {
74     try {
75       return ServiceTemplateWrapper.getInstance().getServiceTemplateByCsarId(packageId);
76     } catch (CatalogBadRequestException ignore) {
77       logger.info("delete package model data ignore.", ignore);
78     } catch (CatalogResourceException ignore) {
79       logger.info("delete package model data ignore.", ignore);
80     }
81
82     return null;
83   }
84
85   /**
86    * delete service template data only, not undeploy operation package.
87    * 
88    * @param packageId package id
89    * @throws CatalogBadRequestException e1
90    * @throws CatalogResourceException e2
91    */
92   public void deleteServiceTemplateData(String packageId) throws CatalogResourceException {
93     logger.info("delete service template data begin.");
94
95     ServiceTemplate st = getServiceTemplateByCsarIdIgnoreError(packageId);
96     if (st == null) {
97       return;
98     }
99
100     TemplateManager.getInstance().deleteServiceTemplateById(st.getServiceTemplateId());
101     TemplateManager.getInstance().deleteServiceTemplateMapping(null, st.getServiceTemplateId());
102
103     logger.info("delete service template data end.");
104   }
105
106   /**
107    * undeploy operation package of the service template.
108    * 
109    * @param packageId package id
110    * @throws CatalogBadRequestException e1
111    * @throws CatalogResourceException e2
112    */
113   public void undeployOperationPackage(String packageId) throws CatalogResourceException {
114     logger.info("undeploy operation package begin.");
115
116     ServiceTemplate st = getServiceTemplateByCsarIdIgnoreError(packageId);
117     if (st == null) {
118       return;
119     }
120
121     undeployOperationPackage(st.getOperations());
122
123     logger.info("undeploy operation package end.");
124   }
125
126 }