Adding examples module to apex-pdp
[policy/apex-pdp.git] / examples / pcvs / src / main / resources / org / onap / policy / apex / examples / pcvs / vpnsla / logic / ctxt-customer.js
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 load("nashorn:mozilla_compat.js");
22
23 var logger = executor.logger;
24 logger.trace("start: " + executor.subject.id);
25 logger.trace("-- infields: " + executor.inFields);
26
27 var ifCustomerName = executor.inFields["customerName"];
28 var ifLinks = executor.inFields["links"];
29
30 logger.trace("-- got infields, testing existing customer");
31 var ctxtCustomer = executor.getContextAlbum("albumCustomerMap").get(ifCustomerName);
32 if (ctxtCustomer != null) {
33     executor.getContextAlbum("albumCustomerMap").remove(ifCustomerName);
34     logger.trace("-- removed customer: <" + ifCustomerName + ">");
35 }
36
37 logger.trace("-- creating customer: <" + ifCustomerName + ">");
38 var links = new Array();
39 for (var i = 0; i < ifLinks.split(" ").length; i++) {
40     var link = executor.getContextAlbum("albumTopoEdges").get(ifLinks.split(" ")[i]);
41     if (link != null) {
42         logger.trace("-- link: <" + ifLinks.split(" ")[i] + ">");
43         links.push(ifLinks.split(" ")[i]);
44     } else {
45         logger.trace("-- unknown link: <" + ifLinks.split(" ")[i] + "> for customer <" + ifCustomerName + ">");
46     }
47 }
48 logger.trace("-- links: <" + links + ">");
49 ctxtCustomer = "{customerName:" + ifCustomerName + ", dtSLA:" + executor.inFields["dtSLA"] + ", dtYTD:"
50         + executor.inFields["dtYTD"] + ", priority:" + executor.inFields["priority"] + ", satisfaction:"
51         + executor.inFields["satisfaction"] + ", links:[" + links + "]}";
52
53 executor.getContextAlbum("albumCustomerMap").put(ifCustomerName, ctxtCustomer);
54
55 if (logger.isTraceEnabled()) {
56     logger.trace("   >> *** Customers ***");
57     if (executor.getContextAlbum("albumCustomerMap") != null) {
58         for (var i = 0; i < executor.getContextAlbum("albumCustomerMap").values().size(); i++) {
59             logger.trace("   >> >> " + executor.getContextAlbum("albumCustomerMap").values().get(i).get("customerName")
60                     + " : " + "dtSLA=" + executor.getContextAlbum("albumCustomerMap").values().get(i).get("dtSLA")
61                     + " : " + "dtYTD=" + executor.getContextAlbum("albumCustomerMap").values().get(i).get("dtYTD")
62                     + " : " + "links=" + executor.getContextAlbum("albumCustomerMap").values().get(i).get("links")
63                     + " : " + "priority="
64                     + executor.getContextAlbum("albumCustomerMap").values().get(i).get("priority") + " : "
65                     + "satisfaction="
66                     + executor.getContextAlbum("albumCustomerMap").values().get(i).get("satisfaction"));
67         }
68     } else {
69         logger.trace("   >> >> customer album is null");
70     }
71 }
72
73 executor.outFields["report"] = "customer ctxt :: added customer: " + ifCustomerName;
74
75 logger.info("vpnsla: ctxt added customer " + ifCustomerName);
76
77 var returnValueType = Java.type("java.lang.Boolean");
78 var returnValue = new returnValueType(true);
79 logger.trace("finished: " + executor.subject.id);
80 logger.debug(".");