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