71b3e49a2072f86ed6a58603d0c08eeda1854377
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
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
17 package org.openecomp.core.model.types;
18
19 import com.datastax.driver.mapping.annotations.ClusteringColumn;
20 import com.datastax.driver.mapping.annotations.Column;
21 import com.datastax.driver.mapping.annotations.Frozen;
22 import com.datastax.driver.mapping.annotations.PartitionKey;
23 import com.datastax.driver.mapping.annotations.Table;
24 import com.google.common.io.ByteStreams;
25 import org.openecomp.sdc.common.errors.SdcRuntimeException;
26 import org.openecomp.sdc.datatypes.error.ErrorLevel;
27 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
28 import org.openecomp.sdc.logging.types.LoggerConstants;
29 import org.openecomp.sdc.logging.types.LoggerErrorCode;
30 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
31 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
32 import org.openecomp.sdc.versioning.dao.types.Version;
33
34 import java.io.IOException;
35 import java.nio.ByteBuffer;
36
37 @Table(keyspace = "dox", name = "vsp_service_template")
38 public class ServiceTemplateEntity implements ServiceElementEntity {
39
40     private static final String ENTITY_TYPE;
41
42     static {
43         ENTITY_TYPE = "Vendor Software Product Service model";
44     }
45
46     @PartitionKey
47     @Column(name = "vsp_id")
48     public String id;
49
50     @PartitionKey(value = 1)
51     @Frozen
52     public Version version;
53
54     @ClusteringColumn
55     @Column(name = "name")
56     public String name;
57
58     @Column(name = "content_data")
59     public ByteBuffer contentData;
60
61     @Column(name = "base_name")
62     private String baseName;
63
64     /**
65      * Every entity class must have a default constructor according to
66      * <a href="http://docs.datastax.com/en/developer/java-driver/2.1/manual/object_mapper/creating/">
67      * Definition of mapped classes</a>.
68      */
69     public ServiceTemplateEntity() {
70         // Don't delete! Default constructor is required by DataStax driver
71     }
72
73     /**
74      * Instantiates a new Service template entity.
75      *
76      * @param entity the entity
77      */
78     public ServiceTemplateEntity(ServiceTemplate entity) {
79         this.id = entity.getVspId();
80         this.version = entity.getVersion();
81         this.name = entity.getName();
82         this.setBaseName(entity.getBaseName());
83         try {
84             this.contentData = ByteBuffer.wrap(ByteStreams.toByteArray(entity.getContent()));
85         } catch (IOException ioException) {
86             MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
87                     LoggerTragetServiceName.CREATE_SERVICE_TEMPLATE, ErrorLevel.ERROR.name(),
88                     LoggerErrorCode.DATA_ERROR.getErrorCode(),
89                     LoggerErrorDescription.CREATE_SERVICE_TEMPLATE);
90             throw new SdcRuntimeException(ioException);
91         }
92
93     }
94
95     public String getBaseName() {
96         return baseName;
97     }
98
99     public void setBaseName(String baseName) {
100         this.baseName = baseName;
101     }
102
103     @Override
104     public String getEntityType() {
105         return ENTITY_TYPE;
106     }
107
108     @Override
109     public String getFirstClassCitizenId() {
110         return getId();
111     }
112
113     @Override
114     public String getId() {
115         return id;
116     }
117
118     @Override
119     public void setId(String id) {
120         this.id = id;
121     }
122
123     @Override
124     public Version getVersion() {
125         return version;
126     }
127
128     @Override
129     public void setVersion(Version version) {
130         this.version = version;
131     }
132
133     @Override
134     public String getName() {
135         return name;
136     }
137
138     public void setName(String name) {
139         this.name = name;
140     }
141
142     @Override
143     public ByteBuffer getContentData() {
144         return contentData;
145     }
146
147     public void setContentData(ByteBuffer contentData) {
148         this.contentData = contentData;
149     }
150
151     public ServiceTemplate getServiceTemplate() {
152         ServiceTemplate serviceTemplate = new ServiceTemplate();
153         serviceTemplate.setName(getName());
154         serviceTemplate.setVersion(getVersion());
155         serviceTemplate.setContentData(getContentData().array());
156         serviceTemplate.setVspId(getId());
157         serviceTemplate.setBaseName(getBaseName());
158         return serviceTemplate;
159     }
160 }