379b110ff2ddb6b1e28ce22c1c56bcf3d1cc532c
[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_enriched_service_artifact")
37 public class EnrichedServiceArtifactEntity implements ServiceElementEntity {
38   private static final String ENTITY_TYPE;
39
40   static {
41     ENTITY_TYPE = "Vendor Software Product Service artifact";
42   }
43
44   @PartitionKey
45   @Column(name = "vsp_id")
46   public String id;
47
48   @PartitionKey(value = 1)
49   @Frozen
50   public Version version;
51
52   @ClusteringColumn
53   @Column(name = "name")
54   public String name;
55
56   @Column(name = "content_data")
57   public ByteBuffer contentData;
58
59   /**
60    * Every entity class must have a default constructor according to
61    * <a href="http://docs.datastax.com/en/developer/java-driver/2.1/manual/object_mapper/creating/">
62    * Definition of mapped classes</a>.
63    */
64   public EnrichedServiceArtifactEntity() {
65     // Don't delete! Default constructor is required by DataStax driver
66   }
67
68   /**
69    * Instantiates a new Enriched service artifact entity.
70    *
71    * @param entity the entity
72    */
73   public EnrichedServiceArtifactEntity(ServiceArtifact entity) {
74     this.id = entity.getVspId();
75     this.version = entity.getVersion();
76     this.name = entity.getName();
77
78     try {
79       this.contentData = ByteBuffer.wrap(ByteStreams.toByteArray(entity.getContent()));
80     } catch (IOException ioException) { //todo fix typo in Traget
81       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
82           LoggerTragetServiceName.CREATE_SERVICE_ENRICH_ARTIFACT, ErrorLevel.ERROR.name(),
83           LoggerErrorCode.DATA_ERROR.getErrorCode(),
84           LoggerErrorDescription.CREATE_ENRICH_SERVICE_ARTIFACT);
85       throw new RuntimeException(ioException);
86     }
87
88   }
89
90   @Override
91   public String getEntityType() {
92     return ENTITY_TYPE;
93   }
94
95   @Override
96   public String getFirstClassCitizenId() {
97     return getId();
98   }
99
100   @Override
101   public String getId() {
102     return id;
103   }
104
105   @Override
106   public void setId(String id) {
107     this.id = id;
108   }
109
110   @Override
111   public Version getVersion() {
112     return version;
113   }
114
115   @Override
116   public void setVersion(Version version) {
117     this.version = version;
118   }
119
120   @Override
121   public String getName() {
122     return name;
123   }
124
125   public void setName(String name) {
126     this.name = name;
127   }
128
129   @Override
130   public ByteBuffer getContentData() {
131     return contentData;
132   }
133
134   public void setContentData(ByteBuffer contentData) {
135     this.contentData = contentData;
136   }
137
138   /**
139    * Gets service artifact.
140    *
141    * @return the service artifact
142    */
143   public ServiceArtifact getServiceArtifact() {
144     ServiceArtifact serviceArtifact = new ServiceArtifact();
145     serviceArtifact.setName(this.getName());
146     serviceArtifact.setVersion(this.getVersion());
147     serviceArtifact.setContentData(this.getContentData().array());
148     serviceArtifact.setVspId(this.getId());
149     return serviceArtifact;
150   }
151 }