Refactor AppConfigService with tests
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / main / java / org / onap / blueprintgenerator / service / common / StreamService.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
5  *  *  ================================================================================
6  *  *  Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
7  *  *  ================================================================================
8  *  *  Modifications Copyright (c) 2021 Nokia
9  *  *  ================================================================================
10  *  *  Licensed under the Apache License, Version 2.0 (the "License");
11  *  *  you may not use this file except in compliance with the License.
12  *  *  You may obtain a copy of the License at
13  *  *
14  *  *       http://www.apache.org/licenses/LICENSE-2.0
15  *  *
16  *  *  Unless required by applicable law or agreed to in writing, software
17  *  *  distributed under the License is distributed on an "AS IS" BASIS,
18  *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  *  *  See the License for the specific language governing permissions and
20  *  *  limitations under the License.
21  *  *  ============LICENSE_END=========================================================
22  *
23  *
24  */
25
26 package org.onap.blueprintgenerator.service.common;
27
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30 import java.util.TreeMap;
31 import org.onap.blueprintgenerator.constants.Constants;
32 import org.onap.blueprintgenerator.model.common.Dmaap;
33 import org.onap.blueprintgenerator.model.componentspec.OnapComponentSpec;
34 import org.onap.blueprintgenerator.model.componentspec.common.Publishes;
35 import org.onap.blueprintgenerator.model.componentspec.common.Subscribes;
36 import org.onap.blueprintgenerator.service.base.BlueprintHelperService;
37 import org.springframework.stereotype.Service;
38
39 /**
40  * @author : Joanna Jeremicz
41  * @date 01/15/2021 Application: ONAP - Blueprint Generator Common ONAP Service
42  * to create publishes and subscribes streams
43  */
44 @Service("streamService")
45 public class StreamService {
46
47     /**
48      * Creates publishes stream for given Inputs and ComponentSpec
49      *
50      * @param onapComponentSpec Onap Component Specification
51      * @param blueprintHelperService Blueprint Helper Service
52      * @param dmaapService Dmaap Service
53      * @param inputs Inputs
54      * @param isDmaap Dmaap Argument
55      * @return
56      */
57     public Map<String, Dmaap> createStreamPublishes(
58         OnapComponentSpec onapComponentSpec,
59         BlueprintHelperService blueprintHelperService,
60         DmaapService dmaapService,
61         Map<String, LinkedHashMap<String, Object>> inputs,
62         boolean isDmaap) {
63
64         Map<String, Dmaap> streamPublishes = new TreeMap<>();
65         if (onapComponentSpec.getStreams() == null || onapComponentSpec.getStreams().getPublishes() == null) {
66             return streamPublishes;
67         }
68
69         for (Publishes publishes : onapComponentSpec.getStreams().getPublishes()) {
70             if (blueprintHelperService.isDataRouterType(publishes.getType())) {
71                 String config = publishes.getConfig_key();
72                 String name = config + Constants._FEED;
73                 Map<String, Object> dmaapDataRouterResponse =
74                     dmaapService.createDmaapDataRouter(inputs, config, name, isDmaap);
75                 inputs =
76                     (Map<String, LinkedHashMap<String, Object>>) dmaapDataRouterResponse
77                         .get("inputs");
78                 Dmaap dmaap = (Dmaap) dmaapDataRouterResponse.get("dmaap");
79                 dmaap.setType(publishes.getType());
80                 streamPublishes.put(config, dmaap);
81             } else if (blueprintHelperService.isMessageRouterType(publishes.getType())) {
82                 String config = publishes.getConfig_key();
83                 String name = config + Constants._TOPIC;
84                 Map<String, Object> dmaapDataRouterResponse =
85                     dmaapService
86                         .createDmaapMessageRouter(inputs, config, 'p', name, name, isDmaap);
87                 inputs =
88                     (Map<String, LinkedHashMap<String, Object>>) dmaapDataRouterResponse
89                         .get("inputs");
90                 Dmaap dmaap = (Dmaap) dmaapDataRouterResponse.get("dmaap");
91                 dmaap.setType(publishes.getType());
92                 streamPublishes.put(config, dmaap);
93             }
94         }
95         return streamPublishes;
96     }
97
98     /**
99      * Creates subscribes stream for given Inputs and ComponentSpec
100      *
101      * @param onapComponentSpec Onap Component Specification
102      * @param blueprintHelperService Blueprint Helper Service
103      * @param dmaapService Dmaap Service
104      * @param inputs Inputs
105      * @param isDmaap Dmaap Argument
106      * @return
107      */
108     public Map<String, Dmaap> createStreamSubscribes(
109         OnapComponentSpec onapComponentSpec,
110         BlueprintHelperService blueprintHelperService,
111         DmaapService dmaapService,
112         Map<String, LinkedHashMap<String, Object>> inputs,
113         boolean isDmaap) {
114
115         Map<String, Dmaap> streamSubscribes = new TreeMap<>();
116         if (onapComponentSpec.getStreams() == null || onapComponentSpec.getStreams().getSubscribes() == null) {
117             return streamSubscribes;
118         }
119
120         for (Subscribes subscribes : onapComponentSpec.getStreams().getSubscribes()) {
121             if (blueprintHelperService.isDataRouterType(subscribes.getType())) {
122                 String config = subscribes.getConfig_key();
123                 String name = config + Constants._FEED;
124                 Map<String, Object> dmaapDataRouterResponse =
125                     dmaapService.createDmaapDataRouter(inputs, config, name, isDmaap);
126                 inputs =
127                     (Map<String, LinkedHashMap<String, Object>>) dmaapDataRouterResponse
128                         .get("inputs");
129                 Dmaap dmaap = (Dmaap) dmaapDataRouterResponse.get("dmaap");
130                 dmaap.setType(subscribes.getType());
131                 streamSubscribes.put(config, dmaap);
132             } else if (blueprintHelperService.isMessageRouterType(subscribes.getType())) {
133                 String config = subscribes.getConfig_key();
134                 String name = config + Constants._TOPIC;
135                 Map<String, Object> dmaapDataRouterResponse =
136                     dmaapService
137                         .createDmaapMessageRouter(inputs, config, 's', name, name, isDmaap);
138                 inputs =
139                     (Map<String, LinkedHashMap<String, Object>>) dmaapDataRouterResponse
140                         .get("inputs");
141                 Dmaap dmaap = (Dmaap) dmaapDataRouterResponse.get("dmaap");
142                 dmaap.setType(subscribes.getType());
143                 streamSubscribes.put(config, dmaap);
144             }
145         }
146         return streamSubscribes;
147     }
148
149 }