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