summaryrefslogtreecommitdiffstats
path: root/rules/ccvpn-rule.drl
blob: 1f2d82db77acedefffeb4bef973f57377e9362e9 (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
package org.onap.holmes.ccvpn;

dialect "java"

import org.onap.holmes.common.api.stat.VesAlarm;
import org.onap.holmes.common.api.stat.AlarmAdditionalField;
import org.onap.holmes.common.aai.AaiQuery4Ccvpn;
import org.onap.holmes.common.exception.CorrelationException;
import org.onap.holmes.common.dmaap.entity.PolicyMsg;
import org.onap.holmes.common.dmaap.DmaapService;
import org.onap.holmes.common.utils.DroolsLog;
import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;

function String getAdditionalField(VesAlarm a, String field) {
	List<AlarmAdditionalField> fields = a.getAlarmAdditionalInformation();
	for (AlarmAdditionalField f : fields) {
		if (f.getName().equals(field)) {
			return f.getValue();
		}
	}
	return null;
}

function String getLogicLink(VesAlarm alarm) {
	AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance();
	return aai.getLogicLink(
		getAdditionalField(alarm, "networkId"),
		getAdditionalField(alarm, "node"),
		getAdditionalField(alarm, "tp-id"),
		null
	);
}

function boolean isCorrelated(VesAlarm a, VesAlarm b) {
	String logicLinkA = getLogicLink(a);
	if (logicLinkA == null)	{
		return false;
	}

	String logicLinkB = getLogicLink(b);
	if (logicLinkB == null) {
		return false;
	}

	return logicLinkA.equals(logicLinkB);
}

function void updateAaiLinkStatus(String linkName, String status) {
	AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance();
	Map<String, Object> body = new HashMap<String, Object>(){
		{
			put("operational-status", status);
		}
	};
	aai.updateLogicLinkStatus(linkName, body);
}

function void updateAaiTpStatus(String networkId, String pnfName, String ifName, String status) {
	AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance();
	Map<String, Object> body = new HashMap<String, Object>(){
		{
			put("operational-status", status);
		}
	};
	aai.updateTerminalPointStatus(networkId, pnfName, ifName, body);
}

function Map<String, Object> getAdditionalResourceInfo(String networkId, String pnfName, String ifName, String status) {
    AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance();
    JSONObject instance = aai.getServiceInstance(networkId, pnfName, ifName, status);
 
    Map<String, Object> ret = new HashMap<String, Object>();
    ret.put("service-instance.service-instance-id", instance.getString("service-instance-id"));
    ret.put("vserver.vserver-name", "TBD");
    ret.put("globalSubscriberId", instance.getString("globalSubscriberId"));
    ret.put("serviceType", instance.getString("serviceType"));
    ret.put("network-information.network-id", networkId);
 
    return ret;
}

function PolicyMsg createPolicyMsg(VesAlarm alarm) {
	PolicyMsg m = new PolicyMsg();
	m.setPolicyVersion("1.0.0.5");
	m.setPolicyName("CCVPN");
	m.setPolicyScope("service=SOTNService,type=SampleType,closedLoopControlName=CL-CCVPN-d925ed73-8231-4d02-9545-db4e101f88f8");
	m.setClosedLoopControlName(DmaapService.loopControlNames.get("org.onap.holmes.ccvpn"));
	m.setRequestID(UUID.randomUUID().toString());
	m.setClosedLoopAlarmStart(alarm.getStartEpochMicrosec());
	m.setClosedLoopAlarmEnd(alarm.getLastEpochMicrosec());
	m.setTarget("vserver.vserver-name");
	m.setAai(getAdditionalResourceInfo(
		getAdditionalField(alarm, "networkId"),
		getAdditionalField(alarm, "node"),
		getAdditionalField(alarm, "tp-id"),
		getAdditionalField(alarm, "oper-status")
	));

	DmaapService.alarmUniqueRequestID.put(alarm.getEventId(), m.getRequestID());

	return m;
}

rule "Update AAI Information"
	no-loop true
	salience 300
	when
		$a: VesAlarm(eventName.indexOf("Fault_Route_Status") != -1)
	then
		updateAaiTpStatus (
			getAdditionalField($a, "networkId"),
			getAdditionalField($a, "node"),
			getAdditionalField($a, "tp-id"),
			getAdditionalField($a, "oper-status")
		);
end

rule "Set Up Correlation"
	no-loop true
	salience 200
	when
		$a: VesAlarm($id: eventId, 
						$start: startEpochMicrosec, 
						eventName.indexOf("Fault_Route_Status") != -1)
		$b: VesAlarm(eventId != $id, 
						eventName.indexOf("Fault_Route_Status") != -1, 
						Math.abs(startEpochMicrosec - $start) < 60000)
	then
		String status = "down";
		if (status.equalsIgnoreCase(getAdditionalField($a, "oper-status")) 
				&& status.equalsIgnoreCase(getAdditionalField($b, "oper-status"))) {
		    if (isCorrelated($a, $b)){
		    	// If any of the alarms have been marked as root, a policy message has ever been created and sent. Do NOT send it again.
		    	if ($a.getRootFlag() != 1 && $b.getRootFlag() != 1) {
			    	PolicyMsg msg = createPolicyMsg($a);
			        DmaapService dmaapService = ServiceLocatorHolder.getLocator().getService(DmaapService.class);
			        dmaapService.publishPolicyMsg(msg, "unauthenticated.DCAE_CL_OUTPUT");
			        updateAaiLinkStatus(getLogicLink($a), status);
		    	}
		        $a.setRootFlag(1);
		        $b.setRootFlag(1);
		        update($a);
		        update($b);
		    }
	    }
end

rule "Clear Alarms"
	no-loop true
	salience 100
	when
		$a: VesAlarm(eventName.indexOf("Fault_Route_Status") != -1)
	then
		if ("up".equalsIgnoreCase(getAdditionalField($a, "oper-status"))) {
			if (DmaapService.alarmUniqueRequestID.containsKey($a.getEventId())) {
				DmaapService.alarmUniqueRequestID.remove($a.getEventId());
			}
	
			//TODO: send alarm clearing message to Policy - for now it's not needed.
	        //...
	
			retract($a);
		}
end