d3d125b849b20a4ae7377f914db80ea364c9a94c
[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_artifact")
39 public class EnrichedServiceArtifactEntity implements ServiceElementEntity {
40   private static final String ENTITY_TYPE;
41
42   static {
43     ENTITY_TYPE = "Vendor Software Product Service artifact";
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   /**
62    * Instantiates a new Enriched service artifact entity.
63    *
64    * @param entity the entity
65    */
66   public EnrichedServiceArtifactEntity(ServiceArtifact entity) {
67     this.id = entity.getVspId();
68     this.version = entity.getVersion();
69     this.name = entity.getName();
70
71     try {
72       this.contentData = ByteBuffer.wrap(ByteStreams.toByteArray(entity.getContent()));
73     } catch (IOException ioException) { //todo fix typo in Traget
74       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
75           LoggerTragetServiceName.CREATE_SERVICE_ENRICH_ARTIFACT, ErrorLevel.ERROR.name(),
76           LoggerErrorCode.DATA_ERROR.getErrorCode(),
77           LoggerErrorDescription.CREATE_ENRICH_SERVICE_ARTIFACT);
78       throw new RuntimeException(ioException);
79     }
80
81   }
82
83   @Override
84   public String getEntityType() {
85     return ENTITY_TYPE;
86   }
87
88   @Override
89   public String getFirstClassCitizenId() {
90     return getId();
91   }
92
93   @Override
94   public String getId() {
95     return id;
96   }
97
98   @Override
99   public void setId(String id) {
100     this.id = id;
101   }
102
103   @Override
104   public Version getVersion() {
105     return version;
106   }
107
108   @Override
109   public void setVersion(Version version) {
110     this.version = version;
111   }
112
113   @Override
114   public String getName() {
115     return name;
116   }
117
118   public void setName(String name) {
119     this.name = name;
120   }
121
122   @Override
123   public ByteBuffer getContentData() {
124     return contentData;
125   }
126
127   public void setContentData(ByteBuffer contentData) {
128     this.contentData = contentData;
129   }
130
131   /**
132    * Gets service artifact.
133    *
134    * @return the service artifact
135    */
136   public ServiceArtifact getServiceArtifact() {
137     ServiceArtifact serviceArtifact = new ServiceArtifact();
138     serviceArtifact.setName(this.getName());
139     serviceArtifact.setVersion(this.getVersion());
140     serviceArtifact.setContentData(this.getContentData().array());
141     serviceArtifact.setVspId(this.getId());
142     return serviceArtifact;
143   }
144 }