aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/clamp/clds/service/CldsTemplateService.java
blob: 0ffa955c4f0cd9de66e4497bd1080c0e43625373 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 *                             reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 * ============LICENSE_END============================================
 * ===================================================================
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */

package org.onap.clamp.clds.service;

import com.att.ajsc.common.AjscService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onap.clamp.clds.dao.CldsDao;
import org.onap.clamp.clds.model.CldsTemplate;
import org.onap.clamp.clds.model.ValueItem;
import org.onap.clamp.clds.model.prop.ModelBpmn;
import org.onap.clamp.clds.transform.XslTransformer;
import org.camunda.bpm.engine.RuntimeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.xml.transform.TransformerException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Service to save and retrieve the CLDS model attributes.
 */
@AjscService
@Path("/cldsTempate")
public class CldsTemplateService extends SecureServiceBase {

    private static final Logger logger = LoggerFactory.getLogger(CldsTemplateService.class);

    private static final String collectorKey = "Collector";
    private static final String stringMatchKey = "StringMatch";
    private static final String policyKey = "Policy";

    private static final String CLDS_PERMISSION_TYPE_TEMPLATE = System.getProperty("CLDS_PERMISSION_TYPE_TEMPLATE");
    private static final String CLDS_PERMISSION_INSTANCE = System.getProperty("CLDS_PERMISSION_INSTANCE");

    private static final SecureServicePermission PERMISSION_READ_TEMPLATE = SecureServicePermission.create(CLDS_PERMISSION_TYPE_TEMPLATE, CLDS_PERMISSION_INSTANCE, "read");
    private static final SecureServicePermission PERMISSION_UPDATE_TEMPLATE = SecureServicePermission.create(CLDS_PERMISSION_TYPE_TEMPLATE, CLDS_PERMISSION_INSTANCE, "update");

    @Autowired
    private CldsDao cldsDao;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private XslTransformer cldsBpmnTransformer;

    private static String userid;

    /**
     * REST service that retrieves BPMN for a CLDS template name from the database.
     * This is subset of the json getModel.
     * This is only expected to be used for testing purposes, not by the UI.
     *
     * @param templateName
     * @return bpmn xml text - content of bpmn given name
     */
    @GET
    @Path("/template/bpmn/{templateName}")
    @Produces(MediaType.TEXT_XML)
    public String getBpmnTemplate(@PathParam("templateName") String templateName) {
        isAuthorized(PERMISSION_READ_TEMPLATE);
        logger.info("GET bpmnText for templateName=" + templateName);
        CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
        return template.getBpmnText();
    }

    /**
     * REST service that saves BPMN for a CLDS template by name in the database.
     * This is subset of the json putModel.
     * This is only expected to be used for testing purposes, not by the UI.
     *
     * @param templateName
     * @param bpmnText
     */
    @PUT
    @Path("/template/bpmn/{templateName}")
    @Consumes(MediaType.TEXT_XML)
    public String putBpmnTemplateXml(@PathParam("templateName") String templateName, String bpmnText) {
        isAuthorized(PERMISSION_UPDATE_TEMPLATE);
        logger.info("PUT bpmnText for templateName=" + templateName);
        logger.info("PUT bpmnText=" + bpmnText);
        CldsTemplate cldsTemplate = CldsTemplate.retrieve(cldsDao, templateName, true);
        cldsTemplate.setBpmnText(bpmnText);
        cldsTemplate.save(cldsDao, userid);
        return "wrote bpmnText for templateName=" + templateName;
    }

    /**
     * REST service that retrieves image for a CLDS template name from the database.
     * This is subset of the json getModel.
     * This is only expected to be used for testing purposes, not by the UI.
     *
     * @param templateName
     * @return image xml text - content of image given name
     */
    @GET
    @Path("/template/image/{templateName}")
    @Produces(MediaType.TEXT_XML)
    public String getImageXml(@PathParam("templateName") String templateName) {
        isAuthorized(PERMISSION_READ_TEMPLATE);
        logger.info("GET imageText for templateName=" + templateName);
        CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
        return template.getImageText();
    }

    /**
     * REST service that saves image for a CLDS template by name in the database.
     * This is subset of the json putModel.
     * This is only expected to be used for testing purposes, not by the UI.
     *
     * @param templateName
     * @param imageText
     */
    @PUT
    @Path("/template/image/{templateName}")
    @Consumes(MediaType.TEXT_XML)
    public String putImageXml(@PathParam("templateName") String templateName, String imageText) {
        isAuthorized(PERMISSION_UPDATE_TEMPLATE);
        logger.info("PUT iamgeText for modelName=" + templateName);
        logger.info("PUT imageText=" + imageText);
        CldsTemplate cldsTemplate = CldsTemplate.retrieve(cldsDao, templateName, true);
        cldsTemplate.setImageText(imageText);
        cldsTemplate.save(cldsDao, userid);
        return "wrote imageText for modelName=" + templateName;
    }

