aboutsummaryrefslogtreecommitdiffstats
path: root/tapisimulator/src/main/java/org/onap/tapisimulator/service/TapiService.java
blob: 49b568991ffb4eff2084ef09484d283345a0a39f (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
/*
 * ============LICENSE_START=======================================================
 * TAPI-SIMULATOR
 * ================================================================================
 * Copyright (C) 2020 Fujitsu Limited. 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=========================================================
 */

package org.onap.tapisimulator.service;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;

import org.json.JSONArray;
import org.json.JSONObject;
import org.onap.tapisimulator.model.Sip;
import org.onap.tapisimulator.model.SipList;
import org.onap.tapisimulator.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TapiService {

    private static Logger log = LoggerFactory.getLogger(TapiService.class);

    private volatile Map<String, String> serviceMap = new HashMap<>();

    @Autowired
    ObjectMapper mapper;

    @Autowired
    Utils utils;

    private SipList siplist = null;

    private List<Sip> sip = null;

    private String topology;

    @PostConstruct
    public void init() throws IOException {
        String domainController = System.getenv("Controller");
        String servicePoints = "";
        if (domainController.equals("TAPI1")) {
            servicePoints = utils.readFromFile("/opt/onap/tapisimulator/templates/tapi1-siplist.json");
            topology = utils.readFromFile("/opt/onap/tapisimulator/templates/tapi1-topology.json");
            siplist = mapper.readValue(servicePoints, SipList.class);
            sip = siplist.getSip();
            log.debug("Inside init method of TapiService class");
        } else if (domainController.equals("TAPI2")) {
            servicePoints = utils.readFromFile("/opt/onap/tapisimulator/templates/tapi2-siplist.json");
            topology = utils.readFromFile("/opt/onap/tapisimulator/templates/tapi2-topology.json");
            siplist = mapper.readValue(servicePoints, SipList.class);
            sip = siplist.getSip();
            log.debug("Inside init method of TapiService class");
        }

    }

    public SipList getServiceInterfacePoints(String connectionPoint) {

        List<Sip> curSip = sip.stream().filter(c -> c.getUuid().equals(connectionPoint)).collect(Collectors.toList());

        SipList newSip = new SipList();
        newSip.setSip(curSip);
        return newSip;
    }

    public Map<String, String> getServiceMap() {
        return serviceMap;
    }

    public void setServiceMap(String key, String value) {
        serviceMap.put(key, value);
    }

    public void processService(String request) {
        JSONObject json = new JSONObject(request);
        JSONArray conectivityList = json.getJSONArray("create-connectivity-service-input-list");
        JSONObject connec = conectivityList.getJSONObject(0);
        JSONArray eplist = connec.getJSONArray("end-point");
        JSONArray name = connec.getJSONArray("name");
        String serviceName = name.getJSONObject(0).getString("value");

        createSErviceMap(eplist, serviceName, name);

    }

    public String returnService(String name) {

        return serviceMap.get(name);

    }

    public String processDeleteService(String name) {

        return serviceMap.remove(name);

    }

    public String getTopology() {

        return topology;

    }

    private void createSErviceMap(JSONArray eplist, String serviceName, JSONArray name) {
        JSONObject serviceEntry = new JSONObject();
        serviceEntry.put("end-point", eplist);
        serviceEntry.put("topology-constraint", new JSONArray());
        serviceEntry.put("routing-constraint", new JSONObject());
        serviceEntry.put("connectivity-constraint", new JSONObject());
        serviceEntry.put("uuid", UUID.randomUUID().toString());
        serviceEntry.put("layer-protocol-name", "ODU");
        serviceEntry.put("direction", "BIDIRECTIONAL");
        serviceEntry.put("administrative-state", "UNLOCKED");
        serviceEntry.put("operational-state", "ENABLED");
        JSONArray connection = new JSONArray();
        JSONObject connectionObject = new JSONObject();
        connectionObject.put("connection-uuid", UUID.randomUUID().toString());
        connection.put(0, connectionObject);
        serviceEntry.put("connection", connection);
        serviceEntry.put("name", name);

        JSONObject service = new JSONObject();
        JSONArray serviceList = new JSONArray();
        serviceList.put(0, serviceEntry);
        service.put("service", serviceList);

        serviceMap.put(serviceName, service.toString());
    }

}