Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-sdc-model-lib / openecomp-sdc-model-api / src / main / java / org / openecomp / core / model / types / ServiceArtifactEntity.java
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_artifact")
37 public class ServiceArtifactEntity 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    * Instantiates a new Service artifact entity.
61    *
62    * @param entity the entity
63    */
64   public ServiceArtifactEntity(ServiceArtifact entity) {
65     this.id = entity.getVspId();
66     this.version = entity.getVersion();
67     this.name = entity.getName();
68
69     try {
70       this.contentData = ByteBuffer.wrap(ByteStreams.toByteArray(entity.getContent()));
71     } catch (IOException ioException) {
72       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
73           LoggerTragetServiceName.CREATE_SERVICE_ARTIFACT, ErrorLevel.ERROR.name(),
74           LoggerErrorCode.DATA_ERROR.getErrorCode(),
75           LoggerErrorDescription.CREATE_SERVICE_ARTIFACT);
76       throw new RuntimeException(ioException);
77     }
78
79   }
80
81   @Override
82   public String getEntityType() {
83     return ENTITY_TYPE;
84   }
85
86   @Override
87   public String getFirstClassCitizenId() {
88     return getId();
89   }
90
91   @Override
92   public String getId() {
93     return id;
94   }
95
96   @Override
97   public void setId(String id) {
98     this.id = id;
99   }
100
101   @Override
102   public Version getVersion() {
103     return version;
104   }
105
106   @Override
107   public void setVersion(Version version) {
108     this.version = version;
109   }
110
111   @Override
112   public String getName() {
113     return name;
114   }
115
116   public void setName(String name) {
117     this.name = name;
118   }
119
120   @Override
121   public ByteBuffer getContentData() {
122     return contentData;
123   }
124
125   public void setContentData(ByteBuffer contentData) {
126     this.contentData = contentData;
127   }
128
129   /**
130    * Gets service artifact.
131    *
132    * @return the service artifact
133    */
134   public ServiceArtifact getServiceArtifact() {
135     ServiceArtifact serviceArtifact = new ServiceArtifact();
136     serviceArtifact.setName(this.getName());
137     serviceArtifact.setVersion(this.getVersion());
138     serviceArtifact.setContentData(this.getContentData().array());
139     serviceArtifact.setVspId(this.getId());
140     return serviceArtifact;
141   }
142 }