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