Bump to odlparent 3.1.0 and yangtools 2.0.3
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / ospdp / COPSPdpOSMsgSender.java
1 package org.umu.cops.ospdp;
2
3 import org.umu.cops.prpdp.COPSPdpMsgSender;
4 import org.umu.cops.stack.*;
5 import org.umu.cops.stack.COPSContext.RType;
6 import org.umu.cops.stack.COPSDecision.Command;
7 import org.umu.cops.stack.COPSHeader.Flag;
8
9 import java.io.IOException;
10 import java.net.Socket;
11 import java.util.*;
12
13 /**
14  * COPS message transceiver class for outsourcing connections at the PDP side.
15  *
16  * TODO - change all references of Vector to List<>
17  */
18 public class COPSPdpOSMsgSender extends COPSPdpMsgSender {
19
20     /**
21      * Creates a COPSPepMsgSender
22      *
23      * @param clientType        COPS client-type
24      * @param clientHandle      Client handle
25      * @param sock              Socket to the PEP
26      */
27     public COPSPdpOSMsgSender(final short clientType, final COPSHandle clientHandle, final Socket sock) {
28         super(clientType, clientHandle, sock);
29     }
30
31     /**
32      * Sends a decision message which was requested by the PEP
33      * @param removeDecs    Decisions to be removed
34      * @param installDecs   Decisions to be installed
35      * @throws COPSPdpException
36      * TODO - Add type to List once we know what it should be.
37      */
38     public void sendSolicitedDecision(final List removeDecs, final List installDecs) throws COPSPdpException {
39         sendDecision(removeDecs, installDecs, true);
40     }
41
42     /**
43      * Sends a decision message which was not requested by the PEP
44      * @param removeDecs    Decisions to be removed
45      * @param installDecs   Decisions to be installed
46      * @throws COPSPdpException
47      * TODO - Add type to List once we know what it should be.
48      */
49     public void sendUnsolicitedDecision(final List removeDecs, final List installDecs) throws COPSPdpException {
50         sendDecision(removeDecs, installDecs, false);
51     }
52
53     /**
54      * Sends a decision message to the PEP
55      * @param removeDecs    Decisions to be removed
56      * @param installDecs   Decisions to be installed
57      * @param solicited     <tt>true</tt> if the PEP requested this decision, <tt>false</tt> otherwise
58      * @throws COPSPdpException
59      * TODO - Add type to List once we know what it should be.
60      */
61     public void sendDecision(final List removeDecs, final List installDecs, boolean solicited) throws COPSPdpException {
62         // Common Header holding the same ClientType as the request
63         final Flag flag;
64         if (solicited)
65             flag= Flag.SOLICITED;
66         else
67             flag = Flag.UNSOLICITED;
68
69         final Map<COPSContext, Set<COPSDecision>> decisions = new HashMap<>();
70
71         // Decisions (no flags supplied)
72         //  <Context>
73         final COPSContext cntxt = new COPSContext(RType.CONFIG, (short)0);
74
75         // Remove Decisions
76         //  <Decision: Flags>
77         final COPSDecision rdec1;
78         if (installDecs.size() == 0)
79             rdec1 = new COPSDecision(Command.REMOVE);
80         else
81             rdec1 = new COPSDecision(Command.INSTALL);
82
83         if (decisions.get(cntxt) == null) {
84             final Set<COPSDecision> decisionSet = new HashSet<>();
85             decisionSet.add(rdec1);
86             decisions.put(cntxt, decisionSet);
87         } else {
88             decisions.get(cntxt).add(rdec1);
89         }
90
91         // Client Handle with the same clientHandle as the request
92         final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(1, flag, getClientType(), _handle, decisions,
93                 null, null);
94
95         //** Send decision
96         //**
97         try {
98             decisionMsg.writeData(_sock);
99         } catch (IOException e) {
100             throw new COPSPdpException("Failed to send the decision, reason: " + e.getMessage());
101         }
102     }
103
104 }