fdbd58bb3b539e244f4639054885999de9a63ae6
[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_template")
32 public class EnrichedServiceTemplateEntity implements ServiceElementEntity {
33
34     private static final String ENTITY_TYPE;
35
36     static {
37         ENTITY_TYPE = "Vendor Software Product Service model";
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     @Column(name = "base_name")
56     private String baseName;
57
58     /**
59      * Every entity class must have a default constructor according to
60      * <a href="http://docs.datastax.com/en/developer/java-driver/2.1/manual/object_mapper/creating/">
61      * Definition of mapped classes</a>.
62      */
63     public EnrichedServiceTemplateEntity() {
64         // Don't delete! Default constructor is required by DataStax driver
65     }
66
67     /**
68      * Instantiates a new Enriched service template entity.
69      *
70      * @param entity the entity
71      */
72     public EnrichedServiceTemplateEntity(ServiceTemplate entity) {
73         this.id = entity.getVspId();
74         this.version = entity.getVersion();
75         this.name = entity.getName();
76         this.setBaseName(entity.getBaseName());
77         try {
78             this.contentData = ByteBuffer.wrap(ByteStreams.toByteArray(entity.getContent()));
79         } catch (IOException ioException) {
80             throw new SdcRuntimeException(ioException);
81         }
82
83     }
84
85     public String getBaseName() {
86         return baseName;
87     }
88
89     public void setBaseName(String baseName) {
90         this.baseName = baseName;
91     }
92
93     @Override
94     public String getEntityType() {
95         return ENTITY_TYPE;
96     }
97
98     @Override
99     public String getFirstClassCitizenId() {
100         return getId();
101     }
102
103     @Override
104     public String getId() {
105         return id;
106     }
107
108     @Override
109     public void setId(String id) {
110         this.id = id;
111     }
112
113     @Override
114     public Version getVersion() {
115         return version;
116     }
117
118     @Override
119     public void setVersion(Version version) {
120         this.version = version;
121     }
122
123     @Override
124     public String getName() {
125         return name;
126     }
127
128     public void setName(String name) {
129         this.name = name;
130     }
131
132     @Override
133     public ByteBuffer getContentData() {
134         return contentData;
135     }
136
137     public void setContentData(ByteBuffer contentData) {
138         this.contentData = contentData;
139     }
140
141     public ServiceTemplate getServiceTemplate() {
142         ServiceTemplate serviceTemplate = new ServiceTemplate();
143         serviceTemplate.setName(getName());
144         serviceTemplate.setVersion(getVersion());
145         serviceTemplate.setContentData(getContentData().array());
146         serviceTemplate.setVspId(getId());
147         serviceTemplate.setBaseName(getBaseName());
148         return serviceTemplate;
149     }
150 }