    /**
     * REST service that retrieves a CLDS template by name from the database.
     *
     * @param templateName
     * @return clds template - clds template for the given template name
     */
    @GET
    @Path("/template/{templateName}")
    @Produces(MediaType.APPLICATION_JSON)
    public CldsTemplate getTemplate(@PathParam("templateName") String templateName) {
        isAuthorized(PERMISSION_READ_TEMPLATE);
        logger.info("GET model for  templateName=" + templateName);
        return CldsTemplate.retrieve(cldsDao, templateName, false);
    }

    /**
     * REST service that saves a CLDS template by name in the database.
     *
     * @param templateName
     * @throws IOException
     * @throws JsonMappingException
     * @throws JsonParseException
     */
    @PUT
    @Path("/template/{templateName}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public CldsTemplate putTemplate(@PathParam("templateName") String templateName, CldsTemplate cldsTemplate) throws TransformerException, IOException {
        isAuthorized(PERMISSION_UPDATE_TEMPLATE);
        logger.info("PUT Template for  templateName=" + templateName);
        logger.info("PUT bpmnText=" + cldsTemplate.getBpmnText());
        logger.info("PUT propText=" + cldsTemplate.getPropText());
        logger.info("PUT imageText=" + cldsTemplate.getImageText());
        cldsTemplate.setName(templateName);
        String bpmnText = cldsTemplate.getBpmnText();
        String imageText = cldsTemplate.getImageText();
        String propText = cldsTemplate.getPropText();
        Map<String, String> newBpmnIdsMap = getNewBpmnIdsMap(bpmnText, cldsTemplate.getPropText());
        for (String currBpmnId : newBpmnIdsMap.keySet()) {
            if (currBpmnId != null && newBpmnIdsMap.get(currBpmnId) != null) {
                bpmnText = bpmnText.replace(currBpmnId, newBpmnIdsMap.get(currBpmnId));
                imageText = imageText.replace(currBpmnId, newBpmnIdsMap.get(currBpmnId));
                propText = propText.replace(currBpmnId, newBpmnIdsMap.get(currBpmnId));
            }
        }
        cldsTemplate.setBpmnText(bpmnText);
        cldsTemplate.setImageText(imageText);
        cldsTemplate.setPropText(propText);
        logger.info(" bpmnText : " + cldsTemplate.getBpmnText());
        logger.info(" Image Text : " + cldsTemplate.getImageText());
        logger.info(" Prop Text : " + cldsTemplate.getPropText());
        cldsTemplate.save(cldsDao, userid);
        return cldsTemplate;
    }

    /**
     * REST service that retrieves a list of CLDS template names.
     *
     * @return template names in JSON
     */
    @GET
    @Path("/template-names")
    @Produces(MediaType.APPLICATION_JSON)
    public List<ValueItem> getTemplateNames() {
        isAuthorized(PERMISSION_READ_TEMPLATE);
        logger.info("GET list of template names");
        return cldsDao.getTemplateNames();
    }


    private Map<String, String> getNewBpmnIdsMap(String bpmnText, String propText) throws TransformerException, IOException {
        /**
         *  Test sample code start
         */
        String bpmnJson = cldsBpmnTransformer.doXslTransformToString(bpmnText);
        ModelBpmn templateBpmn = ModelBpmn.create(bpmnJson);
        List<String> bpmnElementIds = templateBpmn.getBpmnElementIds();
        logger.info("value of elementIds:" + bpmnElementIds);
        logger.info("value of prop text:" + propText);
        Map<String, String> bpmnIoIdsMap = new HashMap<>();
        if (bpmnElementIds != null && bpmnElementIds.size() > 0) {
            ObjectMapper objectMapper = new ObjectMapper();
            ObjectNode root = objectMapper.readValue(propText, ObjectNode.class);
            Iterator<Entry<String, JsonNode>> entryItr = root.fields();
            while (entryItr.hasNext()) {
                // process the entry
                Entry<String, JsonNode> entry = entryItr.next();
                String keyPropName = entry.getKey();
                for (String currElementId : bpmnElementIds) {
                    if (keyPropName != null && keyPropName.equalsIgnoreCase(currElementId)) {
                        ArrayNode arrayNode = (ArrayNode) entry.getValue();
                        // process each id/from object, like: {"id":"Collector_11r50j1", "from":"StartEvent_1"}
                        for (JsonNode anArrayNode : arrayNode) {
                            ObjectNode node = (ObjectNode) anArrayNode;
                            String valueNode = node.get("value").asText();
                            logger.info("value of node:" + valueNode);
                            if (keyPropName.startsWith(collectorKey)) {
                                valueNode = collectorKey + "_" + valueNode;
                            } else if (keyPropName.startsWith(stringMatchKey)) {
                                valueNode = stringMatchKey + "_" + valueNode;
                            } else if (keyPropName.startsWith(policyKey)) {
                                valueNode = policyKey + "_" + valueNode;
                            }
                            bpmnIoIdsMap.put(keyPropName, valueNode);
                        }
                        break;
                    }
                }
            }
        }
        logger.info("value of hashmap:" + bpmnIoIdsMap);
        /**
         *  Test sample code end
         */
        return bpmnIoIdsMap;
    }
}