d47ec10f2c8c1df14add0e2cda207b0dfafc2af2
[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 package org.openecomp.core.model.types;
17
18 import com.datastax.driver.mapping.annotations.ClusteringColumn;
19 import com.datastax.driver.mapping.annotations.Column;
20 import com.datastax.driver.mapping.annotations.Frozen;
21 import com.datastax.driver.mapping.annotations.PartitionKey;
22 import com.datastax.driver.mapping.annotations.Table;
23 import com.google.common.io.ByteStreams;
24 import java.io.IOException;
25 import java.nio.ByteBuffer;
26 import org.openecomp.sdc.common.errors.SdcRuntimeException;
27 import org.openecomp.sdc.versioning.dao.types.Version;
28
29 @Table(keyspace = "dox", name = "vsp_enriched_service_artifact")
30 public class EnrichedServiceArtifactEntity implements ServiceElementEntity {
31
32     private static final String ENTITY_TYPE;
33
34     static {
35         ENTITY_TYPE = "Vendor Software Product Service artifact";
36     }
37
38     @PartitionKey
39     @Column(name = "vsp_id")
40     public String id;
41     @PartitionKey(value = 1)
42     @Frozen
43     public Version version;
44     @ClusteringColumn
45     @Column(name = "name")
46     public String name;
47     @Column(name = "content_data")
48     public ByteBuffer contentData;
49
50     /**
51      * Every entity class must have a default constructor according to
52      * <a href="http://docs.datastax.com/en/developer/java-driver/2.1/manual/object_mapper/creating/">
53      * Definition of mapped classes</a>.
54      */
55     public EnrichedServiceArtifactEntity() {
56         // Don't delete! Default constructor is required by DataStax driver
57     }
58
59     /**
60      * Instantiates a new Enriched service artifact entity.
61      *
62      * @param entity the entity
63      */
64     public EnrichedServiceArtifactEntity(ServiceArtifact entity) {
65         this.id = entity.getVspId();
66         this.version = entity.getVersion();
67         this.name = entity.getName();
68         try {
69             this.contentData = ByteBuffer.wrap(ByteStreams.toByteArray(entity.getContent()));
70         } catch (IOException ioException) {
71             throw new SdcRuntimeException(ioException);
72         }
73     }
74
75     @Override
76     public String getEntityType() {
77         return ENTITY_TYPE;
78     }
79
80     @Override
81     public String getFirstClassCitizenId() {
82         return getId();
83     }
84
85     @Override
86     public String getId() {
87         return id;
88     }
89
90     @Override
91     public void setId(String id) {
92         this.id = id;
93     }
94
95     @Override
96     public Version getVersion() {
97         return version;
98     }
99
100     @Override
101     public void setVersion(Version version) {
102         this.version = version;
103     }
104
105     @Override
106     public String getName() {
107         return name;
108     }
109
110     public void setName(String name) {
111         this.name = name;
112     }
113
114     @Override
115     public ByteBuffer getContentData() {
116         return contentData;
117     }
118
119     public void setContentData(ByteBuffer contentData) {
120         this.contentData = contentData;
121     }
122
123     public ServiceArtifact getServiceArtifact() {
124         ServiceArtifact serviceArtifact = new ServiceArtifact();
125         serviceArtifact.setName(this.getName());
126         serviceArtifact.setVersion(this.getVersion());
127         serviceArtifact.setContentData(this.getContentData().array());
128         serviceArtifact.setVspId(this.getId());
129         return serviceArtifact;
130     }
131 }