8f886c2e74f455817a3f80b956e7c6b63c9ea178
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.core.model.types;
22
23 import com.datastax.driver.mapping.annotations.ClusteringColumn;
24 import com.datastax.driver.mapping.annotations.Column;
25 import com.datastax.driver.mapping.annotations.Frozen;
26 import com.datastax.driver.mapping.annotations.PartitionKey;
27 import com.datastax.driver.mapping.annotations.Table;
28 import com.google.common.io.ByteStreams;
29 import org.openecomp.sdc.datatypes.error.ErrorLevel;
30 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
31 import org.openecomp.sdc.logging.types.LoggerConstants;
32 import org.openecomp.sdc.logging.types.LoggerErrorCode;
33 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
34 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
35 import org.openecomp.sdc.versioning.dao.types.Version;
36
37 import java.io.IOException;
38 import java.nio.ByteBuffer;
39
40 @Table(keyspace = "dox", name = "vsp_service_template")
41 public class ServiceTemplateEntity implements ServiceElementEntity {
42
43   private static final String ENTITY_TYPE;
44
45   static {
46     ENTITY_TYPE = "Vendor Software Product Service model";
47   }
48
49   @PartitionKey
50   @Column(name = "vsp_id")
51   public String id;
52
53   @PartitionKey(value = 1)
54   @Frozen
55   public Version version;
56
57   @ClusteringColumn
58   @Column(name = "name")
59   public String name;
60
61   @Column(name = "content_data")
62   public ByteBuffer contentData;
63
64   @Column(name = "base_name")
65   private String baseName;
66
67   public ServiceTemplateEntity() {
68   }
69
70   /**
71    * Instantiates a new Service template entity.
72    *
73    * @param entity the entity
74    */
75   public ServiceTemplateEntity(ServiceTemplate entity) {
76     this.id = entity.getVspId();
77     this.version = entity.getVersion();
78     this.name = entity.getName();
79     this.setBaseName(entity.getBaseName());
80     try {
81       this.contentData = ByteBuffer.wrap(ByteStreams.toByteArray(entity.getContent()));
82     } catch (IOException ioException) {
83       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
84           LoggerTragetServiceName.CREATE_SERVICE_TEMPLATE, ErrorLevel.ERROR.name(),
85           LoggerErrorCode.DATA_ERROR.getErrorCode(),
86           LoggerErrorDescription.CREATE_SERVICE_TEMPLATE);
87       throw new RuntimeException(ioException);
88     }
89
90   }
91
92   public String getBaseName() {
93     return baseName;
94   }
95
96   public void setBaseName(String baseName) {
97     this.baseName = baseName;
98   }
99
100   @Override
101   public String getEntityType() {
102     return ENTITY_TYPE;
103   }
104
105   @Override
106   public String getFirstClassCitizenId() {
107     return getId();
108   }
109
110   public String getId() {
111     return id;
112   }
113
114   public void setId(String id) {
115     this.id = id;
116   }
117
118   @Override
119   public Version getVersion() {
120     return version;
121   }
122
123   @Override
124   public void setVersion(Version version) {
125     this.version = version;
126   }
127
128   public String getName() {
129     return name;
130   }
131
132   public void setName(String name) {
133     this.name = name;
134   }
135
136   public ByteBuffer getContentData() {
137     return contentData;
138   }
139
140   public void setContentData(ByteBuffer contentData) {
141     this.contentData = contentData;
142   }
143
144
145   /**
146    * Gets service template.
147    *
148    * @return the service template
149    */
150   public ServiceTemplate getServiceTemplate() {
151     ServiceTemplate serviceTemplate = new ServiceTemplate();
152     serviceTemplate.setName(this.getName());
153     serviceTemplate.setVersion(this.getVersion());
154     serviceTemplate.setContentData(this.getContentData().array());
155     serviceTemplate.setVspId(this.getId());
156     serviceTemplate.setBaseName(this.getBaseName());
157     return serviceTemplate;
158
159   }
160 }