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